jaroslav@111: /** jaroslav@111: * Back 2 Browser Bytecode Translator jaroslav@111: * Copyright (C) 2012 Jaroslav Tulach jaroslav@111: * jaroslav@111: * This program is free software: you can redistribute it and/or modify jaroslav@111: * it under the terms of the GNU General Public License as published by jaroslav@111: * the Free Software Foundation, version 2 of the License. jaroslav@111: * jaroslav@111: * This program is distributed in the hope that it will be useful, jaroslav@111: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@111: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@111: * GNU General Public License for more details. jaroslav@111: * jaroslav@111: * You should have received a copy of the GNU General Public License jaroslav@111: * along with this program. Look for COPYING file in the top folder. jaroslav@111: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@111: */ jaroslav@109: package org.apidesign.bck2brwsr.mojo; jaroslav@109: jaroslav@382: import java.io.Closeable; jaroslav@109: import org.apache.maven.plugin.AbstractMojo; jaroslav@109: jaroslav@109: import java.io.File; jaroslav@305: import java.io.IOException; jaroslav@109: import java.net.MalformedURLException; jaroslav@109: import java.net.URL; jaroslav@109: import java.net.URLClassLoader; jaroslav@109: import java.util.ArrayList; jaroslav@109: import java.util.Collection; jaroslav@109: import java.util.List; jaroslav@109: import org.apache.maven.artifact.Artifact; jaroslav@1173: import org.apache.maven.model.Resource; jaroslav@109: import org.apache.maven.plugin.MojoExecutionException; jaroslav@109: import org.apache.maven.plugins.annotations.LifecyclePhase; jaroslav@109: import org.apache.maven.plugins.annotations.Mojo; jaroslav@109: import org.apache.maven.plugins.annotations.Parameter; jaroslav@1164: import org.apache.maven.plugins.annotations.ResolutionScope; jaroslav@109: import org.apache.maven.project.MavenProject; jaroslav@382: import org.apidesign.bck2brwsr.launcher.Launcher; jaroslav@109: jaroslav@357: /** Executes given HTML page in a browser. */ jaroslav@1164: @Mojo( jaroslav@1164: name="brwsr", jaroslav@1164: requiresDependencyResolution = ResolutionScope.RUNTIME, jaroslav@1164: defaultPhase=LifecyclePhase.NONE jaroslav@1164: ) jaroslav@1047: public class BrwsrMojo extends AbstractMojo { jaroslav@1047: public BrwsrMojo() { jaroslav@109: } jaroslav@1043: jaroslav@1043: /** The identification of a launcher to use. Known values fxbrwsr, jaroslav@1043: * bck2brwsr, or jaroslav@1043: * name of an external process to execute. jaroslav@1043: */ jaroslav@1043: @Parameter jaroslav@1043: private String launcher; jaroslav@1043: jaroslav@1043: jaroslav@357: /** Resource to show as initial page */ jaroslav@109: @Parameter jaroslav@357: private String startpage; jaroslav@421: jaroslav@109: @Parameter(defaultValue="${project}") jaroslav@109: private MavenProject prj; jaroslav@109: jaroslav@357: /** Root of the class files */ jaroslav@357: @Parameter(defaultValue="${project.build.directory}/classes") jaroslav@357: private File classes; jaroslav@613: jaroslav@613: /** Root of all pages, and files, etc. */ jaroslav@613: @Parameter jaroslav@613: private File directory; jaroslav@109: jaroslav@109: @Override jaroslav@109: public void execute() throws MojoExecutionException { jaroslav@357: if (startpage == null) { jaroslav@357: throw new MojoExecutionException("You have to provide a start page"); jaroslav@134: } jaroslav@109: try { jaroslav@382: Closeable httpServer; jaroslav@613: if (directory != null) { jaroslav@1271: URLClassLoader url = buildClassLoader(classes, prj.getArtifacts()); jaroslav@1271: httpServer = Launcher.showDir(launcher, directory, url, startpage); jaroslav@613: } else { jaroslav@1164: URLClassLoader url = buildClassLoader(classes, prj.getArtifacts()); jaroslav@613: try { jaroslav@1173: for (Resource r : prj.getResources()) { jaroslav@1173: File f = new File(r.getDirectory(), startpage().replace('/', File.separatorChar)); jaroslav@1173: if (f.exists()) { jaroslav@1173: System.setProperty("startpage.file", f.getPath()); jaroslav@1173: } jaroslav@1173: } jaroslav@1173: jaroslav@1043: httpServer = Launcher.showURL(launcher, url, startpage()); jaroslav@613: } catch (Exception ex) { jaroslav@613: throw new MojoExecutionException("Can't open " + startpage(), ex); jaroslav@613: } jaroslav@372: } jaroslav@357: System.in.read(); jaroslav@382: httpServer.close(); jaroslav@305: } catch (IOException ex) { jaroslav@357: throw new MojoExecutionException("Can't show the browser", ex); jaroslav@109: } jaroslav@109: } jaroslav@421: jaroslav@421: private String startpage() { jaroslav@422: return startpage; jaroslav@109: } jaroslav@109: jaroslav@109: private static URLClassLoader buildClassLoader(File root, Collection deps) throws MalformedURLException { jaroslav@109: List arr = new ArrayList(); jaroslav@109: arr.add(root.toURI().toURL()); jaroslav@109: for (Artifact a : deps) { jaroslav@515: final File f = a.getFile(); jaroslav@515: if (f != null) { jaroslav@515: arr.add(f.toURI().toURL()); jaroslav@515: } jaroslav@109: } jaroslav@1047: return new URLClassLoader(arr.toArray(new URL[0]), BrwsrMojo.class.getClassLoader()); jaroslav@109: } jaroslav@109: }