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.cmds;
018
019import java.util.Map;
020
021import org.nuxeo.shell.Argument;
022import org.nuxeo.shell.Command;
023import org.nuxeo.shell.Context;
024import org.nuxeo.shell.Parameter;
025import org.nuxeo.shell.Shell;
026import org.nuxeo.shell.ShellException;
027import org.nuxeo.shell.equinox.EquinoxFeature;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032@Command(name = "connect", help = "Connect to a remote osgi platform")
033public class Connect implements Runnable {
034
035    @Context
036    protected Shell shell;
037
038    @Argument(name = "url", index = 0, required = false, help = "The url of the automation server")
039    protected String url;
040
041    @Parameter(name = "-u", hasValue = true, help = "The username")
042    protected String username;
043
044    @Parameter(name = "-p", hasValue = true, help = "The password")
045    protected String password;
046
047    public void run() {
048        Map<String, String> args = (Map<String, String>) shell.getMainArguments();
049        if (username == null && args != null) {
050            username = args.get("-u");
051        }
052        if (password == null && args != null) {
053            password = args.get("-p");
054        }
055        if (url == null && args != null) {
056            url = args.get("#1");
057        }
058        if (username != null && password == null) {
059            password = shell.getConsole().readLine("Password: ", '*');
060        }
061        try {
062            shell.getFeature(EquinoxFeature.class).connect(url, username, password);
063        } catch (Exception e) {
064            throw new ShellException("Failed to connect to " + url, e);
065        }
066    }
067
068}