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

org.ow2.jonas.admin.osgi.shell.ShellPlugin Maven / Gradle / Ivy

The newest version!
/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2009 Bull S.A.S.
 * Contact: [email protected]
 *
 * 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
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.ow2.jonas.admin.osgi.shell;

import java.io.PrintStream;

import org.apache.felix.shell.ShellService;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.ow2.jonas.admin.osgi.StringBufferOutputStream;

public class ShellPlugin {

    public ShellPlugin(final BundleContext bc) {

    }

    public String command(String line, final BundleContext bc) {
        StringBufferOutputStream os = new StringBufferOutputStream();
        PrintStream m_out = new PrintStream(os);
        if (line == null) {
            os.close();
            return "";
        }

        line = line.trim();

        if (line.length() == 0) {
            os.close();
            return "";
        }

        // Get shell service.
        ServiceReference ref = bc.getServiceReference(org.apache.felix.shell.ShellService.class.getName());
        if (ref == null) {
            m_out.println("No shell service is available.");
            return "";
        }

        ShellService shell = (ShellService) bc.getService(ref);

        // Print the command line in the output window.
        m_out.println("-> " + line);

        try {
            shell.executeCommand(line, m_out, m_out);
        } catch (Exception ex) {
            m_out.println(ex.toString());
            ex.printStackTrace(m_out);
        }

        bc.ungetService(ref);
        os.close();
        return os.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy