camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r902112 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/impl/converter/ camel-core/src/test/java/org/apache/camel/converter/ camel-core/src/test/java/org/apache/camel/impl/ exampl...
Date Fri, 22 Jan 2010 15:19:55 GMT
Author: davsclaus
Date: Fri Jan 22 15:19:50 2010
New Revision: 902112

URL: http://svn.apache.org/viewvc?rev=902112&view=rev
Log:
CAMEL-2392: DefaultTypeConverter loads @Converter from classpath up-front which means reduced
contention for detecting whether its initialized at runtime. Fixed spelling in example.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
    camel/trunk/examples/camel-example-cxf-async/src/main/resources/META-INF/spring/camel-client.xml

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=902112&r1=902111&r2=902112&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
Jan 22 15:19:50 2010
@@ -685,6 +685,11 @@
                 // we can synchronize on this as there is only one instance
                 // of the camel context (its the container)
                 typeConverter = createTypeConverter();
+                try {
+                    addService(typeConverter);
+                } catch (Exception e) {
+                    throw ObjectHelper.wrapRuntimeCamelException(e);
+                }
             }
         }
         return typeConverter;

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=902112&r1=902111&r2=902112&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 Jan 22 15:19:50 2010
@@ -25,13 +25,13 @@
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
 import org.apache.camel.NoFactoryAvailableException;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.TypeConverter;
+import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.Injector;
 import org.apache.camel.spi.PackageScanClassResolver;
@@ -40,7 +40,6 @@
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
 
 /**
  * Default implementation of a type converter registry used for
@@ -48,7 +47,7 @@
  *
  * @version $Revision$
  */
-public class DefaultTypeConverter implements TypeConverter, TypeConverterRegistry {
+public class DefaultTypeConverter extends ServiceSupport implements TypeConverter, TypeConverterRegistry
{
     private static final transient Log LOG = LogFactory.getLog(DefaultTypeConverter.class);
     private final Map<TypeMapping, TypeConverter> typeMappings = new ConcurrentHashMap<TypeMapping,
TypeConverter>();
     private final Map<TypeMapping, TypeMapping> misses = new ConcurrentHashMap<TypeMapping,
TypeMapping>();
@@ -56,13 +55,11 @@
     private final List<TypeConverter> fallbackConverters = new ArrayList<TypeConverter>();
     private Injector injector;
     private final FactoryFinder factoryFinder;
-    private AtomicBoolean loaded = new AtomicBoolean();
 
     public DefaultTypeConverter(PackageScanClassResolver resolver, Injector injector, FactoryFinder
factoryFinder) {
         this.injector = injector;
         this.factoryFinder = factoryFinder;
-
-        typeConverterLoaders.add(new AnnotationTypeConverterLoader(resolver));
+        this.typeConverterLoaders.add(new AnnotationTypeConverterLoader(resolver));
 
         // add to string first as it will then be last in the last as to string can nearly
         // always convert something to a string so we want it only as the last resort
@@ -156,9 +153,6 @@
             return Void.TYPE;
         }
 
-        // make sure we have loaded the converters
-        checkLoaded();
-
         // try to find a suitable type converter
         TypeConverter converter = getOrFindTypeConverter(type, value);
         if (converter != null) {
@@ -250,9 +244,6 @@
     }
 
     public Set<Class<?>> getFromClassMappings() {
-        // make sure we have loaded the converters
-        checkLoaded();
-
         Set<Class<?>> answer = new HashSet<Class<?>>();
         synchronized (typeMappings) {
             for (TypeMapping mapping : typeMappings.keySet()) {
@@ -276,9 +267,6 @@
     }
 
     public Map<TypeMapping, TypeConverter> getTypeMappings() {
-        // make sure we have loaded the converters
-        checkLoaded();
-
         return typeMappings;
     }
 
@@ -302,9 +290,6 @@
     }
 
     public TypeConverter lookup(Class<?> toType, Class<?> fromType) {
-        // make sure we have loaded the converters
-        checkLoaded();
-
         return doLookup(toType, fromType, false);
     }
 
@@ -368,31 +353,22 @@
     /**
      * Checks if the registry is loaded and if not lazily load it
      */
-    protected synchronized void checkLoaded() {
-        // must be synchronized to let other threads wait for it to initialize
-        // also use a atomic boolean so its state is visible for the other threads
-        // this ensure that at most one thread is loading all the type converters
-        if (loaded.compareAndSet(false, true)) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Loading type converters ...");
-            }
-            try {
-                for (TypeConverterLoader typeConverterLoader : typeConverterLoaders) {
-                    typeConverterLoader.load(this);
-                }
+    protected void loadTypeConverters() throws Exception {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Loading type converters ...");
+        }
+        for (TypeConverterLoader typeConverterLoader : typeConverterLoaders) {
+            typeConverterLoader.load(this);
+        }
 
-                // lets try load any other fallback converters
-                try {
-                    loadFallbackTypeConverters();
-                } catch (NoFactoryAvailableException e) {
-                    // ignore its fine to have none
-                }
-            } catch (Exception e) {
-                throw wrapRuntimeCamelException(e);
-            }
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Loading type converters done");
-            }
+        // lets try load any other fallback converters
+        try {
+            loadFallbackTypeConverters();
+        } catch (NoFactoryAvailableException e) {
+            // ignore its fine to have none
+        }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Loading type converters done");
         }
     }
 
@@ -403,6 +379,15 @@
         }
     }
 
