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

org.ow2.util.protocol.impl.ProcessSession Maven / Gradle / Ivy

/**
 * Copyright 2010-2012 Bull S.A.S.
 *
 * 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 org.ow2.util.protocol.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;
import org.ow2.util.protocol.api.IProtocolSession;
import org.ow2.util.protocol.api.ProtocolConfig;
import org.ow2.util.protocol.api.ProtocolException;

/**
 *
 * @author Loris Bouzonnet
 */
public class ProcessSession implements IProtocolSession {

    private static Log logger = LogFactory.getLog(ProcessSession.class);

    private final ProtocolConfig config;

    public ProcessSession(final ProtocolConfig config) {
        this.config = config;
    }

    public void close() {
    }

    public void send(final String command) throws ProtocolException {
        final Process process;
        try {
            process = Runtime.getRuntime().exec(command);
        } catch (final IOException e) {
            throw new ProtocolException(
                    "Error when starting a process for the command: " + command, e);
        }

        final StreamReader errStreamReader =
                new StreamReader(process.getErrorStream(), this.config.getErrorStream());
        new Thread(errStreamReader).start();

        final StreamReader inStreamReader =
                new StreamReader(process.getInputStream(), this.config.getOutputStream());
        new Thread(inStreamReader).start();

        final int exitCode;
        try {
            exitCode = process.waitFor();
            if (exitCode == 0) {
                logger.debug("Exit code: " + exitCode);
            } else {
                logger.warn("Exit code: " + exitCode);
            }
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
        }

    }

    public static void main(final String[] arg) throws ProtocolException {

        final ProtocolConfig config = new ProtocolConfig();
        config.setErrorStream(System.err);
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        config.setOutputStream(outputStream);

        final ProcessSession session = new ProcessSession(config);
        session.send(arg[0]);
        session.close();

        System.out.println(outputStream.toString());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy