rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     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.Flushable;
    25 import java.io.IOException;
    26 import java.net.MalformedURLException;
    27 import java.net.URL;
    28 import java.net.URLClassLoader;
    29 import java.util.ArrayList;
    30 import java.util.Collection;
    31 import java.util.List;
    32 import org.apache.maven.artifact.Artifact;
    33 import org.apache.maven.model.Resource;
    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.plugins.annotations.ResolutionScope;
    39 import org.apache.maven.project.MavenProject;
    40 import org.apidesign.bck2brwsr.launcher.Launcher;
    41 
    42 /** Executes given HTML page in a browser. */
    43 @Mojo(
    44     name="show",
    45     requiresDependencyResolution = ResolutionScope.RUNTIME,
    46     defaultPhase=LifecyclePhase.NONE
    47 )
    48 public class ShowMojo extends AbstractMojo {
    49     public ShowMojo() {
    50     }
    51     
    52     /** The identification of a launcher to use. Known values <code>fxbrwsr</code>, 
    53      * <code>bck2brwsr</code>, or 
    54      * name of an external process to execute.
    55      */
    56     @Parameter
    57     private String launcher;
    58     
    59     
    60     /** Resource to show as initial page */
    61     @Parameter
    62     private String startpage;
    63 
    64     /** Root of all pages, and files, etc. */
    65     @Parameter
    66     private File directory;
    67     
    68     @Override
    69     public void execute() throws MojoExecutionException {
    70         if (startpage == null) {
    71             throw new MojoExecutionException("You have to provide a start page");
    72         }
    73         if (directory == null) {
    74             throw new MojoExecutionException("You have to provide a root directory");
    75         }
    76         try {
    77             Closeable httpServer;
    78             httpServer = Launcher.showDir(launcher, directory, null, startpage);
    79             if (httpServer instanceof Flushable) {
    80                 ((Flushable)httpServer).flush();
    81             } else {
    82                 System.in.read();
    83             }
    84             httpServer.close();
    85         } catch (IOException ex) {
    86             throw new MojoExecutionException("Can't show the browser", ex);
    87         }
    88     }
    89 }