All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dyorgio.runtime.out.process.OutProcessUtils Maven / Gradle / Ivy

There is a newer version: 1.2.7
Show newest version
/** *****************************************************************************
 * Copyright 2017 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***************************************************************************** */
package dyorgio.runtime.out.process;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.concurrent.Callable;

/**
 * Constants and utility methods used in an out process execution.
 *
 * @author dyorgio
 */
public class OutProcessUtils {

    /**
     * System property flag to identify an out process code at runtime.
     */
    public static final String RUNNING_AS_OUT_PROCESS = "$RunnningAsOutProcess";

    /**
     * Get current Thread classpath.
     *
     * @return A string of current classpath elements splited by
     * File.pathSeparatorChar
     */
    public static String getCurrentClasspath() {
        StringBuilder buffer = new StringBuilder();
        for (URL url : ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs()) {
            buffer.append(new File(url.getPath()));
            buffer.append(File.pathSeparatorChar);
        }
        String classpath = buffer.toString();
        classpath = classpath.substring(0, classpath.lastIndexOf(File.pathSeparatorChar));
        return classpath;
    }

    /**
     * Creates a new ObjectInputStream from
     * inputStream parameter, reads a Callable command
     * from it, executes call, and write results on objOut.
     * 
* After executing Callable.call() a primitive boolean is wrote * in objOut to sinalize the execution state: *
* true: OK execution. Result is wrote on * objOut
* false: An Exception occurred. * Exception is wrote on objOut
* * @param inputStream A source of the command. * @param objOut The output for result. * @throws IOException * @see ObjectInputStream * @see Callable * @see ObjectOutputStream */ public static void readCommandExecuteAndRespond(InputStream inputStream, ObjectOutputStream objOut) throws IOException { try { // Read current command Callable callable = (Callable) new ObjectInputStream(inputStream).readObject(); Serializable result = callable.call(); // Reply with result objOut.writeBoolean(true); objOut.writeObject(result); objOut.flush(); } catch (Throwable e) { try { // Reply with error objOut.writeBoolean(false); objOut.writeObject(e); objOut.flush(); } catch (Throwable ex) { // Reply with safe error (without not-serializable objects). objOut.writeObject(new RuntimeException(ex.getMessage())); objOut.flush(); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy