Correcting a typo model
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 22:02:21 +0200
branchmodel
changeset 1047338e0ce0e2dd
parent 1046 88e1ec58f93a
child 1048 a4b4d500e7e6
Correcting a typo
rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java	Sun Apr 28 22:02:21 2013 +0200
     1.3 @@ -0,0 +1,108 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.mojo;
    1.22 +
    1.23 +import java.io.Closeable;
    1.24 +import org.apache.maven.plugin.AbstractMojo;
    1.25 +
    1.26 +import java.io.File;
    1.27 +import java.io.IOException;
    1.28 +import java.net.MalformedURLException;
    1.29 +import java.net.URL;
    1.30 +import java.net.URLClassLoader;
    1.31 +import java.util.ArrayList;
    1.32 +import java.util.Collection;
    1.33 +import java.util.List;
    1.34 +import org.apache.maven.artifact.Artifact;
    1.35 +import org.apache.maven.plugin.MojoExecutionException;
    1.36 +import org.apache.maven.plugins.annotations.LifecyclePhase;
    1.37 +import org.apache.maven.plugins.annotations.Mojo;
    1.38 +import org.apache.maven.plugins.annotations.Parameter;
    1.39 +import org.apache.maven.project.MavenProject;
    1.40 +import org.apidesign.bck2brwsr.launcher.Launcher;
    1.41 +
    1.42 +/** Executes given HTML page in a browser. */
    1.43 +@Mojo(name="brwsr", defaultPhase=LifecyclePhase.NONE)
    1.44 +public class BrwsrMojo extends AbstractMojo {
    1.45 +    public BrwsrMojo() {
    1.46 +    }
    1.47 +    
    1.48 +    /** The identification of a launcher to use. Known values <code>fxbrwsr</code>, 
    1.49 +     * <code>bck2brwsr</code>, or 
    1.50 +     * name of an external process to execute.
    1.51 +     */
    1.52 +    @Parameter
    1.53 +    private String launcher;
    1.54 +    
    1.55 +    
    1.56 +    /** Resource to show as initial page */
    1.57 +    @Parameter
    1.58 +    private String startpage;
    1.59 +
    1.60 +    @Parameter(defaultValue="${project}")
    1.61 +    private MavenProject prj;
    1.62 +    
    1.63 +    /** Root of the class files */
    1.64 +    @Parameter(defaultValue="${project.build.directory}/classes")
    1.65 +    private File classes;
    1.66 +    
    1.67 +    /** Root of all pages, and files, etc. */
    1.68 +    @Parameter
    1.69 +    private File directory;
    1.70 +
    1.71 +    @Override
    1.72 +    public void execute() throws MojoExecutionException {
    1.73 +        if (startpage == null) {
    1.74 +            throw new MojoExecutionException("You have to provide a start page");
    1.75 +        }
    1.76 +        
    1.77 +        try {
    1.78 +            Closeable httpServer;
    1.79 +            if (directory != null) {
    1.80 +                httpServer = Launcher.showDir(directory, startpage);
    1.81 +            } else {
    1.82 +                URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    1.83 +                try {
    1.84 +                    httpServer = Launcher.showURL(launcher, url, startpage());
    1.85 +                } catch (Exception ex) {
    1.86 +                    throw new MojoExecutionException("Can't open " + startpage(), ex);
    1.87 +                }
    1.88 +            }
    1.89 +            System.in.read();
    1.90 +            httpServer.close();
    1.91 +        } catch (IOException ex) {
    1.92 +            throw new MojoExecutionException("Can't show the browser", ex);
    1.93 +        }
    1.94 +    }
    1.95 +    
    1.96 +    private String startpage() {
    1.97 +        return startpage;
    1.98 +    }
    1.99 +
   1.100 +    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   1.101 +        List<URL> arr = new ArrayList<URL>();
   1.102 +        arr.add(root.toURI().toURL());
   1.103 +        for (Artifact a : deps) {
   1.104 +            final File f = a.getFile();
   1.105 +            if (f != null) {
   1.106 +                arr.add(f.toURI().toURL());
   1.107 +            }
   1.108 +        }
   1.109 +        return new URLClassLoader(arr.toArray(new URL[0]), BrwsrMojo.class.getClassLoader());
   1.110 +    }
   1.111 +}