activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r520054 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel: ./ builder/ impl/ queue/ util/
Date Mon, 19 Mar 2007 19:25:57 GMT
Author: chirino
Date: Mon Mar 19 12:25:56 2007
New Revision: 520054

URL: http://svn.apache.org/viewvc?view=rev&rev=520054
Log:
Got rid of the ugly singleton component and by lazily creating and registering it with the
container.


Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContainer.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/EndpointResolver.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointResolver.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueEndpointResolver.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContainer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContainer.java?view=diff&rev=520054&r1=520053&r2=520054
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContainer.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContainer.java Mon
Mar 19 12:25:56 2007
@@ -17,9 +17,9 @@
  */
 package org.apache.camel;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.Callable;
 
 import org.apache.camel.impl.DefaultEndpointResolver;
 import org.apache.camel.impl.DefaultExchangeConverter;
@@ -33,7 +33,7 @@
 	
     private EndpointResolver<E> endpointResolver;
     private ExchangeConverter exchangeConverter;
-    private Map<String, Object> components = (Map<String, Object>)Collections.synchronizedMap(new
HashMap<String, Object>());
+    private Map<String, Component> components = new HashMap<String, Component>();
 
     public EndpointResolver<E> getEndpointResolver() {
         if (endpointResolver == null) {
@@ -70,10 +70,19 @@
         return new DefaultExchangeConverter();
     }
 
-	public Map<String, Object> getComponents() {
-		return components;
-	}
-	public void setComponents(Map<String, Object> components) {
-		this.components = Collections.synchronizedMap(components);
+	public Component getOrCreateComponent(String componentName, Callable<Component> factory)
{
+		synchronized(components) { 
+			Component component = components.get(componentName);
+			if( component == null ) {
+				try {
+					component = factory.call();
+					if( component == null )
+						throw new IllegalArgumentException("Factory failed to create the "+componentName+"
component, it returned null.");
+				} catch (Exception e) {
+					throw new IllegalArgumentException("Factory failed to create the "+componentName+" component",
e);
+				}
+			}
+			return component;
+		}
 	}
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/EndpointResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/EndpointResolver.java?view=diff&rev=520054&r1=520053&r2=520054
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/EndpointResolver.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/EndpointResolver.java Mon
Mar 19 12:25:56 2007
@@ -24,7 +24,14 @@
 public interface EndpointResolver<E> {
 
     /**
-     * Resolves the given uri or returns null if no endpoint could be found
+     * Resolves the component for a given uri or returns null if now component handles it.
      */
-    public Endpoint<E> resolve(CamelContainer container, String uri);
+    public Component resolveComponent(CamelContainer container, String uri);
+
+
+    /**
+     * Resolves the endpoint for a given uri or returns null if no endpoint could be found
+     */
+    public Endpoint<E> resolveEndpoint(CamelContainer container, String uri);
+    
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?view=diff&rev=520054&r1=520053&r2=520054
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
Mon Mar 19 12:25:56 2007
@@ -52,7 +52,7 @@
     public Endpoint<E> endpoint(String uri) {
          CamelContainer<E> c = getContainer();
          EndpointResolver<E> er = c.getEndpointResolver();
-         return er.resolve(c, uri);
+         return er.resolveEndpoint(c, uri);
     }
 
     public DestinationBuilder<E> from(String uri) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointResolver.java?view=diff&rev=520054&r1=520053&r2=520054
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointResolver.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointResolver.java
Mon Mar 19 12:25:56 2007
@@ -17,22 +17,40 @@
 package org.apache.camel.impl;
 
 import org.apache.camel.CamelContainer;
+import org.apache.camel.Component;
 import org.apache.camel.Endpoint;
 import org.apache.camel.EndpointResolver;
 import org.apache.camel.util.FactoryFinder;
 import org.apache.camel.util.ObjectHelper;
 
 /**
- * A default implementation of {@link org.apache.camel.EndpointResolver}
+ * An implementation of {@link org.apache.camel.EndpointResolver} that delegates to 
+ * other {@link EndpointResolver} which are selected based on the uri prefix.
+ * 
+ * The delegate {@link EndpointResolver} are associated with uri prefixes by 
+ * adding a property file with the same uri prefix in the
+ * META-INF/services/org/apache/camel/EndpointResolver/
+ * directory on the classpath.
  *
  * @version $Revision$
  */
 public class DefaultEndpointResolver<E> implements EndpointResolver<E> {
     static final private FactoryFinder endpointResolverFactory = new FactoryFinder("META-INF/services/org/apache/camel/EndpointResolver/");
     
-    public Endpoint<E> resolve(CamelContainer container, String uri) {
-    	String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":");
-    	if( splitURI == null )
+    public Endpoint<E> resolveEndpoint(CamelContainer container, String uri) {
+    	EndpointResolver resolver = getDelegate(uri);
+		return resolver.resolveEndpoint(container, uri);
+    }
+
+
+	public Component resolveComponent(CamelContainer container, String uri) {
+    	EndpointResolver resolver = getDelegate(uri);
+		return resolver.resolveComponent(container, uri);
+	}
+
+	private EndpointResolver getDelegate(String uri) {
+		String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 2);
+    	if( splitURI[1] == null )
     		throw new IllegalArgumentException("Invalid URI, it did not contain a scheme: "+uri);
     	EndpointResolver resolver;
 		try {
@@ -40,11 +58,7 @@
 		} catch (Throwable e) {
 			throw new IllegalArgumentException("Invalid URI, no EndpointResolver registered for scheme
: "+splitURI[0], e);
 		}
-
-		return resolver.resolve(container, uri);
-		
-		// EndpointResolvers could be more recusive in nature if we resolved the reset of the of
the URI 
-		//return resolver.resolve(container, splitURI[1]);
-    }
+		return resolver;
+	}
 
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueComponent.java?view=diff&rev=520054&r1=520053&r2=520054
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueComponent.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueComponent.java
Mon Mar 19 12:25:56 2007
@@ -20,13 +20,15 @@
 import java.util.Queue;
 import java.util.concurrent.LinkedBlockingQueue;
 
+import org.apache.camel.Component;
+
 /**
  * Represents the component that manages {@link QueueEndpoint}.  It holds the 
  * list of named queues that queue endpoints reference.
  *
  * @version $Revision: 519973 $
  */
-public class QueueComponent<E> {
+public class QueueComponent<E> implements Component<E> {
 	
     private HashMap<String, Queue<E>> registry = new HashMap<String, Queue<E>>();
 

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueEndpointResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueEndpointResolver.java?view=diff&rev=520054&r1=520053&r2=520054
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueEndpointResolver.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/queue/QueueEndpointResolver.java
Mon Mar 19 12:25:56 2007
@@ -17,29 +17,64 @@
 package org.apache.camel.queue;
 
 import java.util.Queue;
+import java.util.concurrent.Callable;
 
 import org.apache.camel.CamelContainer;
+import org.apache.camel.Component;
 import org.apache.camel.Endpoint;
 import org.apache.camel.EndpointResolver;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * An implementation of {@link EndpointResolver} that creates 
  * {@link QueueEndpoint} objects.
  *
+ * The synatx for a Queue URI looks like:
+ * 
+ * <pre><code>queue:[component:]queuename</code></pre>
+ * the component is optional, and if it is not specified, the default component name
+ * is assumed.
+ * 
  * @version $Revision: 519901 $
  */
 public class QueueEndpointResolver<E> implements EndpointResolver<E> {
 	
-	static QueueComponent defaultComponent = new QueueComponent();
+	public static final String DEFAULT_COMPONENT_NAME = QueueComponent.class.getName();
 	
-    public Endpoint<E> resolve(CamelContainer container, String uri) {
-        
-    	// TODO: we could look at the uri scheme to look for a named
-    	// component registered on the container
-    	QueueComponent<E> component = defaultComponent;
-        
+	/**
+	 * Finds the {@see QueueComponent} specified by the uri.  If the {@see QueueComponent} 
+	 * object do not exist, it will be created.
+	 * 
+	 * @see org.apache.camel.EndpointResolver#resolveComponent(org.apache.camel.CamelContainer,
java.lang.String)
+	 */
+	public Component resolveComponent(CamelContainer container, String uri) {
+		String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 3);
+		return resolveQueueComponent(container, splitURI);
+	}
+
+	/**
+	 * Finds the {@see QueueEndpoint} specified by the uri.  If the {@see QueueEndpoint} or
it's associated
+	 * {@see QueueComponent} object do not exist, they will be created.
+	 * 
+	 * @see org.apache.camel.EndpointResolver#resolveEndpoint(org.apache.camel.CamelContainer,
java.lang.String)
+	 */
+	public Endpoint<E> resolveEndpoint(CamelContainer container, String uri) {
+		String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 3);        
+    	QueueComponent<E> component = resolveQueueComponent(container, splitURI);    
   
         Queue<E> queue = component.getOrCreateQueue(uri);
 		return new QueueEndpoint<E>(uri, container, queue);
     }
+
+	private QueueComponent<E> resolveQueueComponent(CamelContainer container, String[]
splitURI) {
+		String componentName = DEFAULT_COMPONENT_NAME;
+    	if( splitURI[2] != null ) {
+    		componentName =  splitURI[1];
+    	}
+    	Component rc = container.getOrCreateComponent(componentName, new Callable<Component<E>>(){
+			public Component<E> call() throws Exception {
+				return new QueueComponent<E>();
+			}});
+    	return (QueueComponent<E>) rc;
+	}
 
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?view=diff&rev=520054&r1=520053&r2=520054
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
Mon Mar 19 12:25:56 2007
@@ -33,12 +33,19 @@
         }
     }
     
-    public static String[] splitOnCharacter(String value, String needle) {
-		int i = value.indexOf(needle);
-		if( i < 0 ) {
-			return null;
-		}		
-		return new String[] {value.substring(0, i), value.substring(i+1)};
+    public static String[] splitOnCharacter(String value, String needle, int count) {
+    	String rc [] = new String[count];
+    	rc[0] = value;
+    	for ( int i=1; i < count; i++ ) {
+    		String v = rc[i-1];
+			int p = v.indexOf(needle);
+			if( p < 0 ) {
+				return rc;
+			} 
+			rc[i-1] = v.substring(0,p);
+			rc[i] = v.substring(p+1);
+    	}
+    	return rc;
 	}
 
 }



Mime
View raw message