mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java
branchlauncher
changeset 357 dc375a56fd15
parent 305 503b158cc093
child 372 3485327d3080
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java	Thu Dec 20 16:17:31 2012 +0100
     1.3 @@ -0,0 +1,114 @@
     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 org.apache.maven.plugin.AbstractMojo;
    1.24 +
    1.25 +import java.io.File;
    1.26 +import java.io.FileWriter;
    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.Bck2BrwsrLauncher;
    1.41 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.42 +
    1.43 +/** Executes given HTML page in a browser. */
    1.44 +@Mojo(name="brwsr", defaultPhase=LifecyclePhase.DEPLOY)
    1.45 +public class BrswrMojo extends AbstractMojo {
    1.46 +    public BrswrMojo() {
    1.47 +    }
    1.48 +    /** Resource to show as initial page */
    1.49 +    @Parameter
    1.50 +    private String startpage;
    1.51 +    
    1.52 +    @Parameter(defaultValue="${project}")
    1.53 +    private MavenProject prj;
    1.54 +    
    1.55 +    /** Root of the class files */
    1.56 +    @Parameter(defaultValue="${project.build.directory}/classes")
    1.57 +    private File classes;
    1.58 +
    1.59 +    @Override
    1.60 +    public void execute() throws MojoExecutionException {
    1.61 +        if (startpage == null) {
    1.62 +            throw new MojoExecutionException("You have to provide a start page");
    1.63 +        }
    1.64 +
    1.65 +        try {
    1.66 +            URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    1.67 +            
    1.68 +            Bck2BrwsrLauncher httpServer = new Bck2BrwsrLauncher();
    1.69 +            httpServer.setStartPage(startpage);
    1.70 +            httpServer.addClassLoader(url);
    1.71 +            httpServer.execute();
    1.72 +            
    1.73 +            System.in.read();
    1.74 +        } catch (IOException ex) {
    1.75 +            throw new MojoExecutionException("Can't show the browser", ex);
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    1.80 +        if (!dir.isDirectory()) {
    1.81 +            throw new MojoExecutionException("Not a directory " + dir);
    1.82 +        }
    1.83 +        File[] arr = dir.listFiles();
    1.84 +        if (arr.length == 1 && arr[0].isDirectory()) {
    1.85 +            return findNonEmptyFolder(arr[0]);
    1.86 +        }
    1.87 +        return dir;
    1.88 +    }
    1.89 +
    1.90 +    private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    1.91 +        File[] files = toCheck.listFiles();
    1.92 +        if (files != null) {
    1.93 +            long newest = 0L;
    1.94 +            for (File f : files) {
    1.95 +                long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
    1.96 +                if (newest < lastModified) {
    1.97 +                    newest = lastModified;
    1.98 +                }
    1.99 +            }
   1.100 +            return newest;
   1.101 +        } else if (toCheck.getName().endsWith(".class")) {
   1.102 +            arr.add(prefix.substring(0, prefix.length() - 7));
   1.103 +            return toCheck.lastModified();
   1.104 +        } else {
   1.105 +            return 0L;
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   1.110 +        List<URL> arr = new ArrayList<URL>();
   1.111 +        arr.add(root.toURI().toURL());
   1.112 +        for (Artifact a : deps) {
   1.113 +            arr.add(a.getFile().toURI().toURL());
   1.114 +        }
   1.115 +        return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
   1.116 +    }
   1.117 +}