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.plugin.local;
019    
020    import java.util.List;
021    import javax.enterprise.deploy.shared.CommandType;
022    import javax.enterprise.deploy.shared.ModuleType;
023    import javax.enterprise.deploy.spi.TargetModuleID;
024    
025    import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
026    import org.apache.geronimo.kernel.Kernel;
027    import org.apache.geronimo.kernel.config.Configuration;
028    import org.apache.geronimo.kernel.config.ConfigurationManager;
029    import org.apache.geronimo.kernel.config.ConfigurationUtil;
030    import org.apache.geronimo.kernel.repository.Artifact;
031    import org.apache.geronimo.gbean.AbstractName;
032    
033    /**
034     * @version $Rev:392614 $ $Date: 2007-05-11 12:25:51 -0400 (Fri, 11 May 2007) $
035     */
036    public class StartCommand extends CommandSupport {
037        private final Kernel kernel;
038        private final TargetModuleID[] modules;
039    
040        public StartCommand(Kernel kernel, TargetModuleID modules[]) {
041            super(CommandType.START);
042            this.kernel = kernel;
043            this.modules = modules;
044        }
045    
046        public void run() {
047            try {
048                ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
049                try {
050                    for (int i = 0; i < modules.length; i++) {
051                        TargetModuleID module = modules[i];
052    
053                        // Check to see whether the module is already started
054                        Artifact moduleID = Artifact.create(module.getModuleID());
055                        if (configurationManager.isRunning(moduleID)) {
056                            updateStatus("Module " + moduleID + " is already running");
057                            Thread.sleep(100);
058                            continue;
059                        }
060    
061                        // Load
062                        if(!configurationManager.isLoaded(moduleID)) {
063                            configurationManager.loadConfiguration(moduleID);
064                        }
065    
066                        // Start
067                        org.apache.geronimo.kernel.config.LifecycleResults lcresult = configurationManager.startConfiguration(moduleID);
068    
069                        // Determine the child modules of the configuration
070                        //TODO might be a hack
071                        List kids = loadChildren(kernel, moduleID.toString());
072    
073                        // Build a response obect containg the started configuration and a list of it's contained modules
074                        TargetModuleIDImpl id = new TargetModuleIDImpl(modules[i].getTarget(), module.getModuleID(),
075                                (String[]) kids.toArray(new String[kids.size()]));
076                        if (isWebApp(kernel, moduleID.toString())) {
077                            id.setType(ModuleType.WAR);
078                        }
079                        if (id.getChildTargetModuleID() != null) {
080                            for (int k = 0; k < id.getChildTargetModuleID().length; k++) {
081                                TargetModuleIDImpl child = (TargetModuleIDImpl) id.getChildTargetModuleID()[k];
082                                if (isWebApp(kernel, child.getModuleID())) {
083                                    child.setType(ModuleType.WAR);
084                                }
085                            }
086                        }
087                        addModule(id);
088                        java.util.Iterator iterator = lcresult.getStarted().iterator();
089                        while (iterator.hasNext()) {
090                            Artifact config = (Artifact)iterator.next();
091                            if (!config.toString().equals(id.getModuleID())) {
092                                //TODO might be a hack
093                                List kidsChild = loadChildren(kernel, config.toString());
094    
095                                // Build a response obect containg the started configuration and a list of it's contained modules
096                                TargetModuleIDImpl idChild = new TargetModuleIDImpl(null, config.toString(),
097                                        (String[]) kidsChild.toArray(new String[kidsChild.size()]));
098                                if (isWebApp(kernel, config.toString())) {
099                                    idChild.setType(ModuleType.WAR);
100                                }
101                                if (idChild.getChildTargetModuleID() != null) {
102                                    for (int k = 0; k < idChild.getChildTargetModuleID().length; k++) {
103                                        TargetModuleIDImpl child = (TargetModuleIDImpl) idChild.getChildTargetModuleID()[k];
104                                        if (isWebApp(kernel, child.getModuleID())) {
105                                            child.setType(ModuleType.WAR);
106                                        }
107                                    }
108                                }
109                                addModule(idChild);                            
110                            }
111                        }
112                    }
113                } finally {
114                    ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
115                }
116                addWebURLs(kernel);
117                complete("Completed");
118            } catch (Exception e) {
119                e.printStackTrace();
120                doFail(e);
121            }
122        }
123    }