KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > endpoint > MuleEndpointURI


1 /*
2  * $Id: MuleEndpointURI.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl.endpoint;
12
13 import java.net.URI JavaDoc;
14 import java.net.URISyntaxException JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.mule.MuleManager;
21 import org.mule.providers.service.ConnectorFactory;
22 import org.mule.providers.service.ConnectorFactoryException;
23 import org.mule.providers.service.ConnectorServiceDescriptor;
24 import org.mule.umo.endpoint.MalformedEndpointException;
25 import org.mule.umo.endpoint.UMOEndpointURI;
26 import org.mule.util.PropertiesUtils;
27
28 /**
29  * <code>MuleEndpointURI</code> is used to determine how a message is sent of
30  * received. The url defines the protocol, the endpointUri destination of the message
31  * and optionally the endpoint to use when dispatching the event. Mule urls take the
32  * form of - protocol://[host]:[port]/[provider]/endpointUri or
33  * protocol://[host]:[port]/endpointUri i.e. vm:///my.object or The protocol can be
34  * any of any connector registered with Mule. The endpoint name if specified must be
35  * the name of a register global endpoint The endpointUri can be any endpointUri
36  * recognised by the endpoint type.
37  */

38
39 public class MuleEndpointURI implements UMOEndpointURI
40 {
41     /**
42      * Serial version
43      */

44     private static final long serialVersionUID = 3906735768171252877L;
45
46     /**
47      * logger used by this class
48      */

49     protected static final Log logger = LogFactory.getLog(MuleEndpointURI.class);
50
51     public static boolean isMuleUri(String JavaDoc url)
52     {
53         return url.indexOf(":/") != -1;
54     }
55
56     private String JavaDoc address;
57     private String JavaDoc filterAddress;
58     private String JavaDoc endpointName;
59     private String JavaDoc connectorName;
60     private String JavaDoc transformers;
61     private String JavaDoc responseTransformers;
62     private int createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR;
63     private Properties JavaDoc params = new Properties JavaDoc();
64     private URI JavaDoc uri;
65     private String JavaDoc uriString;
66     private String JavaDoc userInfo;
67     private String JavaDoc schemeMetaInfo;
68     private String JavaDoc resourceInfo;
69
70     MuleEndpointURI(String JavaDoc address,
71                     String JavaDoc endpointName,
72                     String JavaDoc connectorName,
73                     String JavaDoc transformers,
74                     String JavaDoc responseTransformers,
75                     int createConnector,
76                     Properties JavaDoc properties,
77                     URI JavaDoc uri,
78                     String JavaDoc userInfo)
79     {
80         this(address, endpointName, connectorName, transformers, responseTransformers, createConnector,
81             properties, uri);
82         if (userInfo != null)
83         {
84             this.userInfo = userInfo;
85         }
86     }
87
88     public MuleEndpointURI(String JavaDoc address,
89                            String JavaDoc endpointName,
90                            String JavaDoc connectorName,
91                            String JavaDoc transformers,
92                            String JavaDoc responseTransformers,
93                            int createConnector,
94                            Properties JavaDoc properties,
95                            URI JavaDoc uri)
96     {
97         this.address = address;
98         this.endpointName = endpointName;
99         this.connectorName = connectorName;
100         this.transformers = transformers;
101         this.responseTransformers = responseTransformers;
102         this.createConnector = createConnector;
103         this.params = properties;
104         this.uri = uri;
105         this.uriString = uri.toASCIIString();
106         this.userInfo = uri.getUserInfo();
107         if (properties != null)
108         {
109             resourceInfo = (String JavaDoc)properties.remove("resourceInfo");
110         }
111     }
112
113     public MuleEndpointURI(UMOEndpointURI endpointUri)
114     {
115         initialise(endpointUri);
116     }
117
118     public MuleEndpointURI(UMOEndpointURI endpointUri, String JavaDoc filterAddress)
119     {
120         initialise(endpointUri);
121         this.filterAddress = filterAddress;
122     }
123
124     public MuleEndpointURI(String JavaDoc uri) throws MalformedEndpointException
125     {
126         String JavaDoc uriIdentifier = MuleManager.getInstance().lookupEndpointIdentifier(uri, uri);
127         if (!uriIdentifier.equals(uri))
128         {
129             endpointName = uri;
130             uri = uriIdentifier;
131         }
132
133         uri = uri.trim().replaceAll(" ", "%20");
134
135         if (!validateUrl(uri))
136         {
137             throw new MalformedEndpointException(uri);
138         }
139         try
140         {
141             schemeMetaInfo = retrieveSchemeMetaInfo(uri);
142             if (schemeMetaInfo != null)
143             {
144                 uri = uri.replaceFirst(schemeMetaInfo + ":", "");
145             }
146             this.uri = new URI JavaDoc(uri);
147             this.userInfo = this.uri.getRawUserInfo();
148         }
149         catch (URISyntaxException JavaDoc e)
150         {
151             throw new MalformedEndpointException(uri, e);
152         }
153
154         try
155         {
156             String JavaDoc scheme = (schemeMetaInfo == null ? this.uri.getScheme() : schemeMetaInfo);
157             ConnectorServiceDescriptor csd = ConnectorFactory.getServiceDescriptor(scheme);
158             EndpointBuilder builder = csd.createEndpointBuilder();
159             UMOEndpointURI built = builder.build(this.uri);
160             initialise(built);
161         }
162         catch (ConnectorFactoryException e)
163         {
164             throw new MalformedEndpointException(e);
165         }
166     }
167
168     private String JavaDoc retrieveSchemeMetaInfo(String JavaDoc url)
169     {
170         int i = url.indexOf(':');
171         if (i == -1)
172         {
173             return null;
174         }
175         if (url.charAt(i + 1) == '/')
176         {
177             return null;
178         }
179         else
180         {
181             return url.substring(0, i);
182         }
183     }
184
185     protected boolean validateUrl(String JavaDoc url)
186     {
187         return (url.indexOf(":/") > 0);
188     }
189
190     private void initialise(UMOEndpointURI endpointUri)
191     {
192         this.address = endpointUri.getAddress();
193         if (this.endpointName == null)
194         {
195             this.endpointName = endpointUri.getEndpointName();
196         }
197         this.connectorName = endpointUri.getConnectorName();
198         this.transformers = endpointUri.getTransformers();
199         this.responseTransformers = endpointUri.getResponseTransformers();
200         this.createConnector = endpointUri.getCreateConnector();
201         this.params = endpointUri.getParams();
202         this.uri = endpointUri.getUri();
203         this.uriString = this.uri.toASCIIString();
204         this.resourceInfo = endpointUri.getResourceInfo();
205         this.userInfo = endpointUri.getUserInfo();
206     }
207
208     public String JavaDoc getAddress()
209     {
210         return address;
211     }
212
213     public String JavaDoc getEndpointName()
214     {
215         return (StringUtils.isEmpty(endpointName) ? null : endpointName);
216     }
217
218     public Properties JavaDoc getParams()
219     {
220         // TODO fix this so that the query string properties are not lost.
221
// not sure whats causing this at the moment
222
if (params.size() == 0 && getQuery() != null)
223         {
224             params = PropertiesUtils.getPropertiesFromQueryString(getQuery());
225         }
226         return params;
227     }
228
229     public Properties JavaDoc getUserParams()
230     {
231         Properties JavaDoc p = new Properties JavaDoc();
232         p.putAll(params);
233         p.remove(PROPERTY_ENDPOINT_NAME);
234         p.remove(PROPERTY_ENDPOINT_URI);
235         p.remove(PROPERTY_CREATE_CONNECTOR);
236         p.remove(PROPERTY_TRANSFORMERS);
237         return p;
238     }
239
240     public URI JavaDoc parseServerAuthority() throws URISyntaxException JavaDoc
241     {
242         return uri.parseServerAuthority();
243     }
244
245     public URI JavaDoc normalize()
246     {
247         return uri.normalize();
248     }
249
250     public URI JavaDoc resolve(URI JavaDoc uri)
251     {
252         return uri.resolve(uri);
253     }
254
255     public URI JavaDoc resolve(String JavaDoc str)
256     {
257         return uri.resolve(str);
258     }
259
260     public URI JavaDoc relativize(URI JavaDoc uri)
261     {
262         return uri.relativize(uri);
263     }
264
265     public String JavaDoc getScheme()
266     {
267         return uri.getScheme();
268     }
269
270     public String JavaDoc getFullScheme()
271     {
272         return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo + ':' + uri.getScheme());
273
274     }
275
276     public boolean isAbsolute()
277     {
278         return uri.isAbsolute();
279     }
280
281     public boolean isOpaque()
282     {
283         return uri.isOpaque();
284     }
285
286     public String JavaDoc getRawSchemeSpecificPart()
287     {
288         return uri.getRawSchemeSpecificPart();
289     }
290
291     public String JavaDoc getSchemeSpecificPart()
292     {
293         return uri.getSchemeSpecificPart();
294     }
295
296     public String JavaDoc getRawAuthority()
297     {
298         return uri.getRawAuthority();
299     }
300
301     public String JavaDoc getAuthority()
302     {
303         return uri.getAuthority();
304     }
305
306     public String JavaDoc getRawUserInfo()
307     {
308         return uri.getRawUserInfo();
309     }
310
311     public String JavaDoc getUserInfo()
312     {
313         return userInfo;
314     }
315
316     public String JavaDoc getHost()
317     {
318         return uri.getHost();
319     }
320
321     public int getPort()
322     {
323         return uri.getPort();
324     }
325
326     public String JavaDoc getRawPath()
327     {
328         return uri.getRawPath();
329     }
330
331     public String JavaDoc getPath()
332     {
333         return uri.getPath();
334     }
335
336     public String JavaDoc getRawQuery()
337     {
338         return uri.getRawQuery();
339     }
340
341     public String JavaDoc getQuery()
342     {
343         return uri.getQuery();
344     }
345
346     public String JavaDoc getRawFragment()
347     {
348         return uri.getRawFragment();
349     }
350
351     public String JavaDoc getFragment()
352     {
353         return uri.getFragment();
354     }
355
356     public String JavaDoc toString()
357     {
358         return uriString;
359     }
360
361     public String JavaDoc getTransformers()
362     {
363         return transformers;
364     }
365
366     public int getCreateConnector()
367     {
368         return createConnector;
369     }
370
371     public URI JavaDoc getUri()
372     {
373         return uri;
374     }
375
376     public String JavaDoc getConnectorName()
377     {
378         return connectorName;
379     }
380
381     public String JavaDoc getSchemeMetaInfo()
382     {
383         return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo);
384     }
385
386     public String JavaDoc getResourceInfo()
387     {
388         return resourceInfo;
389     }
390
391     public String JavaDoc getFilterAddress()
392     {
393         return filterAddress;
394     }
395
396     public void setEndpointName(String JavaDoc name)
397     {
398         endpointName = name;
399     }
400
401     public String JavaDoc getUsername()
402     {
403         if (StringUtils.isNotBlank(userInfo))
404         {
405             int i = userInfo.indexOf(':');
406             if (i == -1)
407             {
408                 return userInfo;
409             }
410             else
411             {
412                 return userInfo.substring(0, i);
413             }
414         }
415         return null;
416     }
417
418     public String JavaDoc getResponseTransformers()
419     {
420         return responseTransformers;
421     }
422
423     public String JavaDoc getPassword()
424     {
425         if (StringUtils.isNotBlank(userInfo))
426         {
427             int i = userInfo.indexOf(':');
428             if (i > -1)
429             {
430                 return userInfo.substring(i + 1);
431             }
432         }
433         return null;
434     }
435
436     public boolean equals(Object JavaDoc o)
437     {
438         if (this == o)
439         {
440             return true;
441         }
442         if (!(o instanceof MuleEndpointURI))
443         {
444             return false;
445         }
446
447         final MuleEndpointURI muleEndpointURI = (MuleEndpointURI)o;
448
449         if (createConnector != muleEndpointURI.createConnector)
450         {
451             return false;
452         }
453         if (address != null ? !address.equals(muleEndpointURI.address) : muleEndpointURI.address != null)
454         {
455             return false;
456         }
457         if (connectorName != null
458                         ? !connectorName.equals(muleEndpointURI.connectorName)
459                         : muleEndpointURI.connectorName != null)
460         {
461             return false;
462         }
463         if (endpointName != null
464                         ? !endpointName.equals(muleEndpointURI.endpointName)
465                         : muleEndpointURI.endpointName != null)
466         {
467             return false;
468         }
469         if (filterAddress != null
470                         ? !filterAddress.equals(muleEndpointURI.filterAddress)
471                         : muleEndpointURI.filterAddress != null)
472         {
473             return false;
474         }
475         if (params != null ? !params.equals(muleEndpointURI.params) : muleEndpointURI.params != null)
476         {
477             return false;
478         }
479         if (resourceInfo != null
480                         ? !resourceInfo.equals(muleEndpointURI.resourceInfo)
481                         : muleEndpointURI.resourceInfo != null)
482         {
483             return false;
484         }
485         if (schemeMetaInfo != null
486                         ? !schemeMetaInfo.equals(muleEndpointURI.schemeMetaInfo)
487                         : muleEndpointURI.schemeMetaInfo != null)
488         {
489             return false;
490         }
491         if (transformers != null
492                         ? !transformers.equals(muleEndpointURI.transformers)
493                         : muleEndpointURI.transformers != null)
494         {
495             return false;
496         }
497         if (responseTransformers != null
498                         ? !responseTransformers.equals(muleEndpointURI.responseTransformers)
499                         : muleEndpointURI.responseTransformers != null)
500         {
501             return false;
502         }
503         if (uri != null ? !uri.equals(muleEndpointURI.uri) : muleEndpointURI.uri != null)
504         {
505             return false;
506         }
507
508         return true;
509     }
510
511     public int hashCode()
512     {
513         int result = (address != null ? address.hashCode() : 0);
514         result = 29 * result + (filterAddress != null ? filterAddress.hashCode() : 0);
515         result = 29 * result + (endpointName != null ? endpointName.hashCode() : 0);
516         result = 29 * result + (connectorName != null ? connectorName.hashCode() : 0);
517         result = 29 * result + (transformers != null ? transformers.hashCode() : 0);
518         result = 29 * result + (responseTransformers != null ? responseTransformers.hashCode() : 0);
519         result = 29 * result + createConnector;
520         result = 29 * result + (params != null ? params.hashCode() : 0);
521         result = 29 * result + (uri != null ? uri.hashCode() : 0);
522         result = 29 * result + (schemeMetaInfo != null ? schemeMetaInfo.hashCode() : 0);
523         return 29 * result + (resourceInfo != null ? resourceInfo.hashCode() : 0);
524     }
525 }
526
Popular Tags