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    package org.apache.geronimo.deployment.plugin.local;
018    
019    import java.io.File;
020    import java.io.FileOutputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.OutputStream;
024    import java.net.URL;
025    import java.util.Iterator;
026    import java.util.List;
027    import java.util.Set;
028    import javax.enterprise.deploy.shared.CommandType;
029    import javax.enterprise.deploy.shared.ModuleType;
030    import javax.enterprise.deploy.spi.Target;
031    
032    import org.apache.geronimo.common.DeploymentException;
033    import org.apache.geronimo.deployment.Deployer;
034    import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
035    import org.apache.geronimo.gbean.AbstractName;
036    import org.apache.geronimo.gbean.AbstractNameQuery;
037    import org.apache.geronimo.kernel.Kernel;
038    
039    /**
040     * @version $Rev: 399026 $ $Date: 2006-05-02 13:03:28 -0700 (Tue, 02 May 2006) $
041     */
042    public abstract class AbstractDeployCommand extends CommandSupport {
043        protected final Kernel kernel;
044        private static final String[] DEPLOY_SIG = {boolean.class.getName(), File.class.getName(), File.class.getName(), String.class.getName()};
045        protected final boolean spool;
046        protected File moduleArchive;
047        protected File deploymentPlan;
048        protected InputStream moduleStream;
049        protected InputStream deploymentStream;
050        protected AbstractName deployer;
051    
052        public AbstractDeployCommand(CommandType command, Kernel kernel, File moduleArchive, File deploymentPlan, InputStream moduleStream, InputStream deploymentStream, boolean spool) {
053            super(command);
054            this.kernel = kernel;
055            this.moduleArchive = moduleArchive;
056            this.deploymentPlan = deploymentPlan;
057            this.moduleStream = moduleStream;
058            this.deploymentStream = deploymentStream;
059            this.spool = spool;
060            deployer = getDeployerName();
061        }
062    
063        private AbstractName getDeployerName() {
064            Set deployers = kernel.listGBeans(new AbstractNameQuery("org.apache.geronimo.deployment.Deployer"));
065            if (deployers.isEmpty()) {
066                fail("No Deployer GBean present in running Geronimo server. " +
067                     "This usually indicates a serious problem with the configuration of " +
068                     "your running Geronimo server.  If " +
069                     "the deployer is present but not started, the workaround is to run " +
070                     "a deploy command like 'start geronimo/geronimo-gbean-deployer/1.0/car'.  " +
071                     "If the deployer service is not present at all (it was undeployed) then " +
072                     "you need to either re-install Geronimo or get a deployment plan for the " +
073                     "runtime deployer and distribute it while the server is not running and " +
074                     "then start the server with a command like the above.  For help on this, " +
075                     "write to user@geronimo.apache.org and include the contents of your " +
076                     "var/config/config.xml file.");
077                return null;
078            }
079            Iterator j = deployers.iterator();
080            AbstractName deployer = (AbstractName) j.next();
081            if (j.hasNext()) {
082                fail("More than one deployer found");
083                return null;
084            }
085            return deployer;
086    
087        }
088    
089        protected void copyTo(File outfile, InputStream is) throws IOException {
090            byte[] buffer = new byte[4096];
091            int count;
092            OutputStream os = new FileOutputStream(outfile);
093            try {
094                while ((count = is.read(buffer)) > 0) {
095                    os.write(buffer, 0, count);
096                }
097            } finally {
098                os.close();
099            }
100        }
101    
102        protected void doDeploy(Target target, boolean finished) throws Exception {
103            File[] args = {moduleArchive, deploymentPlan};
104            massageFileNames(args);
105            Object deployParams[] = new Object[] {Boolean.valueOf(commandContext.isInPlace()), args[0], args[1], target.getName()};
106            List objectNames = (List) kernel.invoke(deployer, "deploy", deployParams, DEPLOY_SIG);
107            if (objectNames == null || objectNames.isEmpty()) {
108                throw new DeploymentException("Server didn't deploy anything");
109            }
110            String parentName = (String) objectNames.get(0);
111            String[] childIDs = new String[objectNames.size()-1];
112            for (int j=0; j < childIDs.length; j++) {
113                childIDs[j] = (String)objectNames.get(j+1);
114            }
115    
116            TargetModuleIDImpl moduleID = new TargetModuleIDImpl(target, parentName, childIDs);
117            if(isWebApp(kernel, parentName)) {
118                moduleID.setType(ModuleType.WAR);
119            }
120            if(moduleID.getChildTargetModuleID() != null) {
121                for (int i = 0; i < moduleID.getChildTargetModuleID().length; i++) {
122                    TargetModuleIDImpl id = (TargetModuleIDImpl) moduleID.getChildTargetModuleID()[i];
123                    if(isWebApp(kernel, id.getModuleID())) {
124                        id.setType(ModuleType.WAR);
125                    }
126                }
127            }
128            addModule(moduleID);
129            if(finished) {
130                addWebURLs(kernel);
131                complete("Completed with id " + parentName);
132            }
133        }
134    
135        protected void massageFileNames(File[] inputs) {
136        }
137    
138        public URL getRemoteDeployUploadURL() throws Exception {
139           return new URL((String)kernel.getAttribute(deployer, "remoteDeployUploadURL"));
140        }
141    }