+    @Override
+    protected void doStart() throws Exception {
+        loadTypeConverters();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+    }
+
     /**
      * Represents a mapping from one type (which can be null) to another
      */

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java?rev=902112&r1=902111&r2=902112&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java Fri
Jan 22 15:19:50 2010
@@ -40,6 +40,7 @@
 import org.apache.camel.util.CastUtils;
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ReflectionInjector;
+import org.apache.camel.util.ServiceHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -66,6 +67,7 @@
     @Override
     protected void setUp() throws Exception {
         PropertyEditorManager.registerEditor(Integer.class, IntegerPropertyEditor.class);
+        ServiceHelper.startService(converter);
     }
 
     public void testIntegerPropertyEditorConversion() throws Exception {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java?rev=902112&r1=902111&r2=902112&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java Fri Jan
22 15:19:50 2010
@@ -22,6 +22,7 @@
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamSource;
 
+import org.apache.camel.util.ServiceHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -44,6 +45,12 @@
     protected TypeConverter converter = new DefaultTypeConverter(new DefaultPackageScanClassResolver(),
             new ReflectionInjector(), new DefaultFactoryFinderResolver().resolveDefaultFactoryFinder(new
DefaultClassResolver()));
 
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        ServiceHelper.startService(converter);
+    }
+
     public void testConvertToDocument() throws Exception {
         Document document = converter
                 .convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java?rev=902112&r1=902111&r2=902112&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
Fri Jan 22 15:19:50 2010
@@ -29,6 +29,7 @@
 import org.apache.camel.impl.DefaultPackageScanClassResolver;
 import org.apache.camel.impl.converter.DefaultTypeConverter;
 import org.apache.camel.util.ReflectionInjector;
+import org.apache.camel.util.ServiceHelper;
 
 /**
  * @version $Revision$
@@ -38,6 +39,12 @@
             new ReflectionInjector(), new DefaultFactoryFinderResolver().resolveDefaultFactoryFinder(new
DefaultClassResolver()));
     protected String expectedBody = "<hello>world!</hello>";
 
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        ServiceHelper.startService(converter);
+    }
+
     public void testSerialization() throws Exception {
         StringSource expected = new StringSource(expectedBody, "mySystemID", "utf-8");
         expected.setPublicId("myPublicId");

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java?rev=902112&r1=902111&r2=902112&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
Fri Jan 22 15:19:50 2010
@@ -50,8 +50,8 @@
 
         context.stop();
 
-        assertEquals(7, dummy1.getEvents().size());
-        assertEquals(7, dummy2.getEvents().size());
+        assertEquals(8, dummy1.getEvents().size());
+        assertEquals(8, dummy2.getEvents().size());
 
         assertEquals("onContextStart", dummy1.getEvents().get(0));
         assertEquals("onContextStart", dummy2.getEvents().get(0));
@@ -59,14 +59,16 @@
         assertEquals("onServiceAdd", dummy2.getEvents().get(1));
         assertEquals("onServiceAdd", dummy1.getEvents().get(2));
         assertEquals("onServiceAdd", dummy2.getEvents().get(2));
-        assertEquals("onComponentAdd", dummy1.getEvents().get(3));
-        assertEquals("onComponentAdd", dummy2.getEvents().get(3));
-        assertEquals("onEndpointAdd", dummy1.getEvents().get(4));
-        assertEquals("onEndpointAdd", dummy2.getEvents().get(4));
-        assertEquals("onComponentRemove", dummy1.getEvents().get(5));
-        assertEquals("onComponentRemove", dummy2.getEvents().get(5));
-        assertEquals("onContextStop", dummy1.getEvents().get(6));
-        assertEquals("onContextStop", dummy2.getEvents().get(6));
+        assertEquals("onServiceAdd", dummy1.getEvents().get(3));
+        assertEquals("onServiceAdd", dummy2.getEvents().get(3));
+        assertEquals("onComponentAdd", dummy1.getEvents().get(4));
+        assertEquals("onComponentAdd", dummy2.getEvents().get(4));
+        assertEquals("onEndpointAdd", dummy1.getEvents().get(5));
+        assertEquals("onEndpointAdd", dummy2.getEvents().get(5));
+        assertEquals("onComponentRemove", dummy1.getEvents().get(6));
+        assertEquals("onComponentRemove", dummy2.getEvents().get(6));
+        assertEquals("onContextStop", dummy1.getEvents().get(7));
+        assertEquals("onContextStop", dummy2.getEvents().get(7));
     }
 
 }

Modified: camel/trunk/examples/camel-example-cxf-async/src/main/resources/META-INF/spring/camel-client.xml
URL: http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-cxf-async/src/main/resources/META-INF/spring/camel-client.xml?rev=902112&r1=902111&r2=902112&view=diff
==============================================================================
--- camel/trunk/examples/camel-example-cxf-async/src/main/resources/META-INF/spring/camel-client.xml
(original)
+++ camel/trunk/examples/camel-example-cxf-async/src/main/resources/META-INF/spring/camel-client.xml
Fri Jan 22 15:19:50 2010
@@ -33,7 +33,7 @@
         <!-- this template is used to send the messages from the client -->
         <template id="camelTemplate"/>
 
-        <!-- this is a mock endpoint which we can use to test that the messages is routed
as expectd -->
+        <!-- this is a mock endpoint which we can use to test that the messages is routed
as expected -->
         <endpoint id="result" uri="mock:result"/>
 
         <route>



Mime
View raw message