KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > StubContainerBase


1 package org.sapia.ubik.rmi.server;
2
3 import java.lang.reflect.Proxy JavaDoc;
4 import java.rmi.RemoteException JavaDoc;
5
6 import org.sapia.ubik.rmi.Consts;
7
8
9 /**
10  * Base implementation of the <code>StubContainer</code> interface.
11  *
12  * @author Yanick Duchesne
13  *
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class StubContainerBase implements StubContainer, HealthCheck {
21   private StubInvocationHandler _ref;
22   private String JavaDoc[] _interfaceNames;
23   private static final boolean CODE_DOWNLOAD =
24     System.getProperty(Consts.ALLOW_CODE_DOWNLOAD) != null &&
25     System.getProperty(Consts.ALLOW_CODE_DOWNLOAD).equals("true");
26
27   protected StubContainerBase(String JavaDoc[] interfaceNames,
28     StubInvocationHandler handler) {
29     _ref = handler;
30     _interfaceNames = interfaceNames;
31   }
32
33   /**
34    * Returns <code>true</code> if the connection to the remote server of the stub
35    * that this instance wraps is still valid.
36    *
37    * @throws RemoteException if a problem occurs performing the check; if such an
38    * error occurs, this instance should be treated as invalid.
39    */

40   public boolean isValid() throws RemoteException JavaDoc {
41     if (_ref instanceof HealthCheck) {
42       return ((HealthCheck) _ref).isValid();
43     }
44
45     return true;
46   }
47
48   /**
49    * @see StubContainer#toStub(ClassLoader)
50    */

51   public Object JavaDoc toStub(ClassLoader JavaDoc loader) throws RemoteException JavaDoc {
52     Class JavaDoc[] interfaces = new Class JavaDoc[_interfaceNames.length];
53
54     if(_ref.getOID().getCodebase() != null && CODE_DOWNLOAD){
55       loader = new RmiClassLoader(loader, _ref.getOID().getCodebase());
56     }
57     for (int i = 0; i < _interfaceNames.length; i++) {
58       try {
59         interfaces[i] = loader.loadClass(_interfaceNames[i]);
60       } catch (ClassNotFoundException JavaDoc e) {
61         throw new RemoteException JavaDoc("Could not find interface definition: " +
62           _interfaceNames[i], e);
63       }
64     }
65
66     return Proxy.newProxyInstance(loader, interfaces, _ref);
67   }
68
69   /**
70    * @see StubContainer#getStubInvocationHandler()
71    */

72   public StubInvocationHandler getStubInvocationHandler() {
73     return _ref;
74   }
75
76   public String JavaDoc toString() {
77     return _ref.toString();
78   }
79 }
80
Popular Tags