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