KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractEndpointBuilder.java 4219 2006-12-09 10:15:14Z lajos $
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 org.mule.MuleManager;
14 import org.mule.providers.service.ConnectorFactory;
15 import org.mule.umo.endpoint.MalformedEndpointException;
16 import org.mule.umo.endpoint.UMOEndpointURI;
17 import org.mule.util.PropertiesUtils;
18
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.net.URLDecoder JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 /**
25  * <code>UrlEndpointBuilder</code> is the default endpointUri strategy suitable for
26  * most connectors
27  *
28  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
29  * @version $Revision: 4219 $
30  */

31
32 public abstract class AbstractEndpointBuilder implements EndpointBuilder
33 {
34     protected String JavaDoc address;
35     protected String JavaDoc endpointName;
36     protected String JavaDoc connectorName;
37     protected String JavaDoc transformers;
38     protected String JavaDoc responseTransformers;
39     protected String JavaDoc userInfo;
40
41     protected int createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR;
42
43     public UMOEndpointURI build(URI uri) throws MalformedEndpointException
44     {
45         Properties JavaDoc props = getPropertiesForURI(uri);
46         String JavaDoc replaceAddress = null;
47         //If the address has been set as a parameter on the URI, then we must ensure that that value is used
48
//for the address. We still call the setEndpoint() method so that other information on the URI
49
//is still processed
50
if (address != null)
51         {
52             replaceAddress = address;
53             setEndpoint(uri, props);
54             address = replaceAddress;
55         }
56         else
57         {
58             setEndpoint(uri, props);
59         }
60
61         UMOEndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers,
62             responseTransformers, createConnector, props, uri, userInfo);
63         address = null;
64         endpointName = null;
65         connectorName = null;
66         transformers = null;
67         responseTransformers = null;
68         createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR;
69         return ep;
70     }
71
72     protected abstract void setEndpoint(URI uri, Properties JavaDoc props) throws MalformedEndpointException;
73
74     protected Properties JavaDoc getPropertiesForURI(URI uri) throws MalformedEndpointException
75     {
76         Properties JavaDoc properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery());
77
78         String JavaDoc tempEndpointName = (String JavaDoc)properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_NAME);
79         if (tempEndpointName != null)
80         {
81             this.endpointName = tempEndpointName;
82         }
83         // override the endpointUri if set
84
String JavaDoc endpoint = (String JavaDoc)properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_URI);
85         if (endpoint != null)
86         {
87             this.address = endpoint;
88             address = decode(address, uri);
89         }
90
91         String JavaDoc cnnName = (String JavaDoc)properties.get(UMOEndpointURI.PROPERTY_CONNECTOR_NAME);
92         if (cnnName != null)
93         {
94             this.connectorName = cnnName;
95         }
96
97         String JavaDoc create = (String JavaDoc)properties.get(UMOEndpointURI.PROPERTY_CREATE_CONNECTOR);
98         if (create != null)
99         {
100             if ("0".equals(create))
101             {
102                 this.createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR;
103             }
104             else if ("1".equals(create))
105             {
106                 this.createConnector = ConnectorFactory.ALWAYS_CREATE_CONNECTOR;
107             }
108             else if ("2".equals(create))
109             {
110                 this.createConnector = ConnectorFactory.NEVER_CREATE_CONNECTOR;
111             }
112             else if ("IF_NEEDED".equals(create))
113             {
114                 this.createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR;
115             }
116             else if ("ALWAYS".equals(create))
117             {
118                 this.createConnector = ConnectorFactory.ALWAYS_CREATE_CONNECTOR;
119             }
120             else if ("NEVER".equals(create))
121             {
122                 this.createConnector = ConnectorFactory.NEVER_CREATE_CONNECTOR;
123             }
124             else if (connectorName == null)
125             {
126                 this.createConnector = ConnectorFactory.USE_CONNECTOR;
127                 connectorName = create;
128             }
129
130         }
131
132         transformers = (String JavaDoc)properties.get(UMOEndpointURI.PROPERTY_TRANSFORMERS);
133         if (transformers != null)
134         {
135             transformers = transformers.replaceAll(" ", ",");
136         }
137         responseTransformers = (String JavaDoc)properties.get(UMOEndpointURI.PROPERTY_RESPONSE_TRANSFORMERS);
138         if (responseTransformers != null)
139         {
140             responseTransformers = responseTransformers.replaceAll(" ", ",");
141         }
142         // If we have user info, decode it as it might contain '@' or other encodable
143
// characters
144
userInfo = uri.getUserInfo();
145         if (userInfo != null)
146         {
147             userInfo = decode(userInfo, uri);
148         }
149         return properties;
150     }
151
152     private String JavaDoc decode(String JavaDoc string, URI uri) throws MalformedEndpointException
153     {
154         try
155         {
156             return URLDecoder.decode(string, MuleManager.getConfiguration().getEncoding());
157         }
158         catch (UnsupportedEncodingException JavaDoc e)
159         {
160             throw new MalformedEndpointException(uri.toString(), e);
161         }
162     }
163 }
164
Popular Tags