<commit_before>/*
 * ################################################################
 *
 * 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: <EMAIL>
 *
 * 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.http;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.UnknownHostException;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import org.objectweb.proactive.Body;
import org.objectweb.proactive.core.ProActiveException;
import org.objectweb.proactive.core.body.UniversalBody;
import org.objectweb.proactive.core.body.ft.checkpointing.Checkpoint;
import org.objectweb.proactive.core.body.http.util.exceptions.HTTPRemoteException;
import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
import org.objectweb.proactive.core.descriptor.data.VirtualNode;
import org.objectweb.proactive.core.mop.ConstructorCall;
import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException;
import org.objectweb.proactive.core.node.NodeException;
import org.objectweb.proactive.core.process.UniversalProcess;
import org.objectweb.proactive.core.rmi.ClassServer;
import org.objectweb.proactive.core.rmi.ClassServerServlet;
import org.objectweb.proactive.core.runtime.ProActiveRuntime;
import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
import org.objectweb.proactive.core.runtime.RemoteProActiveRuntime;
import org.objectweb.proactive.core.runtime.VMInformation;
import org.objectweb.proactive.core.runtime.http.messages.RuntimeRequest;
import org.objectweb.proactive.core.util.UrlBuilder;
import org.objectweb.proactive.ext.security.Communication;
import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
import org.objectweb.proactive.ext.security.SecurityContext;
import org.objectweb.proactive.ext.security.crypto.KeyExchangeException;
import org.objectweb.proactive.ext.security.exceptions.RenegotiateSessionException;
import org.objectweb.proactive.ext.security.exceptions.SecurityNotAvailableException;
import org.objectweb.proactive.osgi.OsgiParameters;


/**
 * An HTTP adapter for a ProActiveRuntime to be able to receive remote calls with HTTP. This helps isolate
 * HTTP specific code into a small set of specific classes.
 * @author ProActiveTeam
 * @version 1.0, 9 ao?t 2005
 * @since ProActive 2.2
 * @see <a href="http://www.javaworld.com/javaworld/jw-05-1999/jw-05-networked_p.html">Adapter Pattern</a>
 */
public class HttpProActiveRuntime implements RemoteProActiveRuntime {
    private transient ProActiveRuntime localruntime;
    private boolean isLocal;
    private String url;
    protected VMInformation vmInformation;

    //    private int port;
    //this boolean is used when killing the runtime. Indeed in case of co-allocation, we avoid a second call to the runtime
    // which is already dead
    protected boolean alreadykilled = false;

    public HttpProActiveRuntime() {
        isLocal = true;
        this.localruntime = ProActiveRuntimeImpl.getProActiveRuntime();
        if (OsgiParameters.servletEnabled()) {
            this.url = ClassServerServlet.getUrl();
        } else {
            this.url = ClassServer.getUrl();
        }

        //we also cache the information here to avoid crossing the network when formatting
        //urls
        this.vmInformation = localruntime.getVMInformation();
    }

