Coverage Report - org.apache.camel.component.jbi.CamelJbiComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
CamelJbiComponent
78% 
75% 
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.Processor;
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.FailedToCreateProducerException;
 21  
 import org.apache.servicemix.common.DefaultComponent;
 22  
 import org.apache.servicemix.jbi.util.IntrospectionSupport;
 23  
 import org.apache.servicemix.jbi.util.URISupport;
 24  
 import org.apache.servicemix.jbi.resolver.URIResolver;
 25  
 
 26  
 import javax.jbi.servicedesc.ServiceEndpoint;
 27  
 import javax.xml.namespace.QName;
 28  
 import java.net.URI;
 29  
 import java.net.URISyntaxException;
 30  
 import java.util.ArrayList;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 import java.util.concurrent.ScheduledExecutorService;
 34  
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 35  
 
 36  
 /**
 37  
  * Deploys the camel endpoints within JBI
 38  
  *
 39  
  * @version $Revision: 426415 $
 40  
  */
 41  3
 public class CamelJbiComponent extends DefaultComponent implements Component<Exchange> {
 42  
     private JbiBinding binding;
 43  
     private CamelContext camelContext;
 44  
     private ScheduledExecutorService executorService;
 45  
 
 46  
     /**
 47  
      * @return List of endpoints
 48  
      * @see org.apache.servicemix.common.DefaultComponent#getConfiguredEndpoints()
 49  
      */
 50  
     @Override
 51  
     protected List<CamelJbiEndpoint> getConfiguredEndpoints() {
 52  
         // TODO need to register to the context for new endpoints...
 53  3
         List<CamelJbiEndpoint> answer = new ArrayList<CamelJbiEndpoint>();
 54  
 //        Collection<Endpoint> endpoints = camelContext.getEndpoints();
 55  
 //        for (Endpoint endpoint : endpoints) {
 56  
 //          answer.add(createJbiEndpoint(endpoint));
 57  
 //        }
 58  3
         return answer;
 59  
     }
 60  
 
 61  
     /**
 62  
      * @return Class[]
 63  
      * @see org.apache.servicemix.common.DefaultComponent#getEndpointClasses()
 64  
      */
 65  
     @Override
 66  
     protected Class[] getEndpointClasses() {
 67  4
         return new Class[]{CamelJbiEndpoint.class};
 68  
     }
 69  
 
 70  
 
 71  
     /**
 72  
      * @return the binding
 73  
      */
 74  
     public JbiBinding getBinding() {
 75  7
         if (binding == null) {
 76  3
             binding = new JbiBinding();
 77  
         }
 78  7
         return binding;
 79  
     }
 80  
 
 81  
     /**
 82  
      * @param binding the binding to set
 83  
      */
 84  
     public void setBinding(JbiBinding binding) {
 85  0
         this.binding = binding;
 86  0
     }
 87  
 
 88  
     @Override
 89  
     protected String[] getEPRProtocols() {
 90  1
         return new String[]{"camel"};
 91  
     }
 92  
 
 93  
     protected org.apache.servicemix.common.Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
 94  1
         CamelJbiEndpoint endpoint = createEndpoint(ep);
 95  1
         endpoint.activate();
 96  1
         return endpoint;
 97  
     }
 98  
 
 99  
     public CamelJbiEndpoint createEndpoint(ServiceEndpoint ep) throws URISyntaxException {
 100  1
         URI uri = new URI(ep.getEndpointName());
 101  1
         Map map = URISupport.parseQuery(uri.getQuery());
 102  1
         String camelUri = uri.getSchemeSpecificPart();
 103  1
         Endpoint camelEndpoint = getCamelContext().getEndpoint(camelUri);
 104  1
         Processor processor = null;
 105  
         try {
 106  1
             processor = camelEndpoint.createProducer();
 107  
         }
 108  0
         catch (Exception e) {
 109  0
             throw new FailedToCreateProducerException(camelEndpoint, e);
 110  1
         }
 111  1
         CamelJbiEndpoint endpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor);
 112  
 
 113  1
         IntrospectionSupport.setProperties(endpoint, map);
 114  
 
 115  
         // TODO
 116  
         //endpoint.setRole(MessageExchange.Role.PROVIDER);
 117  
 
 118  1
         return endpoint;
 119  
     }
 120  
 
 121  
     // Resolve Camel Endpoints
 122  
     //-------------------------------------------------------------------------
 123  
     public Endpoint<Exchange> createEndpoint(String uri) {
 124  3
         if (uri.startsWith("jbi:")) {
 125  3
             uri = uri.substring("jbi:".length());
 126  
 
 127  3
             return new JbiEndpoint(this, uri);
 128  
         }
 129  0
         return null;
 130  
     }
 131  
 
 132  
     public CamelContext getCamelContext() {
 133  4
         return camelContext;
 134  
     }
 135  
 
 136  
     public void setCamelContext(CamelContext camelContext) {
 137  3
         this.camelContext = camelContext;
 138  3
     }
 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  
      * 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  1
         String endpointUri = camelEndpoint.getEndpointUri();
 153  1
         if (endpointUri.startsWith("endpoint:")) {
 154  
             // lets decode "service:serviceNamespace:serviceName:endpointName
 155  1
             String uri = endpointUri.substring("endpoint:".length());
 156  1
             String[] parts = new String[0];
 157  
             try {
 158  1
                 parts = URIResolver.split3(uri);
 159  
             }
 160  0
             catch (IllegalArgumentException e) {
 161  0
                 throw new IllegalArgumentException("Expected syntax endpoint:[serviceNamespace]:[serviceName]:[endpointName] but was given: " + endpointUri + ". Cause: " + e, e);
 162  1
             }
 163  1
             QName service = new QName(parts[0], parts[1]);
 164  1
             String endpoint = parts[2];
 165  1
             jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), service, endpoint, camelEndpoint, getBinding(), processor);
 166  1
         }
 167  
         else {
 168  0
             jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor);
 169  
         }
 170  
 
 171  
         // the following method will activate the new dynamic JBI endpoint
 172  1
         addEndpoint(jbiEndpoint);
 173  1
         return jbiEndpoint;
 174  
     }
 175  
 }