Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/remote/perf/ServiceImpl.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.remote.perf; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.File; | |
5 import java.io.ObjectOutputStream; | |
6 import java.io.FileOutputStream; | |
7 | |
8 import java.rmi.RemoteException; | |
9 import java.rmi.MarshalledObject; | |
10 import java.rmi.RMISecurityManager; | |
11 | |
12 import java.rmi.activation.ActivationID; | |
13 import java.rmi.activation.ActivationException; | |
14 import java.rmi.activation.ActivationSystem; | |
15 import java.rmi.activation.Activatable; | |
16 | |
17 import java.util.logging.Logger; | |
18 import java.util.logging.Level; | |
19 | |
20 import java.security.PrivilegedExceptionAction; | |
21 import java.security.PrivilegedActionException; | |
22 | |
23 import javax.security.auth.Subject; | |
24 import javax.security.auth.login.LoginContext; | |
25 import javax.security.auth.login.LoginException; | |
26 | |
27 import net.jini.id.Uuid; | |
28 import net.jini.id.UuidFactory; | |
29 | |
30 import net.jini.config.ConfigurationException; | |
31 import net.jini.config.Configuration; | |
32 | |
33 import net.jini.export.ProxyAccessor; | |
34 import net.jini.export.Exporter; | |
35 | |
36 import net.jini.jeri.BasicILFactory; | |
37 import net.jini.jeri.BasicJeriExporter; | |
38 | |
39 import net.jini.jeri.tcp.TcpServerEndpoint; | |
40 | |
41 import net.jini.core.transaction.Transaction; | |
42 import net.jini.core.transaction.UnknownTransactionException; | |
43 import net.jini.core.transaction.TransactionException; | |
44 | |
45 import net.jini.core.lease.Lease; | |
46 import net.jini.core.lease.UnknownLeaseException; | |
47 import net.jini.core.lease.LeaseDeniedException; | |
48 | |
49 import net.jini.core.entry.Entry; | |
50 | |
51 import com.sun.jini.start.LifeCycle; | |
52 | |
53 import org.dancres.blitz.mangler.EntryMangler; | |
54 import org.dancres.blitz.mangler.MangledEntry; | |
55 | |
56 import org.dancres.blitz.lease.SpaceUID; | |
57 | |
58 import org.dancres.blitz.config.ConfigurationFactory; | |
59 | |
60 import org.dancres.blitz.notify.EventGenerator; | |
61 | |
62 import org.dancres.blitz.Logging; | |
63 | |
64 /** | |
65 */ | |
66 public class ServiceImpl implements Server { | |
67 static final String STUB_FILE = "stub.ser"; | |
68 | |
69 static final Logger theLogger = | |
70 Logging.newLogger("org.dancres.blitz.remote.perf", Level.INFO); | |
71 | |
72 private static ServiceImpl theServiceImpl; | |
73 | |
74 private Exporter theExporter; | |
75 | |
76 private Server theStub; | |
77 | |
78 private boolean isStopped = false; | |
79 | |
80 private Uuid theUuid = UuidFactory.generate(); | |
81 | |
82 private long theFakeExpiry = System.currentTimeMillis(); | |
83 | |
84 private MangledEntry theEntry; | |
85 | |
86 public static void main(String anArgs[]) { | |
87 | |
88 try { | |
89 theServiceImpl = new ServiceImpl(anArgs, null); | |
90 } catch (Exception anE) { | |
91 theLogger.log(Level.SEVERE, "Init failed", anE); | |
92 } | |
93 } | |
94 | |
95 /** | |
96 This constructor is required to use | |
97 com.sun.jini.start.NonActivatableServiceDescriptor. | |
98 */ | |
99 public ServiceImpl(String [] anArgs, LifeCycle aLifeCycle) | |
100 throws RemoteException, ConfigurationException { | |
101 | |
102 try { | |
103 init(anArgs); | |
104 } catch (ConfigurationException aCE) { | |
105 throw aCE; | |
106 } catch (RemoteException anRE) { | |
107 throw anRE; | |
108 } | |
109 | |
110 EntryMangler myMangler = new EntryMangler(); | |
111 | |
112 theEntry = myMangler.mangle(new DummyEntry("rhubarb")); | |
113 | |
114 /* | |
115 Keep a static reference to ourselves in order to avoid premature | |
116 GC | |
117 */ | |
118 theServiceImpl = this; | |
119 } | |
120 | |
121 /** | |
122 This constructor is required to use | |
123 com.sun.jini.start.SharedActivatableServiceDescriptor which invokes | |
124 on ActivateWrapper which will call this constructor. | |
125 SharedActivatabkeServiceDescriptor's serverConfigArgs are wrapped | |
126 into a MarshalledObject and pass in with the ActivationID. | |
127 */ | |
128 public ServiceImpl(ActivationID anActivationID, | |
129 MarshalledObject aData) | |
130 throws RemoteException, ConfigurationException { | |
131 | |
132 String[] myArgs = null; | |
133 | |
134 try { | |
135 myArgs = (String[]) aData.get(); | |
136 } catch (Exception anE) { | |
137 theLogger.log(Level.SEVERE, "Failed to unpacked marshalled args", | |
138 anE); | |
139 throw new RemoteException("Failed to unpack marshalled args"); | |
140 } | |
141 | |
142 try { | |
143 init(myArgs); | |
144 } catch (ConfigurationException aCE) { | |
145 throw aCE; | |
146 } catch (RemoteException anRE) { | |
147 throw anRE; | |
148 } | |
149 } | |
150 | |
151 private void init(String[] anArgs) throws ConfigurationException, | |
152 RemoteException { | |
153 | |
154 if (System.getSecurityManager() == null) | |
155 System.setSecurityManager(new RMISecurityManager()); | |
156 | |
157 if ((anArgs != null) && (anArgs.length > 0)) { | |
158 ConfigurationFactory.setup(anArgs); | |
159 } | |
160 | |
161 try { | |
162 doPriv(new PrivilegedInitImpl()); | |
163 } catch (Exception anE) { | |
164 anE.printStackTrace(System.err); | |
165 theLogger.log(Level.SEVERE, "Oops privilege problem?", anE); | |
166 throw new ConfigurationException("loginContext has insufficient privileges", anE); | |
167 } | |
168 } | |
169 | |
170 private Object doPriv(PrivilegedExceptionAction aPAE) | |
171 throws Exception { | |
172 | |
173 return aPAE.run(); | |
174 } | |
175 | |
176 private class PrivilegedInitImpl implements PrivilegedExceptionAction { | |
177 public Object run() throws Exception { | |
178 try { | |
179 initImpl(); | |
180 } catch (Exception anE) { | |
181 theLogger.log(Level.SEVERE, "Could init with subject", anE); | |
182 throw anE; | |
183 } | |
184 return null; | |
185 } | |
186 } | |
187 | |
188 private void initImpl() throws ConfigurationException, RemoteException { | |
189 Exporter myDefaultExporter = | |
190 new BasicJeriExporter(TcpServerEndpoint.getInstance(0), | |
191 new BasicILFactory(), false, true); | |
192 | |
193 ConfigurationFactory.setup(new String[] {"config/nettest.config"}); | |
194 | |
195 theExporter = | |
196 (Exporter) ConfigurationFactory.getEntry("serverExporter", | |
197 Exporter.class, | |
198 myDefaultExporter); | |
199 if (theExporter == null) { | |
200 throw new ConfigurationException("serverExporter must be non-null if it's specified"); | |
201 } | |
202 | |
203 System.err.println("Using exporter: " + theExporter); | |
204 | |
205 theStub = (Server) theExporter.export(this); | |
206 | |
207 Class[] myInterfaces = theStub.getClass().getInterfaces(); | |
208 | |
209 for (int i = 0; i < myInterfaces.length; i++) { | |
210 System.err.println("Stub supports: " + myInterfaces[i].getName()); | |
211 } | |
212 | |
213 try { | |
214 File myFlattenedStub = new File(STUB_FILE); | |
215 | |
216 ObjectOutputStream myOOS = | |
217 new ObjectOutputStream(new FileOutputStream(myFlattenedStub)); | |
218 | |
219 myOOS.writeObject(theStub); | |
220 | |
221 myOOS.close(); | |
222 } catch (IOException anIOE) { | |
223 throw new RemoteException("Oops, didn't save stub", anIOE); | |
224 } | |
225 | |
226 System.err.println("Stub saved in " + STUB_FILE + " - start client with it"); | |
227 } | |
228 | |
229 public Lease write(MangledEntry anEntry, Transaction aTxn, | |
230 long aLeaseTime) | |
231 throws RemoteException, TransactionException { | |
232 | |
233 stopBarrier(); | |
234 | |
235 return | |
236 ProxyFactory.newLeaseImpl(theStub, theUuid, | |
237 new FakeUID(), | |
238 theFakeExpiry); | |
239 } | |
240 | |
241 public MangledEntry read(MangledEntry anEntry, Transaction aTxn, | |
242 long aWaitTime) | |
243 throws RemoteException, TransactionException { | |
244 | |
245 stopBarrier(); | |
246 | |
247 return theEntry; | |
248 } | |
249 | |
250 /** | |
251 Used to check whether calls are blocked and, if they are, throws | |
252 a RemoteException back to the client. | |
253 */ | |
254 private void stopBarrier() throws RemoteException { | |
255 synchronized(this) { | |
256 if (isStopped) | |
257 throw new RemoteException("Space has been stopped"); | |
258 } | |
259 } | |
260 | |
261 public long renew(SpaceUID aUID, long aDuration) | |
262 throws UnknownLeaseException, LeaseDeniedException, RemoteException { | |
263 | |
264 // System.out.println("Renew: " + aUID + ", " + aDuration); | |
265 | |
266 stopBarrier(); | |
267 | |
268 throw new org.dancres.util.NotImplementedException(); | |
269 } | |
270 | |
271 public void cancel(SpaceUID aUID) | |
272 throws UnknownLeaseException, RemoteException { | |
273 | |
274 stopBarrier(); | |
275 | |
276 | |
277 throw new org.dancres.util.NotImplementedException(); | |
278 } | |
279 | |
280 public LeaseResults renew(SpaceUID[] aLeases, long[] aDurations) | |
281 throws RemoteException { | |
282 | |
283 stopBarrier(); | |
284 | |
285 throw new org.dancres.util.NotImplementedException(); | |
286 } | |
287 | |
288 public LeaseResults cancel(SpaceUID[] aLeases) | |
289 throws RemoteException { | |
290 | |
291 stopBarrier(); | |
292 | |
293 throw new org.dancres.util.NotImplementedException(); | |
294 } | |
295 | |
296 private Exception[] initFails(int aLength, int anOffset) { | |
297 return new Exception[aLength]; | |
298 } | |
299 } |