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 javax.xml.namespace.QName;
020
021 import org.apache.camel.CamelContext;
022 import org.apache.camel.Component;
023 import org.apache.camel.Endpoint;
024 import org.apache.camel.Exchange;
025 import org.apache.camel.FailedToCreateProducerException;
026 import org.apache.camel.Processor;
027 import org.apache.servicemix.id.IdGenerator;
028 import org.apache.servicemix.jbi.resolver.URIResolver;
029
030 public class JbiComponent implements Component<Exchange> {
031 private CamelJbiComponent camelJbiComponent;
032
033 private CamelContext camelContext;
034 private IdGenerator idGenerator;
035 private String suName;
036
037 public JbiComponent(CamelJbiComponent component) {
038 camelJbiComponent = component;
039 camelJbiComponent.addJbiComponent(this);
040 }
041
042 public CamelContext getCamelContext() {
043 return camelContext;
044 }
045
046 public void setCamelContext(CamelContext context) {
047 camelContext = context;
048 }
049
050 public CamelJbiComponent getCamelJbiComponent() {
051 return camelJbiComponent;
052 }
053
054 public void setSuName(String su) {
055 suName = su;
056 }
057
058 public String getSuName() {
059 return suName;
060 }
061
062 // Resolve Camel Endpoints
063 // -------------------------------------------------------------------------
064 public Endpoint<Exchange> createEndpoint(String uri) {
065 if (uri.startsWith("jbi:")) {
066 return new JbiEndpoint(this, uri.substring("jbi:".length()));
067 }
068 return null;
069 }
070
071
072 protected CamelProviderEndpoint createJbiEndpointFromCamel(
073 Endpoint camelEndpoint, Processor processor) {
074 CamelProviderEndpoint jbiEndpoint;
075 String endpointUri = camelEndpoint.getEndpointUri();
076 if (camelEndpoint instanceof JbiEndpoint) {
077 QName service = null;
078 String endpoint = null;
079 if (endpointUri.startsWith("name:")) {
080 endpoint = endpointUri.substring("name:".length());
081 service = CamelProviderEndpoint.SERVICE_NAME;
082 } else if (endpointUri.startsWith("endpoint:")) {
083 String uri = endpointUri.substring("endpoint:".length());
084 // lets decode "serviceNamespace sep serviceName sep
085 // endpointName
086 String[] parts;
087 try {
088 parts = URIResolver.split3(uri);
089 } catch (IllegalArgumentException e) {
090 throw new IllegalArgumentException(
091 "Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName][sep][endpointName] "
092 + "where sep = '/' or ':' depending on the serviceNamespace, but was given: "
093 + endpointUri + ". Cause: " + e, e);
094 }
095 service = new QName(parts[0], parts[1]);
096 endpoint = parts[2];
097 } else if (endpointUri.startsWith("service:")) {
098 String uri = endpointUri.substring("service:".length());
099 // lets decode "serviceNamespace sep serviceName
100 String[] parts;
101 try {
102 parts = URIResolver.split2(uri);
103 } catch (IllegalArgumentException e) {
104 throw new IllegalArgumentException(
105 "Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName] "
106 + "where sep = '/' or ':' depending on the serviceNamespace, but was given: "
107 + endpointUri + ". Cause: " + e, e);
108 }
109 service = new QName(parts[0], parts[1]);
110 endpoint = createEndpointName();
111 } else {
112 throw new IllegalArgumentException(
113 "Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName][sep][endpointName] "
114 + "or jbi:service:[serviceNamespace][sep][serviceName or jbi:name:[endpointName] but was given: "
115 + endpointUri);
116 }
117 jbiEndpoint = new CamelProviderEndpoint(getCamelJbiComponent().getServiceUnit(), service,
118 endpoint, createBinding(camelEndpoint), processor);
119 } else {
120 jbiEndpoint = new CamelProviderEndpoint(getCamelJbiComponent().getServiceUnit(), camelEndpoint,
121 createBinding(camelEndpoint), processor);
122 }
123 return jbiEndpoint;
124 }
125
126 /*
127 * Creates a binding for the given endpoint
128 */
129 protected JbiBinding createBinding(Endpoint camelEndpoint) {
130 if (camelEndpoint instanceof JbiEndpoint) {
131 return ((JbiEndpoint) camelEndpoint).createBinding();
132 } else {
133 return new JbiBinding(camelContext);
134 }
135 }
136
137 protected String createEndpointName() {
138 if (idGenerator == null) {
139 idGenerator = new IdGenerator("camel");
140 }
141 return idGenerator.generateSanitizedId();
142 }
143
144 /**
145 * Returns a JBI endpoint created for the given Camel endpoint
146 */
147 public CamelProviderEndpoint createJbiEndpointFromCamel(Endpoint camelEndpoint) {
148 Processor processor = createCamelProcessor(camelEndpoint);
149 return createJbiEndpointFromCamel(camelEndpoint, processor);
150 }
151
152 protected Processor createCamelProcessor(Endpoint camelEndpoint) {
153 Processor processor = null;
154 try {
155 processor = camelEndpoint.createProducer();
156 } catch (Exception e) {
157 throw new FailedToCreateProducerException(camelEndpoint, e);
158 }
159 return processor;
160 }
161
162
163
164 }