00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.node;
00032 
00033 import java.net.UnknownHostException;
00034 import java.rmi.AlreadyBoundException;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.ProActive;
00038 import org.objectweb.proactive.core.ProActiveException;
00039 import org.objectweb.proactive.core.UniqueID;
00040 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00041 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
00042 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
00043 import org.objectweb.proactive.core.runtime.RuntimeFactory;
00044 import org.objectweb.proactive.core.util.UrlBuilder;
00045 import org.objectweb.proactive.core.util.log.Loggers;
00046 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00047 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
00048 
00049 
00077 public class NodeFactory {
00078     protected static Logger logger = ProActiveLogger.getLogger(Loggers.DEPLOYMENT);
00079     private static final String DEFAULT_NODE_NAME;
00080     private static Node defaultNode = null;
00081 
00082     static {
00083         ProActiveConfiguration.load();
00084 
00085         String protocol = UrlBuilder.checkProtocol(System.getProperty(
00086                     "proactive.communication.protocol"));
00087         String port = System.getProperty("proactive.rmi.port");
00088 
00089         if (port != null) {
00090             DEFAULT_NODE_NAME = UrlBuilder.buildUrl("localhost", "Node",
00091                     protocol, new Integer(port).intValue());
00092         } else {
00093             DEFAULT_NODE_NAME = UrlBuilder.buildUrl("localhost", "Node",
00094                     protocol);
00095         }
00096     }
00097 
00098     
00099     
00100     
00101     
00102     
00103     public static synchronized Node getDefaultNode() throws NodeException {
00104         String nodeURL = null;
00105 
00106         ProActiveRuntime defaultRuntime = null;
00107         String jobID = ProActive.getJobId();
00108         ProActiveSecurityManager securityManager = null;
00109 
00110         if (defaultNode == null) {
00111             try {
00112                 defaultRuntime = RuntimeFactory.getDefaultRuntime();
00113                 nodeURL = defaultRuntime.createLocalNode(DEFAULT_NODE_NAME +
00114                         Integer.toString(ProActiveRuntimeImpl.getNextInt()),
00115                         false, securityManager, "DefaultVN", jobID);
00116             } catch (ProActiveException e) {
00117                 throw new NodeException("Cannot create the default Node", e);
00118             } catch (AlreadyBoundException e) { 
00119                 getDefaultNode();
00120             }
00121 
00122             defaultNode = new NodeImpl(defaultRuntime, nodeURL,
00123                     UrlBuilder.checkProtocol(System.getProperty(
00124                             "proactive.communication.protocol")), jobID );
00125         }
00126 
00127         return defaultNode;
00128     }
00129 
00134     public static boolean isNodeLocal(Node node) {
00135         return node.getNodeInformation().getVMID()
00136                    .equals(UniqueID.getCurrentVMID());
00137     }
00138 
00153     public static Node createNode(String nodeURL)
00154         throws NodeException, AlreadyBoundException {
00155         return createNode(nodeURL, false, null, null);
00156     }
00157 
00173     public static Node createNode(String url, boolean replacePreviousBinding,
00174         ProActiveSecurityManager psm, String vnname)
00175         throws NodeException, AlreadyBoundException {
00176         ProActiveRuntime proActiveRuntime;
00177         String nodeURL;
00178         String jobID = ProActive.getJobId();
00179 
00180         if (logger.isDebugEnabled()) {
00181             logger.debug("NodeFactory: createNode(" + url + ")");
00182         }
00183 
00184         
00185         String protocol = UrlBuilder.getProtocol(url);
00186 
00187         
00188         
00189         try {
00190             proActiveRuntime = RuntimeFactory.getProtocolSpecificRuntime(protocol);
00191             nodeURL = proActiveRuntime.createLocalNode(url,
00192                     replacePreviousBinding, psm, vnname, jobID);
00193         } catch (ProActiveException e) {
00194             throw new NodeException("Cannot create a Node based on " + url, e);
00195         }
00196 
00197         Node node = new NodeImpl(proActiveRuntime, nodeURL, protocol, jobID);
00198 
00199         return node;
00200     }
00201 
00209     public static Node getNode(String nodeURL) throws NodeException {
00210         ProActiveRuntime proActiveRuntime;
00211         String url;
00212         String jobID;
00213 
00214         if (logger.isDebugEnabled()) {
00215             logger.debug("NodeFactory: getNode() for " + nodeURL);
00216         }
00217 
00218         
00219         String protocol = UrlBuilder.getProtocol(nodeURL);
00220 
00221         
00222         try {
00223             url = UrlBuilder.checkUrl(nodeURL);
00224             proActiveRuntime = RuntimeFactory.getRuntime(url, protocol);
00225             jobID = proActiveRuntime.getJobID(url);
00226         } catch (ProActiveException e) {
00227             throw new NodeException("Cannot get the node based on " + nodeURL, e);
00228         } catch (UnknownHostException e) {
00229             throw new NodeException("Cannot get the node based on " + nodeURL, e);
00230         }
00231 
00232         Node node = new NodeImpl(proActiveRuntime, url, protocol, jobID);
00233 
00234         return node;
00235     }
00236 
00242     public static void killNode(String nodeURL) throws NodeException {
00243         ProActiveRuntime proActiveRuntime;
00244         String url;
00245 
00246         String protocol = UrlBuilder.getProtocol(nodeURL);
00247 
00248         try {
00249             url = UrlBuilder.checkUrl(nodeURL);
00250             proActiveRuntime = RuntimeFactory.getRuntime(url, protocol);
00251             proActiveRuntime.killNode(url);
00252         } catch (ProActiveException e) {
00253             throw new NodeException("Cannot get the node based on " + nodeURL, e);
00254         } catch (UnknownHostException e) {
00255             throw new NodeException("Cannot get the node based on " + nodeURL, e);
00256         }
00257     }
00258 }