rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 21:17:04 +0200
branchmodel
changeset 1043 bd80952bfd11
parent 772 d382dacfd73f
permissions -rw-r--r--
Using profiles. Twitter demo can execute and test properly in FX profile now
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@382
    20
import java.io.Closeable;
jaroslav@109
    21
import org.apache.maven.plugin.AbstractMojo;
jaroslav@109
    22
jaroslav@109
    23
import java.io.File;
jaroslav@305
    24
import java.io.IOException;
jaroslav@109
    25
import java.net.MalformedURLException;
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@109
    31
import org.apache.maven.artifact.Artifact;
jaroslav@109
    32
import org.apache.maven.plugin.MojoExecutionException;
jaroslav@109
    33
import org.apache.maven.plugins.annotations.LifecyclePhase;
jaroslav@109
    34
import org.apache.maven.plugins.annotations.Mojo;
jaroslav@109
    35
import org.apache.maven.plugins.annotations.Parameter;
jaroslav@109
    36
import org.apache.maven.project.MavenProject;
jaroslav@382
    37
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@109
    38
jaroslav@357
    39
/** Executes given HTML page in a browser. */
jaroslav@726
    40
@Mojo(name="brwsr", defaultPhase=LifecyclePhase.NONE)
jaroslav@357
    41
public class BrswrMojo extends AbstractMojo {
jaroslav@357
    42
    public BrswrMojo() {
jaroslav@109
    43
    }
jaroslav@1043
    44
    
jaroslav@1043
    45
    /** The identification of a launcher to use. Known values <code>fxbrwsr</code>, 
jaroslav@1043
    46
     * <code>bck2brwsr</code>, or 
jaroslav@1043
    47
     * name of an external process to execute.
jaroslav@1043
    48
     */
jaroslav@1043
    49
    @Parameter
jaroslav@1043
    50
    private String launcher;
jaroslav@1043
    51
    
jaroslav@1043
    52
    
jaroslav@357
    53
    /** Resource to show as initial page */
jaroslav@109
    54
    @Parameter
jaroslav@357
    55
    private String startpage;
jaroslav@421
    56
jaroslav@109
    57
    @Parameter(defaultValue="${project}")
jaroslav@109
    58
    private MavenProject prj;
jaroslav@109
    59
    
jaroslav@357
    60
    /** Root of the class files */
jaroslav@357
    61
    @Parameter(defaultValue="${project.build.directory}/classes")
jaroslav@357
    62
    private File classes;
jaroslav@613
    63
    
jaroslav@613
    64
    /** Root of all pages, and files, etc. */
jaroslav@613
    65
    @Parameter
jaroslav@613
    66
    private File directory;
jaroslav@109
    67
jaroslav@109
    68
    @Override
jaroslav@109
    69
    public void execute() throws MojoExecutionException {
jaroslav@357
    70
        if (startpage == null) {
jaroslav@357
    71
            throw new MojoExecutionException("You have to provide a start page");
jaroslav@134
    72
        }
jaroslav@613
    73
        
jaroslav@109
    74
        try {
jaroslav@382
    75
            Closeable httpServer;
jaroslav@613
    76
            if (directory != null) {
jaroslav@613
    77
                httpServer = Launcher.showDir(directory, startpage);
jaroslav@613
    78
            } else {
jaroslav@613
    79
                URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
jaroslav@613
    80
                try {
jaroslav@1043
    81
                    httpServer = Launcher.showURL(launcher, url, startpage());
jaroslav@613
    82
                } catch (Exception ex) {
jaroslav@613
    83
                    throw new MojoExecutionException("Can't open " + startpage(), ex);
jaroslav@613
    84
                }
jaroslav@372
    85
            }
jaroslav@357
    86
            System.in.read();
jaroslav@382
    87
            httpServer.close();
jaroslav@305
    88
        } catch (IOException ex) {
jaroslav@357
    89
            throw new MojoExecutionException("Can't show the browser", ex);
jaroslav@109
    90
        }
jaroslav@109
    91
    }
jaroslav@421
    92
    
jaroslav@421
    93
    private String startpage() {
jaroslav@422
    94
        return startpage;
jaroslav@109
    95
    }
jaroslav@109
    96
jaroslav@109
    97
    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
jaroslav@109
    98
        List<URL> arr = new ArrayList<URL>();
jaroslav@109
    99
        arr.add(root.toURI().toURL());
jaroslav@109
   100
        for (Artifact a : deps) {
jaroslav@515
   101
            final File f = a.getFile();
jaroslav@515
   102
            if (f != null) {
jaroslav@515
   103
                arr.add(f.toURI().toURL());
jaroslav@515
   104
            }
jaroslav@109
   105
        }
jaroslav@357
   106
        return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
jaroslav@109
   107
    }
jaroslav@109
   108
}