KickJava   Java API By Example, From Geeks To Geeks.

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


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: MultiPRODelegate.java,v 1.14 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.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38
39 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
40
41 import org.objectweb.carol.util.configuration.ConfigurationRepository;
42 import org.objectweb.carol.util.configuration.ProtocolConfiguration;
43 import org.objectweb.carol.util.configuration.TraceCarol;
44
45 /**
46  * Class <code> MultiPRODelegate </code><br>
47  * This is a proxy for multi orb portable remote object delegate reference this
48  * class with the systeme property : java
49  * -Djavax.rmi.CORBA.PortableRemoteObjectClass=org.objectweb.carol.rmi.multi.MultiPRODelegate
50  * ... for the moment this class is only for one orb
51  * @author Guillaume Riviere
52  * @author Florent Benoit (Refactoring : this class has no static methods,
53  * remove all static methods and init is done only once in constructor)
54  */

55 public class MultiPRODelegate implements PortableRemoteObjectDelegate JavaDoc {
56
57     /**
58      * exported HashTable
59      */

60     private Hashtable JavaDoc exported = new Hashtable JavaDoc();
61
62     /**
63      * Prodelegate object available
64      */

65     private List JavaDoc proDelegates = null;
66
67     /**
68      * constructor for this PortableRemoteObjectDelegateProxy
69      */

70     public MultiPRODelegate() {
71
72         // Build a PortableRemoteObjectDelegate object for each protocol configuration
73
ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations();
74         proDelegates = new ArrayList JavaDoc();
75         for (int i = 0; i < protocolConfigurations.length; i++) {
76             PortableRemoteObjectDelegate JavaDoc proDelegate = protocolConfigurations[i].getProtocol().getPortableRemoteObject();
77             proDelegates.add(proDelegate);
78         }
79     }
80
81     /**
82      * Makes a server object ready to receive remote calls. Note that subclasses
83      * of PortableRemoteObject do not need to call this method, as it is called
84      * by the constructor.
85      * @param obj the server object to export.
86      * @exception RemoteException if export fails.
87      */

88     public void exportObject(Remote JavaDoc obj) throws RemoteException JavaDoc {
89         for (Iterator JavaDoc it = proDelegates.iterator(); it.hasNext();) {
90             ((PortableRemoteObjectDelegate JavaDoc) it.next()).exportObject(obj);
91         }
92         if (TraceCarol.isDebugExportCarol()) {
93             TraceCarol.debugExportCarol("Export object " + obj.getClass().getName());
94             addObject(obj.getClass().getName());
95         }
96     }
97
98     /**
99      * Deregisters a server object from the runtime, allowing the object to
100      * become available for garbage collection.
101      * @param obj the object to unexport.
102      * @exception NoSuchObjectException if the remote object is not currently
103      * exported.
104      */

105     public void unexportObject(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
106         for (Iterator JavaDoc it = proDelegates.iterator(); it.hasNext();) {
107             ((PortableRemoteObjectDelegate JavaDoc) it.next()).unexportObject(obj);
108         }
109         if (TraceCarol.isDebugExportCarol()) {
110             TraceCarol.debugExportCarol("Unexport object " + obj.getClass().getName());
111             TraceCarol.debugExportCarol("UnExported objects list:\n" + getExportedObjects());
112             removeObject(obj.getClass().getName());
113         }
114     }
115
116     /**
117      * Makes a Remote object ready for remote communication. This normally
118      * happens implicitly when the object is sent or received as an argument on
119      * a remote method call, but in some circumstances it is useful to perform
120      * this action by making an explicit call.
121      * @param target the object to connect.
122      * @param source a previously connected object.
123      * @throws RemoteException if <code>source</code> is not connected or if
124      * <code>target</code> is already connected to a different ORB
125      * than <code>source</code>.
126      */

127     public void connect(Remote JavaDoc target, Remote JavaDoc source) throws RemoteException JavaDoc {
128         for (Iterator JavaDoc it = proDelegates.iterator(); it.hasNext();) {
129             ((PortableRemoteObjectDelegate JavaDoc) it.next()).connect(target, source);
130         }
131     }
132
133     /**
134      * Checks to ensure that an object of a remote or abstract interface type
135      * can be cast to a desired type.
136      * @param narrowFrom the object to check.
137      * @param narrowTo the desired type.
138      * @return an object which can be cast to the desired type.
139      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
140      */

141     public Object JavaDoc narrow(Object JavaDoc narrowFrom, Class JavaDoc narrowTo) throws ClassCastException JavaDoc {
142         return ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject().narrow(narrowFrom, narrowTo);
143     }
144
145     /**
146      * Returns a stub for the given server object.
147      * @param obj the server object for which a stub is required. Must either be
148      * a subclass of PortableRemoteObject or have been previously the
149      * target of a call to {@link #exportObject}.
150      * @return the most derived stub for the object.
151      * @exception NoSuchObjectException if a stub cannot be located for the
152      * given server object.
153      */

154     public Remote JavaDoc toStub(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
155         return ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject().toStub(obj);
156     }
157
158     /**
159      * Used only in debug mode
160      * @return list of unexported objects
161      */

162     private String JavaDoc getExportedObjects() {
163         String JavaDoc result = "Exported Objects:\n";
164         int resultInt = 0;
165         for (Enumeration JavaDoc e = exported.keys(); e.hasMoreElements();) {
166             String JavaDoc ck = (String JavaDoc) e.nextElement();
167             int on = ((Integer JavaDoc) exported.get(ck)).intValue();
168             result += "" + on + " instances of " + ck + "\n";
169             resultInt += on;
170         }
171         result += "Total number of exported objects=" + resultInt;
172         return result;
173     }
174
175     /**
176      * This method is used only in debug mode Removes an exported object
177      * @param className of exported object
178      */

179     private void removeObject(String JavaDoc className) {
180         if (exported.containsKey(className)) {
181             if (((Integer JavaDoc) exported.get(className)).intValue() != 1) {
182                 exported.put(className, new Integer JavaDoc(((Integer JavaDoc) exported.get(className)).intValue() - 1));
183             } else {
184                 exported.remove(className);
185             }
186         }
187     }
188
189     /**
190      * This method is used only in debug mode Add exported object to a the list
191      * @param className of exported object
192      */

193     private void addObject(String JavaDoc className) {
194         if (exported.containsKey(className)) {
195             exported.put(className, new Integer JavaDoc(((Integer JavaDoc) exported.get(className)).intValue() + 1));
196         } else {
197             exported.put(className, new Integer JavaDoc(1));
198         }
199     }
200
201 }
Popular Tags