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.automation.cmds;
018
019import java.util.Map;
020
021import org.nuxeo.ecm.automation.client.model.Document;
022import org.nuxeo.shell.CommandRegistry;
023import org.nuxeo.shell.Shell;
024import org.nuxeo.shell.ShellException;
025import org.nuxeo.shell.automation.AutomationFeature;
026import org.nuxeo.shell.automation.RemoteContext;
027import org.nuxeo.shell.cmds.GlobalCommands;
028import org.nuxeo.shell.utils.Path;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class RemoteCommands extends CommandRegistry {
034
035    public static final RemoteCommands INSTANCE = new RemoteCommands();
036
037    public RemoteCommands() {
038        super(GlobalCommands.INSTANCE, "remote");
039        onDisconnect();
040    }
041
042    public void onConnect() {
043        addAnnotatedCommand(Disconnect.class);
044        addAnnotatedCommand(Ls.class);
045        addAnnotatedCommand(Cd.class);
046        addAnnotatedCommand(Pwd.class);
047        addAnnotatedCommand(Popd.class);
048        addAnnotatedCommand(Pushd.class);
049        addAnnotatedCommand(MkDir.class);
050        addAnnotatedCommand(Update.class);
051        addAnnotatedCommand(Rm.class);
052        addAnnotatedCommand(Query.class);
053        addAnnotatedCommand(Cat.class);
054        addAnnotatedCommand(Tree.class);
055        addAnnotatedCommand(Script.class);
056        addAnnotatedCommand(SetBlob.class);
057        addAnnotatedCommand(GetBlob.class);
058        addAnnotatedCommand(GetBlobs.class);
059        addAnnotatedCommand(RemoveBlob.class);
060        addAnnotatedCommand(RunChainWithDoc.class);
061        addAnnotatedCommand(RunChainWithFile.class);
062        addAnnotatedCommand(MkRelation.class);
063        addAnnotatedCommand(GetRelations.class);
064        addAnnotatedCommand(SetProperty.class);
065        addAnnotatedCommand(GetProperty.class);
066        addAnnotatedCommand(Lock.class);
067        addAnnotatedCommand(Unlock.class);
068        addAnnotatedCommand(Cp.class);
069        addAnnotatedCommand(Mv.class);
070        addAnnotatedCommand(Rename.class);
071        addAnnotatedCommand(Publish.class);
072        addAnnotatedCommand(Perms.class);
073        addAnnotatedCommand(LifeCycleState.class);
074        addAnnotatedCommand(Fire.class);
075        addAnnotatedCommand(Audit.class);
076    }
077
078    public void onDisconnect() {
079        clear();
080        addAnnotatedCommand(Connect.class);
081    }
082
083    @Override
084    public String getPrompt(Shell shell) {
085        RemoteContext ctx = shell.getContextObject(RemoteContext.class);
086        if (ctx == null) {
087            return "remote> ";
088        }
089        Document doc = ctx.getDocument();
090        Path path = new Path(doc.getPath());
091        String p = path.isRoot() ? "/" : path.lastSegment();
092        return ctx.getUserName() + "@" + ctx.getHost() + ":" + p + "> ";
093    }
094
095    @Override
096    public String getTitle() {
097        return "Nuxeo Server Commands";
098    }
099
100    @Override
101    public String getDescription() {
102        return "High level commands exposed by a remote Nuxeo Server";
103    }
104
105    @Override
106    public void autorun(Shell shell) {
107        // check if connection info is already available and connect to remote
108        // if so.
109        Map<String, String> args = (Map<String, String>) shell.getMainArguments();
110        if (args != null) {
111            String url = args.get("#1");
112            String username = args.get("-u");
113            String password = args.get("-p");
114            String dir = args.get("-d");
115            if (dir == null) {
116                dir = "/";
117            }
118            if (url != null && username != null && password != null) {
119                try {
120                    shell.getConsole().println("Connecting to " + url + " ...");
121                    shell.getFeature(AutomationFeature.class).connect(url, username, password, dir);
122                } catch (Throwable t) {
123                    throw new ShellException("Failed to connect to " + url, t);
124                }
125            }
126        }
127    }
128
129}