001: /*
002: * $Id: MuleEndpointURI.java 10808 2008-02-14 20:36:57Z acooke $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.endpoint;
012:
013: import org.mule.RegistryContext;
014: import org.mule.api.endpoint.EndpointException;
015: import org.mule.api.endpoint.EndpointURI;
016: import org.mule.api.endpoint.EndpointURIBuilder;
017: import org.mule.api.endpoint.MalformedEndpointException;
018: import org.mule.api.lifecycle.InitialisationException;
019: import org.mule.api.lifecycle.LifecycleTransitionResult;
020: import org.mule.api.registry.ServiceDescriptorFactory;
021: import org.mule.api.registry.ServiceException;
022: import org.mule.config.i18n.CoreMessages;
023: import org.mule.transport.service.TransportServiceDescriptor;
024: import org.mule.util.PropertiesUtils;
025: import org.mule.util.StringUtils;
026: import org.mule.util.ClassUtils;
027:
028: import java.net.URI;
029: import java.net.URISyntaxException;
030: import java.util.Properties;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: /**
036: * <code>MuleEndpointURI</code> is used to determine how a message is sent of
037: * received. The url defines the protocol, the endpointUri destination of the message
038: * and optionally the endpoint to use when dispatching the event. Mule urls take the
039: * form of - protocol://[host]:[port]/[provider]/endpointUri or
040: * protocol://[host]:[port]/endpointUri i.e. vm:///my.object or The protocol can be
041: * any of any connector registered with Mule. The endpoint name if specified must be
042: * the name of a register global endpoint The endpointUri can be any endpointUri
043: * recognised by the endpoint type.
044: */
045:
046: public class MuleEndpointURI implements EndpointURI {
047: /**
048: * Serial version
049: */
050: private static final long serialVersionUID = 3906735768171252877L;
051:
052: /**
053: * logger used by this class
054: */
055: protected static final Log logger = LogFactory
056: .getLog(MuleEndpointURI.class);
057:
058: public static boolean isMuleUri(String url) {
059: return url.indexOf(":/") != -1;
060: }
061:
062: private String address;
063: private String filterAddress;
064: private String endpointName;
065: private String connectorName;
066: private String transformers;
067: private String responseTransformers;
068: private Properties params = new Properties();
069: private URI uri;
070: private String userInfo;
071: private String schemeMetaInfo;
072: private String resourceInfo;
073:
074: MuleEndpointURI(String address, String endpointName,
075: String connectorName, String transformers,
076: String responseTransformers, Properties properties,
077: URI uri, String userInfo) {
078: this (address, endpointName, connectorName, transformers,
079: responseTransformers, properties, uri);
080: if (userInfo != null) {
081: this .userInfo = userInfo;
082: }
083: }
084:
085: public MuleEndpointURI(String address, String endpointName,
086: String connectorName, String transformers,
087: String responseTransformers, Properties properties, URI uri) {
088: this .address = address;
089: this .endpointName = endpointName;
090: this .connectorName = connectorName;
091: this .transformers = transformers;
092: this .responseTransformers = responseTransformers;
093: this .params = properties;
094: this .uri = uri;
095: this .userInfo = uri.getUserInfo();
096: if (properties != null) {
097: resourceInfo = (String) properties.remove("resourceInfo");
098: }
099: }
100:
101: public MuleEndpointURI(EndpointURI endpointUri) {
102: initialise(endpointUri);
103: }
104:
105: public MuleEndpointURI(EndpointURI endpointUri, String filterAddress) {
106: initialise(endpointUri);
107: this .filterAddress = filterAddress;
108: }
109:
110: /**
111: * Creates but does not initialize the endpoint URI. It is up to the caller
112: * to call initialise() at some point.
113: */
114: public MuleEndpointURI(String uri) throws EndpointException {
115: uri = uri.trim().replaceAll(" ", "%20");
116:
117: if (!validateUrl(uri)) {
118: throw new MalformedEndpointException(uri);
119: }
120: try {
121: schemeMetaInfo = retrieveSchemeMetaInfo(uri);
122: if (schemeMetaInfo != null) {
123: uri = uri.replaceFirst(schemeMetaInfo + ":", "");
124: }
125: this .uri = new URI(uri);
126: this .userInfo = this .uri.getRawUserInfo();
127: } catch (URISyntaxException e) {
128: throw new MalformedEndpointException(uri, e);
129: }
130: }
131:
132: public LifecycleTransitionResult initialise()
133: throws InitialisationException {
134: try {
135: String scheme = (schemeMetaInfo == null ? this .uri
136: .getScheme() : schemeMetaInfo);
137: TransportServiceDescriptor sd;
138: sd = (TransportServiceDescriptor) RegistryContext
139: .getRegistry()
140: .lookupServiceDescriptor(
141: ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE,
142: scheme, null);
143: if (sd == null) {
144: throw new ServiceException(CoreMessages
145: .noServiceTransportDescriptor(scheme));
146: }
147: EndpointURIBuilder builder = sd.createEndpointBuilder();
148: EndpointURI built = builder.build(this .uri);
149: initialise(built);
150: } catch (Exception e) {
151: throw new InitialisationException(e, this );
152: }
153: return LifecycleTransitionResult.OK;
154: }
155:
156: private String retrieveSchemeMetaInfo(String url) {
157: int i = url.indexOf(':');
158: if (i == -1) {
159: return null;
160: }
161: if (url.charAt(i + 1) == '/') {
162: return null;
163: } else {
164: return url.substring(0, i);
165: }
166: }
167:
168: protected boolean validateUrl(String url) {
169: return (url.indexOf(":/") > 0);
170: }
171:
172: private void initialise(EndpointURI endpointUri) {
173: this .address = endpointUri.getAddress();
174: if (this .endpointName == null) {
175: this .endpointName = endpointUri.getEndpointName();
176: }
177: this .connectorName = endpointUri.getConnectorName();
178: this .transformers = endpointUri.getTransformers();
179: this .responseTransformers = endpointUri
180: .getResponseTransformers();
181: this .params = endpointUri.getParams();
182: this .uri = endpointUri.getUri();
183: this .resourceInfo = endpointUri.getResourceInfo();
184: this .userInfo = endpointUri.getUserInfo();
185: }
186:
187: public String getAddress() {
188: return address;
189: }
190:
191: public String getEndpointName() {
192: return (StringUtils.isEmpty(endpointName) ? null : endpointName);
193: }
194:
195: public Properties getParams() {
196: // TODO fix this so that the query string properties are not lost.
197: // not sure whats causing this at the moment
198: if (params.size() == 0 && getQuery() != null) {
199: params = PropertiesUtils
200: .getPropertiesFromQueryString(getQuery());
201: }
202: return params;
203: }
204:
205: public Properties getUserParams() {
206: Properties p = new Properties();
207: p.putAll(getParams());
208: p.remove(PROPERTY_ENDPOINT_NAME);
209: p.remove(PROPERTY_ENDPOINT_URI);
210: p.remove(PROPERTY_TRANSFORMERS);
211: return p;
212: }
213:
214: public URI parseServerAuthority() throws URISyntaxException {
215: return uri.parseServerAuthority();
216: }
217:
218: public URI normalize() {
219: return uri.normalize();
220: }
221:
222: public URI resolve(URI uri) {
223: return uri.resolve(uri);
224: }
225:
226: public URI resolve(String str) {
227: return uri.resolve(str);
228: }
229:
230: public URI relativize(URI uri) {
231: return uri.relativize(uri);
232: }
233:
234: public String getScheme() {
235: return uri.getScheme();
236: }
237:
238: public String getFullScheme() {
239: return (schemeMetaInfo == null ? uri.getScheme()
240: : schemeMetaInfo + ':' + uri.getScheme());
241:
242: }
243:
244: public boolean isAbsolute() {
245: return uri.isAbsolute();
246: }
247:
248: public boolean isOpaque() {
249: return uri.isOpaque();
250: }
251:
252: public String getRawSchemeSpecificPart() {
253: return uri.getRawSchemeSpecificPart();
254: }
255:
256: public String getSchemeSpecificPart() {
257: return uri.getSchemeSpecificPart();
258: }
259:
260: public String getRawAuthority() {
261: return uri.getRawAuthority();
262: }
263:
264: public String getAuthority() {
265: return uri.getAuthority();
266: }
267:
268: public String getRawUserInfo() {
269: return uri.getRawUserInfo();
270: }
271:
272: public String getUserInfo() {
273: return userInfo;
274: }
275:
276: public String getHost() {
277: return uri.getHost();
278: }
279:
280: public int getPort() {
281: return uri.getPort();
282: }
283:
284: public String getRawPath() {
285: return uri.getRawPath();
286: }
287:
288: public String getPath() {
289: return uri.getPath();
290: }
291:
292: public String getRawQuery() {
293: return uri.getRawQuery();
294: }
295:
296: public String getQuery() {
297: return uri.getQuery();
298: }
299:
300: public String getRawFragment() {
301: return uri.getRawFragment();
302: }
303:
304: public String getFragment() {
305: return uri.getFragment();
306: }
307:
308: public String toString() {
309: return uri.toASCIIString();
310: }
311:
312: public String getTransformers() {
313: return transformers;
314: }
315:
316: public URI getUri() {
317: return uri;
318: }
319:
320: public String getConnectorName() {
321: return connectorName;
322: }
323:
324: public String getSchemeMetaInfo() {
325: return (schemeMetaInfo == null ? uri.getScheme()
326: : schemeMetaInfo);
327: }
328:
329: public String getResourceInfo() {
330: return resourceInfo;
331: }
332:
333: public String getFilterAddress() {
334: return filterAddress;
335: }
336:
337: public void setEndpointName(String name) {
338: endpointName = name;
339: }
340:
341: public String getUser() {
342: if (StringUtils.isNotBlank(userInfo)) {
343: int i = userInfo.indexOf(':');
344: if (i == -1) {
345: return userInfo;
346: } else {
347: return userInfo.substring(0, i);
348: }
349: }
350: return null;
351: }
352:
353: public String getResponseTransformers() {
354: return responseTransformers;
355: }
356:
357: public String getPassword() {
358: if (StringUtils.isNotBlank(userInfo)) {
359: int i = userInfo.indexOf(':');
360: if (i > -1) {
361: return userInfo.substring(i + 1);
362: }
363: }
364: return null;
365: }
366:
367: public boolean equals(Object o) {
368: if (this == o) {
369: return true;
370: }
371: if (!(o instanceof MuleEndpointURI)) {
372: return false;
373: }
374: MuleEndpointURI muleEndpointURI = (MuleEndpointURI) o;
375: return ClassUtils.equal(address, muleEndpointURI.address)
376: && ClassUtils.equal(connectorName,
377: muleEndpointURI.connectorName)
378: && ClassUtils.equal(endpointName,
379: muleEndpointURI.endpointName)
380: && ClassUtils.equal(filterAddress,
381: muleEndpointURI.filterAddress)
382: && ClassUtils.equal(params, muleEndpointURI.params)
383: && ClassUtils.equal(resourceInfo,
384: muleEndpointURI.resourceInfo)
385: && ClassUtils.equal(schemeMetaInfo,
386: muleEndpointURI.schemeMetaInfo)
387: && ClassUtils.equal(transformers,
388: muleEndpointURI.transformers)
389: && ClassUtils.equal(responseTransformers,
390: muleEndpointURI.responseTransformers)
391: && ClassUtils.equal(uri, muleEndpointURI.uri);
392: }
393:
394: public int hashCode() {
395: return ClassUtils.hash(new Object[] { address, filterAddress,
396: endpointName, connectorName, transformers,
397: responseTransformers, params, uri, schemeMetaInfo,
398: resourceInfo });
399: }
400: }
|