Author: davsclaus
Date: Fri Apr  9 06:11:51 2010
New Revision: 932265

URL: http://svn.apache.org/viewvc?rev=932265&view=rev
Log:
CAMEL-2627: Added StopWatch and time startup/shutdown times of Camel. Also time 
@Converter loader.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/StopWatch.java   
(with props)
    
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StopWatchTest.java   
(with props)
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java
 Fri Apr  9 06:11:51 2010
@@ -23,6 +23,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.component.file.GenericFile;
 import org.apache.camel.component.file.GenericFileEndpoint;
 import org.apache.camel.component.file.GenericFileOperations;
+import org.apache.camel.util.StopWatch;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -50,13 +51,12 @@ public class FileChangedExclusiveReadLoc
         try {
             long lastModified = Long.MIN_VALUE;
             long length = Long.MIN_VALUE;
-
-            long start = System.currentTimeMillis();
+            StopWatch watch = new StopWatch();
 
             while (!exclusive) {
                 // timeout check
                 if (timeout > 0) {
-                    long delta = System.currentTimeMillis() - start;
+                    long delta = watch.taken();
                     if (delta > timeout) {
                         LOG.warn("Cannot acquire read lock within " + timeout 
+ " millis. Will skip the file: " + file);
                         // we could not get the lock within the timeout 
period, so return false

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
 Fri Apr  9 06:11:51 2010
@@ -30,6 +30,7 @@ import org.apache.camel.component.file.G
 import org.apache.camel.component.file.GenericFileOperations;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StopWatch;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -57,13 +58,13 @@ public class FileLockExclusiveReadLockSt
             // try to acquire rw lock on the file before we can consume it
             FileChannel channel = new RandomAccessFile(target, 
"rw").getChannel();
 
-            long start = System.currentTimeMillis();
             boolean exclusive = false;
+            StopWatch watch = new StopWatch();
 
             while (!exclusive) {
                 // timeout check
                 if (timeout > 0) {
-                    long delta = System.currentTimeMillis() - start;
+                    long delta = watch.taken();
                     if (delta > timeout) {
                         LOG.warn("Cannot acquire read lock within " + timeout 
+ " millis. Will skip the file: " + target);
                         // we could not get the lock within the timeout 
period, so return false

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java
 Fri Apr  9 06:11:51 2010
@@ -21,6 +21,7 @@ import org.apache.camel.component.file.G
 import org.apache.camel.component.file.GenericFileEndpoint;
 import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
 import org.apache.camel.component.file.GenericFileOperations;
+import org.apache.camel.util.StopWatch;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -50,14 +51,13 @@ public class GenericFileRenameExclusiveR
         // make a copy as result and change its file name
         GenericFile<T> newFile = file.copyFrom(file);
         newFile.changeFileName(newName);
-
-        long start = System.currentTimeMillis();
+        StopWatch watch = new StopWatch();
 
         boolean exclusive = false;
         while (!exclusive) {
              // timeout check
             if (timeout > 0) {
-                long delta = System.currentTimeMillis() - start;
+                long delta = watch.taken();
                 if (delta > timeout) {
                     LOG.warn("Cannot acquire read lock within " + timeout + " 
millis. Will skip the file: " + file);
                     // we could not get the lock within the timeout period, so 
return false

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
 Fri Apr  9 06:11:51 2010
@@ -47,6 +47,7 @@ import org.apache.camel.util.CamelContex
 import org.apache.camel.util.ExpressionComparator;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StopWatch;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -884,10 +885,12 @@ public class MockEndpoint extends Defaul
             fail("Should have a latch!");
         }
 
-        long start = System.currentTimeMillis();
+        StopWatch watch = new StopWatch();
         waitForCompleteLatch(resultWaitTime);
-        long delta = System.currentTimeMillis() - start;
-        LOG.debug("Took " + delta + " millis to complete latch");
+        long delta = watch.stop();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Took " + delta + " millis to complete latch");
+        }
 
         if (resultMinimumWaitTime > 0 && delta < resultMinimumWaitTime) {
             fail("Expected minimum " + resultMinimumWaitTime

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 Fri Apr  9 06:11:51 2010
@@ -96,6 +96,7 @@ import org.apache.camel.util.LRUCache;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ReflectionInjector;
 import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.util.StopWatch;
 import org.apache.camel.util.URISupport;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -161,6 +162,7 @@ public class DefaultCamelContext extends
     private ShutdownRoute shutdownRoute = ShutdownRoute.Default;
     private ShutdownRunningTask shutdownRunningTask = 
ShutdownRunningTask.CompleteCurrentTaskOnly;
     private ExecutorServiceStrategy executorServiceStrategy = new 
DefaultExecutorServiceStrategy(this);
+    private final StopWatch stopWatch = new StopWatch(false);
 
     public DefaultCamelContext() {
         super();
@@ -1111,7 +1113,7 @@ public class DefaultCamelContext extends
 
         LOG.info("Started " + getRoutes().size() + " routes");
 
-        LOG.info("Apache Camel " + getVersion() + " (CamelContext: " + 
getName() + ") started");
+        LOG.info("Apache Camel " + getVersion() + " (CamelContext: " + 
getName() + ") started in " + stopWatch.stop() + " millis");
         EventHelper.notifyCamelContextStarted(this);
     }
 
@@ -1119,6 +1121,7 @@ public class DefaultCamelContext extends
     // -----------------------------------------------------------------------
 
     protected synchronized void doStart() throws Exception {
+        stopWatch.restart();
         LOG.info("Apache Camel " + getVersion() + " (CamelContext: " + 
getName() + ") is starting");
 
         try {
@@ -1200,6 +1203,7 @@ public class DefaultCamelContext extends
     }
 
     protected synchronized void doStop() throws Exception {
+        stopWatch.restart();
         LOG.info("Apache Camel " + getVersion() + " (CamelContext:" + 
getName() + ") is shutting down");
         EventHelper.notifyCamelContextStopping(this);
 
@@ -1236,7 +1240,7 @@ public class DefaultCamelContext extends
         // shutdown management as the last one
         shutdownServices(managementStrategy);
 
-        LOG.info("Apache Camel " + getVersion() + " (CamelContext: " + 
getName() + ") is shutdown");
+        LOG.info("Apache Camel " + getVersion() + " (CamelContext: " + 
getName() + ") is shutdown in " + stopWatch.stop() + " millis");
     }
 
     private void shutdownServices(Object service) {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
 Fri Apr  9 06:11:51 2010
@@ -38,6 +38,7 @@ import org.apache.camel.spi.ShutdownStra
 import org.apache.camel.util.EventHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.util.StopWatch;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -83,7 +84,7 @@ public class DefaultShutdownStrategy ext
     }
 
     public void shutdown(CamelContext context, List<RouteStartupOrder> routes, 
long timeout, TimeUnit timeUnit) throws Exception {
-        long start = System.currentTimeMillis();
+        StopWatch watch = new StopWatch();
 
         // should the order of routes be reversed?
         List<RouteStartupOrder> routesOrdered = new 
ArrayList<RouteStartupOrder>(routes);
@@ -121,9 +122,8 @@ public class DefaultShutdownStrategy ext
             throw ObjectHelper.wrapRuntimeCamelException(e.getCause());
         }
 
-        long delta = System.currentTimeMillis() - start;
         // convert to seconds as its easier to read than a big milli seconds 
number
-        long seconds = TimeUnit.SECONDS.convert(delta, TimeUnit.MILLISECONDS);
+        long seconds = TimeUnit.SECONDS.convert(watch.stop(), 
TimeUnit.MILLISECONDS);
 
         LOG.info("Graceful shutdown of " + routesOrdered.size() + " routes 
completed in " + seconds + " seconds");
     }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java 
Fri Apr  9 06:11:51 2010
@@ -32,6 +32,7 @@ import org.apache.camel.util.CamelContex
 import org.apache.camel.util.EventHelper;
 import org.apache.camel.util.LRUCache;
 import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.util.StopWatch;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -173,10 +174,10 @@ public class ProducerCache extends Servi
             }
         }
 
-        long start = 0;
+        StopWatch watch = null;
         if (exchange != null) {
             // record timing for sending the exchange using the producer
-            start = System.currentTimeMillis();
+            watch = new StopWatch();
         }
 
         try {
@@ -184,7 +185,7 @@ public class ProducerCache extends Servi
             return callback.doInProducer(producer, exchange, pattern);
         } finally {
             if (exchange != null) {
-                long timeTaken = System.currentTimeMillis() - start;
+                long timeTaken = watch.stop();
                 // emit event that the exchange was sent to the endpoint
                 EventHelper.notifyExchangeSent(exchange.getContext(), 
exchange, endpoint, timeTaken);
             }
@@ -220,12 +221,12 @@ public class ProducerCache extends Servi
                 exchange.setProperty(Exchange.TO_ENDPOINT, 
endpoint.getEndpointUri());
 
                 // send the exchange using the processor
-                long start = System.currentTimeMillis();
+                StopWatch watch = new StopWatch();
                 try {
                     producer.process(exchange);
                 } finally {
                     // emit event that the exchange was sent to the endpoint
-                    long timeTaken = System.currentTimeMillis() - start;
+                    long timeTaken = watch.stop();
                     EventHelper.notifyExchangeSent(exchange.getContext(), 
exchange, endpoint, timeTaken);
                 }
                 return exchange;

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
 Fri Apr  9 06:11:51 2010
@@ -52,6 +52,7 @@ public class AnnotationTypeConverterLoad
     private static final transient Log LOG = 
LogFactory.getLog(AnnotationTypeConverterLoader.class);
     protected PackageScanClassResolver resolver;
     private Set<Class<?>> visitedClasses = new HashSet<Class<?>>();
+    private Set<URL> visitedURLs = new HashSet<URL>();
 
     public AnnotationTypeConverterLoader(PackageScanClassResolver resolver) {
         this.resolver = resolver;
@@ -61,12 +62,19 @@ public class AnnotationTypeConverterLoad
     public void load(TypeConverterRegistry registry) throws Exception {
         String[] packageNames = findPackageNames();
         Set<Class<?>> classes = resolver.findAnnotated(Converter.class, 
packageNames);
+
+        LOG.info("Found " + packageNames.length + " packages with " + 
classes.size() + " @Converter classes to load");
+
         for (Class type : classes) {
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Loading converter class: " + 
ObjectHelper.name(type));
             }
             loadConverterMethods(registry, type);
         }
+
+        // now clear the maps so we do not hold references
+        visitedClasses.clear();
+        visitedURLs.clear();
     }
 
     /**
@@ -90,7 +98,12 @@ public class AnnotationTypeConverterLoad
         Enumeration<URL> resources = 
classLoader.getResources(META_INF_SERVICES);
         while (resources.hasMoreElements()) {
             URL url = resources.nextElement();
-            if (url != null) {
+            if (url != null && !visitedURLs.contains(url)) {
+                // remember we have visited this url so we wont read it twice
+                visitedURLs.add(url);
+                if (LOG.isDebugEnabled()) {
+                    LOG.info("Loading file " + META_INF_SERVICES + " to 
retrieve list of packages, from url: " + url);
+                }
                 BufferedReader reader = new BufferedReader(new 
InputStreamReader(url.openStream()));
                 try {
                     while (true) {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
 Fri Apr  9 06:11:51 2010
@@ -38,6 +38,7 @@ import org.apache.camel.spi.PackageScanC
 import org.apache.camel.spi.TypeConverterAware;
 import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StopWatch;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -354,10 +355,10 @@ public class DefaultTypeConverter extend
      * Checks if the registry is loaded and if not lazily load it
      */
     protected void loadTypeConverters() throws Exception {
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Loading type converters ...");
-        }
-        for (TypeConverterLoader typeConverterLoader : typeConverterLoaders) {
+        StopWatch watch = new StopWatch();
+
+        LOG.debug("Loading type converters ...");
+        for (TypeConverterLoader typeConverterLoader : 
getTypeConverterLoaders()) {
             typeConverterLoader.load(this);
         }
 
@@ -367,9 +368,10 @@ public class DefaultTypeConverter extend
         } catch (NoFactoryAvailableException e) {
             // ignore its fine to have none
         }
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Loading type converters done");
-        }
+        LOG.debug("Loading type converters done");
+
+        // report how long time it took to load
+        LOG.info("Loaded " + typeMappings.size() + " type converters in " + 
watch.stop() + " millis");
     }
 
     protected void loadFallbackTypeConverters() throws IOException, 
ClassNotFoundException {
@@ -386,6 +388,8 @@ public class DefaultTypeConverter extend
 
     @Override
     protected void doStop() throws Exception {
+        typeMappings.clear();
+        misses.clear();
     }
 
     /**

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
 Fri Apr  9 06:11:51 2010
@@ -44,6 +44,7 @@ import org.apache.camel.util.EventHelper
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.util.StopWatch;
 import org.apache.camel.util.concurrent.AtomicExchange;
 import org.apache.camel.util.concurrent.SubmitOrderedCompletionService;
 import org.apache.commons.logging.Log;
@@ -258,9 +259,9 @@ public class MulticastProcessor extends 
         TracedRouteNodes traced = exchange.getUnitOfWork() != null ? 
exchange.getUnitOfWork().getTracedRouteNodes() : null;
 
         // compute time taken if sending to another endpoint
-        long start = 0;
+        StopWatch watch = null;
         if (processor instanceof Producer) {
-            start = System.currentTimeMillis();
+            watch = new StopWatch();
         }
 
         try {
@@ -279,7 +280,7 @@ public class MulticastProcessor extends 
                 traced.popBlock();
             }
             if (processor instanceof Producer) {
-                long timeTaken = System.currentTimeMillis() - start;
+                long timeTaken = watch.stop();
                 Endpoint endpoint = ((Producer) processor).getEndpoint();
                 // emit event that the exchange was sent to the endpoint
                 EventHelper.notifyExchangeSent(exchange.getContext(), 
exchange, endpoint, timeTaken);

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/util/StopWatch.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/StopWatch.java?rev=932265&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/StopWatch.java 
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/StopWatch.java 
Fri Apr  9 06:11:51 2010
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.camel.util;
+
+/**
+ * A very simple stop watch.
+ * <p/>
+ * This implementation is not thread safe and can only time one task at any 
given time.
+ *
+ * @version $Revision$
+ */
+public final class StopWatch {
+
+    private long start;
+    private long stop;
+
+    /**
+     * Starts the stop watch
+     */
+    public StopWatch() {
+        this(true);
+    }
+
+    /**
+     * Creates the stop watch
+     *
+     * @param started whether it should start immediately
+     */
+    public StopWatch(boolean started) {
+        if (started) {
+            restart();
+        }
+    }
+
+    /**
+     * Starts or restarts the stop watch
+     */
+    public void restart() {
+        start = System.currentTimeMillis();
+        stop = 0;
+    }
+
+    /**
+     * Stops the stop watch
+     *
+     * @return the time taken in millis.
+     */
+    public long stop() {
+        stop = System.currentTimeMillis();
+        return taken();
+    }
+
+    /**
+     * Returns the time taken in millis.
+     *
+     * @return time in millis
+     */
+    public long taken() {
+        if (start > 0 && stop > 0) {
+            return stop - start;
+        } else if (start > 0) {
+            return System.currentTimeMillis() - start;
+        } else {
+            return 0;
+        }
+    }
+
+}

Propchange: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StopWatch.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StopWatch.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java?rev=932265&r1=932264&r2=932265&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java 
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java Fri 
Apr  9 06:11:51 2010
@@ -23,7 +23,9 @@ import java.util.concurrent.TimeUnit;
  * A helper class for working with times in various units
  * 
  * @version $Revision$
+ * @deprecated
  */
+...@deprecated
 public class Time {
     private long number;
     private TimeUnit timeUnit = TimeUnit.MILLISECONDS;

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StopWatchTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/StopWatchTest.java?rev=932265&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StopWatchTest.java 
(added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StopWatchTest.java 
Fri Apr  9 06:11:51 2010
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.camel.util;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Revision$
+ */
+public class StopWatchTest extends TestCase {
+
+    public void testStopWatch() throws Exception {
+        StopWatch watch = new StopWatch();
+        Thread.sleep(200);
+        long taken = watch.stop();
+
+        assertEquals(taken, watch.taken());
+        assertTrue("Should take approx 200 millis, was: " + taken, taken > 
190);
+    }
+
+    public void testStopWatchNotStarted() throws Exception {
+        StopWatch watch = new StopWatch(false);
+        long taken = watch.stop();
+        assertEquals(0, taken);
+
+        watch.restart();
+        Thread.sleep(200);
+        taken = watch.stop();
+
+        assertEquals(taken, watch.taken());
+        assertTrue("Should take approx 200 millis, was: " + taken, taken > 
190);
+    }
+
+    public void testStopWatchRestart() throws Exception {
+        StopWatch watch = new StopWatch();
+        Thread.sleep(200);
+        long taken = watch.stop();
+
+        assertEquals(taken, watch.taken());
+        assertTrue("Should take approx 200 millis, was: " + taken, taken > 
190);
+
+        watch.restart();
+        Thread.sleep(100);
+        taken = watch.stop();
+
+        assertEquals(taken, watch.taken());
+        assertTrue("Should take approx 100 millis, was: " + taken, taken > 90);
+    }
+
+    public void testStopWatchTaken() throws Exception {
+        StopWatch watch = new StopWatch();
+        Thread.sleep(100);
+        long taken = watch.taken();
+        Thread.sleep(100);
+        long taken2 = watch.taken();
+        assertNotSame(taken, taken2);
+        assertTrue(taken2 > taken);
+    }
+
+}

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StopWatchTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StopWatchTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to