001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    
020    package org.apache.geronimo.mavenplugins.geronimo.server;
021    
022    import java.io.File;
023    
024    import org.apache.tools.ant.taskdefs.Java;
025    
026    import org.apache.maven.plugin.MojoExecutionException;
027    
028    import org.apache.geronimo.mavenplugins.geronimo.ServerProxy;
029    import org.apache.geronimo.mavenplugins.geronimo.reporting.ReportingMojoSupport;
030    
031    import org.codehaus.plexus.util.FileUtils;
032    
033    /**
034     * Execute application client.
035     *
036     * @goal run-client
037     *
038     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039     */
040    public class RunClientMojo extends ReportingMojoSupport
041    {
042        /**
043         * The id of the client module to be executed
044         *
045         * @parameter expression="${moduleId}
046         * @required
047         */
048        protected String moduleId = null;
049    
050        /**
051         * Set the maximum memory for the forked JVM.
052         *
053         * @parameter expression="${maximumMemory}"
054         */
055        private String maximumMemory = null;
056    
057        /**
058         * The location of the Java Virtual Machine executable to launch the client with.
059         *
060         * @paramter
061         */
062        private File javaVirtualMachine;
063        
064        /**
065         * Time in seconds to wait before terminating the forked JVM.
066         *
067         * @parameter expression="${timeout}" default-value="-1"
068         */
069        private int timeout = -1;
070    
071        /**
072         * The arguments
073         *
074         * @parameter expression="${arg}
075         * @optional
076         */
077        protected String[] arg = null;
078    
079        protected void doExecute() throws Exception {
080            ServerProxy server =
081                new ServerProxy(hostname, port, username, password);
082    
083            String geronimoHomeStr = server.getGeronimoHome();
084    
085            server.closeConnection();
086            
087            log.info("Geronimo Home: " + geronimoHomeStr);
088    
089            if (geronimoHomeStr == null) {
090                throw new MojoExecutionException("Unable to determine Geronimo installation directory");
091            }
092    
093            File geronimoHome = new File(geronimoHomeStr);
094    
095            if (!geronimoHome.exists()) {
096                throw new MojoExecutionException("Geronimo installation directory does not exist: " + geronimoHomeStr);
097            }
098    
099            log.info("Starting Geronimo client...");
100    
101            Java java = (Java)createTask("java");
102            java.setJar(new File(geronimoHome, "bin/client.jar"));
103            java.setDir(geronimoHome);
104            java.setFailonerror(true);
105            java.setFork(true);
106    
107            if (javaVirtualMachine != null) {
108                if (!javaVirtualMachine.exists()) {
109                    throw new MojoExecutionException("Java virtual machine is not valid: " + javaVirtualMachine);
110                }
111                
112                log.info("Using Java virtual machine: " + javaVirtualMachine);
113                java.setJvm(javaVirtualMachine.getCanonicalPath());
114            }
115            
116            if (timeout > 0) {
117                java.setTimeout(new Long(timeout * 1000));
118            }
119    
120            if (maximumMemory != null) {
121                java.setMaxmemory(maximumMemory);
122            }
123    
124            // Set the properties which we pass to the JVM from the startup script
125            setSystemProperty(java, "org.apache.geronimo.base.dir", geronimoHome);
126            setSystemProperty(java, "java.io.tmpdir", "var/temp");
127            setSystemProperty(java, "java.endorsed.dirs", prefixSystemPath("java.endorsed.dirs", new File(geronimoHome, "lib/endorsed")));
128            setSystemProperty(java, "java.ext.dirs", prefixSystemPath("java.ext.dirs", new File(geronimoHome, "lib/ext")));
129    
130            java.createArg().setValue(moduleId);
131    
132            for (int i=0;arg != null && i<arg.length;i++) {
133                java.createArg().setValue(arg[i]);
134            }
135    
136            if (logOutput) {
137                File file = getLogFile();
138                FileUtils.forceMkdir(file.getParentFile());
139    
140                log.info("Redirecting output to: " + file);
141    
142                java.setOutput(file);
143            }
144    
145            java.execute();
146        }
147    
148        private String prefixSystemPath(final String name, final File file) {
149            assert name != null;
150            assert file != null;
151    
152            String dirs = file.getPath();
153            String prop = System.getProperty(name, "");
154            if (prop.length() > 0) {
155                dirs += File.pathSeparator;
156                dirs += prop;
157            }
158            return dirs;
159        }
160    
161        protected String getFullClassName() {
162            return this.getClass().getName();
163        }
164    }