1 19 20 package org.apache.geronimo.mavenplugins.selenium; 21 22 import java.io.File ; 23 import java.io.PrintWriter ; 24 import java.io.BufferedWriter ; 25 import java.io.FileWriter ; 26 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 import java.util.Map ; 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 54 public class StartServerMojo 55 extends AntMojoSupport 56 { 57 62 private int port = -1; 63 64 69 private int timeout = -1; 70 71 76 private boolean debug = false; 77 78 83 private String defaultUserExtensions = null; 84 85 90 private boolean defaultUserExtensionsEnabled = true; 91 92 98 private String userExtensions = null; 99 100 107 private Map pluginArtifactMap = null; 108 109 115 private File workingDirectory = null; 116 117 122 protected boolean logOutput = false; 123 124 130 private File logFile = null; 131 132 138 private boolean background = false; 139 140 144 151 private MavenProject project = null; 152 153 protected MavenProject getProject() { 154 return project; 155 } 156 157 161 private File getPluginArchive() { 162 String path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); 163 return new File (path); 164 } 165 166 private Artifact getPluginArtifact(final String 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 { 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 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 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 final ObjectHolder errorHolder = new ObjectHolder(); 244 245 Thread t = new Thread ("Selenium Server Runner") { 247 public void run() { 248 try { 249 java.execute(); 250 } 251 catch (Exception e) { 252 errorHolder.set(e); 253 254 } 259 } 260 }; 261 t.start(); 262 263 log.debug("Waiting for Selenium server..."); 264 265 URL url = new URL ("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 )errorHolder.get()); 271 } 272 273 log.debug("Trying connection to: " + url); 274 275 try { 276 Object input = url.openConnection().getContent(); 277 started = true; 278 } 279 catch (Exception e) { 280 } 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 298 private URL resolveResource(final String name) throws MalformedURLException , MojoFailureException { 299 assert name != null; 300 301 URL url; 302 303 File file = new File (name); 304 if (file.exists()) { 305 url = file.toURL(); 306 } 307 else { 308 try { 309 url = new URL (name); 310 } 311 catch (MalformedURLException 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 328 private File getUserExtentionsFile() throws Exception { 329 if (!defaultUserExtensionsEnabled && userExtensions == null) { 330 return null; 331 } 332 333 File file = new File (workingDirectory, "user-extensions.js"); 335 if (file.exists()) { 336 log.debug("Reusing previously generated file: " + file); 337 338 return file; 339 } 340 341 PrintWriter writer = new PrintWriter (new BufferedWriter (new FileWriter (file))); 342 343 if (defaultUserExtensionsEnabled) { 344 URL 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 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 |