mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Dec 2012 07:57:33 +0100
changeset 374 e5b4bd29f268
parent 357 dc375a56fd15
child 382 57fc3a0563c9
permissions -rw-r--r--
Merging launcher branch to default - seems to work reliably well
jaroslav@111
     1
/**
jaroslav@111
     2
 * Back 2 Browser Bytecode Translator
jaroslav@111
     3
 * Copyright (C) 2012 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@109
    20
import org.apache.maven.plugin.AbstractMojo;
jaroslav@109
    21
jaroslav@109
    22
import java.io.File;
jaroslav@305
    23
import java.io.IOException;
jaroslav@109
    24
import java.net.MalformedURLException;
jaroslav@372
    25
import java.net.URISyntaxException;
jaroslav@109
    26
import java.net.URL;
jaroslav@109
    27
import java.net.URLClassLoader;
jaroslav@109
    28
import java.util.ArrayList;
jaroslav@109
    29
import java.util.Collection;
jaroslav@109
    30
import java.util.List;
jaroslav@372
    31
import java.util.logging.Level;
jaroslav@372
    32
import java.util.logging.Logger;
jaroslav@109
    33
import org.apache.maven.artifact.Artifact;
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@109
    38
import org.apache.maven.project.MavenProject;
jaroslav@357
    39
import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
jaroslav@109
    40
jaroslav@357
    41
/** Executes given HTML page in a browser. */
jaroslav@357
    42
@Mojo(name="brwsr", defaultPhase=LifecyclePhase.DEPLOY)
jaroslav@357
    43
public class BrswrMojo extends AbstractMojo {
jaroslav@357
    44
    public BrswrMojo() {
jaroslav@109
    45
    }
jaroslav@357
    46
    /** Resource to show as initial page */
jaroslav@109
    47
    @Parameter
jaroslav@357
    48
    private String startpage;
jaroslav@109
    49
    
jaroslav@109
    50
    @Parameter(defaultValue="${project}")
jaroslav@109
    51
    private MavenProject prj;
jaroslav@109
    52
    
jaroslav@357
    53
    /** Root of the class files */
jaroslav@357
    54
    @Parameter(defaultValue="${project.build.directory}/classes")
jaroslav@357
    55
    private File classes;
jaroslav@109
    56
jaroslav@109
    57
    @Override
jaroslav@109
    58
    public void execute() throws MojoExecutionException {
jaroslav@357
    59
        if (startpage == null) {
jaroslav@357
    60
            throw new MojoExecutionException("You have to provide a start page");
jaroslav@134
    61
        }
jaroslav@109
    62
jaroslav@109
    63
        try {
jaroslav@109
    64
            URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
jaroslav@357
    65
            
jaroslav@357
    66
            Bck2BrwsrLauncher httpServer = new Bck2BrwsrLauncher();
jaroslav@357
    67
            httpServer.addClassLoader(url);
jaroslav@372
    68
            try {
jaroslav@372
    69
                httpServer.showURL(startpage);
jaroslav@372
    70
            } catch (Exception ex) {
jaroslav@372
    71
                throw new MojoExecutionException("Can't open " + startpage, ex);
jaroslav@372
    72
            }
jaroslav@357
    73
            
jaroslav@357
    74
            System.in.read();
jaroslav@305
    75
        } catch (IOException ex) {
jaroslav@357
    76
            throw new MojoExecutionException("Can't show the browser", ex);
jaroslav@109
    77
        }
jaroslav@109
    78
    }
jaroslav@109
    79
jaroslav@109
    80
    private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
jaroslav@109
    81
        if (!dir.isDirectory()) {
jaroslav@109
    82
            throw new MojoExecutionException("Not a directory " + dir);
jaroslav@109
    83
        }
jaroslav@109
    84
        File[] arr = dir.listFiles();
jaroslav@109
    85
        if (arr.length == 1 && arr[0].isDirectory()) {
jaroslav@109
    86
            return findNonEmptyFolder(arr[0]);
jaroslav@109
    87
        }
jaroslav@109
    88
        return dir;
jaroslav@109
    89
    }
jaroslav@109
    90
jaroslav@134
    91
    private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
jaroslav@109
    92
        File[] files = toCheck.listFiles();
jaroslav@109
    93
        if (files != null) {
jaroslav@134
    94
            long newest = 0L;
jaroslav@109
    95
            for (File f : files) {
jaroslav@134
    96
                long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
jaroslav@134
    97
                if (newest < lastModified) {
jaroslav@134
    98
                    newest = lastModified;
jaroslav@134
    99
                }
jaroslav@109
   100
            }
jaroslav@134
   101
            return newest;
jaroslav@109
   102
        } else if (toCheck.getName().endsWith(".class")) {
jaroslav@109
   103
            arr.add(prefix.substring(0, prefix.length() - 7));
jaroslav@134
   104
            return toCheck.lastModified();
jaroslav@134
   105
        } else {
jaroslav@134
   106
            return 0L;
jaroslav@109
   107
        }
jaroslav@109
   108
    }
jaroslav@109
   109
jaroslav@109
   110
    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
jaroslav@109
   111
        List<URL> arr = new ArrayList<URL>();
jaroslav@109
   112
        arr.add(root.toURI().toURL());
jaroslav@109
   113
        for (Artifact a : deps) {
jaroslav@109
   114
            arr.add(a.getFile().toURI().toURL());
jaroslav@109
   115
        }
jaroslav@357
   116
        return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
jaroslav@109
   117
    }
jaroslav@109
   118
}