001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.shell.equinox;
020
021import java.util.Map;
022
023import org.nuxeo.shell.CommandRegistry;
024import org.nuxeo.shell.Shell;
025import org.nuxeo.shell.ShellFeature;
026import org.nuxeo.shell.cmds.GlobalCommands;
027import org.nuxeo.shell.equinox.cmds.Bundle;
028import org.nuxeo.shell.equinox.cmds.Bundles;
029import org.nuxeo.shell.equinox.cmds.Connect;
030import org.nuxeo.shell.equinox.cmds.Diag;
031import org.nuxeo.shell.equinox.cmds.Disconnect;
032import org.nuxeo.shell.equinox.cmds.Headers;
033import org.nuxeo.shell.equinox.cmds.Packages;
034import org.nuxeo.shell.equinox.cmds.Run;
035import org.nuxeo.shell.equinox.cmds.Ss;
036import org.nuxeo.shell.equinox.cmds.Status;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class EquinoxFeature extends CommandRegistry implements ShellFeature {
042
043    protected Connector connector;
044
045    public EquinoxFeature() {
046        super(GlobalCommands.INSTANCE, "equinox");
047    }
048
049    @Override
050    public void install(Shell shell) {
051        shell.addRegistry(this);
052        addAnnotatedCommand(Connect.class);
053    }
054
055    public void loadCommands() {
056        addAnnotatedCommand(Disconnect.class);
057        addAnnotatedCommand(Run.class);
058        addAnnotatedCommand(Ss.class);
059        addAnnotatedCommand(Status.class);
060        addAnnotatedCommand(Bundle.class);
061        addAnnotatedCommand(Bundles.class);
062        addAnnotatedCommand(Packages.class);
063        addAnnotatedCommand(Headers.class);
064        addAnnotatedCommand(Diag.class);
065    }
066
067    @Override
068    public String getTitle() {
069        return "Equinox Commands";
070    }
071
072    @Override
073    public String getDescription() {
074        return "Provides Equinox console commands for a remote OSGi platform";
075    }
076
077    @Override
078    public String getPrompt(Shell shell) {
079        return "osgi> ";
080    }
081
082    public void connect(String url, String username, String password) {
083        if (connector != null) {
084            return;
085        }
086        connector = Connector.newConnector(url);
087        Shell.get().putContextObject(Connector.class, connector);
088        loadCommands();
089    }
090
091    public void disconnect() {
092        if (connector == null) {
093            return;
094        }
095        connector.disconnect();
096        connector = null;
097        Shell.get().removeContextObject(Connector.class);
098        clear();
099        addAnnotatedCommand(Connect.class);
100    }
101
102    @Override
103    public void autorun(Shell shell) {
104        Map<String, String> args = shell.getMainArguments();
105        if (args != null) {
106            String url = args.get("#1");
107            if (url != null) {
108                shell.getConsole().println("Connecting to " + url + " ...");
109                connect(url, null, null);
110            }
111        }
112        super.autorun(shell);
113    }
114
115}