org/objectweb/proactive/core/node/NodeImpl.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.node;
00032 
00033 import java.io.IOException;
00034 import java.io.ObjectInputStream;
00035 import java.io.Serializable;
00036 import java.util.ArrayList;
00037 
00038 import org.objectweb.proactive.ActiveObjectCreationException;
00039 import org.objectweb.proactive.core.Constants;
00040 import org.objectweb.proactive.core.ProActiveException;
00041 import org.objectweb.proactive.core.body.UniversalBody;
00042 import org.objectweb.proactive.core.mop.ConstructionOfProxyObjectFailedException;
00043 import org.objectweb.proactive.core.mop.MOP;
00044 import org.objectweb.proactive.core.mop.MOPException;
00045 import org.objectweb.proactive.core.runtime.DeployerTag;
00046 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
00047 import org.objectweb.proactive.core.runtime.RuntimeFactory;
00048 
00049 
00068 public class NodeImpl implements Node, Serializable {
00069     protected NodeInformation nodeInformation;
00070     protected ProActiveRuntime proActiveRuntime;
00071     protected String vnName;
00072     protected ArrayList fileTransferServicePool;
00073 
00074     //
00075     // ----------Constructors--------------------
00076     //
00077     public NodeImpl() {
00078     }
00079 
00080     public NodeImpl(ProActiveRuntime proActiveRuntime, String nodeURL,
00081         String protocol, String jobID) {
00082         this(proActiveRuntime, nodeURL, protocol, jobID, null);
00083     }
00084 
00085     public NodeImpl(ProActiveRuntime proActiveRuntime, String nodeURL,
00086         String protocol, String jobID, String vmName) {
00087         this.proActiveRuntime = proActiveRuntime;
00088         this.nodeInformation = new NodeInformationImpl(nodeURL, protocol,
00089                 jobID, vmName, proActiveRuntime.getVMInformation().getDeployerTag());
00090         this.fileTransferServicePool = new ArrayList();
00091     }
00092 
00093     //
00094     //--------------------------Implements Node-----------------------------
00095 
00099     public NodeInformation getNodeInformation() {
00100         return nodeInformation;
00101     }
00102 
00106     public ProActiveRuntime getProActiveRuntime() {
00107         return proActiveRuntime;
00108     }
00109 
00113     public Object[] getActiveObjects()
00114         throws NodeException, ActiveObjectCreationException {
00115         ArrayList bodyArray;
00116         try {
00117             bodyArray = this.proActiveRuntime.getActiveObjects(this.nodeInformation.getName());
00118         } catch (ProActiveException e) {
00119             throw new NodeException(
00120                 "Cannot get Active Objects registered on this node: " +
00121                 this.nodeInformation.getURL(), e);
00122         }
00123         if (bodyArray.size() == 0) {
00124             return new Object[0];
00125         } else {
00126             Object[] stubOnAO = new Object[bodyArray.size()];
00127             for (int i = 0; i < bodyArray.size(); i++) {
00128                 UniversalBody body = (UniversalBody) ((ArrayList) bodyArray.get(i)).get(0);
00129                 String className = (String) ((ArrayList) bodyArray.get(i)).get(1);
00130                 try {
00131                     stubOnAO[i] = createStubObject(className, body);
00132                 } catch (MOPException e) {
00133                     throw new ActiveObjectCreationException("Exception occured when trying to create stub-proxy",
00134                         e);
00135                 }
00136             }
00137             return stubOnAO;
00138         }
00139     }
00140 
00144     public int getNumberOfActiveObjects() throws NodeException {
00145         ArrayList bodyArray;
00146         try {
00147             bodyArray = this.proActiveRuntime.getActiveObjects(this.nodeInformation.getName());
00148         } catch (ProActiveException e) {
00149             throw new NodeException(
00150                 "Cannot get Active Objects registered on this node: " +
00151                 this.nodeInformation.getURL(), e);
00152         }
00153         return bodyArray.size();
00154     }
00155 
00159     public Object[] getActiveObjects(String className)
00160         throws NodeException, ActiveObjectCreationException {
00161         ArrayList bodyArray;
00162         try {
00163             bodyArray = this.proActiveRuntime.getActiveObjects(this.nodeInformation.getName(),
00164                     className);
00165         } catch (ProActiveException e) {
00166             throw new NodeException("Cannot get Active Objects of type " +
00167                 className + " registered on this node: " +
00168                 this.nodeInformation.getURL(), e);
00169         }
00170         if (bodyArray.size() == 0) {
00171             throw new NodeException("no ActiveObjects of type " + className +
00172                 " are registered for this node: " +
00173                 this.nodeInformation.getURL());
00174         } else {
00175             Object[] stubOnAO = new Object[bodyArray.size()];
00176             for (int i = 0; i < bodyArray.size(); i++) {
00177                 UniversalBody body = (UniversalBody) bodyArray.get(i);
00178                 try {
00179                     stubOnAO[i] = createStubObject(className, body);
00180                 } catch (MOPException e) {
00181                     throw new ActiveObjectCreationException("Exception occured when trying to create stub-proxy",
00182                         e);
00183                 }
00184             }
00185             return stubOnAO;
00186         }
00187     }
00188 
00189     private void readObject(ObjectInputStream in)
00190         throws java.io.IOException, ClassNotFoundException, ProActiveException {
00191         in.defaultReadObject();
00192         if (NodeFactory.isNodeLocal(this)) {
00193             this.proActiveRuntime = RuntimeFactory.getProtocolSpecificRuntime(nodeInformation.getProtocol());
00194         }
00195     }
00196 
00197     // -------------------------------------------------------------------------------------------
00198     // 
00199     // STUB CREATION
00200     // 
00201     // -------------------------------------------------------------------------------------------
00202     private static Object createStubObject(String className, UniversalBody body)
00203         throws MOPException {
00204         return createStubObject(className, null, new Object[] { body });
00205     }
00206 
00207     private static Object createStubObject(String className,
00208         Object[] constructorParameters, Object[] proxyParameters)
00209         throws MOPException {
00210         try {
00211             return MOP.newInstance(className, (Class[])null, constructorParameters,
00212                     Constants.DEFAULT_BODY_PROXY_CLASS_NAME, proxyParameters);
00213 
00214         } catch (ClassNotFoundException e) {
00215             throw new ConstructionOfProxyObjectFailedException(
00216                 "Class can't be found e=" + e);
00217         }
00218     }
00219 
00220     //
00221     //------------------------INNER CLASS---------------------------------------
00222     //
00223     protected class NodeInformationImpl implements NodeInformation {
00224         private String nodeName;
00225         private String nodeURL;
00226         private String protocol;
00227         private String jobID;
00228         private java.net.InetAddress hostInetAddress;
00229         private java.rmi.dgc.VMID hostVMID;
00230         private String hostname;
00231         private String vmName;
00232         private DeployerTag deployerTag;
00233 
00234         public NodeInformationImpl(String url, String protocol, String jobID,
00235             String vmName, DeployerTag deployerTag) {
00236             this.nodeURL = url;
00237             this.hostVMID = proActiveRuntime.getVMInformation().getVMID();
00238             this.hostInetAddress = proActiveRuntime.getVMInformation()
00239                                                    .getInetAddress();
00240             this.hostname = proActiveRuntime.getVMInformation().getHostName();
00241             this.protocol = protocol;
00242             this.nodeName = extractNameFromUrl(url);
00243             this.jobID = jobID;
00244             this.vmName = vmName;
00245             this.deployerTag = deployerTag;
00246         }
00247 
00251         public java.rmi.dgc.VMID getVMID() {
00252             return hostVMID;
00253         }
00254 
00258         public String getName() {
00259             return nodeName;
00260         }
00261 
00265         public String getProtocol() {
00266             return protocol;
00267         }
00268 
00272         public String getURL() {
00273             return nodeURL;
00274         }
00275 
00279         public java.net.InetAddress getInetAddress() {
00280             return hostInetAddress;
00281         }
00282 
00283         public String getCreationProtocolID() {
00284             return proActiveRuntime.getVMInformation().getCreationProtocolID();
00285         }
00286 
00291         public void setCreationProtocolID(String protocolId) {
00292             //Do nothing since we do not want to be able to set vm infos from Node
00293         }
00294 
00300         private String extractNameFromUrl(String url) {
00301             int n = url.lastIndexOf("/");
00302             String name = url.substring(n + 1);
00303             return name;
00304         }
00305 
00309         public String getJobID() {
00310             return jobID;
00311         }
00312 
00318         public void setJobID(String jobId) {
00319             this.jobID = jobId;
00320         }
00321 
00325         public String getHostName() {
00326             return this.hostname;
00327         }
00328 
00332         public String getDescriptorVMName() {
00333             return vmName;
00334         }
00335 
00339         public DeployerTag getDeployerTag() {
00340             return deployerTag;
00341         }
00342     }
00343 
00344     // SECURITY
00345 
00351     public void killAllActiveObjects() throws NodeException, IOException {
00352         ArrayList bodyArray;
00353         try {
00354             bodyArray = this.proActiveRuntime.getActiveObjects(this.nodeInformation.getName());
00355         } catch (ProActiveException e) {
00356             throw new NodeException(
00357                 "Cannot get Active Objects registered on this node: " +
00358                 this.nodeInformation.getURL(), e);
00359         }
00360         for (int i = 0; i < bodyArray.size(); i++) {
00361             UniversalBody body = (UniversalBody) ((ArrayList) bodyArray.get(i)).get(0);
00362             body.terminate();
00363         }
00364     }
00365 
00369     public Object setProperty(String key, String value)
00370         throws ProActiveException {
00371         return this.proActiveRuntime.setLocalNodeProperty(this.nodeInformation.getName(),
00372             key, value);
00373     }
00374 
00378     public String getProperty(String key) throws ProActiveException {
00379         return this.proActiveRuntime.getLocalNodeProperty(this.nodeInformation.getName(),
00380             key);
00381     }
00382 
00401 }

Generated on Mon Jan 22 15:16:08 2007 for ProActive by  doxygen 1.5.1