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: 547378 $ $Date: 2007-06-14 15:44:10 -0400 (Thu, 14 Jun 2007) $
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            log.info("Geronimo Home: " + geronimoHomeStr);
086    
087            if (geronimoHomeStr == null) {
088                throw new MojoExecutionException("Unable to determine Geronimo installation directory");
089            }
090    
091            File geronimoHome = new File(geronimoHomeStr);
092    
093            if (!geronimoHome.exists()) {
094                throw new MojoExecutionException("Geronimo installation directory does not exist: " + geronimoHomeStr);
095            }
096    
097            log.info("Starting Geronimo client...");
098    
099            Java java = (Java)createTask("java");
100            java.setJar(new File(geronimoHome, "bin/client.jar"));
101            java.setDir(geronimoHome);
102            java.setFailonerror(true);
103            java.setFork(true);
104    
105            if (javaVirtualMachine != null) {
106                if (!javaVirtualMachine.exists()) {
107                    throw new MojoExecutionException("Java virtual machine is not valid: " + javaVirtualMachine);
108                }
109                
110                log.info("Using Java virtual machine: " + javaVirtualMachine);
111                java.setJvm(javaVirtualMachine.getCanonicalPath());
112            }
113            
114            if (timeout > 0) {
115                java.setTimeout(new Long(timeout * 1000));
116            }
117    
118            if (maximumMemory != null) {
119                java.setMaxmemory(maximumMemory);
120            }
121    
122            // Set the properties which we pass to the JVM from the startup script
123            setSystemProperty(java, "org.apache.geronimo.base.dir", geronimoHome);
124            setSystemProperty(java, "java.io.tmpdir", "var/temp");
125            setSystemProperty(java, "java.endorsed.dirs", prefixSystemPath("java.endorsed.dirs", new File(geronimoHome, "lib/endorsed")));
126            setSystemProperty(java, "java.ext.dirs", prefixSystemPath("java.ext.dirs", new File(geronimoHome, "lib/ext")));
127    
128            java.createArg().setValue(moduleId);
129    
130            for (int i=0;arg != null && i<arg.length;i++) {
131                java.createArg().setValue(arg[i]);
132            }
133    
134            if (logOutput) {
135                File file = getLogFile();
136                FileUtils.forceMkdir(file.getParentFile());
137    
138                log.info("Redirecting output to: " + file);
139    
140                java.setOutput(file);
141            }
142    
143            java.execute();
144        }
145    
146        private String prefixSystemPath(final String name, final File file) {
147            assert name != null;
148            assert file != null;
149    
150            String dirs = file.getPath();
151            String prop = System.getProperty(name, "");
152            if (prop.length() > 0) {
153                dirs += File.pathSeparator;
154                dirs += prop;
155            }
156            return dirs;
157        }
158    
159        protected String getFullClassName() {
160            return this.getClass().getName();
161        }
162    }