mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 21 Dec 2012 09:18:52 +0100
branchlauncher
changeset 361 98eb1066dab1
parent 305 503b158cc093
child 372 3485327d3080
permissions -rw-r--r--
Print out the output of launched brwsr
     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.FileWriter;
    24 import java.io.IOException;
    25 import java.net.MalformedURLException;
    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 org.apache.maven.artifact.Artifact;
    32 import org.apache.maven.plugin.MojoExecutionException;
    33 import org.apache.maven.plugins.annotations.LifecyclePhase;
    34 import org.apache.maven.plugins.annotations.Mojo;
    35 import org.apache.maven.plugins.annotations.Parameter;
    36 import org.apache.maven.project.MavenProject;
    37 import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
    38 import org.apidesign.vm4brwsr.Bck2Brwsr;
    39 
    40 /** Executes given HTML page in a browser. */
    41 @Mojo(name="brwsr", defaultPhase=LifecyclePhase.DEPLOY)
    42 public class BrswrMojo extends AbstractMojo {
    43     public BrswrMojo() {
    44     }
    45     /** Resource to show as initial page */
    46     @Parameter
    47     private String startpage;
    48     
    49     @Parameter(defaultValue="${project}")
    50     private MavenProject prj;
    51     
    52     /** Root of the class files */
    53     @Parameter(defaultValue="${project.build.directory}/classes")
    54     private File classes;
    55 
    56     @Override
    57     public void execute() throws MojoExecutionException {
    58         if (startpage == null) {
    59             throw new MojoExecutionException("You have to provide a start page");
    60         }
    61 
    62         try {
    63             URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    64             
    65             Bck2BrwsrLauncher httpServer = new Bck2BrwsrLauncher();
    66             httpServer.setStartPage(startpage);
    67             httpServer.addClassLoader(url);
    68             httpServer.execute();
    69             
    70             System.in.read();
    71         } catch (IOException ex) {
    72             throw new MojoExecutionException("Can't show the browser", ex);
    73         }
    74     }
    75 
    76     private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    77         if (!dir.isDirectory()) {
    78             throw new MojoExecutionException("Not a directory " + dir);
    79         }
    80         File[] arr = dir.listFiles();
    81         if (arr.length == 1 && arr[0].isDirectory()) {
    82             return findNonEmptyFolder(arr[0]);
    83         }
    84         return dir;
    85     }
    86 
    87     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    88         File[] files = toCheck.listFiles();
    89         if (files != null) {
    90             long newest = 0L;
    91             for (File f : files) {
    92                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
    93                 if (newest < lastModified) {
    94                     newest = lastModified;
    95                 }
    96             }
    97             return newest;
    98         } else if (toCheck.getName().endsWith(".class")) {
    99             arr.add(prefix.substring(0, prefix.length() - 7));
   100             return toCheck.lastModified();
   101         } else {
   102             return 0L;
   103         }
   104     }
   105 
   106     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   107         List<URL> arr = new ArrayList<URL>();
   108         arr.add(root.toURI().toURL());
   109         for (Artifact a : deps) {
   110             arr.add(a.getFile().toURI().toURL());
   111         }
   112         return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
   113     }
   114 }