1 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 ; 24 25 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.DocumentFragment ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Text ; 30 31 public class URIResolver extends EndpointResolverSupport { 32 33 36 private String uri; 37 38 public URIResolver() { 39 } 40 41 public URIResolver(String 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 uri = this.uri.substring(10); 53 String [] parts = split2(uri); 54 return context.getEndpoints(new QName (parts[0], parts[1])); 55 } else if (uri.startsWith("operation:")) { 56 String uri = this.uri.substring(10); 58 String [] parts = split3(uri); 59 return context.getEndpoints(new QName (parts[0], parts[1])); 60 } else if (uri.startsWith("service:")) { 61 String uri = this.uri.substring(8); 62 String [] parts = split2(uri); 63 return context.getEndpointsForService(new QName (parts[0], parts[1])); 64 } else if (uri.startsWith("endpoint:")) { 65 String uri = this.uri.substring(9); 66 String [] parts = split3(uri); 67 ServiceEndpoint se = context.getEndpoint(new QName (parts[0], parts[1]), parts[2]); 68 if (se != null) { 69 return new ServiceEndpoint[] { se }; 70 } 71 } else { 73 DocumentFragment 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 85 public String getUri() { 86 return uri; 87 } 88 89 92 public void setUri(String uri) { 93 this.uri = uri; 94 } 95 96 public static DocumentFragment createWSAEPR(String uri) { 97 Document doc; 98 try { 99 doc = new SourceTransformer().createDocument(); 100 } catch (Exception e) { 101 throw new RuntimeException (e); 102 } 103 DocumentFragment epr = doc.createDocumentFragment(); 104 Element root = doc.createElement("epr"); 105 Element address = doc.createElementNS("http://www.w3.org/2005/08/addressing", "wsa:Address"); 106 Text txt = doc.createTextNode(uri); 107 address.appendChild(txt); 108 root.appendChild(address); 109 epr.appendChild(root); 110 return epr; 111 } 112 113 120 public static void configureExchange(MessageExchange exchange, ComponentContext context, String uri) { 121 if (exchange == null) throw new NullPointerException ("exchange is null"); 122 if (context == null) throw new NullPointerException ("context is null"); 123 if (uri == null) throw new NullPointerException ("uri is null"); 124 if (uri.startsWith("interface:")) { 125 String uri2 = uri.substring(10); 126 String [] parts = URIResolver.split2(uri2); 127 exchange.setInterfaceName(new QName (parts[0], parts[1])); 128 } else if (uri.startsWith("operation:")) { 129 String uri2 = uri.substring(10); 130 String [] parts = URIResolver.split3(uri2); 131 exchange.setInterfaceName(new QName (parts[0], parts[1])); 132 exchange.setOperation(new QName (parts[0], parts[2])); 133 } else if (uri.startsWith("service:")) { 134 String uri2 = uri.substring(8); 135 String [] parts = URIResolver.split2(uri2); 136 exchange.setService(new QName (parts[0], parts[1])); 137 } else if (uri.startsWith("endpoint:")) { 138 String uri2 = uri.substring(9); 139 String [] parts = URIResolver.split3(uri2); 140 ServiceEndpoint se = context.getEndpoint(new QName (parts[0], parts[1]), parts[2]); 141 exchange.setEndpoint(se); 142 } else { 143 DocumentFragment epr = URIResolver.createWSAEPR(uri); 144 ServiceEndpoint se = context.resolveEndpointReference(epr); 145 exchange.setEndpoint(se); 146 } 147 } 148 149 public static String [] split3(String 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 epName = uri.substring(idx1 + 1); 159 String svcName = uri.substring(idx2 + 1, idx1); 160 String nsUri = uri.substring(0, idx2); 161 return new String [] { nsUri, svcName, epName }; 162 } 163 164 public static String [] split2(String uri) { 165 char sep; 166 if (uri.indexOf('/') > 0) { 167 sep = '/'; 168 } else { 169 sep = ':'; 170 } 171 int idx1 = uri.lastIndexOf(sep); 172 String svcName = uri.substring(idx1 + 1); 173 String nsUri = uri.substring(0, idx1); 174 return new String [] { nsUri, svcName }; 175 } 176 177 } 178 | Popular Tags |