KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > AbstractServiceEnabledConnector


1 /*
2  * $Id: AbstractServiceEnabledConnector.java 3798 2006-11-04 04:07:14Z 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.providers;
12
13 import org.mule.MuleManager;
14 import org.mule.config.i18n.Message;
15 import org.mule.config.i18n.Messages;
16 import org.mule.providers.service.ConnectorFactory;
17 import org.mule.providers.service.ConnectorServiceDescriptor;
18 import org.mule.providers.service.ConnectorServiceException;
19 import org.mule.umo.MessagingException;
20 import org.mule.umo.UMOComponent;
21 import org.mule.umo.endpoint.UMOEndpoint;
22 import org.mule.umo.endpoint.UMOEndpointURI;
23 import org.mule.umo.lifecycle.InitialisationException;
24 import org.mule.umo.provider.UMOMessageAdapter;
25 import org.mule.umo.provider.UMOMessageReceiver;
26 import org.mule.umo.provider.UMOStreamMessageAdapter;
27 import org.mule.util.BeanUtils;
28 import org.mule.util.ObjectNameHelper;
29 import org.mule.util.PropertiesUtils;
30
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 /**
38  * <code>AbstractServiceEnabledConnector</code> initialises a connector from a
39  * service descriptor. Using this method greatly reduces the code required to
40  * implement a connector and means that Mule can create connectors and endpoints from
41  * a url if the connector has a service descriptor.
42  *
43  * @see ConnectorServiceDescriptor
44  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
45  * @version $Revision: 3798 $
46  */

47
48 public abstract class AbstractServiceEnabledConnector extends AbstractConnector
49 {
50     /**
51      * Holds the service configuration for this connector
52      */

53     protected ConnectorServiceDescriptor serviceDescriptor;
54
55     protected Properties JavaDoc serviceOverrides;
56
57     public void doInitialise() throws InitialisationException
58     {
59         initFromServiceDescriptor();
60     }
61
62     public void initialiseFromUrl(UMOEndpointURI endpointUri) throws InitialisationException
63     {
64         if (!supportsProtocol(endpointUri.getFullScheme()))
65         {
66             throw new InitialisationException(new Message(Messages.SCHEME_X_NOT_COMPATIBLE_WITH_CONNECTOR_X,
67                 endpointUri.getFullScheme(), getClass().getName()), this);
68         }
69         Properties JavaDoc props = new Properties JavaDoc();
70         props.putAll(endpointUri.getParams());
71         // auto set username and password
72
if (endpointUri.getUserInfo() != null)
73         {
74             props.setProperty("username", endpointUri.getUsername());
75             String JavaDoc passwd = endpointUri.getPassword();
76             if (passwd != null)
77             {
78                 props.setProperty("password", passwd);
79             }
80         }
81         String JavaDoc host = endpointUri.getHost();
82         if (host != null)
83         {
84             props.setProperty("hostname", host);
85             props.setProperty("host", host);
86         }
87         if (endpointUri.getPort() > -1)
88         {
89             props.setProperty("port", String.valueOf(endpointUri.getPort()));
90         }
91
92         BeanUtils.populateWithoutFail(this, props, true);
93
94         setName(ObjectNameHelper.getConnectorName(this));
95     }
96
97     protected synchronized void initFromServiceDescriptor() throws InitialisationException
98     {
99         try
100         {
101             serviceDescriptor = ConnectorFactory.getServiceDescriptor(getProtocol().toLowerCase(),
102                 serviceOverrides);
103
104             if (serviceDescriptor.getDispatcherFactory() != null)
105             {
106                 logger.info("Loading DispatcherFactory: " + serviceDescriptor.getDispatcherFactory());
107                 dispatcherFactory = serviceDescriptor.createDispatcherFactory();
108             }
109
110             defaultInboundTransformer = serviceDescriptor.createInboundTransformer();
111             defaultOutboundTransformer = serviceDescriptor.createOutboundTransformer();
112             defaultResponseTransformer = serviceDescriptor.createResponseTransformer();
113
114             sessionHandler = serviceDescriptor.createSessionHandler();
115             // set any manager default properties for the connector
116
// these are set on the Manager with a protocol i.e.
117
// jms.specification=1.1
118
// This provides a really convenient way to set properties on object form
119
// unit
120
// tests
121
Map JavaDoc props = new HashMap JavaDoc();
122             PropertiesUtils.getPropertiesWithPrefix(MuleManager.getInstance().getProperties(),
123                 getProtocol().toLowerCase(), props);
124             if (props.size() > 0)
125             {
126                 props = PropertiesUtils.removeNamespaces(props);
127                 org.mule.util.BeanUtils.populateWithoutFail(this, props, true);
128             }
129         }
130         catch (Exception JavaDoc e)
131         {
132             throw new InitialisationException(e, this);
133         }
134     }
135
136     protected ConnectorServiceDescriptor getServiceDescriptor()
137     {
138         if (serviceDescriptor == null)
139         {
140             throw new IllegalStateException JavaDoc("This connector has not yet been initialised: " + name);
141         }
142         return serviceDescriptor;
143     }
144
145     public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception JavaDoc
146     {
147         return getServiceDescriptor().createMessageReceiver(this, component, endpoint);
148     }
149
150     /**
151      * Gets a <code>UMOMessageAdapter</code> for the endpoint for the given message
152      * (data)
153      *
154      * @param message the data with which to initialise the
155      * <code>UMOMessageAdapter</code>
156      * @return the <code>UMOMessageAdapter</code> for the endpoint
157      * @throws org.mule.umo.MessagingException if the message parameter is not
158      * supported
159      * @see org.mule.umo.provider.UMOMessageAdapter
160      */

161     public UMOMessageAdapter getMessageAdapter(Object JavaDoc message) throws MessagingException
162     {
163         try
164         {
165             return serviceDescriptor.createMessageAdapter(message);
166         }
167         catch (ConnectorServiceException e)
168         {
169             throw new MessagingException(new Message(Messages.FAILED_TO_CREATE_X, "Message Adapter"),
170                 message, e);
171         }
172     }
173
174     public UMOStreamMessageAdapter getStreamMessageAdapter(InputStream JavaDoc in, OutputStream JavaDoc out)
175         throws MessagingException
176     {
177         try
178         {
179             return serviceDescriptor.createStreamMessageAdapter(in, out);
180         }
181         catch (ConnectorServiceException e)
182         {
183             throw new MessagingException(new Message(Messages.FAILED_TO_CREATE_X, "Stream Message Adapter"),
184                 in, e);
185         }
186     }
187
188     public Map JavaDoc getServiceOverrides()
189     {
190         return serviceOverrides;
191     }
192
193     public void setServiceOverrides(Map JavaDoc serviceOverrides)
194     {
195         this.serviceOverrides = new Properties JavaDoc();
196         this.serviceOverrides.putAll(serviceOverrides);
197     }
198 }
199
Popular Tags