KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > selenium > StartServerMojo


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */

19
20 package org.apache.geronimo.mavenplugins.selenium;
21
22 import java.io.File JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.FileWriter JavaDoc;
26
27 import java.net.URL JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugin.MojoExecutionException;
35
36 import org.apache.geronimo.genesis.ant.AntMojoSupport;
37 import org.apache.geronimo.genesis.util.ObjectHolder;
38
39 import org.apache.commons.io.IOUtils;
40
41 import org.apache.tools.ant.taskdefs.Java;
42 import org.apache.tools.ant.types.Environment;
43 import org.apache.tools.ant.types.Path;
44
45 import org.codehaus.plexus.util.FileUtils;
46
47 /**
48  * Start the Selenium server.
49  *
50  * @goal start-server
51  *
52  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
53  */

54 public class StartServerMojo
55     extends AntMojoSupport
56 {
57     /**
58      * The port number the server will use.
59      *
60      * @parameter expression="${port}" default-value="4444"
61      */

62     private int port = -1;
63
64     /**
65      * Timeout for the server in seconds.
66      *
67      * @parameter expression="${timeout}" default-value="-1"
68      */

69     private int timeout = -1;
70
71     /**
72      * Enable the server's debug mode..
73      *
74      * @parameter expression="${debug}" default-value="false"
75      */

76     private boolean debug = false;
77
78     /**
79      * The file or resource to use for default user-extentions.js.
80      *
81      * @parameter default-value="org/apache/geronimo/mavenplugins/selenium/default-user-extentions.js"
82      */

83     private String JavaDoc defaultUserExtensions = null;
84
85     /**
86      * Enable or disable default user-extentions.js
87      *
88      * @parameter default-value="true"
89      */

90     private boolean defaultUserExtensionsEnabled = true;
91
92     /**
93      * Location of the user-extentions.js to load into the server.
94      * If defaultUserExtensionsEnabled is true, then this file will be appended to the defaults.
95      *
96      * @parameter
97      */

98     private String JavaDoc userExtensions = null;
99
100     /**
101      * Map of of plugin artifacts.
102      *
103      * @parameter expression="${plugin.artifactMap}"
104      * @required
105      * @readonly
106      */

107     private Map JavaDoc pluginArtifactMap = null;
108
109     /**
110      * Working directory where Selenium server will be started from.
111      *
112      * @parameter expression="${project.build.directory}/selenium"
113      * @required
114      */

115     private File JavaDoc workingDirectory = null;
116     
117     /**
118      * Enable logging mode.
119      *
120      * @parameter expression="${logOutput}" default-value="false"
121      */

122     protected boolean logOutput = false;
123     
124     /**
125      * The file that Selenium server logs will be written to.
126      *
127      * @parameter expression="${logFile}" default-value="${project.build.directory}/selenium/server.log"
128      * @required
129      */

130     private File JavaDoc logFile = null;
131
132     /**
133      * Flag to control if we background the server or block Maven execution.
134      *
135      * @parameter default-value="false"
136      * @required
137      */

138     private boolean background = false;
139
140     //
141
// MojoSupport Hooks
142
//
143

144     /**
145      * The maven project.
146      *
147      * @parameter expression="${project}"
148      * @required
149      * @readonly
150      */

151     private MavenProject project = null;
152
153     protected MavenProject getProject() {
154         return project;
155     }
156
157     //
158
// Mojo
159
//
160

161     private File JavaDoc getPluginArchive() {
162         String JavaDoc path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
163         return new File JavaDoc(path);
164     }
165
166     private Artifact getPluginArtifact(final String JavaDoc name) throws MojoExecutionException {
167         Artifact artifact = (Artifact)pluginArtifactMap.get(name);
168         if (artifact == null) {
169             throw new MojoExecutionException("Unable to locate '" + name + "' in the list of plugin artifacts");
170         }
171
172         return artifact;
173     }
174
175     protected void doExecute() throws Exception JavaDoc {
176         log.info("Starting Selenium server...");
177
178         final Java java = (Java)createTask("java");
179         
180         FileUtils.forceMkdir(workingDirectory);
181         
182         java.setFork(true);
183         java.setDir(workingDirectory);
184         java.setFailonerror(true);
185         
186         if (logOutput) {
187             FileUtils.forceMkdir(logFile.getParentFile());
188
189             log.info("Redirecting output to: " + logFile);
190             
191             java.setOutput(logFile);
192         }
193         
194         java.setClassname("org.openqa.selenium.server.SeleniumServer");
195
196         Path classpath = java.createClasspath();
197         classpath.createPathElement().setLocation(getPluginArchive());
198         classpath.createPathElement().setLocation(getPluginArtifact("log4j:log4j").getFile());
199         classpath.createPathElement().setLocation(getPluginArtifact("org.openqa.selenium.server:selenium-server").getFile());
200
201         Environment.Variable var;
202
203         var = new Environment.Variable();
204         var.setKey("selenium.log");
205         var.setFile(logFile);
206         java.addSysproperty(var);
207
208         var = new Environment.Variable();
209         var.setKey("selenium.loglevel");
210         var.setValue(debug == true ? "DEBUG" : "INFO");
211         java.addSysproperty(var);
212
213         var = new Environment.Variable();
214         var.setKey("log4j.configuration");
215         var.setValue("org/apache/geronimo/mavenplugins/selenium/log4j.properties");
216         java.addSysproperty(var);
217
218         // Server arguments
219

220         java.createArg().setValue("-port");
221         java.createArg().setValue(String.valueOf(port));
222
223         if (debug) {
224             java.createArg().setValue("-debug");
225         }
226
227         if (timeout > 0) {
228             log.info("Timeout after: " + timeout + " seconds");
229
230             java.createArg().setValue("-timeout");
231             java.createArg().setValue(String.valueOf(timeout));
232         }
233
234         File JavaDoc userExtentionsFile = getUserExtentionsFile();
235         if (userExtentionsFile != null) {
236             log.info("User extensions: " + userExtentionsFile);
237
238             java.createArg().setValue("-userExtensions");
239             java.createArg().setFile(userExtentionsFile);
240         }
241
242         // Holds any exception that was thrown during startup
243
final ObjectHolder errorHolder = new ObjectHolder();
244
245         // Start the server int a seperate thread
246
Thread JavaDoc t = new Thread JavaDoc("Selenium Server Runner") {
247             public void run() {
248                 try {
249                     java.execute();
250                 }
251                 catch (Exception JavaDoc e) {
252                     errorHolder.set(e);
253
254                     //
255
// NOTE: Don't log here, as when the JVM exists an exception will get thrown by Ant
256
// but that should be fine.
257
//
258
}
259             }
260         };
261         t.start();
262
263         log.debug("Waiting for Selenium server...");
264
265         // Verify server started
266
URL JavaDoc url = new URL JavaDoc("http://localhost:" + port + "/selenium-server");
267         boolean started = false;
268         while (!started) {
269             if (errorHolder.isSet()) {
270                 throw new MojoExecutionException("Failed to start Selenium server", (Throwable JavaDoc)errorHolder.get());
271             }
272
273             log.debug("Trying connection to: " + url);
274
275             try {
276                 Object JavaDoc input = url.openConnection().getContent();
277                 started = true;
278             }
279             catch (Exception JavaDoc e) {
280                 // ignore
281
}
282
283             Thread.sleep(1000);
284         }
285
286         log.info("Selenium server started");
287
288         if (!background) {
289             log.info("Waiting for Selenium to shutdown...");
290
291             t.join();
292         }
293     }
294
295     /**
296      * Resolve a resource to a file, URL or resource.
297      */

298     private URL JavaDoc resolveResource(final String JavaDoc name) throws MalformedURLException JavaDoc, MojoFailureException {
299         assert name != null;
300
301         URL JavaDoc url;
302
303         File JavaDoc file = new File JavaDoc(name);
304         if (file.exists()) {
305             url = file.toURL();
306         }
307         else {
308             try {
309                 url = new URL JavaDoc(name);
310             }
311             catch (MalformedURLException JavaDoc e) {
312                 url = Thread.currentThread().getContextClassLoader().getResource(name);
313             }
314         }
315
316         if (url == null) {
317             throw new MojoFailureException("Could not resolve resource: " + name);
318         }
319
320         log.debug("Resolved resource '" + name + "' as: " + url);
321
322         return url;
323     }
324
325     /**
326      * Retutn the user-extentions.js file to use, or null if it should not be installed.
327      */

328     private File JavaDoc getUserExtentionsFile() throws Exception JavaDoc {
329         if (!defaultUserExtensionsEnabled && userExtensions == null) {
330             return null;
331         }
332
333         // File needs to be named 'user-extensions.js' or Selenium server will puke
334
File JavaDoc file = new File JavaDoc(workingDirectory, "user-extensions.js");
335         if (file.exists()) {
336             log.debug("Reusing previously generated file: " + file);
337
338             return file;
339         }
340
341         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(file)));
342
343         if (defaultUserExtensionsEnabled) {
344             URL JavaDoc url = resolveResource(defaultUserExtensions);
345             log.debug("Using defaults: " + url);
346
347             writer.println("//");
348             writer.println("// Default user extentions; from: " + url);
349             writer.println("//");
350
351             IOUtils.copy(url.openStream(), writer);
352         }
353
354         if (userExtensions != null) {
355             URL JavaDoc url = resolveResource(userExtensions);
356             log.debug("Using user extentions: " + url);
357
358             writer.println("//");
359             writer.println("// User extentions; from: " + url);
360             writer.println("//");
361
362             IOUtils.copy(url.openStream(), writer);
363         }
364
365         writer.flush();
366         writer.close();
367
368         return file;
369     }
370 }
371
Popular Tags