001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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 java.io.PrintWriter;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.enterprise.deploy.spi.DeploymentManager;
025    import javax.enterprise.deploy.spi.Target;
026    import javax.enterprise.deploy.spi.TargetModuleID;
027    import javax.enterprise.deploy.spi.exceptions.TargetException;
028    import javax.enterprise.deploy.spi.status.ProgressObject;
029    
030    import org.apache.geronimo.cli.deployer.CommandArgs;
031    import org.apache.geronimo.common.DeploymentException;
032    
033    /**
034     * The CLI deployer logic to start.
035     *
036     * @version $Rev: 537224 $ $Date: 2007-05-11 12:25:51 -0400 (Fri, 11 May 2007) $
037     */
038    public class CommandStart extends AbstractCommand {
039    
040        public void execute(PrintWriter out, ServerConnection connection, CommandArgs commandArgs) throws DeploymentException {
041            String[] args = commandArgs.getArgs();
042            
043            DeploymentManager mgr = connection.getDeploymentManager();
044            Target[] allTargets = mgr.getTargets();
045            TargetModuleID[] allModules;
046            try {
047                allModules = mgr.getAvailableModules(null, allTargets);
048            } catch(TargetException e) {
049                throw new DeploymentException("Unable to load module list from server", e);
050            }
051            List modules = new ArrayList();
052            for(int i=0; i<args.length; i++) {
053                modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, args[i], false));
054            }
055            TargetModuleID[] ids = (TargetModuleID[]) modules.toArray(new TargetModuleID[modules.size()]);
056            boolean multiple = isMultipleTargets(ids);
057            ProgressObject po = runCommand(out, mgr, ids);
058            TargetModuleID[] done = po.getResultTargetModuleIDs();
059            out.println();
060            for(int i = 0; i < done.length; i++) {
061                TargetModuleID id = done[i];
062                out.print(DeployUtils.reformat(getAction()+" "+id.getModuleID()+((multiple && id.getTarget() != null) ? " on "+ id.getTarget().getName() : "")+(id.getWebURL() == null || !getAction().equals("Started") ? "" : " @ "+id.getWebURL()),4, 72));
063                if(id.getChildTargetModuleID() != null) {
064                    for (int j = 0; j < id.getChildTargetModuleID().length; j++) {
065                        TargetModuleID child = id.getChildTargetModuleID()[j];
066                        out.print(DeployUtils.reformat("  `-> "+child.getModuleID()+(child.getWebURL() == null || getAction().toLowerCase().indexOf("started") == -1 ? "" : " @ "+child.getWebURL()),4, 72));
067                    }
068                } // Also print childs if existing in earlier configuration
069                else{
070                    java.util.Iterator iterator = DeployUtils.identifyTargetModuleIDs(allModules, id.getModuleID(), false).iterator();
071                    if(iterator.hasNext()){
072                        TargetModuleID childs = (TargetModuleID)iterator.next();
073                        if(childs.getChildTargetModuleID() != null) {
074                            for (int j = 0; j < childs.getChildTargetModuleID().length; j++) {
075                                TargetModuleID child = childs.getChildTargetModuleID()[j];
076                                out.print(DeployUtils.reformat("  `-> "+child.getModuleID()+(child.getWebURL() == null || getAction().toLowerCase().indexOf("started") == -1 ? "" : " @ "+child.getWebURL()),4, 72));
077                            }
078                        }
079                    }
080                }
081                out.println();
082            }
083            if(po.getDeploymentStatus().isFailed()) {
084                throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
085            }
086        }
087    
088        protected ProgressObject runCommand(PrintWriter out, DeploymentManager mgr, TargetModuleID[] ids) {
089            ProgressObject po = mgr.start(ids);
090            waitForProgress(out, po);
091            return po;
092        }
093    
094        protected String getAction() {
095            return "Started";
096        }
097    
098    }