mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 23 Dec 2012 23:30:06 +0100
branchlauncher
changeset 372 3485327d3080
parent 357 dc375a56fd15
child 382 57fc3a0563c9
permissions -rw-r--r--
Execution time reported by TestNG reflect the time it took to run the query inside of a browser
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.mojo;
    19 
    20 import org.apache.maven.plugin.AbstractMojo;
    21 
    22 import java.io.File;
    23 import java.io.IOException;
    24 import java.net.MalformedURLException;
    25 import java.net.URISyntaxException;
    26 import java.net.URL;
    27 import java.net.URLClassLoader;
    28 import java.util.ArrayList;
    29 import java.util.Collection;
    30 import java.util.List;
    31 import java.util.logging.Level;
    32 import java.util.logging.Logger;
    33 import org.apache.maven.artifact.Artifact;
    34 import org.apache.maven.plugin.MojoExecutionException;
    35 import org.apache.maven.plugins.annotations.LifecyclePhase;
    36 import org.apache.maven.plugins.annotations.Mojo;
    37 import org.apache.maven.plugins.annotations.Parameter;
    38 import org.apache.maven.project.MavenProject;
    39 import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
    40 
    41 /** Executes given HTML page in a browser. */
    42 @Mojo(name="brwsr", defaultPhase=LifecyclePhase.DEPLOY)
    43 public class BrswrMojo extends AbstractMojo {
    44     public BrswrMojo() {
    45     }
    46     /** Resource to show as initial page */
    47     @Parameter
    48     private String startpage;
    49     
    50     @Parameter(defaultValue="${project}")
    51     private MavenProject prj;
    52     
    53     /** Root of the class files */
    54     @Parameter(defaultValue="${project.build.directory}/classes")
    55     private File classes;
    56 
    57     @Override
    58     public void execute() throws MojoExecutionException {
    59         if (startpage == null) {
    60             throw new MojoExecutionException("You have to provide a start page");
    61         }
    62 
    63         try {
    64             URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    65             
    66             Bck2BrwsrLauncher httpServer = new Bck2BrwsrLauncher();
    67             httpServer.addClassLoader(url);
    68             try {
    69                 httpServer.showURL(startpage);
    70             } catch (Exception ex) {
    71                 throw new MojoExecutionException("Can't open " + startpage, ex);
    72             }
    73             
    74             System.in.read();
    75         } catch (IOException ex) {
    76             throw new MojoExecutionException("Can't show the browser", ex);
    77         }
    78     }
    79 
    80     private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    81         if (!dir.isDirectory()) {
    82             throw new MojoExecutionException("Not a directory " + dir);
    83         }
    84         File[] arr = dir.listFiles();
    85         if (arr.length == 1 && arr[0].isDirectory()) {
    86             return findNonEmptyFolder(arr[0]);
    87         }
    88         return dir;
    89     }
    90 
    91     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    92         File[] files = toCheck.listFiles();
    93         if (files != null) {
    94             long newest = 0L;
    95             for (File f : files) {
    96                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
    97                 if (newest < lastModified) {
    98                     newest = lastModified;
    99                 }
   100             }
   101             return newest;
   102         } else if (toCheck.getName().endsWith(".class")) {
   103             arr.add(prefix.substring(0, prefix.length() - 7));
   104             return toCheck.lastModified();
   105         } else {
   106             return 0L;
   107         }
   108     }
   109 
   110     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   111         List<URL> arr = new ArrayList<URL>();
   112         arr.add(root.toURI().toURL());
   113         for (Artifact a : deps) {
   114             arr.add(a.getFile().toURI().toURL());
   115         }
   116         return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
   117     }
   118 }