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.servicemix.camel;
018    
019    import java.net.URI;
020    import java.net.URISyntaxException;
021    import java.util.ArrayList;
022    import java.util.List;
023    import java.util.Map;
024    
025    import javax.jbi.servicedesc.ServiceEndpoint;
026    
027    import org.apache.camel.Endpoint;
028    import org.apache.camel.Processor;
029    import org.apache.servicemix.common.BaseServiceUnitManager;
030    import org.apache.servicemix.common.DefaultComponent;
031    import org.apache.servicemix.common.Deployer;
032    import org.apache.servicemix.jbi.util.IntrospectionSupport;
033    import org.apache.servicemix.jbi.util.URISupport;
034    
035    /**
036     * Deploys the camel endpoints within JBI
037     *
038     * @version $Revision: 426415 $
039     */
040    public class CamelJbiComponent extends DefaultComponent {
041    
042        protected CamelSpringDeployer deployer;
043    
044        private List<JbiComponent> jbiComponents = new ArrayList<JbiComponent>();
045    
046        /*
047         * (non-Javadoc)
048         *
049         * @see org.servicemix.common.BaseComponent#createServiceUnitManager()
050         */
051        @Override
052        public BaseServiceUnitManager createServiceUnitManager() {
053            Deployer[] deployers = new Deployer[] {new CamelSpringDeployer(this)};
054            return new BaseServiceUnitManager(this, deployers);
055        }
056    
057        /**
058         * @return List of endpoints
059         * @see org.apache.servicemix.common.DefaultComponent#getConfiguredEndpoints()
060         */
061        @Override
062        protected List<CamelProviderEndpoint> getConfiguredEndpoints() {
063            return new ArrayList<CamelProviderEndpoint>();
064        }
065    
066        /**
067         * @return Class[]
068         * @see org.apache.servicemix.common.DefaultComponent#getEndpointClasses()
069         */
070        @Override
071        protected Class[] getEndpointClasses() {
072            return new Class[] {CamelProviderEndpoint.class, CamelConsumerEndpoint.class};
073        }
074    
075        @Override
076        protected String[] getEPRProtocols() {
077            return new String[] {"camel"};
078        }
079    
080        @Override
081        protected org.apache.servicemix.common.Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
082            org.apache.servicemix.common.Endpoint endpoint = null;
083            // extract the su name camel:su1:seda:queque
084            JbiComponent jbiComponent = null;
085            String uriString = "";
086            String endpointName = ep.getEndpointName();
087            String names[] = endpointName.split(":");
088            if (names.length > 2) {
089                jbiComponent = getJbiComponent(names[1]);
090    
091            } else {
092                throw new IllegalStateException("Can't find the su name from the endpoint name");
093            }
094            if (jbiComponent != null) {
095                // skip the su-name part
096                int index = 0;
097                for (String name : names) {
098                    if (index == 0) {
099                        uriString = name;
100                    }
101                    if (index > 1) {
102                        uriString += ":" + name;
103                    }
104                    index++;
105                }
106                endpoint = createEndpoint(uriString, jbiComponent);
107                endpoint.activate();
108            } else {
109                throw new IllegalStateException("Can't find the JbiComponent");
110            }
111            return endpoint;
112        }
113    
114        public CamelProviderEndpoint createEndpoint(String uriString, JbiComponent jbiComponent) throws URISyntaxException {
115            URI uri = new URI(uriString);
116            Map map = URISupport.parseQuery(uri.getQuery());
117            String camelUri = uri.getSchemeSpecificPart();
118            Endpoint camelEndpoint = jbiComponent.getCamelContext().getEndpoint(camelUri);
119            Processor processor = jbiComponent.createCamelProcessor(camelEndpoint);
120            CamelProviderEndpoint endpoint =
121                new CamelProviderEndpoint(getServiceUnit(), camelEndpoint,
122                                          jbiComponent.createBinding(camelEndpoint), processor);
123    
124            IntrospectionSupport.setProperties(endpoint, map);
125    
126            // TODO
127            // endpoint.setRole(MessageExchange.Role.PROVIDER);
128    
129            return endpoint;
130        }
131    
132        public synchronized void addJbiComponent(JbiComponent jbiComponent) {
133            jbiComponents.add(jbiComponent);
134        }
135    
136        public synchronized void removeJbiComponent(String suName) {
137            JbiComponent component = getJbiComponent(suName);
138            if (component != null) {
139                jbiComponents.remove(component);
140            }
141        }
142    
143        public synchronized JbiComponent getJbiComponent(String suName) {
144            JbiComponent result = null;
145            for (JbiComponent component : jbiComponents) {
146                if (suName.equals(component.getSuName())) {
147                    result = component;
148                    break;
149                }
150            }
151            return result;
152        }
153    
154        /**
155         * Activating a JBI endpoint created by a camel consumer.
156         *
157         */
158        public void activateJbiEndpoint(CamelProviderEndpoint jbiEndpoint) throws Exception {
159    
160            // the following method will activate the new dynamic JBI endpoint
161            if (deployer != null) {
162                // lets add this to the current service unit being deployed
163                deployer.addService(jbiEndpoint);
164            } else {
165                addEndpoint(jbiEndpoint);
166            }
167    
168        }
169    
170        public void deactivateJbiEndpoint(CamelProviderEndpoint jbiEndpoint) throws Exception {
171            // this will be done by the ServiceUnit
172            // jbiEndpoint.deactivate();
173        }
174    
175    
176        /**
177         * Should we expose the Camel JBI onto the NMR. <p/> We may wish to add some
178         * policy stuff etc.
179         *
180         * @param endpoint
181         *            the camel endpoint
182         * @return true if the endpoint should be exposed in the NMR
183         */
184        public boolean isEndpointExposedOnNmr(Endpoint endpoint) {
185            // TODO we should only expose consuming endpoints
186            return false;
187            //return !(endpoint instanceof JbiEndpoint);
188        }
189    }