Coverage Report - org.apache.camel.component.jbi.CamelJbiComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
CamelJbiComponent
0% 
0% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 3  
  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 4  
  * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 5  
  * License. You may obtain a copy of the License at
 6  
  *
 7  
  * http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 10  
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 11  
  * specific language governing permissions and limitations under the License.
 12  
  */
 13  
 package org.apache.camel.component.jbi;
 14  
 
 15  
 import org.apache.camel.CamelContext;
 16  
 import org.apache.camel.Component;
 17  
 import org.apache.camel.Endpoint;
 18  
 import org.apache.camel.Exchange;
 19  
 import org.apache.camel.FailedToCreateProducerException;
 20  
 import org.apache.camel.Processor;
 21  
 import org.apache.servicemix.common.BaseServiceUnitManager;
 22  
 import org.apache.servicemix.common.DefaultComponent;
 23  
 import org.apache.servicemix.common.Deployer;
 24  
 import org.apache.servicemix.id.IdGenerator;
 25  
 import org.apache.servicemix.jbi.resolver.URIResolver;
 26  
 import org.apache.servicemix.jbi.util.IntrospectionSupport;
 27  
 import org.apache.servicemix.jbi.util.URISupport;
 28  
 
 29  
 import javax.jbi.servicedesc.ServiceEndpoint;
 30  
 import javax.xml.namespace.QName;
 31  
 import java.net.URI;
 32  
 import java.net.URISyntaxException;
 33  
 import java.util.ArrayList;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 import java.util.concurrent.ScheduledExecutorService;
 37  
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 38  
 
 39  
 /**
 40  
  * Deploys the camel endpoints within JBI
 41  
  *
 42  
  * @version $Revision: 426415 $
 43  
  */
 44  0
 public class CamelJbiComponent extends DefaultComponent implements Component<Exchange> {
 45  
     private JbiBinding binding;
 46  
     private CamelContext camelContext;
 47  
     private ScheduledExecutorService executorService;
 48  
     private IdGenerator idGenerator;
 49  
     protected CamelSpringDeployer deployer;
 50  
 
 51  
     /* (non-Javadoc)
 52  
     * @see org.servicemix.common.BaseComponent#createServiceUnitManager()
 53  
     */
 54  
     public BaseServiceUnitManager createServiceUnitManager() {
 55  0
         Deployer[] deployers = new Deployer[]{new CamelSpringDeployer(this)};
 56  0
         return new BaseServiceUnitManager(this, deployers);
 57  
     }
 58  
 
 59  
     /**
 60  
      * @return List of endpoints
 61  
      * @see org.apache.servicemix.common.DefaultComponent#getConfiguredEndpoints()
 62  
      */
 63  
     @Override
 64  
     protected List<CamelJbiEndpoint> getConfiguredEndpoints() {
 65  0
         List<CamelJbiEndpoint> answer = new ArrayList<CamelJbiEndpoint>();
 66  0
         return answer;
 67  
     }
 68  
 
 69  
     /**
 70  
      * @return Class[]
 71  
      * @see org.apache.servicemix.common.DefaultComponent#getEndpointClasses()
 72  
      */
 73  
     @Override
 74  
     protected Class[] getEndpointClasses() {
 75  0
         return new Class[]{CamelJbiEndpoint.class};
 76  
     }
 77  
 
 78  
     /**
 79  
      * @return the binding
 80  
      */
 81  
     public JbiBinding getBinding() {
 82  0
         if (binding == null) {
 83  0
             binding = new JbiBinding();
 84  
         }
 85  0
         return binding;
 86  
     }
 87  
 
 88  
     /**
 89  
      * @param binding the binding to set
 90  
      */
 91  
     public void setBinding(JbiBinding binding) {
 92  0
         this.binding = binding;
 93  0
     }
 94  
 
 95  
     @Override
 96  
     protected String[] getEPRProtocols() {
 97  0
         return new String[]{"camel"};
 98  
     }
 99  
 
 100  
     protected org.apache.servicemix.common.Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
 101  0
         CamelJbiEndpoint endpoint = createEndpoint(ep);
 102  0
         endpoint.activate();
 103  0
         return endpoint;
 104  
     }
 105  
 
 106  
     public CamelJbiEndpoint createEndpoint(ServiceEndpoint ep) throws URISyntaxException {
 107  0
         URI uri = new URI(ep.getEndpointName());
 108  0
         Map map = URISupport.parseQuery(uri.getQuery());
 109  0
         String camelUri = uri.getSchemeSpecificPart();
 110  0
         Endpoint camelEndpoint = getCamelContext().getEndpoint(camelUri);
 111  0
         Processor processor = createCamelProcessor(camelEndpoint);
 112  0
         CamelJbiEndpoint endpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor);
 113  
 
 114  0
         IntrospectionSupport.setProperties(endpoint, map);
 115  
 
 116  
         // TODO
 117  
         //endpoint.setRole(MessageExchange.Role.PROVIDER);
 118  
 
 119  0
         return endpoint;
 120  
     }
 121  
 
 122  
     // Resolve Camel Endpoints
 123  
     //-------------------------------------------------------------------------
 124  
     public Endpoint<Exchange> createEndpoint(String uri) {
 125  0
         if (uri.startsWith("jbi:")) {
 126  0
             uri = uri.substring("jbi:".length());
 127  0
             return new JbiEndpoint(this, uri);
 128  
         }
 129  0
         return null;
 130  
     }
 131  
 
 132  
     public CamelContext getCamelContext() {
 133  0
         return camelContext;
 134  
     }
 135  
 
 136  
     public void setCamelContext(CamelContext camelContext) {
 137  0
         this.camelContext = camelContext;
 138  0
     }
 139  
 
 140  
     public ScheduledExecutorService getExecutorService() {
 141  0
         if (executorService == null) {
 142  0
             executorService = new ScheduledThreadPoolExecutor(5);
 143  
         }
 144  0
         return executorService;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Activating a JBI endpoint created by a camel consumer.
 149  
      *
 150  
      * @returns a JBI endpoint created for the given Camel endpoint
 151  
      */
 152  
     public CamelJbiEndpoint activateJbiEndpoint(Endpoint camelEndpoint, Processor processor) throws Exception {
 153  0
         CamelJbiEndpoint jbiEndpoint = createJbiEndpointFromCamel(camelEndpoint, processor);
 154  
 
 155  
         // the following method will activate the new dynamic JBI endpoint
 156  0
         if (deployer != null) {
 157  
             // lets add this to the current service unit being deployed
 158  0
             deployer.addService(jbiEndpoint);
 159  
         }
 160  
         else {
 161  0
             addEndpoint(jbiEndpoint);
 162  
         }
 163  0
         return jbiEndpoint;
 164  
     }
 165  
 
 166  
     public void deactivateJbiEndpoint(CamelJbiEndpoint jbiEndpoint) throws Exception {
 167  
         // this will be done by the ServiceUnit
 168  
         //jbiEndpoint.deactivate();
 169  0
     }
 170  
 
 171  
     protected CamelJbiEndpoint createJbiEndpointFromCamel(Endpoint camelEndpoint, Processor processor) {
 172  
         CamelJbiEndpoint jbiEndpoint;
 173  0
         String endpointUri = camelEndpoint.getEndpointUri();
 174  0
         if (camelEndpoint instanceof JbiEndpoint) {
 175  0
             QName service = null;
 176  0
             String endpoint = null;
 177  0
             if (endpointUri.startsWith("name:")) {
 178  0
                 endpoint = endpointUri.substring("name:".length());
 179  0
                 service = CamelJbiEndpoint.SERVICE_NAME;
 180  
             }
 181  0
             else if (endpointUri.startsWith("endpoint:")) {
 182  0
                 String uri = endpointUri.substring("endpoint:".length());
 183  
                 // lets decode "serviceNamespace sep serviceName sep endpointName
 184  
                 String[] parts;
 185  
                 try {
 186  0
                     parts = URIResolver.split3(uri);
 187  
                 }
 188  0
                 catch (IllegalArgumentException e) {
 189  0
                     throw new IllegalArgumentException("Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName][sep][endpointName] where sep = '/' or ':' depending on the serviceNamespace, but was given: " + endpointUri + ". Cause: " + e, e);
 190  0
                 }
 191  0
                 service = new QName(parts[0], parts[1]);
 192  0
                 endpoint = parts[2];
 193  0
             }
 194  0
             else if (endpointUri.startsWith("service:")) {
 195  0
                 String uri = endpointUri.substring("service:".length());
 196  
                 // lets decode "serviceNamespace sep serviceName
 197  
                 String[] parts;
 198  
                 try {
 199  0
                     parts = URIResolver.split2(uri);
 200  
                 }
 201  0
                 catch (IllegalArgumentException e) {
 202  0
                     throw new IllegalArgumentException("Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName] where sep = '/' or ':' depending on the serviceNamespace, but was given: " + endpointUri + ". Cause: " + e, e);
 203  0
                 }
 204  0
                 service = new QName(parts[0], parts[1]);
 205  0
                 endpoint = createEndpointName();
 206  0
             }
 207  
             else {
 208  0
                 throw new IllegalArgumentException("Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName][sep][endpointName] or  jbi:service:[serviceNamespace][sep][serviceName or jbi:name:[endpointName] but was given: " + endpointUri);
 209  
             }
 210  0
             jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), service, endpoint, camelEndpoint, getBinding(), processor);
 211  0
         }
 212  
         else {
 213  0
             jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor);
 214  
         }
 215  0
         return jbiEndpoint;
 216  
     }
 217  
 
 218  
     protected String createEndpointName() {
 219  0
         if (idGenerator == null) {
 220  0
             idGenerator = new IdGenerator("camel");
 221  
         }
 222  0
         return idGenerator.generateSanitizedId();
 223  
     }
 224  
 
 225  
     /**
 226  
      * Returns a JBI endpoint created for the given Camel endpoint
 227  
      */
 228  
     public CamelJbiEndpoint createJbiEndpointFromCamel(Endpoint camelEndpoint) {
 229  0
         Processor processor = createCamelProcessor(camelEndpoint);
 230  0
         return createJbiEndpointFromCamel(camelEndpoint, processor);
 231  
     }
 232  
 
 233  
     protected Processor createCamelProcessor(Endpoint camelEndpoint) {
 234  0
         Processor processor = null;
 235  
         try {
 236  0
             processor = camelEndpoint.createProducer();
 237  
         }
 238  0
         catch (Exception e) {
 239  0
             throw new FailedToCreateProducerException(camelEndpoint, e);
 240  0
         }
 241  0
         return processor;
 242  
     }
 243  
 
 244  
     /**
 245  
      * Should we expose the Camel JBI onto the NMR.
 246  
      * <p/>
 247  
      * We may wish to add some policy stuff etc.
 248  
      *
 249  
      * @param endpoint the camel endpoint
 250  
      * @return true if the endpoint should be exposed in the NMR
 251  
      */
 252  
     public boolean isEndpointExposedOnNmr(Endpoint endpoint) {
 253  
         // TODO we should only expose consuming endpoints
 254  0
         return !(endpoint instanceof JbiEndpoint);
 255  
     }
 256  
 }