KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > jrmp > server > JUnicastRemoteObject


1 /**
2  * Copyright (C) 2002,2004 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: JUnicastRemoteObject.java,v 1.8 2005/04/19 16:47:04 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.jrmp.server;
29
30 import java.rmi.NoSuchObjectException JavaDoc;
31 import java.rmi.Remote JavaDoc;
32 import java.rmi.RemoteException JavaDoc;
33 import java.rmi.server.RMIClientSocketFactory JavaDoc;
34 import java.rmi.server.RMIServerSocketFactory JavaDoc;
35 import java.rmi.server.RemoteServer JavaDoc;
36 import java.rmi.server.RemoteStub JavaDoc;
37 import java.rmi.server.ServerCloneException JavaDoc;
38
39 import org.objectweb.carol.rmi.jrmp.interceptor.JClientRequestInterceptor;
40 import org.objectweb.carol.rmi.jrmp.interceptor.JServerRequestInterceptor;
41 import org.objectweb.carol.util.configuration.CarolDefaultValues;
42
43 import sun.rmi.transport.ObjectTable;
44
45 /**
46  * Class Extension of <code>UnicastRemoteObject</code> CAROL class ensuring
47  * the JRMP context propagation Unicast Reference ensuring context propagation
48  * with custom sockets
49  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
50  * @version 1.0, 15/07/2002
51  */

52 public class JUnicastRemoteObject extends RemoteServer JavaDoc {
53
54     protected RMIClientSocketFactory JavaDoc csf = null;
55
56     protected RMIServerSocketFactory JavaDoc ssf = null;
57
58     private static JUnicastThreadFactory defaultThreadFactory = null;
59
60     //static boolean for local call optimization
61
private static boolean localO;
62
63     static {
64         localO = new Boolean JavaDoc(System.getProperty(CarolDefaultValues.LOCAL_JRMP_PROPERTY, "false")).booleanValue();
65     }
66
67     protected JUnicastRemoteObject(JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis)
68             throws RemoteException JavaDoc {
69         // search in the jvm properties if the port number properties exist
70
this(0, sis, cis);
71     }
72
73     protected JUnicastRemoteObject(int p, JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis)
74             throws RemoteException JavaDoc {
75         JUnicastRemoteObject.exportObject(this, p, sis, cis);
76     }
77
78     protected JUnicastRemoteObject(int p, RMIClientSocketFactory JavaDoc csf, RMIServerSocketFactory JavaDoc ssf,
79             JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis) throws RemoteException JavaDoc {
80         this.csf = csf;
81         this.ssf = ssf;
82         JUnicastRemoteObject.exportObject(this, p, csf, ssf, sis, cis);
83     }
84
85     // exports methods
86
protected void exportObject(JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis)
87             throws RemoteException JavaDoc {
88         if (csf == null && ssf == null) {
89             JUnicastRemoteObject.exportObject(this, 0, sis, cis);
90         } else {
91             JUnicastRemoteObject.exportObject(this, 0, csf, ssf, sis, cis);
92         }
93     }
94
95     public static RemoteStub JavaDoc exportObject(Remote JavaDoc obj, JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis)
96             throws RemoteException JavaDoc {
97         return (RemoteStub JavaDoc) JUnicastRemoteObject.exportObject(obj, 0, sis, cis);
98     }
99
100     public static Remote JavaDoc exportObject(Remote JavaDoc obj, int p, JServerRequestInterceptor[] sis,
101             JClientRequestInterceptor[] cis) throws RemoteException JavaDoc {
102
103         return JUnicastRemoteObject.exportObjectR(obj, new JUnicastServerRef(p, sis, cis));
104     }
105
106     public static Remote JavaDoc exportObject(Remote JavaDoc obj, int p, RMIClientSocketFactory JavaDoc csf, RMIServerSocketFactory JavaDoc ssf,
107             JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis) throws RemoteException JavaDoc {
108
109         return JUnicastRemoteObject.exportObjectR(obj, new JUnicastServerRefSf(p, csf, ssf, sis, cis));
110     }
111
112     /**
113      * Real export object (localy and remotly)
114      * @param obj
115      * @param serverRef
116      * @param params
117      * @param args
118      * @return @throws RemoteException
119      */

120     protected static Remote JavaDoc exportObjectR(Remote JavaDoc obj, JUnicastServerRef serverRef) throws RemoteException JavaDoc {
121         int localId = -2;
122         if (localO) {
123             localId = JLocalObjectStore.storeObject(obj);
124         }
125         if (obj instanceof JUnicastRemoteObject) ((JUnicastRemoteObject) obj).ref = serverRef;
126         Remote JavaDoc rob = serverRef.exportObject(obj, null, localId);
127         return rob;
128     }
129
130     /**
131      * Real unexport Object (localy and remotly)
132      * @param obj
133      * @param force
134      * @return @throws NoSuchObjectException
135      */

136     public static boolean unexportObject(Remote JavaDoc obj, boolean force) throws NoSuchObjectException JavaDoc {
137         if (localO) {
138             JUnicastRef remoteref = (JUnicastRef) ((RemoteStub JavaDoc) ObjectTable.getStub(obj)).getRef();
139             JLocalObjectStore.removeObject(remoteref.getLocalId());
140         }
141         return ObjectTable.unexportObject(obj, force);
142     }
143
144     private void readObject(java.io.ObjectInputStream JavaDoc in) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
145         in.defaultReadObject();
146         exportObject(null, null);
147     }
148
149     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
150         try {
151             JUnicastRemoteObject cloned = (JUnicastRemoteObject) super.clone();
152             cloned.exportObject(null, null);
153             return cloned;
154         } catch (RemoteException JavaDoc e) {
155             throw new ServerCloneException JavaDoc("Clone failed", e);
156         }
157     }
158
159     /**
160      * set the default thread factory to to used when dispatching the call. No
161      * new thread is created when the factory is <code>null</code>
162      */

163     public static void setDefaultThreadFactory(JUnicastThreadFactory factory) {
164         defaultThreadFactory = factory;
165     }
166
167     /**
168      * get the current default thread factory
169      */

170     public static JUnicastThreadFactory getDefaultThreadFactory() {
171         return defaultThreadFactory;
172     }
173 }
174
175
Popular Tags