001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment.cli;
019    
020    import org.apache.geronimo.common.DeploymentException;
021    
022    import javax.enterprise.deploy.spi.DeploymentManager;
023    import javax.enterprise.deploy.spi.Target;
024    import javax.enterprise.deploy.spi.TargetModuleID;
025    import javax.enterprise.deploy.spi.exceptions.TargetException;
026    import javax.enterprise.deploy.spi.status.ProgressObject;
027    import java.io.PrintWriter;
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * The CLI deployer logic to start.
033     *
034     * @version $Rev: 405229 $ $Date: 2006-05-08 16:44:03 -0700 (Mon, 08 May 2006) $
035     */
036    public class CommandStart extends AbstractCommand {
037        public CommandStart() {
038            super("start", "1. Common Commands", "[ModuleID|TargetModuleID]+",
039                    "Accepts the configId of a module, or the fully-qualified " +
040                    "TargetModuleID identifying both the module and the server or cluster it's " +
041                    "on, and starts that module.  The module should be available to the server " +
042                    "but not currently running.  If multiple modules are specified, they will " +
043                    "all be started.\n" +
044                    "If the server is not running, the module will be marked to start " +
045                    "next time the server is started.");
046        }
047    
048        public CommandStart(String command, String group, String helpArgumentList, String helpText) {
049            super(command, group, helpArgumentList, helpText);
050        }
051    
052        public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
053            if(args.length == 0) {
054                throw new DeploymentSyntaxException("Must specify at least one module name or TargetModuleID");
055            }
056            DeploymentManager mgr = connection.getDeploymentManager();
057            Target[] allTargets = mgr.getTargets();
058            TargetModuleID[] allModules;
059            try {
060                allModules = mgr.getAvailableModules(null, allTargets);
061            } catch(TargetException e) {
062                throw new DeploymentException("Unable to load module list from server", e);
063            }
064            List modules = new ArrayList();
065            for(int i=0; i<args.length; i++) {
066                modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, args[i], false));
067            }
068            TargetModuleID[] ids = (TargetModuleID[]) modules.toArray(new TargetModuleID[modules.size()]);
069            boolean multiple = isMultipleTargets(ids);
070            ProgressObject po = runCommand(out, mgr, ids);
071            TargetModuleID[] done = po.getResultTargetModuleIDs();
072            out.println();
073            for(int i = 0; i < done.length; i++) {
074                TargetModuleID id = done[i];
075                out.print(DeployUtils.reformat(getAction()+" "+id.getModuleID()+(multiple ? " on "+id.getTarget().getName() : "")+(id.getWebURL() == null || !getAction().equals("Started") ? "" : " @ "+id.getWebURL()),4, 72));
076                if(id.getChildTargetModuleID() != null) {
077                    for (int j = 0; j < id.getChildTargetModuleID().length; j++) {
078                        TargetModuleID child = id.getChildTargetModuleID()[j];
079                        out.print(DeployUtils.reformat("  `-> "+child.getModuleID()+(child.getWebURL() == null || !getAction().equals("Started") ? "" : " @ "+child.getWebURL()),4, 72));
080                    }
081                }
082                out.println();
083            }
084            if(po.getDeploymentStatus().isFailed()) {
085                throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
086            }
087        }
088    
089        protected ProgressObject runCommand(PrintWriter out, DeploymentManager mgr, TargetModuleID[] ids) {
090            ProgressObject po = mgr.start(ids);
091            waitForProgress(out, po);
092            return po;
093        }
094    
095        protected String getAction() {
096            return "Started";
097        }
098    
099    }