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.component.jbi;
018
019 import org.apache.camel.Endpoint;
020 import org.apache.camel.spring.SpringCamelContext;
021 import org.apache.servicemix.common.xbean.AbstractXBeanDeployer;
022 import org.apache.servicemix.common.ServiceUnit;
023 import org.apache.xbean.kernel.Kernel;
024 import org.apache.xbean.server.spring.loader.PureSpringLoader;
025 import org.apache.xbean.server.spring.loader.SpringLoader;
026 import org.springframework.context.ApplicationContext;
027 import org.springframework.context.support.AbstractXmlApplicationContext;
028 import org.springframework.context.support.FileSystemXmlApplicationContext;
029 import org.springframework.context.support.GenericApplicationContext;
030
031 import javax.jbi.management.DeploymentException;
032 import java.util.ArrayList;
033 import java.util.Collection;
034 import java.util.List;
035
036 /**
037 * A deployer of the spring XML file
038 *
039 * @version $Revision: 1.1 $
040 */
041 public class CamelSpringDeployer extends AbstractXBeanDeployer {
042 private final CamelJbiComponent component;
043 private PureSpringLoader springLoader = new PureSpringLoader() {
044 @Override
045 protected AbstractXmlApplicationContext createXmlApplicationContext(String configLocation) {
046 return new FileSystemXmlApplicationContext(new String[]{configLocation}, false, createParentApplicationContext());
047 }
048 };
049 private List<CamelJbiEndpoint> activatedEndpoints = new ArrayList<CamelJbiEndpoint>();
050
051 public CamelSpringDeployer(CamelJbiComponent component) {
052 super(component);
053 this.component = component;
054 }
055
056 protected String getXBeanFile() {
057 return "camel-context";
058 }
059
060 /* (non-Javadoc)
061 * @see org.apache.servicemix.common.Deployer#deploy(java.lang.String, java.lang.String)
062 */
063 @Override
064 public ServiceUnit deploy(String serviceUnitName, String serviceUnitRootPath) throws DeploymentException {
065 // lets register the deployer so that any endpoints activated are added to this SU
066 component.deployer = this;
067 ServiceUnit serviceUnit = super.deploy(serviceUnitName, serviceUnitRootPath);
068 return serviceUnit;
069 }
070
071 public void addService(CamelJbiEndpoint endpoint) {
072 activatedEndpoints.add(endpoint);
073 }
074
075 protected List getServices(Kernel kernel) {
076 try {
077 List<CamelJbiEndpoint> services = new ArrayList<CamelJbiEndpoint>(activatedEndpoints);
078 activatedEndpoints.clear();
079
080 ApplicationContext applicationContext = springLoader.getApplicationContext();
081 SpringCamelContext camelContext = SpringCamelContext.springCamelContext(applicationContext);
082
083 // now lets iterate through all the endpoints
084 Collection<Endpoint> endpoints = camelContext.getSingletonEndpoints();
085 for (Endpoint endpoint : endpoints) {
086 if (component.isEndpointExposedOnNmr(endpoint)) {
087 services.add(component.createJbiEndpointFromCamel(endpoint));
088 }
089 }
090 return services;
091 }
092 catch (Exception e) {
093 throw new RuntimeException(e);
094 }
095 }
096
097 @Override
098 protected SpringLoader createSpringLoader() {
099 return springLoader;
100 }
101
102 /**
103 * Returns the parent application context which can be used to auto-wire any JBI based components
104 * using the jbi prefix
105 */
106 protected ApplicationContext createParentApplicationContext() {
107 GenericApplicationContext answer = new GenericApplicationContext();
108 answer.getBeanFactory().registerSingleton("jbi", component);
109 answer.start();
110 answer.refresh();
111 return answer;
112 }
113
114
115 }