activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r530555 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/spi/ camel-spring/src/main/java/org/apache/camel/spring/spi/
Date Thu, 19 Apr 2007 21:34:08 GMT
Author: chirino
Date: Thu Apr 19 14:34:07 2007
New Revision: 530555

URL: http://svn.apache.org/viewvc?view=rev&rev=530555
Log:
Cleaned up auto component creation.  Added an option to disable auto creation of components.
Also added a test case to verify that it works.

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ComponentResolver.java
    activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringComponentResolver.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?view=diff&rev=530555&r1=530554&r2=530555
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Thu Apr
19 14:34:07 2007
@@ -41,6 +41,9 @@
      */
     void addComponent(String componentName, Component component);
 
+    /**
+     * Gets a component from the container by name.
+     */
     Component getComponent(String componentName);
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?view=diff&rev=530555&r1=530554&r2=530555
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
Thu Apr 19 14:34:07 2007
@@ -62,7 +62,8 @@
     private ExchangeConverter exchangeConverter;
     private Injector injector;
     private ComponentResolver componentResolver;
-
+    private boolean autoCreateComponents=true;
+    
     /**
      * Adds a component to the container.
      */
@@ -76,9 +77,25 @@
         }
     }
 
-    public Component getComponent(String componentName) {
+    public Component getComponent(String name) {
+    	// synchronize the look up and auto create so that 2 threads can't 
+    	// concurrently auto create the same component. 
         synchronized (components) {
-            return components.get(componentName);
+        	Component component = components.get(name);
+        	if( component == null && autoCreateComponents ) {
+                try {
+					component = getComponentResolver().resolveComponent(name, this);
+					addComponent(name, component);
+					if( isStarted() ) {
+						// If the component is looked up after the context is started,
+						// lets start it up.
+						ServiceHelper.startServices(component);
+					}
+				} catch (Exception e) {
+					throw new RuntimeCamelException("Could not auto create component: "+name, e);
+				}
+        	}
+        	return component;
         }
     }
 
@@ -109,13 +126,13 @@
                 try {
                     component = factory.call();
                     if (component == null) {
-                        throw new IllegalArgumentException("Factory failed to create the
" + componentName + " component, it returned null.");
+                        throw new RuntimeCamelException("Factory failed to create the " +
componentName + " component, it returned null.");
                     }
                     components.put(componentName, component);
                     component.setCamelContext(this);
                 }
                 catch (Exception e) {
-                    throw new IllegalArgumentException("Factory failed to create the " +
componentName + " component", e);
+                    throw new RuntimeCamelException("Factory failed to create the " + componentName
+ " component", e);
                 }
             }
             return component;
@@ -147,22 +164,8 @@
                         throw new IllegalArgumentException("Invalid URI, it did not contain
a scheme: " + uri);
                     }
                     String scheme = splitURI[0];
-                    Component component = null;
+                    Component component = getComponent(scheme);
                     
-                	// synchronize the look up and auto create so that 2 threads can't 
-                	// concurrently auto create the same component. 
-                    synchronized (components) {
-                    	component = components.get(scheme);
-                    	if( component == null ) {
-                            component = getComponentResolver().resolveComponent(uri, this);
-                    		addComponent(scheme, component);
-                    		if( isStarted() ) {
-                    			// If the component is looked up after the context is started,
-                    			// lets start it up.
-                    			ServiceHelper.startServices(component);
-                    		}
-                    	}
-                    }
                     
                 	// Ask the component to resolve the endpoint.
                     if (component != null) {
@@ -346,5 +349,13 @@
     protected ComponentResolver createComponentResolver() {
         return new DefaultComponentResolver();
     }
+
+	public boolean isAutoCreateComponents() {
+		return autoCreateComponents;
+	}
+
+	public void setAutoCreateComponents(boolean autoCreateComponents) {
+		this.autoCreateComponents = autoCreateComponents;
+	}
 
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java?view=diff&rev=530555&r1=530554&r2=530555
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponentResolver.java
Thu Apr 19 14:34:07 2007
@@ -23,7 +23,6 @@
 import org.apache.camel.spi.ComponentResolver;
 import org.apache.camel.util.FactoryFinder;
 import org.apache.camel.util.NoFactoryAvailableException;
-import org.apache.camel.util.ObjectHelper;
 
 /**
  * The default implementation of {@link ComponentResolver}
@@ -36,21 +35,16 @@
 public class DefaultComponentResolver<E extends Exchange> implements ComponentResolver<E>
{
     protected static final FactoryFinder componentFactory = new FactoryFinder("META-INF/services/org/apache/camel/component/");
 
-    public Component<E> resolveComponent(String uri, CamelContext context) {
-        String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 2);
-        if (splitURI[1] == null) {
-            throw new IllegalArgumentException("Invalid URI, it did not contain a scheme:
" + uri);
-        }
-        String scheme = splitURI[0];
+    public Component<E> resolveComponent(String name, CamelContext context) {
         Class type;
         try {
-            type = componentFactory.findClass(scheme);
+            type = componentFactory.findClass(name);
         }
         catch (NoFactoryAvailableException e) {
             return null;
         }
         catch (Throwable e) {
-            throw new IllegalArgumentException("Invalid URI, no EndpointResolver registered
for scheme : " + scheme, e);
+            throw new IllegalArgumentException("Invalid URI, no EndpointResolver registered
for scheme : " + name, e);
         }
         if (type == null) {
             return null;

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ComponentResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ComponentResolver.java?view=diff&rev=530555&r1=530554&r2=530555
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ComponentResolver.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ComponentResolver.java
Thu Apr 19 14:34:07 2007
@@ -33,9 +33,9 @@
     /**
      * Attempts to resolve the component for the given URI
      *
-     * @param uri the URI to resolve
+     * @param name the component name to resolve
      * @param context the context to load the component if it can be resolved
      * @return the component which is added to the context or null if it can not be resolved
      */
-    Component<E> resolveComponent(String uri, CamelContext context) throws Exception;
+    Component<E> resolveComponent(String name, CamelContext context) throws Exception;
 }

Modified: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringComponentResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringComponentResolver.java?view=diff&rev=530555&r1=530554&r2=530555
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringComponentResolver.java
(original)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringComponentResolver.java
Thu Apr 19 14:34:07 2007
@@ -17,15 +17,14 @@
  */
 package org.apache.camel.spring.spi;
 
+import static org.apache.camel.util.ObjectHelper.notNull;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.spi.ComponentResolver;
-import static org.apache.camel.util.ObjectHelper.notNull;
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 import org.springframework.context.ApplicationContext;
 
-import java.net.URI;
-
 /**
  * An implementation of {@link ComponentResolver} which tries to find a Camel {@link Component}
  * in the Spring {@link ApplicationContext} first; if its not there it defaults to the auto-discovery
mechanism.
@@ -42,11 +41,10 @@
         this.nextResolver = nextResolver;
     }
 
-    public Component resolveComponent(String uri, CamelContext context) throws Exception
{
-        String scheme = new URI(uri).getScheme();
+    public Component resolveComponent(String name, CamelContext context) throws Exception
{
         Object bean = null;
         try {
-            bean = applicationContext.getBean(scheme);
+            bean = applicationContext.getBean(name);
         }
         catch (NoSuchBeanDefinitionException e) {
             // ignore its not an error
@@ -56,12 +54,12 @@
                 return (Component) bean;
             }
             else {
-                throw new IllegalArgumentException("Bean with name: " + bean + " in spring
context is not a Component: " + bean);
+                throw new IllegalArgumentException("Bean with name: " + name + " in spring
context is not a Component: " + bean);
             }
         }
         if (nextResolver == null) {
             return null;
         }
-        return nextResolver.resolveComponent(uri, context);
+        return nextResolver.resolveComponent(name, context);
     }
 }



Mime
View raw message