/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
 * Contact: proactive-support@inria.fr
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://www.inria.fr/oasis/ProActive/contacts.html
 *  Contributor(s):
 *
 * ################################################################
 */
package org.objectweb.proactive.core.runtime;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import org.objectweb.proactive.core.ProActiveException;
import org.objectweb.proactive.core.config.ProActiveConfiguration;
import org.objectweb.proactive.core.util.UrlBuilder;

import java.io.File;
import java.io.IOException;

import java.net.URL;


/**
 * <i><font size="-1" color="#FF0000">**For internal use only** </font></i><br>
 * <p>
 * This class is a utility class allowing to start a ProActiveRuntime with a JVM.
 * </p><p>
 * This class is mainly used with ProActiveDescriptor to start a ProActiveRuntime
 * on a local or remote JVM.
 * </p>
 *
 * @author  ProActive Team
 * @version 1.0,  2002/08/29
 * @since   ProActive 0.9
 *
 */
public class StartRuntime {
    //Name of the runtime that launched this class reading the ProActiveDescriptor
    //private static final String DefaultRuntimeName = "PART_DEFAULT";
    //Name of the runtime's host that launched this class reading the ProActiveDescriptor
    static Logger logger = Logger.getLogger(StartRuntime.class.getName());
    protected String DefaultRuntimeURL;
    protected String nodeURL;
    protected String creatorID;
    protected ProActiveRuntime proActiveRuntime;

    //protected String acquisitionMethod;
    protected String nodeNumber;
    protected String vmName;
    protected int nodenumber; //it is only the int value of nodeNumber
    protected String protocolId;

    protected StartRuntime() {
    }

    private StartRuntime(String[] args) {
        if (args.length != 0) {
            this.nodeURL = args[0];
            this.creatorID = args[0].trim();
            //System.out.println(creatorID);
            this.DefaultRuntimeURL = args[1];
            //this.acquisitionMethod = args[2];
            this.nodeNumber = args[2];
            //   this.portNumber = Integer.parseInt(args[4]);
            this.nodenumber = (new Integer(nodeNumber)).intValue();
            this.protocolId = args[3];
            this.vmName = args[4];
        }
    }

    public static void main(String[] args) {
        
        if ("true".equals(System.getProperty("log4j.defaultInitOverride")) && System.getProperty("log4j.configuration") != null) {
            // configure log4j here to avoid classloading problems with log4j classes
            try {
                String log4jConfiguration = System.getProperty("log4j.configuration");
                File f = new File(log4jConfiguration);
                PropertyConfigurator.configure(new URL(f.getPath()));
            } catch (IOException e) {
                System.out.println(
                    "Error : incorrect path for log4j configuration : " +
                    System.getProperty("log4j.configuration"));
            }
        }

        ProActiveConfiguration.load();

        try {
            logger.info("**** Starting jvm on " +
                UrlBuilder.getHostNameorIP(java.net.InetAddress.getLocalHost()));
            if (logger.isDebugEnabled()) {
                logger.debug("**** Starting jvm with classpath " +
                    System.getProperty("java.class.path"));
                logger.debug("****              with bootclasspath " +
                    System.getProperty("sun.boot.class.path"));
            }
        } catch (java.net.UnknownHostException e) {
            e.printStackTrace();
        }
        new StartRuntime(args).run();
    }

    /**
     * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
     * Runs the complete creation and registration of a ProActiveRuntime and creates a
     * node once the creation is completed.
     */
    private void run() {
        try {
            proActiveRuntime = RuntimeFactory.getProtocolSpecificRuntime(System.getProperty(
                        //proActiveRuntime = RuntimeFactory.getProtocolSpecificRuntime(acquisitionMethod);
                "proactive.communication.protocol") + ":");

            String comProtocol = System.getProperty(
                    "proactive.communication.protocol");
            if (comProtocol == "http") {
                ;
            }
            comProtocol = "";

            //            logger.info("Runtime started at " + comProtocol+ ":" +
            //                proActiveRuntime.getURL());
            proActiveRuntime.getVMInformation().setCreationProtocolID(protocolId);

            /*
               for (int i = 1; i <= nodenumber; i++) {
                   proActiveRuntime.createLocalNode(nodeURL +
                       Integer.toString(
                           new java.util.Random(System.currentTimeMillis()).nextInt()),
                       false);
                   //Thread.sleep(2000); for windows
               }
               //System.out.println("creation OK");
               //System.out.println(DefaultRuntimeURL);
             */
            if (DefaultRuntimeURL != null) {
                register(DefaultRuntimeURL);
            }
        } catch (ProActiveException e) {
            e.printStackTrace();
        }
    }

    /**
     * <i><font size="-1" color="#FF0000">**For internal use only** </font></i>
     * Performs the registration of a ProActiveRuntime on the runtime that initiated the creation
     * of ProActiveDescriptor.
     */
    private void register(String hostName) {
        try {
            ProActiveRuntime PART = RuntimeFactory.getRuntime(DefaultRuntimeURL,
                    UrlBuilder.getProtocol(DefaultRuntimeURL));
            PART.register(proActiveRuntime, proActiveRuntime.getURL(),
                creatorID,
                System.getProperty("proactive.communication.protocol") + ":",
                vmName);
            proActiveRuntime.setParent(DefaultRuntimeURL);
        } catch (ProActiveException e) {
            e.printStackTrace();
        }
    }
}

@@ -5244,221 +5244,8 @@
 );%0A%0A
-            String comProtocol = System.getProperty(%0A                    %22proactive.communication.protocol%22);%0A            if (comProtocol == %22http%22) %7B%0A                ;%0A            %7D%0A            comProtocol = %22%22;%0A
 %0A   
@@ -5476,18 +5476,16 @@
         
-/*
 %0A       
@@ -5489,475 +5489,176 @@
     
-       for (int i = 1; i %3C= nodenumber; i++) %7B%0A                   proActiveRuntime.createLocalNode(nodeURL +%0A                       Integer.toString(%0A                           new java.util.Random(System.currentTimeMillis()).nextInt()),%0A                       false);%0A                   //Thread.sleep(2000);
+%7D catch (ProActiveException e) %7B%0A            e.printStackTrace();%0A            //we can still try to register if
  for 
-w
 in
-dows%0A               %7D%0A               //System.out.println(%22creation OK%22);%0A               //System.out.println(DefaultRuntimeURL);%0A             */%0A    
+stance an Alreadybound exception occurs%0A        %7D%0A
     
@@ -5706,20 +5706,16 @@
         
-    
 register
@@ -5739,96 +5739,8 @@
 L);%0A
-            %7D%0A        %7D catch (ProActiveException e) %7B%0A            e.printStackTrace();%0A
     
@@ -6507,32 +6507,118 @@
 ntStackTrace();%0A
+            // if we cannot register, this jvm is useless%0A            System.exit(0);%0A
         %7D%0A    %7D%0A

handle exception when registering in registry