rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Dec 2015 08:22:59 +0100
changeset 1850 23babc7d5bb8
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Can tell the server to exit
jaroslav@111
     1
/**
jaroslav@111
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@111
     4
 *
jaroslav@111
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@111
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@111
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@111
     8
 *
jaroslav@111
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@111
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@111
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@111
    12
 * GNU General Public License for more details.
jaroslav@111
    13
 *
jaroslav@111
    14
 * You should have received a copy of the GNU General Public License
jaroslav@111
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@111
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@111
    17
 */
jaroslav@109
    18
package org.apidesign.bck2brwsr.mojo;
jaroslav@109
    19
jaroslav@382
    20
import java.io.Closeable;
jaroslav@109
    21
import org.apache.maven.plugin.AbstractMojo;
jaroslav@109
    22
jaroslav@109
    23
import java.io.File;
jaroslav@1850
    24
import java.io.Flushable;
jaroslav@305
    25
import java.io.IOException;
jaroslav@109
    26
import java.net.MalformedURLException;
jaroslav@109
    27
import java.net.URL;
jaroslav@109
    28
import java.net.URLClassLoader;
jaroslav@109
    29
import java.util.ArrayList;
jaroslav@109
    30
import java.util.Collection;
jaroslav@109
    31
import java.util.List;
jaroslav@109
    32
import org.apache.maven.artifact.Artifact;
jaroslav@1173
    33
import org.apache.maven.model.Resource;
jaroslav@109
    34
import org.apache.maven.plugin.MojoExecutionException;
jaroslav@109
    35
import org.apache.maven.plugins.annotations.LifecyclePhase;
jaroslav@109
    36
import org.apache.maven.plugins.annotations.Mojo;
jaroslav@109
    37
import org.apache.maven.plugins.annotations.Parameter;
jaroslav@1164
    38
import org.apache.maven.plugins.annotations.ResolutionScope;
jaroslav@109
    39
import org.apache.maven.project.MavenProject;
jaroslav@382
    40
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@109
    41
jaroslav@357
    42
/** Executes given HTML page in a browser. */
jaroslav@1164
    43
@Mojo(
jaroslav@1615
    44
    name="show",
jaroslav@1164
    45
    requiresDependencyResolution = ResolutionScope.RUNTIME,
jaroslav@1164
    46
    defaultPhase=LifecyclePhase.NONE
jaroslav@1164
    47
)
jaroslav@1615
    48
public class ShowMojo extends AbstractMojo {
jaroslav@1615
    49
    public ShowMojo() {
jaroslav@109
    50
    }
jaroslav@1043
    51
    
jaroslav@1043
    52
    /** The identification of a launcher to use. Known values <code>fxbrwsr</code>, 
jaroslav@1043
    53
     * <code>bck2brwsr</code>, or 
jaroslav@1043
    54
     * name of an external process to execute.
jaroslav@1043
    55
     */
jaroslav@1043
    56
    @Parameter
jaroslav@1043
    57
    private String launcher;
jaroslav@1043
    58
    
jaroslav@1043
    59
    
jaroslav@357
    60
    /** Resource to show as initial page */
jaroslav@109
    61
    @Parameter
jaroslav@357
    62
    private String startpage;
jaroslav@421
    63
jaroslav@613
    64
    /** Root of all pages, and files, etc. */
jaroslav@613
    65
    @Parameter
jaroslav@613
    66
    private File directory;
jaroslav@1371
    67
    
jaroslav@109
    68
    @Override
jaroslav@109
    69
    public void execute() throws MojoExecutionException {
jaroslav@357
    70
        if (startpage == null) {
jaroslav@357
    71
            throw new MojoExecutionException("You have to provide a start page");
jaroslav@134
    72
        }
jaroslav@1615
    73
        if (directory == null) {
jaroslav@1615
    74
            throw new MojoExecutionException("You have to provide a root directory");
jaroslav@1371
    75
        }
jaroslav@109
    76
        try {
jaroslav@382
    77
            Closeable httpServer;
jaroslav@1615
    78
            httpServer = Launcher.showDir(launcher, directory, null, startpage);
jaroslav@1850
    79
            if (httpServer instanceof Flushable) {
jaroslav@1850
    80
                ((Flushable)httpServer).flush();
jaroslav@1850
    81
            } else {
jaroslav@1850
    82
                System.in.read();
jaroslav@1850
    83
            }
jaroslav@382
    84
            httpServer.close();
jaroslav@305
    85
        } catch (IOException ex) {
jaroslav@357
    86
            throw new MojoExecutionException("Can't show the browser", ex);
jaroslav@109
    87
        }
jaroslav@109
    88
    }
jaroslav@109
    89
}