001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.shell.equinox;
018
019import java.util.Map;
020
021import org.nuxeo.shell.CommandRegistry;
022import org.nuxeo.shell.Shell;
023import org.nuxeo.shell.ShellFeature;
024import org.nuxeo.shell.cmds.GlobalCommands;
025import org.nuxeo.shell.equinox.cmds.Bundle;
026import org.nuxeo.shell.equinox.cmds.Bundles;
027import org.nuxeo.shell.equinox.cmds.Connect;
028import org.nuxeo.shell.equinox.cmds.Diag;
029import org.nuxeo.shell.equinox.cmds.Disconnect;
030import org.nuxeo.shell.equinox.cmds.Headers;
031import org.nuxeo.shell.equinox.cmds.Packages;
032import org.nuxeo.shell.equinox.cmds.Run;
033import org.nuxeo.shell.equinox.cmds.Ss;
034import org.nuxeo.shell.equinox.cmds.Status;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class EquinoxFeature extends CommandRegistry implements ShellFeature {
040
041    protected Connector connector;
042
043    public EquinoxFeature() {
044        super(GlobalCommands.INSTANCE, "equinox");
045    }
046
047    @Override
048    public void install(Shell shell) {
049        shell.addRegistry(this);
050        addAnnotatedCommand(Connect.class);
051    }
052
053    public void loadCommands() {
054        addAnnotatedCommand(Disconnect.class);
055        addAnnotatedCommand(Run.class);
056        addAnnotatedCommand(Ss.class);
057        addAnnotatedCommand(Status.class);
058        addAnnotatedCommand(Bundle.class);
059        addAnnotatedCommand(Bundles.class);
060        addAnnotatedCommand(Packages.class);
061        addAnnotatedCommand(Headers.class);
062        addAnnotatedCommand(Diag.class);
063    }
064
065    @Override
066    public String getTitle() {
067        return "Equinox Commands";
068    }
069
070    @Override
071    public String getDescription() {
072        return "Provides Equinox console commands for a remote OSGi platform";
073    }
074
075    @Override
076    public String getPrompt(Shell shell) {
077        return "osgi> ";
078    }
079
080    public void connect(String url, String username, String password) {
081        if (connector != null) {
082            return;
083        }
084        connector = Connector.newConnector(url);
085        Shell.get().putContextObject(Connector.class, connector);
086        loadCommands();
087    }
088
089    public void disconnect() {
090        if (connector == null) {
091            return;
092        }
093        connector.disconnect();
094        connector = null;
095        Shell.get().removeContextObject(Connector.class);
096        clear();
097        addAnnotatedCommand(Connect.class);
098    }
099
100    @Override
101    public void autorun(Shell shell) {
102        Map<String, String> args = shell.getMainArguments();
103        if (args != null) {
104            String url = args.get("#1");
105            if (url != null) {
106                shell.getConsole().println("Connecting to " + url + " ...");
107                connect(url, null, null);
108            }
109        }
110        super.autorun(shell);
111    }
112
113}