View Javadoc

1   /**
2    *
3    * Copyright 2004-2006 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.plugins.deployment;
19  
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.util.StringTokenizer;
23  
24  import org.apache.geronimo.plugins.util.ServerBehavior;
25  
26  import org.apache.commons.lang.SystemUtils;
27  import org.apache.maven.plugin.MojoExecutionException;
28  
29  import org.codehaus.plexus.util.cli.Commandline;
30  import org.codehaus.plexus.util.cli.CommandLineUtils;
31  import org.codehaus.plexus.util.cli.DefaultConsumer;
32  import org.codehaus.plexus.util.cli.CommandLineException;
33  
34  //
35  // TODO: Rename to StartRemoteServerMojo
36  //
37  
38  /**
39   * ???
40   *
41   * @goal startRemoteServer
42   * 
43   * @version $Rev: 437248 $ $Date: 2006-08-26 16:39:49 -0700 (Sat, 26 Aug 2006) $
44   */
45  public class StartRemoteServerMojo extends AbstractModuleMojo {
46  
47      /**
48       * @parameter
49       */
50      private File geronimoTarget;
51  
52      /**
53       * @parameter expression="${basedir}/target"
54       */
55      private File workingDirectory;
56  
57      //
58      // FIXME: Use <args><args>...</args></args> and let M2 handle parsing
59      //
60  
61      /**
62       * @parameter default-value=""
63       */
64      private String vmArgs;
65  
66      /**
67       * @parameter
68       */
69      private String[] configs;
70  
71      /**
72       * @parameter
73       */
74      private String debugPort;
75  
76      /**
77       * Get the path of Java depending the OS.
78       *
79       * @return the path of the Java
80       */
81      private String getJavaPath() {
82          String javaCommand = "java" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");
83  
84          File javaExe;
85  
86          // For IBM's JDK 1.2
87          if (SystemUtils.IS_OS_AIX) {
88              javaExe = new File(SystemUtils.JAVA_HOME + "/../sh", javaCommand);
89          }
90          else if (SystemUtils.IS_OS_MAC_OSX ) {
91              javaExe = new File(SystemUtils.JAVA_HOME + "/bin", javaCommand);
92          }
93          else {
94              javaExe = new File(SystemUtils.JAVA_HOME + "/../bin", javaCommand);
95          }
96  
97          log.debug("Java executable=[" + javaExe.getAbsolutePath() + "]");
98  
99          return javaExe.getAbsolutePath();
100     }
101 
102     protected void doExecute() throws Exception {
103         Commandline cmd = new Commandline();
104 
105         cmd.setWorkingDirectory(workingDirectory.getAbsolutePath());
106         cmd.setExecutable(getJavaPath());
107 
108         if (debugPort != null) {
109             cmd.createArgument().setValue("-Xdebug");
110             cmd.createArgument().setValue("-Xnoagent");
111             cmd.createArgument().setValue("-Djava.compiler=NONE");
112             cmd.createArgument().setValue("-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=" + debugPort);
113         }
114 
115         for (StringTokenizer st = new StringTokenizer(this.vmArgs); st.hasMoreTokens();) {
116             cmd.createArgument().setValue(st.nextToken());
117         }
118 
119         cmd.createArgument().setValue("-ea");
120         cmd.createArgument().setValue("-jar");
121 
122         File serverJar = new File(new File(geronimoTarget, "bin"), "server.jar");
123         if (serverJar.exists()) {
124             cmd.createArgument().setFile(serverJar);
125         }
126         else {
127             throw new FileNotFoundException(serverJar.getAbsolutePath());
128         }
129 
130         cmd.createArgument().setValue("--quiet");
131 
132         if (this.configs != null && this.configs.length > 0) {
133             cmd.createArgument().setValue("--override");
134 
135             for (int i=0; i < this.configs.length; i++) {
136                 cmd.createArgument().setValue(this.configs[i]);
137             }
138         }
139 
140         if (log.isDebugEnabled()) {
141             log.debug(Commandline.toString(cmd.getCommandline()));
142         }
143 
144         CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
145         try {
146             int exitCode = CommandLineUtils.executeCommandLine(cmd, new DefaultConsumer(), err);
147 
148             if (exitCode != 0) {
149                 throw new MojoExecutionException("Exit code: " + exitCode + " - " + err.getOutput());
150             }
151         }
152         catch (CommandLineException e) {
153             throw new MojoExecutionException("Unable to execute java command", e);
154         }
155 
156         ServerBehavior sb = new ServerBehavior(getUri(), getMaxTries(), getRetryIntervalMilliseconds());
157         if (!sb.isFullyStarted()) {
158             CommandLineUtils.killProcess(cmd.getPid());
159 
160             throw new MojoExecutionException("Server did not start");
161         }
162     }
163 }