/**
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package com.dangdang.ddframe.rdb.sharding.metrics;

import java.util.concurrent.TimeUnit;

import org.slf4j.LoggerFactory;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;
import com.codahale.metrics.Slf4jReporter.LoggingLevel;
import com.codahale.metrics.Timer.Context;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;

/**
 * 度量工具上下文.
 * 
 * @author gaohongtao
 */
public final class MetricsContext {
    
    private static final ThreadLocal<MetricsContext> CONTEXT = new ThreadLocal<>();
    
    private final Optional<MetricRegistry> metricRegistry;
    
    public MetricsContext(final boolean enable, final long period, final String packageName) {
        if (enable) {
            metricRegistry = Optional.of(new MetricRegistry());
            Slf4jReporter reporter = Slf4jReporter.forRegistry(metricRegistry.get())
                    .outputTo(LoggerFactory.getLogger(packageName))
                    .convertRatesTo(TimeUnit.SECONDS)
                    .convertDurationsTo(TimeUnit.MILLISECONDS)
                    .withLoggingLevel(LoggingLevel.DEBUG)
                    .build();
            reporter.start(period, TimeUnit.SECONDS);
        } else {
            metricRegistry = Optional.absent();
        }
    }
    
    /**
     * 注册度量上下文.
     */
    public void register() {
        if (metricRegistry.isPresent() && !this.equals(CONTEXT.get())) {
            CONTEXT.set(this);
        }
    }
    
    /**
     * 开始计时.
     *
     * @param name 度量目标名称
     * 
     * @return 计时上下文
     */
    public static Context start(final String... name) {
        return null == CONTEXT.get() ? null : CONTEXT.get().metricRegistry.get().timer(MetricRegistry.name(Joiner.on("-").join(name))).time();
    }
    
    /**
     * 停止计时.
     *
     * @param context 计时上下文
     */
    public static void stop(final Context context) {
        if (null != context) {
            context.stop();
        }
    }
}

@@ -707,41 +707,8 @@
 t;%0A%0A
-import org.slf4j.LoggerFactory;%0A%0A
 impo
@@ -904,279 +904,255 @@
 com.
-google.common.base.Joiner;%0Aimport com.google.common.base.Optional;%0A%0A/**%0A * %E5%BA%A6%E9%87%8F%E5%B7%A5%E5%85%B7%E4%B8%8A%E4%B8%8B%E6%96%87.%0A * %0A * @author gaohongtao%0A */%0Apublic final class MetricsContext %7B%0A    %0A    private static final ThreadLocal%3CMetricsContext%3E CONTEXT = new ThreadLocal%3C%3E();%0A    %0A    private final Optional%3C
+dangdang.ddframe.rdb.sharding.threadlocal.ThreadLocalObjectRepository;%0Aimport com.google.common.base.Joiner;%0Aimport org.slf4j.LoggerFactory;%0A%0A/**%0A * %E5%BA%A6%E9%87%8F%E5%B7%A5%E5%85%B7%E4%B8%8A%E4%B8%8B%E6%96%87.%0A * %0A * @author gaohongtao%0A */%0Apublic final class MetricsContext %7B%0A    %0A    private final 
 Metr
@@ -1161,17 +1161,16 @@
 Registry
-%3E
  metricR
@@ -1219,30 +1219,8 @@
 nal 
-boolean enable, final 
 long
@@ -1260,34 +1260,8 @@
 ) %7B%0A
-        if (enable) %7B%0A    
     
@@ -1285,20 +1285,8 @@
 y = 
-Optional.of(
 new 
@@ -1301,23 +1301,18 @@
 gistry()
-)
 ;%0A
-    
         
@@ -1380,20 +1380,10 @@
 stry
-.get())%0A    
+)%0A
     
@@ -1442,20 +1442,16 @@
 eName))%0A
-    
         
@@ -1500,36 +1500,32 @@
                 
-    
 .convertDuration
@@ -1567,20 +1567,16 @@
         
-    
 .withLog
@@ -1621,20 +1621,16 @@
         
- 
-   
 .build()
@@ -1627,28 +1627,24 @@
   .build();%0A
-    
         repo
@@ -1693,260 +1693,8 @@
     
-%7D else %7B%0A            metricRegistry = Optional.absent();%0A        %7D%0A    %7D%0A    %0A    /**%0A     * %E6%B3%A8%E5%86%8C%E5%BA%A6%E9%87%8F%E4%B8%8A%E4%B8%8B%E6%96%87.%0A     */%0A    public void register() %7B%0A        if (metricRegistry.isPresent() && !this.equals(CONTEXT.get())) %7B%0A            CONTEXT.set(this);%0A        %7D
 %0A   
@@ -1860,59 +1860,139 @@
     
-return null == CONTEXT.get() ? null : CONTEXT.get()
+MetricsContext context = ThreadLocalObjectRepository.getItem(MetricsContext.class);%0A        return null == context ? null : context
 .met
@@ -2007,14 +2007,8 @@
 try.
-get().
 time

Update MetricsContext.java