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