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.io.IOException;
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import javax.enterprise.deploy.spi.DeploymentManager;
026    import javax.enterprise.deploy.spi.Target;
027    import javax.enterprise.deploy.spi.TargetModuleID;
028    import javax.enterprise.deploy.spi.exceptions.TargetException;
029    import javax.enterprise.deploy.spi.status.ProgressObject;
030    
031    import org.apache.geronimo.cli.deployer.CommandArgs;
032    import org.apache.geronimo.common.DeploymentException;
033    import jline.ConsoleReader;
034    
035    /**
036     * The CLI deployer logic to start.
037     *
038     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039     */
040    public class CommandStart extends AbstractCommand {
041    
042        public void execute(ConsoleReader consoleReader, ServerConnection connection, CommandArgs commandArgs) throws DeploymentException {
043            ProgressObject po = null;
044            try {
045                String[] args = commandArgs.getArgs();
046    
047                DeploymentManager mgr = connection.getDeploymentManager();
048                Target[] allTargets = mgr.getTargets();
049                TargetModuleID[] allModules;
050                try {
051                    allModules = mgr.getAvailableModules(null, allTargets);
052                } catch(TargetException e) {
053                    throw new DeploymentException("Unable to load module list from server", e);
054                }
055                List modules = new ArrayList();
056                for(int i=0; i<args.length; i++) {
057                    modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, args[i], false));
058                }
059                TargetModuleID[] ids = (TargetModuleID[]) modules.toArray(new TargetModuleID[modules.size()]);
060                boolean multiple = isMultipleTargets(ids);
061                po = runCommand(consoleReader, mgr, ids);
062                TargetModuleID[] done = po.getResultTargetModuleIDs();
063                consoleReader.printNewline();
064                for(int i = 0; i < done.length; i++) {
065                    TargetModuleID id = done[i];
066                    DeployUtils.println(getAction()+" "+id.getModuleID()+((multiple && id.getTarget() != null) ? " on "+ id.getTarget().getName() : "")+(id.getWebURL() == null || !getAction().equals("Started") ? "" : " @ "+id.getWebURL()),4, consoleReader);
067                    if(id.getChildTargetModuleID() != null) {
068                        for (int j = 0; j < id.getChildTargetModuleID().length; j++) {
069                            TargetModuleID child = id.getChildTargetModuleID()[j];
070                            DeployUtils.println("  `-> "+child.getModuleID()+(child.getWebURL() == null || getAction().toLowerCase().indexOf("started") == -1 ? "" : " @ "+child.getWebURL()),4, consoleReader);
071                        }
072                    } // Also print childs if existing in earlier configuration
073                    else{
074                        java.util.Iterator iterator = DeployUtils.identifyTargetModuleIDs(allModules, id.getModuleID(), false).iterator();
075                        if(iterator.hasNext()){
076                            TargetModuleID childs = (TargetModuleID)iterator.next();
077                            if(childs.getChildTargetModuleID() != null) {
078                                for (int j = 0; j < childs.getChildTargetModuleID().length; j++) {
079                                    TargetModuleID child = childs.getChildTargetModuleID()[j];
080                                    DeployUtils.println("  `-> "+child.getModuleID()+(child.getWebURL() == null || getAction().toLowerCase().indexOf("started") == -1 ? "" : " @ "+child.getWebURL()),4, consoleReader);
081                                }
082                            }
083                        }
084                    }
085    //                consoleReader.printNewline();
086                }
087            } catch (IOException e) {
088                throw new DeploymentException("could not write to console", e);
089            }
090            if(po.getDeploymentStatus().isFailed()) {
091                throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
092            }
093        }
094    
095        protected ProgressObject runCommand(ConsoleReader out, DeploymentManager mgr, TargetModuleID[] ids) {
096            ProgressObject po = mgr.start(ids);
097            waitForProgress(out, po);
098            return po;
099        }
100    
101        protected String getAction() {
102            return "Started";
103        }
104    
105    }