org/objectweb/proactive/core/node/NodeFactory.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.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     // test with class loader
00099     //private static final ClassLoader myClassLoader = new NodeClassLoader();
00100     //
00101     // -- PUBLIC METHODS - STATIC -----------------------------------------------
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) { //if this exception is risen, we generate an othe random name for the node
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         //first look for the prototcol
00185         String protocol = UrlBuilder.getProtocol(url);
00186 
00187         //NodeFactory factory = getFactory(protocol);
00188         //then create a node
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         //do we have any association for this node?
00219         String protocol = UrlBuilder.getProtocol(nodeURL);
00220 
00221         //String noProtocolUrl = UrlBuilder.removeProtocol(nodeURL, protocol);
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 }

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