001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
003     * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
004     * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
005     * License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     */
013    package org.apache.camel.component.jbi;
014    
015    import org.apache.camel.CamelContext;
016    import org.apache.camel.Component;
017    import org.apache.camel.Endpoint;
018    import org.apache.camel.Processor;
019    import org.apache.camel.Exchange;
020    import org.apache.camel.FailedToCreateProducerException;
021    import org.apache.servicemix.common.DefaultComponent;
022    import org.apache.servicemix.jbi.util.IntrospectionSupport;
023    import org.apache.servicemix.jbi.util.URISupport;
024    import org.apache.servicemix.jbi.resolver.URIResolver;
025    
026    import javax.jbi.servicedesc.ServiceEndpoint;
027    import javax.xml.namespace.QName;
028    import java.net.URI;
029    import java.net.URISyntaxException;
030    import java.util.ArrayList;
031    import java.util.List;
032    import java.util.Map;
033    import java.util.concurrent.ScheduledExecutorService;
034    import java.util.concurrent.ScheduledThreadPoolExecutor;
035    
036    /**
037     * Deploys the camel endpoints within JBI
038     *
039     * @version $Revision: 426415 $
040     */
041    public class CamelJbiComponent extends DefaultComponent implements Component<Exchange> {
042        private JbiBinding binding;
043        private CamelContext camelContext;
044        private ScheduledExecutorService executorService;
045    
046        /**
047         * @return List of endpoints
048         * @see org.apache.servicemix.common.DefaultComponent#getConfiguredEndpoints()
049         */
050        @Override
051        protected List<CamelJbiEndpoint> getConfiguredEndpoints() {
052            // TODO need to register to the context for new endpoints...
053            List<CamelJbiEndpoint> answer = new ArrayList<CamelJbiEndpoint>();
054    //        Collection<Endpoint> endpoints = camelContext.getEndpoints();
055    //        for (Endpoint endpoint : endpoints) {
056    //          answer.add(createJbiEndpoint(endpoint));
057    //        }
058            return answer;
059        }
060    
061        /**
062         * @return Class[]
063         * @see org.apache.servicemix.common.DefaultComponent#getEndpointClasses()
064         */
065        @Override
066        protected Class[] getEndpointClasses() {
067            return new Class[]{CamelJbiEndpoint.class};
068        }
069    
070    
071        /**
072         * @return the binding
073         */
074        public JbiBinding getBinding() {
075            if (binding == null) {
076                binding = new JbiBinding();
077            }
078            return binding;
079        }
080    
081        /**
082         * @param binding the binding to set
083         */
084        public void setBinding(JbiBinding binding) {
085            this.binding = binding;
086        }
087    
088        @Override
089        protected String[] getEPRProtocols() {
090            return new String[]{"camel"};
091        }
092    
093        protected org.apache.servicemix.common.Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
094            CamelJbiEndpoint endpoint = createEndpoint(ep);
095            endpoint.activate();
096            return endpoint;
097        }
098    
099        public CamelJbiEndpoint createEndpoint(ServiceEndpoint ep) throws URISyntaxException {
100            URI uri = new URI(ep.getEndpointName());
101            Map map = URISupport.parseQuery(uri.getQuery());
102            String camelUri = uri.getSchemeSpecificPart();
103            Endpoint camelEndpoint = getCamelContext().getEndpoint(camelUri);
104            Processor processor = null;
105            try {
106                processor = camelEndpoint.createProducer();
107            }
108            catch (Exception e) {
109                throw new FailedToCreateProducerException(camelEndpoint, e);
110            }
111            CamelJbiEndpoint endpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor);
112    
113            IntrospectionSupport.setProperties(endpoint, map);
114    
115            // TODO
116            //endpoint.setRole(MessageExchange.Role.PROVIDER);
117    
118            return endpoint;
119        }
120    
121        // Resolve Camel Endpoints
122        //-------------------------------------------------------------------------
123        public Endpoint<Exchange> createEndpoint(String uri) {
124            if (uri.startsWith("jbi:")) {
125                uri = uri.substring("jbi:".length());
126    
127                return new JbiEndpoint(this, uri);
128            }
129            return null;
130        }
131    
132        public CamelContext getCamelContext() {
133            return camelContext;
134        }
135    
136        public void setCamelContext(CamelContext camelContext) {
137            this.camelContext = camelContext;
138        }
139    
140        public ScheduledExecutorService getExecutorService() {
141            if (executorService == null) {
142                executorService = new ScheduledThreadPoolExecutor(5);
143            }
144            return executorService;
145        }
146    
147        /**
148         * Returns a JBI endpoint created for the given Camel endpoint
149         */
150        public CamelJbiEndpoint activateJbiEndpoint(JbiEndpoint camelEndpoint, Processor processor) throws Exception {
151            CamelJbiEndpoint jbiEndpoint;
152            String endpointUri = camelEndpoint.getEndpointUri();
153            if (endpointUri.startsWith("endpoint:")) {
154                // lets decode "service:serviceNamespace:serviceName:endpointName
155                String uri = endpointUri.substring("endpoint:".length());
156                String[] parts = new String[0];
157                try {
158                    parts = URIResolver.split3(uri);
159                }
160                catch (IllegalArgumentException e) {
161                    throw new IllegalArgumentException("Expected syntax endpoint:[serviceNamespace]:[serviceName]:[endpointName] but was given: " + endpointUri + ". Cause: " + e, e);
162                }
163                QName service = new QName(parts[0], parts[1]);
164                String endpoint = parts[2];
165                jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), service, endpoint, camelEndpoint, getBinding(), processor);
166            }
167            else {
168                jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor);
169            }
170    
171            // the following method will activate the new dynamic JBI endpoint
172            addEndpoint(jbiEndpoint);
173            return jbiEndpoint;
174        }
175    }