    /**
     *
     */
    public HttpProActiveRuntime(String newurl) {
        runtimeLogger.debug("Adapter URL = " + newurl);
        try {
            this.url = UrlBuilder.checkUrl(newurl);
            isLocal = false;
            runtimeLogger.debug("New Remote XML Adapter : " + url);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //
    // -- Implements ProActiveRuntime -----------------------------------------------
    //
    public String createLocalNode(String nodeName,
        boolean replacePreviousBinding,
        ProActiveSecurityManager securityManager, String vname, String jobId)
        throws NodeException, HTTPRemoteException {
        String nodeURL = null;
        try {
            nodeURL = buildNodeURL(nodeName);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }

        //      then take the name of the node
        String name = UrlBuilder.getNameFromUrl(nodeURL);

        if (isLocal) {
            localruntime.createLocalNode(name, replacePreviousBinding,
                securityManager, vname, jobId);
        } else {
            ArrayList paramsList = new ArrayList();
            paramsList.add(name);
            paramsList.add(new Boolean(replacePreviousBinding));
            paramsList.add(securityManager);
            paramsList.add(vname);
            paramsList.add(jobId);
            RuntimeRequest req = new RuntimeRequest("createLocalNode",
                    paramsList, this.url);
            req.send();
        }
        return nodeURL;
    }

    public void killAllNodes() throws HTTPRemoteException, ProActiveException {
        if (isLocal) {
            localruntime.killAllNodes();
            return;
        }
        new RuntimeRequest("killAllNodes", this.url).send();
    }

    public void killNode(String nodeName)
        throws ProActiveException, HTTPRemoteException {
        String name = UrlBuilder.getNameFromUrl(nodeName);
        if (isLocal) {
            localruntime.killNode(name);
            return;
        }
        ArrayList params = new ArrayList();
        params.add(name);
        new RuntimeRequest("killNode", params, this.url).send();
    }

    public void createVM(UniversalProcess remoteProcess)
        throws IOException, ProActiveException {
        if (isLocal) {
            localruntime.createVM(remoteProcess);
            return;
        }<commit_msg>log added when a node is created<commit_after>/*
 * ################################################################
 *
 * 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: <EMAIL>
 *
 * 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.http;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.UnknownHostException;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import org.objectweb.proactive.Body;
import org.objectweb.proactive.core.ProActiveException;
import org.objectweb.proactive.core.body.UniversalBody;
import org.objectweb.proactive.core.body.ft.checkpointing.Checkpoint;
import org.objectweb.proactive.core.body.http.util.exceptions.HTTPRemoteException;
import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
import org.objectweb.proactive.core.descriptor.data.VirtualNode;
import org.objectweb.proactive.core.mop.ConstructorCall;
import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException;
import org.objectweb.proactive.core.node.NodeException;
import org.objectweb.proactive.core.process.UniversalProcess;
import org.objectweb.proactive.core.rmi.ClassServer;
import org.objectweb.proactive.core.rmi.ClassServerServlet;
import org.objectweb.proactive.core.runtime.ProActiveRuntime;
import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
import org.objectweb.proactive.core.runtime.RemoteProActiveRuntime;
import org.objectweb.proactive.core.runtime.VMInformation;
import org.objectweb.proactive.core.runtime.http.messages.RuntimeRequest;
import org.objectweb.proactive.core.util.UrlBuilder;
import org.objectweb.proactive.ext.security.Communication;
import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
import org.objectweb.proactive.ext.security.SecurityContext;
import org.objectweb.proactive.ext.security.crypto.KeyExchangeException;
import org.objectweb.proactive.ext.security.exceptions.RenegotiateSessionException;
import org.objectweb.proactive.ext.security.exceptions.SecurityNotAvailableException;
import org.objectweb.proactive.osgi.OsgiParameters;


/**
 * An HTTP adapter for a ProActiveRuntime to be able to receive remote calls with HTTP. This helps isolate
 * HTTP specific code into a small set of specific classes.
 * @author ProActiveTeam
 * @version 1.0, 9 ao?t 2005
 * @since ProActive 2.2
 * @see <a href="http://www.javaworld.com/javaworld/jw-05-1999/jw-05-networked_p.html">Adapter Pattern</a>
 */
public class HttpProActiveRuntime implements RemoteProActiveRuntime {
    private transient ProActiveRuntime localruntime;
    private boolean isLocal;
    private String url;
    protected VMInformation vmInformation;

    //    private int port;
    //this boolean is used when killing the runtime. Indeed in case of co-allocation, we avoid a second call to the runtime
    // which is already dead
    protected boolean alreadykilled = false;

    public HttpProActiveRuntime() {
        isLocal = true;
        this.localruntime = ProActiveRuntimeImpl.getProActiveRuntime();
        if (OsgiParameters.servletEnabled()) {
            this.url = ClassServerServlet.getUrl();
        } else {
            this.url = ClassServer.getUrl();
        }

        //we also cache the information here to avoid crossing the network when formatting
        //urls
        this.vmInformation = localruntime.getVMInformation();
    }

    /**
     *
     */
    public HttpProActiveRuntime(String newurl) {
        runtimeLogger.debug("Adapter URL = " + newurl);
        try {
            this.url = UrlBuilder.checkUrl(newurl);
            isLocal = false;
            runtimeLogger.debug("New Remote XML Adapter : " + url);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //
    // -- Implements ProActiveRuntime -----------------------------------------------
    //
    public String createLocalNode(String nodeName,
        boolean replacePreviousBinding,
        ProActiveSecurityManager securityManager, String vname, String jobId)
        throws NodeException, HTTPRemoteException {
        String nodeURL = null;
        try {
            nodeURL = buildNodeURL(nodeName);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }

        //      then take the name of the node
        String name = UrlBuilder.getNameFromUrl(nodeURL);

        if (isLocal) {
            localruntime.createLocalNode(name, replacePreviousBinding,
                securityManager, vname, jobId);
        } else {
            ArrayList paramsList = new ArrayList();
            paramsList.add(name);
            paramsList.add(new Boolean(replacePreviousBinding));
            paramsList.add(securityManager);
            paramsList.add(vname);
            paramsList.add(jobId);
            RuntimeRequest req = new RuntimeRequest("createLocalNode",
                    paramsList, this.url);
            req.send();
        }
        runtimeLogger.info(nodeURL + " successfully registered ");
        return nodeURL;
    }

    public void killAllNodes() throws HTTPRemoteException, ProActiveException {
        if (isLocal) {
            localruntime.killAllNodes();
            return;
        }
        new RuntimeRequest("killAllNodes", this.url).send();
    }

    public void killNode(String nodeName)
        throws ProActiveException, HTTPRemoteException {
        String name = UrlBuilder.getNameFromUrl(nodeName);
        if (isLocal) {
            localruntime.killNode(name);
            return;
        }
        ArrayList params = new ArrayList();
        params.add(name);
        new RuntimeRequest("killNode", params, this.url).send();
    }

    public void createVM(UniversalProcess remoteProcess)
        throws IOException, ProActiveException {
        if (isLocal) {
            localruntime.createVM(remoteProcess);
            return;
        }
{'fraction_non_alphanumeric': 0.10914642806419543, 'fraction_numerical': 0.006533162902996733, 'mean_word_length': 3.0843967517401394, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '534731', 'n_tokens_mistral': 3765, 'n_tokens_neox': 3627, 'n_words': 939}