KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RmiMessageDispatcher.java 3982 2006-11-22 14:28:01Z 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.providers.rmi;
12
13 import java.lang.reflect.Method JavaDoc;
14 import java.rmi.RMISecurityManager JavaDoc;
15 import java.rmi.Remote JavaDoc;
16 import java.util.Collections JavaDoc;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.mule.impl.MuleMessage;
21 import org.mule.providers.AbstractMessageDispatcher;
22 import org.mule.umo.UMOEvent;
23 import org.mule.umo.UMOException;
24 import org.mule.umo.UMOMessage;
25 import org.mule.umo.endpoint.UMOImmutableEndpoint;
26 import org.mule.umo.provider.UMOConnector;
27 import org.mule.umo.transformer.TransformerException;
28
29 /**
30  * <code>RmiMessageDispatcher</code> will send transformed mule events over
31  * RMI-JRMP.
32  */

33 public class RmiMessageDispatcher extends AbstractMessageDispatcher
34 {
35     protected static Log logger = LogFactory.getLog(RmiMessageDispatcher.class);
36
37     private final RmiConnector connector;
38     protected volatile Remote JavaDoc remoteObject;
39     protected volatile Method JavaDoc invokedMethod;
40
41     public RmiMessageDispatcher(UMOImmutableEndpoint endpoint)
42     {
43         super(endpoint);
44         this.connector = (RmiConnector)endpoint.getConnector();
45     }
46
47     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
48     {
49         if (remoteObject == null)
50         {
51             String JavaDoc rmiPolicyPath = connector.getSecurityPolicy();
52
53             System.setProperty("java.security.policy", rmiPolicyPath);
54
55             // Set security manager
56
if (System.getSecurityManager() == null)
57             {
58                 System.setSecurityManager(new RMISecurityManager JavaDoc());
59             }
60
61             remoteObject = connector.getRemoteObject(endpoint);
62         }
63     }
64
65     protected void doDisconnect() throws Exception JavaDoc
66     {
67         remoteObject = null;
68         invokedMethod = null;
69     }
70
71     private Object JavaDoc[] getArgs(UMOEvent event) throws TransformerException
72     {
73         Object JavaDoc payload = event.getTransformedMessage();
74         if (payload instanceof Object JavaDoc[])
75         {
76             return (Object JavaDoc[])payload;
77         }
78         else
79         {
80             return new Object JavaDoc[]{payload};
81         }
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.mule.umo.provider.UMOConnectorSession#dispatch(org.mule.umo.UMOEvent)
88      */

89     protected void doDispatch(UMOEvent event) throws Exception JavaDoc
90     {
91         Object JavaDoc[] arguments = getArgs(event);
92         if (invokedMethod == null)
93         {
94             invokedMethod = connector.getMethodObject(remoteObject, event);
95         }
96         invokedMethod.invoke(remoteObject, arguments);
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see org.mule.umo.provider.UMOConnectorSession#send(org.mule.umo.UMOEvent)
103      */

104     public UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
105     {
106         if (invokedMethod == null)
107         {
108             invokedMethod = connector.getMethodObject(remoteObject, event);
109         }
110
111         Object JavaDoc[] arguments = getArgs(event);
112         Object JavaDoc result = invokedMethod.invoke(remoteObject, arguments);
113
114         if (result == null)
115         {
116             return null;
117         }
118         else
119         {
120             return new MuleMessage(connector.getMessageAdapter(result).getPayload(), Collections.EMPTY_MAP);
121         }
122     }
123
124     /**
125      * Make a specific request to the underlying transport
126      *
127      * @param endpoint the endpoint to use when connecting to the resource
128      * @param timeout the maximum time the operation should block before returning.
129      * The call should return immediately if there is data available. If
130      * no data becomes available before the timeout elapses, null will be
131      * returned
132      * @return the result of the request wrapped in a UMOMessage object. Null will be
133      * returned if no data was avaialable
134      * @throws Exception if the call to the underlying protocal cuases an exception
135      */

136     protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
137     {
138         throw new UnsupportedOperationException JavaDoc("doReceive");
139     }
140
141     /**
142      * There is no associated session for a RMI connector
143      *
144      * @return
145      * @throws UMOException
146      */

147     public Object JavaDoc getDelegateSession() throws UMOException
148     {
149         return null;
150     }
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.mule.umo.provider.UMOConnectorSession#getConnector()
156      */

157     public UMOConnector getConnector()
158     {
159         return connector;
160     }
161
162     protected void doDispose()
163     {
164         // template method
165
}
166 }
167
Popular Tags