1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.rmi;
12
13 import org.mule.api.endpoint.EndpointBuilder;
14 import org.mule.api.endpoint.InboundEndpoint;
15 import org.mule.api.lifecycle.InitialisationException;
16 import org.mule.config.i18n.MessageFactory;
17 import org.mule.endpoint.EndpointURIEndpointBuilder;
18 import org.mule.transport.AbstractMessageReceiverTestCase;
19 import org.mule.util.concurrent.Latch;
20
21 import java.rmi.Naming;
22 import java.rmi.registry.LocateRegistry;
23 import java.rmi.registry.Registry;
24 import java.rmi.server.UnicastRemoteObject;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.Hashtable;
28 import java.util.Map;
29
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32
33 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38
39
40
41
42
43 public class RmiMessageReceiverTestCase extends AbstractMessageReceiverTestCase
44 {
45 private static Log LOGGER = LogFactory.getLog(RmiMessageReceiverTestCase.class);
46
47 private RmiConnector connector = null;
48 private RmiMessageReceiver messageReceiver = null;
49 private Registry rmiRegistry = null;
50
51 @Override
52 protected void doSetUp() throws Exception
53 {
54 registerRmi();
55
56 connector = new RmiConnector(muleContext);
57 connector.setName("TestConnector:" + this.getClass());
58 connector.setSecurityPolicy(ClassLoader.getSystemResource("rmi.policy").getPath());
59 connector.setJndiInitialFactory("com.sun.jndi.rmi.registry.RegistryContextFactory");
60 connector.setJndiProviderUrl("rmi://localhost:11099");
61
62 muleContext.getRegistry().registerConnector(connector);
63
64 super.doSetUp();
65 }
66
67 @SuppressWarnings("unchecked")
68 @Override
69 public InboundEndpoint getEndpoint() throws Exception
70 {
71 if (this.endpoint == null)
72 {
73 EndpointBuilder builder = new EndpointURIEndpointBuilder(
74 "rmi://localhost:11099/TestMatchingMethodsComponent?method=reverseString", muleContext);
75
76 if (connector == null)
77 {
78 throw new InitialisationException(
79 MessageFactory.createStaticMessage("Connector has not been initialized."), null);
80 }
81 builder.setConnector(connector);
82
83 Map properties = new HashMap();
84 properties.put("methodArgumentTypes", "java.lang.String");
85 properties.put("methodArgumentsList", Arrays.asList(new String[]{"test"}));
86
87 builder.setProperties(properties);
88 endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(builder);
89
90 return endpoint;
91 }
92 return this.endpoint;
93 }
94
95 @Override
96 public RmiMessageReceiver getMessageReceiver() throws Exception
97 {
98 if (messageReceiver == null)
99 {
100 messageReceiver = new RmiMessageReceiver(this.connector, getTestService(),
101 this.getEndpoint(), 5000)
102 {
103 @Override
104 public void poll()
105 {
106 super.poll();
107 RmiMessageReceiverTestCase.this.callbackCalled.countDown();
108 }
109
110 };
111 messageReceiver.initialise();
112 }
113 return messageReceiver;
114 }
115
116 public void testReceive() throws Exception
117 {
118 RmiMessageReceiver messageReceiver = this.getMessageReceiver();
119
120
121 assertNull(messageReceiver.invokeMethod);
122 messageReceiver.connect();
123
124
125 assertNotNull(messageReceiver.invokeMethod);
126
127
128 callbackCalled = new Latch();
129 messageReceiver.poll();
130 assertTrue(callbackCalled.await(1000, TimeUnit.MILLISECONDS));
131 }
132
133 private void registerRmi() throws Exception
134 {
135 if (null == rmiRegistry)
136 {
137 rmiRegistry = LocateRegistry.createRegistry(11099);
138 Naming.rebind("//localhost:11099/TestMatchingMethodsComponent",
139 new SerializedMatchingMethodsComponent());
140
141 Hashtable<String, String> env = new Hashtable<String, String>();
142 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
143
144 Context context = new InitialContext(env);
145 SerializedMatchingMethodsComponent obj =
146 (SerializedMatchingMethodsComponent)context.lookup("rmi://localhost:11099/TestMatchingMethodsComponent");
147
148 if (obj == null)
149 {
150 throw new RuntimeException("Could not start RMI properly");
151 }
152 }
153 }
154
155 @Override
156 protected void doTearDown() throws Exception
157 {
158 try
159 {
160 messageReceiver.disconnect();
161 messageReceiver.dispose();
162
163 connector.disconnect();
164 connector.dispose();
165
166 UnicastRemoteObject.unexportObject(rmiRegistry, true);
167 }
168 catch (Exception e)
169 {
170 LOGGER.warn(e.toString(), e);
171 }
172
173 super.doTearDown();
174 }
175 }