KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > rmi > RemoteCallServerTest


1 package com.tirsen.nanning.samples.rmi;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.security.PrivilegedExceptionAction JavaDoc;
9 import java.security.AccessController JavaDoc;
10 import java.security.PrivilegedActionException JavaDoc;
11
12 import javax.security.auth.Subject JavaDoc;
13
14 import com.tirsen.nanning.Aspects;
15 import com.tirsen.nanning.attribute.AbstractAttributesTest;
16 import com.tirsen.nanning.config.AspectSystem;
17 import com.tirsen.nanning.config.FindTargetMixinAspect;
18 import com.tirsen.nanning.samples.prevayler.CurrentPrevayler;
19 import com.tirsen.nanning.samples.prevayler.MySystem;
20 import com.tirsen.nanning.samples.prevayler.PrevaylerAspect;
21 import org.prevayler.implementation.SnapshotPrevayler;
22 import com.tirsen.nanning.samples.prevayler.MyObject;
23
24 public class RemoteCallServerTest extends AbstractAttributesTest {
25     private AspectSystem serverAspectSystem;
26     private File JavaDoc prevaylerDir;
27     private int port = 12346;
28     private SnapshotPrevayler prevayler;
29     private SocketRemoteCallServer remoteCallServer;
30     private RemoteMarshaller clientMarshaller;
31
32     protected void setUp() throws Exception JavaDoc {
33         super.setUp();
34         prevaylerDir = File.createTempFile("test", "");
35         prevaylerDir.delete();
36         prevaylerDir.deleteOnExit();
37         prevaylerDir.mkdirs();
38
39         serverAspectSystem = new AspectSystem();
40         serverAspectSystem.addAspect(new FindTargetMixinAspect());
41         serverAspectSystem.addAspect(new PrevaylerAspect());
42
43         clientMarshaller = RemoteMarshaller.createClientSideMarshaller();
44
45         // init server side
46
Aspects.setContextAspectFactory(serverAspectSystem);
47         prevayler = new SnapshotPrevayler(serverAspectSystem.newInstance(MySystem.class),
48                                           prevaylerDir.getAbsolutePath());
49
50         remoteCallServer = new SocketRemoteCallServer();
51         remoteCallServer.setPort(port);
52
53         CurrentPrevayler.withPrevayler(prevayler, new Runnable JavaDoc() {
54             public void run() {
55                 // start server, server-threads will inherit the prevayler and the context-aspect-factory from this thread
56
remoteCallServer.start();
57                 remoteCallServer.setAspectFactory(serverAspectSystem);
58             }
59         });
60     }
61
62     protected void tearDown() throws Exception JavaDoc {
63         super.tearDown();
64         remoteCallServer.stop();
65     }
66
67     public void testAuthenticatedCall() throws PrivilegedActionException JavaDoc {
68         // server side
69
remoteCallServer.bind("MyStatelessService", serverAspectSystem.newInstance(MyStatelessService.class));
70
71         // client side
72
Subject.doAs(new Subject JavaDoc(false, new HashSet JavaDoc(), new HashSet JavaDoc(), new HashSet JavaDoc()), new PrivilegedExceptionAction JavaDoc() {
73             public Object JavaDoc run() throws Exception JavaDoc {
74                 Subject JavaDoc subject = Subject.getSubject(AccessController.getContext());
75                 subject.getPrincipals().add(new MyPrincipal("expectedUserName"));
76
77                 Naming naming = new Naming(clientMarshaller, new SocketConnectionManager("localhost", port));
78                 MyStatelessService myService = (MyStatelessService) naming.lookup("MyStatelessService");
79
80                 myService.authenticatedCall("expectedUserName");
81
82                 return null;
83             }
84         });
85     }
86
87     public void testStatelessRemoteCall() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
88         // server side
89
remoteCallServer.bind("MyStatelessService", serverAspectSystem.newInstance(MyStatelessService.class));
90
91         // client side
92
Naming naming = new Naming(clientMarshaller, new SocketConnectionManager("localhost", port));
93         MyStatelessService myService = (MyStatelessService) naming.lookup("MyStatelessService");
94
95         {
96             myService.createObject("attributeValue");
97         }
98
99         {
100             MyObject myObject = myService.getMyObject();
101             String JavaDoc value = myObject.getValue();
102             assertEquals("attributeValue", value);
103         }
104
105         {
106             Collection JavaDoc col = myService.getAllObjects();
107             assertEquals(1, col.size());
108             MyObject[] myObjects = (MyObject[])col.toArray(new MyObject[1]);
109             MyObject myObject = myObjects[0];
110             String JavaDoc value = myObject.getValue();
111             assertEquals("attributeValue", value);
112         }
113
114         // server side
115
CurrentPrevayler.withPrevayler(prevayler, new Runnable JavaDoc() {
116             public void run() {
117                 MySystem system = (MySystem) CurrentPrevayler.getSystem();
118
119                 // server side
120
assertNotNull("object not created on server side", system.getMyObject());
121                 assertEquals("attribute wrong value", "attributeValue", system.getMyObject().getValue());
122             }
123         });
124     }
125
126     public void testStatefulRemoteCall() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
127         // server side
128
remoteCallServer.bind("MyStatefulService", serverAspectSystem.newInstance(MyStatefulService.class));
129
130         // client side
131
MyStatefulService statefulService = (MyStatefulService) new Naming(clientMarshaller, new SocketConnectionManager("localhost", port)).lookup("MyStatefulService");
132         assertNull(statefulService.value());
133         statefulService.modify("value");
134         assertEquals("value", statefulService.value());
135     }
136 }
137
Popular Tags