KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > rmi > RmiCallbackMessageReceiver


1 /*
2  * $Id: RmiCallbackMessageReceiver.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.rmi;
12
13 import org.mule.providers.AbstractMessageReceiver;
14 import org.mule.providers.ConnectException;
15 import org.mule.umo.provider.UMOConnector;
16 import org.mule.umo.provider.UMOMessageAdapter;
17 import org.mule.umo.UMOComponent;
18 import org.mule.umo.MessagingException;
19 import org.mule.umo.UMOException;
20 import org.mule.umo.lifecycle.InitialisationException;
21 import org.mule.umo.endpoint.UMOEndpoint;
22 import org.mule.umo.endpoint.UMOEndpointURI;
23 import org.mule.impl.MuleMessage;
24
25 import javax.naming.Context JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 /**
30  * TODO
31  */

32
33 public class RmiCallbackMessageReceiver extends AbstractMessageReceiver
34 {
35     /**
36      * The property name for the service object implementing the callback interface
37      * RmiAble This should be set on the inbound endpoint
38      */

39     public static final String JavaDoc PROPERTY_SERVICE_CLASS_NAME = "serviceClassName";
40
41     protected RmiConnector connector;
42
43     protected RmiAble remoteObject = null;
44
45     private Context JavaDoc jndi = null;
46
47     private String JavaDoc bindName = null;
48
49     public RmiCallbackMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
50         throws InitialisationException
51     {
52         super(connector, component, endpoint);
53
54         this.connector = (RmiConnector)connector;
55     }
56
57     /**
58      * Actual initialization. Attempts to rebind service object to Jndi Tree for
59      * discovery
60      *
61      * @param endpoint
62      * @throws org.mule.umo.lifecycle.InitialisationException
63      */

64     private void initialize(UMOEndpoint endpoint) throws InitialisationException
65     {
66         logger.debug("Initializing with endpoint " + endpoint);
67
68         String JavaDoc rmiPolicyPath = connector.getSecurityPolicy();
69
70         System.setProperty("java.security.policy", rmiPolicyPath);
71
72         UMOEndpointURI endpointUri = endpoint.getEndpointURI();
73
74         int port = endpointUri.getPort();
75
76         if (port < 1)
77         {
78             port = RmiConnector.DEFAULT_RMI_REGISTRY_PORT;
79         }
80
81         try
82         {
83             InetAddress JavaDoc inetAddress = InetAddress.getByName(endpointUri.getHost());
84
85             bindName = endpointUri.getPath();
86
87             remoteObject = getRmiObject();
88
89             Method JavaDoc theMethod = remoteObject.getClass().getMethod("setReceiver",
90                 new Class JavaDoc[]{RmiCallbackMessageReceiver.class});
91             theMethod.invoke(remoteObject, new Object JavaDoc[]{this});
92
93             jndi = connector.getJndiContext(inetAddress.getHostAddress() + ":" + port);
94
95             jndi.rebind(bindName, remoteObject);
96         }
97         catch (Exception JavaDoc e)
98         {
99             throw new InitialisationException(e, this);
100         }
101
102         logger.debug("Initialized successfully");
103     }
104
105     /**
106      * Initializes endpoint
107      *
108      * @throws org.mule.providers.ConnectException
109      */

110     public void doConnect() throws ConnectException
111     {
112         try
113         {
114             // Do not reinit if RMI is already bound to JNDI!!!
115
// TODO Test how things work under heavy load!!!
116
// Do we need threadlocals or so!?!?
117

118             // TODO [aperepel] consider AtomicBooleans here
119
// for 'initialised/initialising' status, etc.
120
if (null == remoteObject)
121             {
122                 initialize(getEndpoint());
123             }
124         }
125         catch (Exception JavaDoc e)
126         {
127             throw new ConnectException(e, this);
128         }
129     }
130
131     /**
132      * Unbinds Rmi class from registry
133      */

134     public void doDisconnect()
135     {
136         logger.debug("Disconnecting...");
137
138         try
139         {
140             jndi.unbind(bindName);
141         }
142         catch (Exception JavaDoc e)
143         {
144             logger.error(e);
145         }
146
147         logger.debug("Disconnected successfully.");
148     }
149
150     /**
151      * Gets RmiAble objetc for registry to add in.
152      *
153      * @return java.rmi.Remote and RmiAble implementing class
154      * @throws org.mule.umo.lifecycle.InitialisationException
155      */

156     private RmiAble getRmiObject() throws InitialisationException
157     {
158         String JavaDoc className = (String JavaDoc)endpoint.getProperty(PROPERTY_SERVICE_CLASS_NAME);
159
160         if (null == className)
161         {
162             throw new InitialisationException(new org.mule.config.i18n.Message("rmi",
163                 RmiConnector.NO_RMI_SERVICECLASS_SET), this);
164         }
165
166         RmiAble remote = null;
167
168         try
169         {
170             remote = (RmiAble)Class.forName(className).newInstance();
171         }
172         catch (Exception JavaDoc e)
173         {
174             throw new InitialisationException(new org.mule.config.i18n.Message("rmi",
175                 RmiConnector.RMI_SERVICECLASS_INVOCATION_FAILED), e);
176         }
177
178         return (remote);
179     }
180
181     /**
182      * Routes message forward
183      *
184      * @param message
185      * @return
186      * @throws org.mule.umo.MessagingException
187      * @throws org.mule.umo.UMOException
188      */

189     public Object JavaDoc routeMessage(Object JavaDoc message) throws MessagingException, UMOException
190     {
191         UMOMessageAdapter adapter = connector.getMessageAdapter(message);
192
193         return (routeMessage(new MuleMessage(adapter)));
194     }
195 }
196
Popular Tags