Author: ningjiang
Date: Wed Nov 25 07:19:03 2009
New Revision: 884000

URL: http://svn.apache.org/viewvc?rev=884000&view=rev
Log:
CAMEL-2182 Got camel-example-guice to work in Servicemix

Added:
    
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyOSGiModule.java
   (with props)
    
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java
   (with props)
    
camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties
   (with props)
    
camel/trunk/examples/camel-example-guice-jms/src/test/java/org/apache/camel/example/guice/jms/GuiceTest.java
   (with props)
Modified:
    camel/trunk/examples/camel-example-guice-jms/pom.xml
    
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyActivator.java

Modified: camel/trunk/examples/camel-example-guice-jms/pom.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-guice-jms/pom.xml?rev=884000&r1=883999&r2=884000&view=diff
==============================================================================
--- camel/trunk/examples/camel-example-guice-jms/pom.xml (original)
+++ camel/trunk/examples/camel-example-guice-jms/pom.xml Wed Nov 25 07:19:03 
2009
@@ -48,6 +48,11 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-spring</artifactId>
     </dependency>
+    
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-osgi</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>org.apache.camel</groupId>
@@ -59,10 +64,10 @@
       <artifactId>activemq-core</artifactId>
     </dependency>
     
-     <dependency>
-      <groupId>org.osgi</groupId>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
       <artifactId>org.osgi.core</artifactId>
-      <version>4.0.1</version>
+      <scope>provided</scope>
     </dependency>
 
     <dependency>

Modified: 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyActivator.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyActivator.java?rev=884000&r1=883999&r2=884000&view=diff
==============================================================================
--- 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyActivator.java
 (original)
+++ 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyActivator.java
 Wed Nov 25 07:19:03 2009
@@ -16,23 +16,42 @@
  */
 package org.apache.camel.example.guice.jms;
 
