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.jbi.resolver;
018:
019: import javax.jbi.JBIException;
020: import javax.jbi.component.ComponentContext;
021: import javax.jbi.messaging.MessageExchange;
022: import javax.jbi.servicedesc.ServiceEndpoint;
023: import javax.xml.namespace.QName;
024:
025: import org.w3c.dom.Document;
026: import org.w3c.dom.DocumentFragment;
027: import org.w3c.dom.Element;
028: import org.w3c.dom.Text;
029:
030: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
031: import org.apache.servicemix.jbi.util.WSAddressingConstants;
032:
033: public class URIResolver extends EndpointResolverSupport {
034:
035: /**
036: * The uri to resolve
037: */
038: private String uri;
039:
040: public URIResolver() {
041: }
042:
043: public URIResolver(String uri) {
044: this .uri = uri;
045: }
046:
047: protected JBIException createServiceUnavailableException() {
048: return new JBIException("Unable to resolve uri: " + uri);
049: }
050:
051: public ServiceEndpoint[] resolveAvailableEndpoints(
052: ComponentContext context, MessageExchange exchange)
053: throws JBIException {
054: if (uri.startsWith("interface:")) {
055: String u = this .uri.substring(10);
056: String[] parts = split2(u);
057: return context.getEndpoints(new QName(parts[0], parts[1]));
058: } else if (uri.startsWith("operation:")) {
059: // ignore operation
060: String u = this .uri.substring(10);
061: String[] parts = split3(u);
062: return context.getEndpoints(new QName(parts[0], parts[1]));
063: } else if (uri.startsWith("service:")) {
064: String u = this .uri.substring(8);
065: String[] parts = split2(u);
066: return context.getEndpointsForService(new QName(parts[0],
067: parts[1]));
068: } else if (uri.startsWith("endpoint:")) {
069: String u = this .uri.substring(9);
070: String[] parts = split3(u);
071: ServiceEndpoint se = context.getEndpoint(new QName(
072: parts[0], parts[1]), parts[2]);
073: if (se != null) {
074: return new ServiceEndpoint[] { se };
075: }
076: // Try an EPR resolution
077: } else {
078: DocumentFragment epr = createWSAEPR(uri);
079: ServiceEndpoint se = context.resolveEndpointReference(epr);
080: if (se != null) {
081: return new ServiceEndpoint[] { se };
082: }
083: }
084: return null;
085: }
086:
087: /**
088: * @return the uri
089: */
090: public String getUri() {
091: return uri;
092: }
093:
094: /**
095: * @param uri the uri to set
096: */
097: public void setUri(String uri) {
098: this .uri = uri;
099: }
100:
101: public static DocumentFragment createWSAEPR(String uri) {
102: Document doc;
103: try {
104: doc = new SourceTransformer().createDocument();
105: } catch (Exception e) {
106: throw new RuntimeException(e);
107: }
108: DocumentFragment epr = doc.createDocumentFragment();
109: Element root = doc.createElement("epr");
110: Element address = doc.createElementNS(
111: WSAddressingConstants.WSA_NAMESPACE_200508,
112: WSAddressingConstants.WSA_PREFIX + ":"
113: + WSAddressingConstants.EL_ADDRESS);
114: Text txt = doc.createTextNode(uri);
115: address.appendChild(txt);
116: root.appendChild(address);
117: epr.appendChild(root);
118: return epr;
119: }
120:
121: /**
122: * Configure a JBI exchange with the given URI as the target
123: *
124: * @param exchange the exchange to configure
125: * @param context a component context used to resolve endpoints
126: * @param uri the target uri
127: */
128: public static void configureExchange(MessageExchange exchange,
129: ComponentContext context, String uri) {
130: if (exchange == null) {
131: throw new NullPointerException("exchange is null");
132: }
133: if (context == null) {
134: throw new NullPointerException("context is null");
135: }
136: if (uri == null) {
137: throw new NullPointerException("uri is null");
138: }
139: if (uri.startsWith("interface:")) {
140: String uri2 = uri.substring(10);
141: String[] parts = URIResolver.split2(uri2);
142: exchange.setInterfaceName(new QName(parts[0], parts[1]));
143: } else if (uri.startsWith("operation:")) {
144: String uri2 = uri.substring(10);
145: String[] parts = URIResolver.split3(uri2);
146: exchange.setInterfaceName(new QName(parts[0], parts[1]));
147: exchange.setOperation(new QName(parts[0], parts[2]));
148: } else if (uri.startsWith("service:")) {
149: String uri2 = uri.substring(8);
150: String[] parts = URIResolver.split2(uri2);
151: exchange.setService(new QName(parts[0], parts[1]));
152: } else if (uri.startsWith("endpoint:")) {
153: String uri2 = uri.substring(9);
154: String[] parts = URIResolver.split3(uri2);
155: ServiceEndpoint se = context.getEndpoint(new QName(
156: parts[0], parts[1]), parts[2]);
157: exchange.setEndpoint(se);
158: } else {
159: DocumentFragment epr = URIResolver.createWSAEPR(uri);
160: ServiceEndpoint se = context.resolveEndpointReference(epr);
161: exchange.setEndpoint(se);
162: }
163: }
164:
165: public static String[] split3(String uri) {
166: char sep;
167: uri = uri.trim();
168: if (uri.indexOf('/') > 0) {
169: sep = '/';
170: } else {
171: sep = ':';
172: }
173: int idx1 = uri.lastIndexOf(sep);
174: int idx2 = uri.lastIndexOf(sep, idx1 - 1);
175: if (idx1 < 0 || idx2 < 0) {
176: throw new IllegalArgumentException(
177: "Bad syntax: expected [part0][sep][part1][sep][part2]");
178: }
179: String epName = uri.substring(idx1 + 1);
180: String svcName = uri.substring(idx2 + 1, idx1);
181: String nsUri = uri.substring(0, idx2);
182: return new String[] { nsUri, svcName, epName };
183: }
184:
185: public static String[] split2(String uri) {
186: char sep;
187: uri = uri.trim();
188: if (uri.indexOf('/') > 0) {
189: sep = '/';
190: } else {
191: sep = ':';
192: }
193: int idx1 = uri.lastIndexOf(sep);
194: if (idx1 < 0) {
195: throw new IllegalArgumentException(
196: "Bad syntax: expected [part0][sep][part1]");
197: }
198: String svcName = uri.substring(idx1 + 1);
199: String nsUri = uri.substring(0, idx1);
200: return new String[] { nsUri, svcName };
201: }
202:
203: }
|