KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > node > NodeImpl


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.node;
32
33 import org.objectweb.proactive.ActiveObjectCreationException;
34 import org.objectweb.proactive.core.Constants;
35 import org.objectweb.proactive.core.ProActiveException;
36 import org.objectweb.proactive.core.body.UniversalBody;
37 import org.objectweb.proactive.core.mop.ConstructionOfProxyObjectFailedException;
38 import org.objectweb.proactive.core.mop.MOP;
39 import org.objectweb.proactive.core.mop.MOPException;
40 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
41 import org.objectweb.proactive.core.runtime.RuntimeFactory;
42
43 import java.io.ObjectInputStream JavaDoc;
44 import java.io.Serializable JavaDoc;
45
46 import java.util.ArrayList JavaDoc;
47
48
49 /**
50  * <p>
51  * A <code>Node</code> offers a set of services needed by ProActive to work with
52  * remote JVM. Each JVM that is aimed to hold active objects should contains at least
53  * one instance of the node class. That instance, when created, will be registered
54  * to some registry where it is possible to perform a lookup (such as the RMI registry).
55  * </p><p>
56  * When ProActive needs to interact with a remote JVM, it will lookup for one node associated
57  * with that JVM (using typically the RMI Registry) and use this node to perform the interaction.
58  * </p><p>
59  * We expect several concrete implementations of the Node to be wrtten such as a RMI node, a JINI node ...
60  * </p>
61  *
62  * @author ProActive Team
63  * @version 1.1, 2002/08/28
64  * @since ProActive 0.9
65  *
66  */

