rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 11 Sep 2015 14:51:09 +0200
branchflow
changeset 1842 dd4dabfead82
parent 1371 fd2d4ca28bd3
permissions -rw-r--r--
Giving the flow analyzer chance to generate the whole function body
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.model.Resource;
    33 import org.apache.maven.plugin.MojoExecutionException;
    34 import org.apache.maven.plugins.annotations.LifecyclePhase;
    35 import org.apache.maven.plugins.annotations.Mojo;
    36 import org.apache.maven.plugins.annotations.Parameter;
    37 import org.apache.maven.plugins.annotations.ResolutionScope;
    38 import org.apache.maven.project.MavenProject;
    39 import org.apidesign.bck2brwsr.launcher.Launcher;
    40 
    41 /** Executes given HTML page in a browser. */
    42 @Mojo(
    43     name="brwsr",
    44     requiresDependencyResolution = ResolutionScope.RUNTIME,
    45     defaultPhase=LifecyclePhase.NONE
    46 )
    47 public class BrwsrMojo extends AbstractMojo {
    48     public BrwsrMojo() {
    49     }
    50     
    51     /** The identification of a launcher to use. Known values <code>fxbrwsr</code>, 
    52      * <code>bck2brwsr</code>, or 
    53      * name of an external process to execute.
    54      */
    55     @Parameter
    56     private String launcher;
    57     
    58     
    59     /** Resource to show as initial page */
    60     @Parameter
    61     private String startpage;
    62 
    63     @Parameter(defaultValue="${project}")
    64     private MavenProject prj;
    65     
    66     /** Root of the class files */
    67     @Parameter(defaultValue="${project.build.directory}/classes")
    68     private File classes;
    69     
    70     /** Root of all pages, and files, etc. */
    71     @Parameter
    72     private File directory;
    73     
    74     @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
    75     private File javascript;
    76 
    77     @Override
    78     public void execute() throws MojoExecutionException {
    79         if (startpage == null) {
    80             throw new MojoExecutionException("You have to provide a start page");
    81         }
    82         if (javascript != null && javascript.isFile()) {
    83             System.setProperty("bck2brwsr.js", javascript.toURI().toString());
    84         }
    85         try {
    86             Closeable httpServer;
    87             if (directory != null) {
    88                 URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
    89                 httpServer = Launcher.showDir(launcher, directory, url, startpage);
    90             } else {
    91                 URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
    92                 try {
    93                     for (Resource r : prj.getResources()) {
    94                         File f = new File(r.getDirectory(), startpage().replace('/', File.separatorChar));
    95                         if (f.exists()) {
    96                             System.setProperty("startpage.file", f.getPath());
    97                         }
    98                     }
    99                     
   100                     httpServer = Launcher.showURL(launcher, url, startpage());
   101                 } catch (Exception ex) {
   102                     throw new MojoExecutionException("Can't open " + startpage(), ex);
   103                 }
   104             }
   105             System.in.read();
   106             httpServer.close();
   107         } catch (IOException ex) {
   108             throw new MojoExecutionException("Can't show the browser", ex);
   109         }
   110     }
   111     
   112     private String startpage() {
   113         return startpage;
   114     }
   115 
   116     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   117         List<URL> arr = new ArrayList<URL>();
   118         arr.add(root.toURI().toURL());
   119         for (Artifact a : deps) {
   120             final File f = a.getFile();
   121             if (f != null) {
   122                 arr.add(f.toURI().toURL());
   123             }
   124         }
   125         return new URLClassLoader(arr.toArray(new URL[0]), BrwsrMojo.class.getClassLoader());
   126     }
   127 }