-import org.apache.camel.guice.Main;
+import java.net.URL;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.springframework.osgi.util.BundleDelegatingClassLoader;
 
 public class MyActivator implements BundleActivator {
-    private Main mainInstance;
-
+    CamelContext camelContext;
+   
     public void start(BundleContext context) throws Exception {
-        // start the camel context 
-        mainInstance = new Main();
-        mainInstance.start();
+
+        Injector injector = Guice.createInjector(new MyOSGiModule(context));
+
+        camelContext = injector.getInstance(CamelContext.class);
+
+        if (camelContext instanceof DefaultCamelContext) {
+            if (!((DefaultCamelContext)camelContext).isStarted()) {
+                ((DefaultCamelContext)camelContext).start();
+            }
+
+        }
     }
 
     public void stop(BundleContext context) throws Exception {
         // stop the camel context
-        if (mainInstance != null) {
-            mainInstance.stop();
+        if (camelContext != null) {
+            camelContext.stop();
         }        
     }
 

Added: 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyOSGiModule.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyOSGiModule.java?rev=884000&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyOSGiModule.java
 (added)
+++ 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyOSGiModule.java
 Wed Nov 25 07:19:03 2009
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.example.guice.jms;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Properties;
+
+import com.google.inject.Provides;
+import com.google.inject.name.Names;
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.jms.JmsComponent;
+import org.apache.camel.guice.CamelModuleWithMatchingRoutes;
+import org.apache.camel.osgi.CamelContextFactory;
+import org.guiceyfruit.jndi.JndiBind;
+import org.osgi.framework.BundleContext;
+
+/**
+ * Configures the CamelContext, RouteBuilder, Component and Endpoint instances 
using
+ * Guice within OSGi platform
+ *
+ * @version $Revision$
+ */
+
+public class MyOSGiModule extends MyModule {
+    private OSGiCamelContextProvider provider;
+    private Properties properties;
+    
+    MyOSGiModule(BundleContext bundleContext) {
+        super();
+        provider = new OSGiCamelContextProvider(bundleContext);
+        properties = new Properties();
+        URL jndiPropertiesUrl = null;
+        if (bundleContext != null) {
+            jndiPropertiesUrl = 
bundleContext.getBundle().getEntry("camel.properties");
+            
+        } else {
+            jndiPropertiesUrl = 
this.getClass().getResource("/camel.properties");
+        }
+        try {
+            if (jndiPropertiesUrl != null) {
+                properties.load(jndiPropertiesUrl.openStream());
+            }
+        } catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+    
+    @Override
+    protected void configureCamelContext() {
+        bind(CamelContext.class).toProvider(provider).asEagerSingleton();
+    }
+    
+    @Override
+    protected void configure() {
+        Names.bindProperties(binder(), properties);
+        super.configure();
+    }
+  
+}

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyOSGiModule.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyOSGiModule.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java?rev=884000&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java
 (added)
+++ 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java
 Wed Nov 25 07:19:03 2009
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.example.guice.jms;
+
+import java.net.URL;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Provider;
+import com.google.inject.ProvisionException;
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.osgi.CamelContextFactory;
+import org.apache.camel.osgi.CompositeRegistry;
+import org.apache.camel.spi.Registry;
+import org.guiceyfruit.jndi.JndiBindings;
+import org.guiceyfruit.jndi.internal.JndiContext;
+import org.osgi.framework.BundleContext;
+
+public class OSGiCamelContextProvider implements Provider<CamelContext> {
+    private final CamelContextFactory factory;
+    @Inject
+    private Set<RoutesBuilder> routeBuilders;
+    @Inject
+    private Injector injector;
+    
+    public OSGiCamelContextProvider(BundleContext context) {
+        factory = new CamelContextFactory();
+        factory.setBundleContext(context);
+    }
+    
+    protected Context getJndiContext() {
+        try {
+            JndiContext context = new JndiContext(null);
+            if (injector != null) {
+                //Feed the empty properties to get the code work
+                JndiBindings.bindInjectorAndBindings(context, injector, new 
Properties());
+            }
+            return context;
+        } catch (Exception e) {
+            throw new ProvisionException("Failed to create JNDI bindings. 
Reason: " + e, e);
+        }
+    }
+    
+    // Add the JndiRegistry to the camel context
+    protected void updateRegistry(DefaultCamelContext camelContext) {        
+        CompositeRegistry compositeRegistry = new CompositeRegistry();
+        compositeRegistry.addRegistry(new JndiRegistry(getJndiContext()));
+        compositeRegistry.addRegistry(camelContext.getRegistry());
+        camelContext.setRegistry(compositeRegistry);
+    }
+
+    public CamelContext get() {
+        DefaultCamelContext camelContext = factory.createContext();
+        if (routeBuilders != null) {
+            for (RoutesBuilder builder : routeBuilders) {
+                try {
+                    camelContext.addRoutes(builder);
+                } catch (Exception e) {
+                    throw new ProvisionException("Failed to add the router. 
Reason: " + e, e);
+                }
+            }
+        }
+        updateRegistry(camelContext);        
+        return camelContext;
+    }
+
+}

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties?rev=884000&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties
 (added)
+++ 
camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties
 Wed Nov 25 07:19:03 2009
@@ -0,0 +1,19 @@
+## ------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+## http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ------------------------------------------------------------------------
+
+# bound properties injected to @Named injection points
+activemq.brokerURL = vm://broker1?marshal=false&broker.persistent=false
\ No newline at end of file

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/main/resources/camel.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
camel/trunk/examples/camel-example-guice-jms/src/test/java/org/apache/camel/example/guice/jms/GuiceTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-guice-jms/src/test/java/org/apache/camel/example/guice/jms/GuiceTest.java?rev=884000&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-guice-jms/src/test/java/org/apache/camel/example/guice/jms/GuiceTest.java
 (added)
+++ 
camel/trunk/examples/camel-example-guice-jms/src/test/java/org/apache/camel/example/guice/jms/GuiceTest.java
 Wed Nov 25 07:19:03 2009
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.example.guice.jms;
+
+import junit.framework.TestCase;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.guice.GuiceCamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.guiceyfruit.Injectors;
+
+// Test loading the Guice Module directly 
+public class GuiceTest extends TestCase {
+    
+    public void testCreateGuiceContextOSGi() throws Exception {
+        Injector injector = Guice.createInjector(new MyOSGiModule(null));
+        CamelContext camelContext = injector.getInstance(CamelContext.class);
+
+        if (camelContext instanceof DefaultCamelContext) {
+            if (!((DefaultCamelContext)camelContext).isStarted()) {
+                ((DefaultCamelContext)camelContext).start();
+            }
+        }
+        Thread.sleep(2000);
+        Injectors.close(injector);
+    }
+
+}

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/test/java/org/apache/camel/example/guice/jms/GuiceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/examples/camel-example-guice-jms/src/test/java/org/apache/camel/example/guice/jms/GuiceTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to