67 public class NodeImpl implements Node, Serializable JavaDoc {
68     protected NodeInformation nodeInformation;
69     protected ProActiveRuntime proActiveRuntime;
70
71     protected String JavaDoc vnName;
72     //
73
// ----------Constructors--------------------
74
//
75
public NodeImpl() {
76     }
77
78     public NodeImpl(ProActiveRuntime proActiveRuntime, String JavaDoc nodeURL,
79         String JavaDoc protocol, String JavaDoc jobID) {
80         this.proActiveRuntime = proActiveRuntime;
81         this.nodeInformation = new NodeInformationImpl(nodeURL, protocol, jobID);
82     }
83
84     //
85
//--------------------------Implements Node-----------------------------
86

87     /**
88      * @see org.objectweb.proactive.core.node.Node#getNodeInformation()
89      */

90     public NodeInformation getNodeInformation() {
91         return nodeInformation;
92     }
93
94     /**
95      * @see org.objectweb.proactive.core.node.Node#getProActiveRuntime
96      */

97     public ProActiveRuntime getProActiveRuntime() {
98         return proActiveRuntime;
99     }
100
101     /**
102      * @see org.objectweb.proactive.core.node.Node#getActiveObjects()
103      */

104     public Object JavaDoc[] getActiveObjects()
105         throws NodeException, ActiveObjectCreationException {
106         ArrayList JavaDoc bodyArray;
107         try {
108             bodyArray = this.proActiveRuntime.getActiveObjects(this.nodeInformation.getName());
109         } catch (ProActiveException e) {
110             throw new NodeException(
111                 "Cannot get Active Objects registered on this node: " +
112                 this.nodeInformation.getURL(), e);
113         }
114         if (bodyArray.size() == 0) {
115             throw new NodeException(
116                 "no ActiveObjects are registered for this node: " +
117                 this.nodeInformation.getURL());
118         } else {
119             Object JavaDoc[] stubOnAO = new Object JavaDoc[bodyArray.size()];
120             for (int i = 0; i < bodyArray.size(); i++) {
121                 UniversalBody body = (UniversalBody) ((ArrayList JavaDoc) bodyArray.get(i)).get(0);
122                 String JavaDoc className = (String JavaDoc) ((ArrayList JavaDoc) bodyArray.get(i)).get(1);
123                 try {
124                     stubOnAO[i] = createStubObject(className, body);
125                 } catch (MOPException e) {
126                     throw new ActiveObjectCreationException("Exception occured when trying to create stub-proxy",
127                         e);
128                 }
129             }
130             return stubOnAO;
131         }
132     }
133
134     /**
135      * @see org.objectweb.proactive.core.node.Node#getActiveObjects(String)
136      */

137     public Object JavaDoc[] getActiveObjects(String JavaDoc className)
138         throws NodeException, ActiveObjectCreationException {
139         ArrayList JavaDoc bodyArray;
140         try {
141             bodyArray = this.proActiveRuntime.getActiveObjects(this.nodeInformation.getName(),
142                     className);
143         } catch (ProActiveException e) {
144             throw new NodeException("Cannot get Active Objects of type " +
145                 className + " registered on this node: " +
146                 this.nodeInformation.getURL(), e);
147         }
148         if (bodyArray.size() == 0) {
149             throw new NodeException("no ActiveObjects of type " + className +
150                 " are registered for this node: " +
151                 this.nodeInformation.getURL());
152         } else {
153             Object JavaDoc[] stubOnAO = new Object JavaDoc[bodyArray.size()];
154             for (int i = 0; i < bodyArray.size(); i++) {
155                 UniversalBody body = (UniversalBody) bodyArray.get(i);
156                 try {
157                     stubOnAO[i] = createStubObject(className, body);
158                 } catch (MOPException e) {
159                     throw new ActiveObjectCreationException("Exception occured when trying to create stub-proxy",
160                         e);
161                 }
162             }
163             return stubOnAO;
164         }
165     }
166
167     private void readObject(ObjectInputStream JavaDoc in)
168         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc, ProActiveException {
169         in.defaultReadObject();
170         if (NodeFactory.isNodeLocal(this)) {
171             this.proActiveRuntime = RuntimeFactory.getProtocolSpecificRuntime(nodeInformation.getProtocol());
172         }
173     }
174
175     // -------------------------------------------------------------------------------------------
176
//
177
// STUB CREATION
178
//
179
// -------------------------------------------------------------------------------------------
180
private static Object JavaDoc createStubObject(String JavaDoc className, UniversalBody body)
181         throws MOPException {
182         return createStubObject(className, null, new Object JavaDoc[] { body });
183     }
184
185     private static Object JavaDoc createStubObject(String JavaDoc className,
186         Object JavaDoc[] constructorParameters, Object JavaDoc[] proxyParameters)
187         throws MOPException {
188         try {
189             return MOP.newInstance(className, constructorParameters,
190                 Constants.DEFAULT_BODY_PROXY_CLASS_NAME, proxyParameters);
191         } catch (ClassNotFoundException JavaDoc e) {
192             throw new ConstructionOfProxyObjectFailedException(
193                 "Class can't be found e=" + e);
194         }
195     }
196
197     //
198
//------------------------INNER CLASS---------------------------------------
199
//
200
protected class NodeInformationImpl implements NodeInformation {
201         private String JavaDoc nodeName;
202         private String JavaDoc nodeURL;
203         private String JavaDoc protocol;
204         private String JavaDoc jobID;
205         private java.net.InetAddress JavaDoc hostInetAddress;
206         private java.rmi.dgc.VMID JavaDoc hostVMID;
207
208         public NodeInformationImpl(String JavaDoc url, String JavaDoc protocol, String JavaDoc jobID) {
209             this.nodeURL = url;
210             this.hostVMID = proActiveRuntime.getVMInformation().getVMID();
211             this.hostInetAddress = proActiveRuntime.getVMInformation()
212                                                    .getInetAddress();
213             this.protocol = protocol;
214             this.nodeName = extractNameFromUrl(url);
215             this.jobID = jobID;
216         }
217
218         /**
219          * @see org.objectweb.proactive.core.runtime.VMInformation#getVMID
220          */

221         public java.rmi.dgc.VMID JavaDoc getVMID() {
222             return hostVMID;
223         }
224
225         /**
226          * @see org.objectweb.proactive.core.node.NodeInformation#getName()
227          */

228         public String JavaDoc getName() {
229             return nodeName;
230         }
231
232         /**
233          * @see org.objectweb.proactive.core.node.NodeInformation#getProtocol()
234          */

235         public String JavaDoc getProtocol() {
236             return protocol;
237         }
238
239         /**
240          * @see org.objectweb.proactive.core.node.NodeInformation#getURL()
241          */

242         public String JavaDoc getURL() {
243             return nodeURL;
244         }
245
246         /**
247          * @see org.objectweb.proactive.core.runtime.VMInformation#getInetAddress()
248          */

249         public java.net.InetAddress JavaDoc getInetAddress() {
250             return hostInetAddress;
251         }
252
253         public String JavaDoc getCreationProtocolID() {
254             return proActiveRuntime.getVMInformation().getCreationProtocolID();
255         }
256
257         /**
258          * @see org.objectweb.proactive.core.runtime.VMInformation#setCreationProtocolID(java.lang.String)
259          * This method has no effect.
260          */

261         public void setCreationProtocolID(String JavaDoc protocolId) {
262             //Do nothing since we do not want to be able to set vm infos from Node
263
}
264
265         /**
266          * Returns the name specified in the url
267          * @param url. The url of the node
268          * @return String. The name of the node
269          */

270         private String JavaDoc extractNameFromUrl(String JavaDoc url) {
271             int n = url.lastIndexOf("/");
272             String JavaDoc name = url.substring(n + 1);
273             return name;
274         }
275
276         /**
277          * @see org.objectweb.proactive.Job#getJobID()
278          */

279         public String JavaDoc getJobID() {
280             return jobID;
281         }
282     }
283     
284     // SECURITY
285
/**
286       * @return virtual node name
287       */

288      public String JavaDoc getVnName() {
289          return vnName;
290      }
291
292      /**
293       * @param string
294       */

295      public void setVnName(String JavaDoc string) {
296          vnName = string;
297      }
298 }
299
Popular Tags