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    package org.apache.camel.example.guice.jms;
018    
019    import java.net.URL;
020    import java.util.Properties;
021    import java.util.Set;
022    
023    import javax.naming.Context;
024    import javax.naming.NamingException;
025    
026    import com.google.inject.Inject;
027    import com.google.inject.Injector;
028    import com.google.inject.Provider;
029    import com.google.inject.ProvisionException;
030    import org.apache.camel.CamelContext;
031    import org.apache.camel.RoutesBuilder;
032    import org.apache.camel.impl.DefaultCamelContext;
033    import org.apache.camel.impl.JndiRegistry;
034    import org.apache.camel.osgi.CamelContextFactory;
035    import org.apache.camel.osgi.CompositeRegistry;
036    import org.apache.camel.spi.Registry;
037    import org.guiceyfruit.jndi.JndiBindings;
038    import org.guiceyfruit.jndi.internal.JndiContext;
039    import org.osgi.framework.BundleContext;
040    
041    public class OSGiCamelContextProvider implements Provider<CamelContext> {
042        private final CamelContextFactory factory;
043        @Inject
044        private Set<RoutesBuilder> routeBuilders;
045        @Inject
046        private Injector injector;
047        
048        public OSGiCamelContextProvider(BundleContext context) {
049            factory = new CamelContextFactory();
050            factory.setBundleContext(context);
051        }
052        
053        protected Context getJndiContext() {
054            try {
055                JndiContext context = new JndiContext(null);
056                if (injector != null) {
057                    //Feed the empty properties to get the code work
058                    JndiBindings.bindInjectorAndBindings(context, injector, new Properties());
059                }
060                return context;
061            } catch (Exception e) {
062                throw new ProvisionException("Failed to create JNDI bindings. Reason: " + e, e);
063            }
064        }
065        
066        // Add the JndiRegistry to the camel context
067        protected void updateRegistry(DefaultCamelContext camelContext) {        
068            CompositeRegistry compositeRegistry = new CompositeRegistry();
069            compositeRegistry.addRegistry(new JndiRegistry(getJndiContext()));
070            compositeRegistry.addRegistry(camelContext.getRegistry());
071            camelContext.setRegistry(compositeRegistry);
072        }
073    
074        public CamelContext get() {
075            DefaultCamelContext camelContext = factory.createContext();
076            if (routeBuilders != null) {
077                for (RoutesBuilder builder : routeBuilders) {
078                    try {
079                        camelContext.addRoutes(builder);
080                    } catch (Exception e) {
081                        throw new ProvisionException("Failed to add the router. Reason: " + e, e);
082                    }
083                }
084            }
085            updateRegistry(camelContext);        
086            return camelContext;
087        }
088    
089    }