KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > multi > JrmpPRODelegate


1 /**
2  * Copyright (C) 2002,2005 - 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: JrmpPRODelegate.java,v 1.16 2005/04/07 15:07:08 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.multi;
29
30 import java.rmi.NoSuchObjectException JavaDoc;
31 import java.rmi.Remote JavaDoc;
32 import java.rmi.RemoteException JavaDoc;
33 import java.rmi.server.RemoteObject JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
37
38 import org.objectweb.carol.rmi.jrmp.interceptor.JClientRequestInterceptor;
39 import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorStore;
40 import org.objectweb.carol.rmi.jrmp.interceptor.JServerRequestInterceptor;
41 import org.objectweb.carol.rmi.jrmp.server.JUnicastRemoteObject;
42 import org.objectweb.carol.rmi.util.PortNumber;
43 import org.objectweb.carol.util.configuration.CarolDefaultValues;
44 import org.objectweb.carol.util.configuration.ConfigurationRepository;
45
46 /**
47  * Class <code>JrmpPRODelegate</code> for the mapping between Rmi jrmp
48  * UnicastRemoteObject and PortableRemoteObject
49  */

50 public class JrmpPRODelegate implements PortableRemoteObjectDelegate JavaDoc {
51
52     /**
53      * private port number
54      */

55     private int port;
56
57     /**
58      * private Interceptor for Context propagation
59      */

60     private JServerRequestInterceptor[] sis = null;
61
62     /**
63      * private Interceptor for Context propagation
64      */

65     private JClientRequestInterceptor[] cis = null;
66
67     /**
68      * Constructor
69      * @param usingCmi this prodelegate will be used for CMI protocol
70      */

71     public JrmpPRODelegate(boolean usingCmi) {
72         sis = JInterceptorStore.getLocalServerInterceptors();
73         cis = JInterceptorStore.getLocalClientInterceptors();
74         Properties JavaDoc prop = ConfigurationRepository.getProperties();
75         if (!usingCmi && prop != null) {
76             String JavaDoc propertyName = CarolDefaultValues.SERVER_JRMP_PORT;
77             this.port = PortNumber.strToint(prop.getProperty(propertyName, "0"), propertyName);
78         }
79     }
80
81     /**
82      * By default, this class is not used for Cmi. Cmi has to instantiated the other constructor
83      */

84     public JrmpPRODelegate() {
85         this(false);
86     }
87
88     /**
89      * Makes a server object ready to receive remote calls. Note that subclasses
90      * of PortableRemoteObject do not need to call this method, as it is called
91      * by the constructor.
92      * @param obj the server object to export.
93      * @exception RemoteException if export fails.
94      */

95     public void exportObject(Remote JavaDoc obj) throws RemoteException JavaDoc {
96         JUnicastRemoteObject.exportObject(obj, port, sis, cis);
97     }
98
99     /**
100      * Deregisters a server object from the runtime, allowing the object to
101      * become available for garbage collection.
102      * @param obj the object to unexport.
103      * @exception NoSuchObjectException if the remote object is not currently
104      * exported.
105      */

106     public void unexportObject(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
107         JUnicastRemoteObject.unexportObject(obj, true);
108     }
109
110     /**
111      * Makes a Remote object ready for remote communication. This normally
112      * happens implicitly when the object is sent or received as an argument on
113      * a remote method call, but in some circumstances it is useful to perform
114      * this action by making an explicit call. See the {@link Stub#connect}
115      * method for more information.
116      * @param target the object to connect.
117      * @param source a previously connected object.
118      * @throws RemoteException if <code>source</code> is not connected or if
119      * <code>target</code> is already connected to a different ORB
120      * than <code>source</code>.
121      */

122     public void connect(Remote JavaDoc target, Remote JavaDoc source) throws RemoteException JavaDoc {
123         // do nothing
124
}
125
126     /**
127      * Checks to ensure that an object of a remote or abstract interface type
128      * can be cast to a desired type.
129      * @param narrowFrom the object to check.
130      * @param narrowTo the desired type.
131      * @return an object which can be cast to the desired type.
132      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
133      */

134     public Object JavaDoc narrow(Object JavaDoc narrowFrom, Class JavaDoc narrowTo) throws ClassCastException JavaDoc {
135         if (narrowTo.isAssignableFrom(narrowFrom.getClass())) {
136             return narrowFrom;
137         } else {
138             throw new ClassCastException JavaDoc("Cannot cast '" + narrowFrom.getClass().getName() + "' in '"
139                     + narrowTo.getName() + "'.");
140         }
141     }
142
143     /**
144      * Returns a stub for the given server object.
145      * @param obj the server object for which a stub is required. Must either be
146      * a subclass of PortableRemoteObject or have been previously the
147      * target of a call to {@link #exportObject}.
148      * @return the most derived stub for the object.
149      * @exception NoSuchObjectException if a stub cannot be located for the
150      * given server object.
151      */

152     public Remote JavaDoc toStub(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
153         return RemoteObject.toStub(obj);
154     }
155 }
Popular Tags