KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > resolver > URIResolver


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.resolver;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ComponentContext;
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jbi.servicedesc.ServiceEndpoint;
23 import javax.xml.namespace.QName JavaDoc;
24
25 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.DocumentFragment JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Text JavaDoc;
30
31 public class URIResolver extends EndpointResolverSupport {
32
33     /**
34      * The uri to resolve
35      */

36     private String JavaDoc uri;
37     
38     public URIResolver() {
39     }
40     
41     public URIResolver(String JavaDoc uri) {
42         this.uri = uri;
43     }
44     
45     protected JBIException createServiceUnavailableException() {
46         return new JBIException("Unable to resolve uri: " + uri);
47     }
48
49     public ServiceEndpoint[] resolveAvailableEndpoints(ComponentContext context, MessageExchange exchange)
50             throws JBIException {
51         if (uri.startsWith("interface:")) {
52             String JavaDoc uri = this.uri.substring(10);
53             String JavaDoc[] parts = split2(uri);
54             return context.getEndpoints(new QName JavaDoc(parts[0], parts[1]));
55         } else if (uri.startsWith("operation:")) {
56             // ignore operation
57
String JavaDoc uri = this.uri.substring(10);
58             String JavaDoc[] parts = split3(uri);
59             return context.getEndpoints(new QName JavaDoc(parts[0], parts[1]));
60         } else if (uri.startsWith("service:")) {
61             String JavaDoc uri = this.uri.substring(8);
62             String JavaDoc[] parts = split2(uri);
63             return context.getEndpointsForService(new QName JavaDoc(parts[0], parts[1]));
64         } else if (uri.startsWith("endpoint:")) {
65             String JavaDoc uri = this.uri.substring(9);
66             String JavaDoc[] parts = split3(uri);
67             ServiceEndpoint se = context.getEndpoint(new QName JavaDoc(parts[0], parts[1]), parts[2]);
68             if (se != null) {
69                 return new ServiceEndpoint[] { se };
70             }
71         // Try an EPR resolution
72
} else {
73             DocumentFragment JavaDoc epr = createWSAEPR(uri);
74             ServiceEndpoint se = context.resolveEndpointReference(epr);
75             if (se != null) {
76                 return new ServiceEndpoint[] { se };
77             }
78         }
79         return null;
80     }
81
82     /**
83      * @return the uri
84      */

85     public String JavaDoc getUri() {
86         return uri;
87     }
88
89     /**
90      * @param uri the uri to set
91      */

92     public void setUri(String JavaDoc uri) {
93         this.uri = uri;
94     }
95
96     public static DocumentFragment JavaDoc createWSAEPR(String JavaDoc uri) {
97         Document JavaDoc doc;
98         try {
99             doc = new SourceTransformer().createDocument();
100         } catch (Exception JavaDoc e) {
101             throw new RuntimeException JavaDoc(e);
102         }
103         DocumentFragment JavaDoc epr = doc.createDocumentFragment();
104         Element JavaDoc root = doc.createElement("epr");
105         Element JavaDoc address = doc.createElementNS("http://www.w3.org/2005/08/addressing", "wsa:Address");
106         Text JavaDoc txt = doc.createTextNode(uri);
107         address.appendChild(txt);
108         root.appendChild(address);
109         epr.appendChild(root);
110         return epr;
111     }
112     
113     /**
114      * Configure a JBI exchange with the given URI as the target
115      *
116      * @param exchange the exchange to configure
117      * @param context a component context used to resolve endpoints
118      * @param uri the target uri
119      */

120     public static void configureExchange(MessageExchange exchange, ComponentContext context, String JavaDoc uri) {
121         if (exchange == null) throw new NullPointerException JavaDoc("exchange is null");
122         if (context == null) throw new NullPointerException JavaDoc("context is null");
123         if (uri == null) throw new NullPointerException JavaDoc("uri is null");
124         if (uri.startsWith("interface:")) {
125             String JavaDoc uri2 = uri.substring(10);
126             String JavaDoc[] parts = URIResolver.split2(uri2);
127             exchange.setInterfaceName(new QName JavaDoc(parts[0], parts[1]));
128         } else if (uri.startsWith("operation:")) {
129             String JavaDoc uri2 = uri.substring(10);
130             String JavaDoc[] parts = URIResolver.split3(uri2);
131             exchange.setInterfaceName(new QName JavaDoc(parts[0], parts[1]));
132             exchange.setOperation(new QName JavaDoc(parts[0], parts[2]));
133         } else if (uri.startsWith("service:")) {
134             String JavaDoc uri2 = uri.substring(8);
135             String JavaDoc[] parts = URIResolver.split2(uri2);
136             exchange.setService(new QName JavaDoc(parts[0], parts[1]));
137         } else if (uri.startsWith("endpoint:")) {
138             String JavaDoc uri2 = uri.substring(9);
139             String JavaDoc[] parts = URIResolver.split3(uri2);
140             ServiceEndpoint se = context.getEndpoint(new QName JavaDoc(parts[0], parts[1]), parts[2]);
141             exchange.setEndpoint(se);
142         } else {
143             DocumentFragment JavaDoc epr = URIResolver.createWSAEPR(uri);
144             ServiceEndpoint se = context.resolveEndpointReference(epr);
145             exchange.setEndpoint(se);
146         }
147     }
148     
149     public static String JavaDoc[] split3(String JavaDoc uri) {
150         char sep;
151         if (uri.indexOf('/') > 0) {
152             sep = '/';
153         } else {
154             sep = ':';
155         }
156         int idx1 = uri.lastIndexOf(sep);
157         int idx2 = uri.lastIndexOf(sep, idx1 - 1);
158         String JavaDoc epName = uri.substring(idx1 + 1);
159         String JavaDoc svcName = uri.substring(idx2 + 1, idx1);
160         String JavaDoc nsUri = uri.substring(0, idx2);
161         return new String JavaDoc[] { nsUri, svcName, epName };
162     }
163     
164     public static String JavaDoc[] split2(String JavaDoc uri) {
165         char sep;
166         if (uri.indexOf('/') > 0) {
167             sep = '/';
168         } else {
169             sep = ':';
170         }
171         int idx1 = uri.lastIndexOf(sep);
172         String JavaDoc svcName = uri.substring(idx1 + 1);
173         String JavaDoc nsUri = uri.substring(0, idx1);
174         return new String JavaDoc[] { nsUri, svcName };
175     }
176     
177 }
178
Popular Tags