1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,99 @@
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 BrswrMojo extends AbstractMojo {
1.45 + public BrswrMojo() {
1.46 + }
1.47 + /** Resource to show as initial page */
1.48 + @Parameter
1.49 + private String startpage;
1.50 +
1.51 + @Parameter(defaultValue="${project}")
1.52 + private MavenProject prj;
1.53 +
1.54 + /** Root of the class files */
1.55 + @Parameter(defaultValue="${project.build.directory}/classes")
1.56 + private File classes;
1.57 +
1.58 + /** Root of all pages, and files, etc. */
1.59 + @Parameter
1.60 + private File directory;
1.61 +
1.62 + @Override
1.63 + public void execute() throws MojoExecutionException {
1.64 + if (startpage == null) {
1.65 + throw new MojoExecutionException("You have to provide a start page");
1.66 + }
1.67 +
1.68 + try {
1.69 + Closeable httpServer;
1.70 + if (directory != null) {
1.71 + httpServer = Launcher.showDir(directory, startpage);
1.72 + } else {
1.73 + URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
1.74 + try {
1.75 + httpServer = Launcher.showURL(url, startpage());
1.76 + } catch (Exception ex) {
1.77 + throw new MojoExecutionException("Can't open " + startpage(), ex);
1.78 + }
1.79 + }
1.80 + System.in.read();
1.81 + httpServer.close();
1.82 + } catch (IOException ex) {
1.83 + throw new MojoExecutionException("Can't show the browser", ex);
1.84 + }
1.85 + }
1.86 +
1.87 + private String startpage() {
1.88 + return startpage;
1.89 + }
1.90 +
1.91 + private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
1.92 + List<URL> arr = new ArrayList<URL>();
1.93 + arr.add(root.toURI().toURL());
1.94 + for (Artifact a : deps) {
1.95 + final File f = a.getFile();
1.96 + if (f != null) {
1.97 + arr.add(f.toURI().toURL());
1.98 + }
1.99 + }
1.100 + return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
1.101 + }
1.102 +}