001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.camel.example.guice.jms;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.net.URL;
023    import java.util.Properties;
024    
025    import com.google.inject.Injector;
026    import com.google.inject.Provides;
027    import com.google.inject.name.Names;
028    import org.apache.activemq.ActiveMQConnectionFactory;
029    import org.apache.camel.CamelContext;
030    import org.apache.camel.component.jms.JmsComponent;
031    import org.apache.camel.guice.CamelModuleWithMatchingRoutes;
032    import org.apache.camel.osgi.CamelContextFactory;
033    import org.guiceyfruit.jndi.JndiBind;
034    import org.osgi.framework.BundleContext;
035    
036    /**
037     * Configures the CamelContext, RouteBuilder, Component and Endpoint instances using
038     * Guice within OSGi platform
039     *
040     * @version $Revision: 885196 $
041     */
042    
043    public class MyOSGiModule extends MyModule {
044        private OSGiCamelContextProvider provider;
045        private Properties properties;
046        
047        MyOSGiModule(BundleContext bundleContext) {
048            super();
049            provider = new OSGiCamelContextProvider(bundleContext);
050            properties = new Properties();
051            URL jndiPropertiesUrl = null;
052            if (bundleContext != null) {
053                jndiPropertiesUrl = bundleContext.getBundle().getEntry("camel.properties");
054                
055            } else {
056                jndiPropertiesUrl = this.getClass().getResource("/camel.properties");
057            }
058            try {
059                if (jndiPropertiesUrl != null) {
060                    properties.load(jndiPropertiesUrl.openStream());
061                }
062            } catch (IOException e) {
063                // TODO Auto-generated catch block
064                e.printStackTrace();
065            }
066        }
067        
068        @Override
069        protected void configureCamelContext() {
070            bind(CamelContext.class).toProvider(provider).asEagerSingleton();
071        }
072        
073        @Override
074        protected void configure() {
075            // loading the properties into Guice Context
076            Names.bindProperties(binder(), properties);
077            super.configure();
078        }
079      
080    }