CAMEL-8332: Init using doStart/doStop instead of synchronized code.

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0e50f2c7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0e50f2c7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0e50f2c7

Branch: refs/heads/master
Commit: 0e50f2c70233ee2d4a3a083d5cb63360bd325770
Parents: 63faa18
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Feb 11 09:41:56 2015 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Feb 11 09:41:56 2015 +0100

----------------------------------------------------------------------
 .../camel/component/dozer/CustomMapper.java     |  2 +-
 .../camel/component/dozer/DozerComponent.java   |  3 -
 .../component/dozer/DozerConfiguration.java     |  4 +-
 .../camel/component/dozer/DozerEndpoint.java    | 62 ++++++++++----------
 .../camel/component/dozer/DozerProducer.java    |  9 +--
 5 files changed, 37 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0e50f2c7/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
index 7d46d4b..331de0c 100644
--- 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
+++ 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
@@ -59,7 +59,7 @@ public class CustomMapper extends BaseConverter {
 
     Object mapCustom(Object source) {
         Object customMapObj;
-        Method mapMethod = null;
+        Method mapMethod;
         
         // The converter parameter is stored in a thread local variable, so 
         // we need to parse the parameter on each invocation

http://git-wip-us.apache.org/repos/asf/camel/blob/0e50f2c7/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerComponent.java
 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerComponent.java
index 09a44ce..dcae3ac 100644
--- 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerComponent.java
+++ 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerComponent.java
@@ -22,9 +22,6 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.UriEndpointComponent;
 
-/**
- * Represents the component that manages {@link DozerEndpoint}.
- */
 public class DozerComponent extends UriEndpointComponent {
 
     public DozerComponent() {

http://git-wip-us.apache.org/repos/asf/camel/blob/0e50f2c7/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerConfiguration.java
 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerConfiguration.java
index c390c05..94f9e3d 100644
--- 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerConfiguration.java
+++ 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerConfiguration.java
@@ -16,20 +16,20 @@
  */
 package org.apache.camel.component.dozer;
 
+import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 import org.apache.camel.spi.UriPath;
 
 import static org.dozer.util.DozerConstants.DEFAULT_MAPPING_FILE;
 
-
 /**
  * Configuration used for a Dozer endpoint.
  */
 @UriParams
 public class DozerConfiguration {
 
-    @UriPath
+    @UriPath @Metadata(required = "true")
     private String name;
     @UriParam
     private String marshalId;

http://git-wip-us.apache.org/repos/asf/camel/blob/0e50f2c7/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerEndpoint.java
 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerEndpoint.java
index 0bcbda0..5753645 100644
--- 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerEndpoint.java
+++ 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerEndpoint.java
@@ -26,15 +26,14 @@ import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ResourceHelper;
 import org.dozer.CustomConverter;
 import org.dozer.DozerBeanMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/**
- * Represents a Dozer endpoint.
- */
 @UriEndpoint(scheme = "dozer", label = "transformation")
 public class DozerEndpoint extends DefaultEndpoint {
     private static final Logger LOG = 
LoggerFactory.getLogger(DozerEndpoint.class);
@@ -42,18 +41,14 @@ public class DozerEndpoint extends DefaultEndpoint {
     // IDs for built-in custom converters used with the Dozer component
     private static final String CUSTOM_MAPPING_ID = "_customMapping";
     private static final String LITERAL_MAPPING_ID = "_literalMapping";
-    
-    private DozerConfiguration configuration;
+
     private DozerBeanMapper mapper;
     private LiteralMapper literalMapper;
     private CustomMapper customMapper;
 
-    /**
-     * Create a new TransformEndpoint.
-     * @param endpointUri The uri of the Camel endpoint.
-     * @param component The {@link DozerComponent}.
-     * @param configuration endpoint configuration
-     */
+    @UriParam
+    private DozerConfiguration configuration;
+
     public DozerEndpoint(String endpointUri, Component component, 
DozerConfiguration configuration) throws Exception {
         super(endpointUri, component);
         this.configuration = configuration;
@@ -68,8 +63,7 @@ public class DozerEndpoint extends DefaultEndpoint {
 
     @Override
     public Consumer createConsumer(Processor processor) throws Exception {
-        throw new UnsupportedOperationException(
-                "Consumer not supported for Transform endpoints");
+        throw new UnsupportedOperationException("Consumer not supported for 
Dozer endpoints");
     }
 
     @Override
@@ -77,10 +71,7 @@ public class DozerEndpoint extends DefaultEndpoint {
         return true;
     }
 
-    public synchronized DozerBeanMapper getMapper() throws Exception {
-        if (mapper == null) {
-            initDozer();
-        }
+    public DozerBeanMapper getMapper() throws Exception {
         return mapper;
     }
 
@@ -100,30 +91,41 @@ public class DozerEndpoint extends DefaultEndpoint {
         return literalMapper;
     }
 
-    private void initDozer() throws Exception {
-        mapper = new DozerBeanMapper();
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        if (mapper == null) {
+            mapper = createDozerBeanMapper();
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        // noop
+    }
+
+    private DozerBeanMapper createDozerBeanMapper() throws Exception {
+        DozerBeanMapper answer = new DozerBeanMapper();
         InputStream mapStream = null;
         try {
             LOG.info("Loading Dozer mapping file {}.", 
configuration.getMappingFile());
             // create the mapper instance and add the mapping file
-            mapStream = ResourceHelper.resolveResourceAsInputStream(
-                    getCamelContext().getClassResolver(), 
configuration.getMappingFile());
-            if (mapStream == null) {
-                throw new Exception("Unable to resolve Dozer config: " + 
configuration.getMappingFile());
-            }
-            mapper.addMapping(mapStream);
+            mapStream = 
ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(),
 configuration.getMappingFile());
+            answer.addMapping(mapStream);
             
             // add our built-in converters
             Map<String, CustomConverter> converters = new HashMap<String, 
CustomConverter>();
             converters.put(CUSTOM_MAPPING_ID, customMapper);
             converters.put(LITERAL_MAPPING_ID, literalMapper);
-            converters.putAll(mapper.getCustomConvertersWithId());
-            mapper.setCustomConvertersWithId(converters);
+            converters.putAll(answer.getCustomConvertersWithId());
+            answer.setCustomConvertersWithId(converters);
             
         } finally {
-            if (mapStream != null) {
-                mapStream.close();
-            }
+            IOHelper.close(mapStream);
         }
+
+        return answer;
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/0e50f2c7/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
index fd7f6c4..1de9cf3 100644
--- 
a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
+++ 
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.component.dozer;
 
-import org.apache.camel.CamelContextAware;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.impl.DefaultProducer;
@@ -117,9 +116,7 @@ public class DozerProducer extends DefaultProducer {
             // Stop/shutdown is handled when the corresponding methods are 
             // called on this producer.
             unmarshaller = new UnmarshalProcessor(dataFormat);
-            if (unmarshaller instanceof CamelContextAware) {
-                
((CamelContextAware)unmarshaller).setCamelContext(exchange.getContext());
-            }
+            unmarshaller.setCamelContext(exchange.getContext());
             unmarshaller.start();
         }
         return unmarshaller;
@@ -142,9 +139,7 @@ public class DozerProducer extends DefaultProducer {
             // Stop/shutdown is handled when the corresponding methods are 
             // called on this producer.
             marshaller = new MarshalProcessor(dataFormat);
-            if (marshaller instanceof CamelContextAware) {
-                
((CamelContextAware)marshaller).setCamelContext(exchange.getContext());
-            }
+            marshaller.setCamelContext(exchange.getContext());
             marshaller.start();
         }
         return marshaller;

Reply via email to