Author: bvahdat
Date: Fri Apr 27 07:42:02 2012
New Revision: 1331270
URL: http://svn.apache.org/viewvc?rev=1331270&view=rev
Log:
CAMEL-4952: org.apache.camel.main.Main now provides API to bind/lookup beans
inside the Camel context this command line tool runs with.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/main/Main.java
camel/trunk/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainExample.java
camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/main/Main.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/main/Main.java?rev=1331270&r1=1331269&r2=1331270&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/main/Main.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/main/Main.java Fri
Apr 27 07:42:02 2012
@@ -23,6 +23,8 @@ import javax.xml.bind.JAXBException;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.SimpleRegistry;
+import org.apache.camel.spi.Registry;
import org.apache.camel.view.ModelFileGenerator;
/**
@@ -32,6 +34,7 @@ import org.apache.camel.view.ModelFileGe
*/
public class Main extends MainSupport {
protected static Main instance;
+ private final SimpleRegistry registery = new SimpleRegistry();
public Main() {
}
@@ -51,6 +54,49 @@ public class Main extends MainSupport {
public static Main getInstance() {
return instance;
}
+
+ /**
+ * Binds the given <code>name</code> to the <code>bean</code> object, so
+ * that it can be looked up inside the CamelContext this command line tool
+ * runs with.
+ *
+ * @param name the used name through which we do bind
+ * @param bean the object to bind
+ */
+ public void bind(String name, Object bean) {
+ registery.put(name, bean);
+ }
+
+ /**
+ * Using the given <code>name</code> does lookup for the bean being already
+ * bound using the {@link #bind(String, Object)} method.
+ *
+ * @see Registry#lookup(String)
+ */
+ public Object lookup(String name) {
+ return registery.get(name);
+ }
+
+ /**
+ * Using the given <code>name</code> and <code>type</code> does lookup for
+ * the bean being already bound using the {@link #bind(String, Object)}
+ * method.
+ *
+ * @see Registry#lookup(String, Class)
+ */
+ public <T> T lookup(String name, Class<T> type) {
+ return registery.lookup(name, type);
+ }
+
+ /**
+ * Using the given <code>type</code> does lookup for the bean being already
+ * bound using the {@link #bind(String, Object)} method.
+ *
+ * @see Registry#lookupByType(Class)
+ */
+ public <T> Map<String, T> lookupByType(Class<T> type) {
+ return registery.lookupByType(type);
+ }
// Implementation methods
//
-------------------------------------------------------------------------
@@ -73,7 +119,14 @@ public class Main extends MainSupport {
protected Map<String, CamelContext> getCamelContextMap() {
Map<String, CamelContext> answer = new HashMap<String, CamelContext>();
- answer.put("camel-1", new DefaultCamelContext());
+
+ CamelContext camelContext = new DefaultCamelContext();
+ if (registery.size() > 0) {
+ // set the registry through which we've already bound some beans
+ ((DefaultCamelContext) camelContext).setRegistry(registery);
+ }
+
+ answer.put("camel-1", camelContext);
return answer;
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/main/MainSupport.java?rev=1331270&r1=1331269&r2=1331270&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
Fri Apr 27 07:42:02 2012
@@ -136,7 +136,7 @@ public abstract class MainSupport extend
*/
public void run() throws Exception {
if (!completed.get()) {
- // if we have an issue starting the propagate exception to caller
+ // if we have an issue starting then propagate the exception to
caller
start();
try {
afterStart();
@@ -144,7 +144,7 @@ public abstract class MainSupport extend
beforeStop();
stop();
} catch (Exception e) {
- // while running then just log errors
+ // however while running then just log errors
LOG.error("Failed: " + e, e);
}
}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainExample.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainExample.java?rev=1331270&r1=1331269&r2=1331270&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainExample.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainExample.java
Fri Apr 27 07:42:02 2012
@@ -40,6 +40,8 @@ public class MainExample {
main = new Main();
// enable hangup support so you can press ctrl + c to terminate the JVM
main.enableHangupSupport();
+ // bind MyBean into the registery
+ main.bind("foo", new MyBean());
// add routes
main.addRouteBuilder(new MyRouteBuilder());
@@ -56,9 +58,15 @@ public class MainExample {
public void process(Exchange exchange) throws Exception {
System.out.println("Invoked timer at " + new Date());
}
- });
+ })
+ .beanRef("foo");
}
}
+ public static class MyBean {
+ public void callMe() {
+ System.out.println("MyBean.calleMe method has been called");
+ }
+ }
}
// END SNIPPET: e1
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainTest.java?rev=1331270&r1=1331269&r2=1331270&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/main/MainTest.java
Fri Apr 27 07:42:02 2012
@@ -23,6 +23,7 @@ import junit.framework.TestCase;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
/**
* @version
@@ -38,14 +39,16 @@ public class MainTest extends TestCase {
from("direct:start").to("mock:results");
}
});
+ main.bind("foo", new Integer(31));
main.start();
main.getCamelTemplate().sendBody("direct:start",
"<message>1</message>");
List<CamelContext> contextList = main.getCamelContexts();
assertNotNull(contextList);
- assertEquals("size", 1, contextList.size());
+ assertEquals("Did not get the expected count of Camel contexts", 1,
contextList.size());
CamelContext camelContext = contextList.get(0);
+ assertEquals("Could not find the registry bound object", 31,
((DefaultCamelContext) camelContext).getRegistry().lookup("foo"));
MockEndpoint endpoint = camelContext.getEndpoint("mock:results",
MockEndpoint.class);
endpoint.expectedMinimumMessageCount(1);