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