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@1173
|
32 |
import org.apache.maven.model.Resource;
|
jaroslav@109
|
33 |
import org.apache.maven.plugin.MojoExecutionException;
|
jaroslav@109
|
34 |
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
jaroslav@109
|
35 |
import org.apache.maven.plugins.annotations.Mojo;
|
jaroslav@109
|
36 |
import org.apache.maven.plugins.annotations.Parameter;
|
jaroslav@1164
|
37 |
import org.apache.maven.plugins.annotations.ResolutionScope;
|
jaroslav@109
|
38 |
import org.apache.maven.project.MavenProject;
|
jaroslav@382
|
39 |
import org.apidesign.bck2brwsr.launcher.Launcher;
|
jaroslav@109
|
40 |
|
jaroslav@357
|
41 |
/** Executes given HTML page in a browser. */
|
jaroslav@1164
|
42 |
@Mojo(
|
jaroslav@1164
|
43 |
name="brwsr",
|
jaroslav@1164
|
44 |
requiresDependencyResolution = ResolutionScope.RUNTIME,
|
jaroslav@1164
|
45 |
defaultPhase=LifecyclePhase.NONE
|
jaroslav@1164
|
46 |
)
|
jaroslav@1047
|
47 |
public class BrwsrMojo extends AbstractMojo {
|
jaroslav@1047
|
48 |
public BrwsrMojo() {
|
jaroslav@109
|
49 |
}
|
jaroslav@1043
|
50 |
|
jaroslav@1043
|
51 |
/** The identification of a launcher to use. Known values <code>fxbrwsr</code>,
|
jaroslav@1043
|
52 |
* <code>bck2brwsr</code>, or
|
jaroslav@1043
|
53 |
* name of an external process to execute.
|
jaroslav@1043
|
54 |
*/
|
jaroslav@1043
|
55 |
@Parameter
|
jaroslav@1043
|
56 |
private String launcher;
|
jaroslav@1043
|
57 |
|
jaroslav@1043
|
58 |
|
jaroslav@357
|
59 |
/** Resource to show as initial page */
|
jaroslav@109
|
60 |
@Parameter
|
jaroslav@357
|
61 |
private String startpage;
|
jaroslav@421
|
62 |
|
jaroslav@109
|
63 |
@Parameter(defaultValue="${project}")
|
jaroslav@109
|
64 |
private MavenProject prj;
|
jaroslav@109
|
65 |
|
jaroslav@357
|
66 |
/** Root of the class files */
|
jaroslav@357
|
67 |
@Parameter(defaultValue="${project.build.directory}/classes")
|
jaroslav@357
|
68 |
private File classes;
|
jaroslav@613
|
69 |
|
jaroslav@613
|
70 |
/** Root of all pages, and files, etc. */
|
jaroslav@613
|
71 |
@Parameter
|
jaroslav@613
|
72 |
private File directory;
|
jaroslav@1371
|
73 |
|
jaroslav@1371
|
74 |
@Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
|
jaroslav@1371
|
75 |
private File javascript;
|
jaroslav@109
|
76 |
|
jaroslav@109
|
77 |
@Override
|
jaroslav@109
|
78 |
public void execute() throws MojoExecutionException {
|
jaroslav@357
|
79 |
if (startpage == null) {
|
jaroslav@357
|
80 |
throw new MojoExecutionException("You have to provide a start page");
|
jaroslav@134
|
81 |
}
|
jaroslav@1371
|
82 |
if (javascript != null && javascript.isFile()) {
|
jaroslav@1371
|
83 |
System.setProperty("bck2brwsr.js", javascript.toURI().toString());
|
jaroslav@1371
|
84 |
}
|
jaroslav@109
|
85 |
try {
|
jaroslav@382
|
86 |
Closeable httpServer;
|
jaroslav@613
|
87 |
if (directory != null) {
|
jaroslav@1271
|
88 |
URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
|
jaroslav@1271
|
89 |
httpServer = Launcher.showDir(launcher, directory, url, startpage);
|
jaroslav@613
|
90 |
} else {
|
jaroslav@1164
|
91 |
URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
|
jaroslav@613
|
92 |
try {
|
jaroslav@1173
|
93 |
for (Resource r : prj.getResources()) {
|
jaroslav@1173
|
94 |
File f = new File(r.getDirectory(), startpage().replace('/', File.separatorChar));
|
jaroslav@1173
|
95 |
if (f.exists()) {
|
jaroslav@1173
|
96 |
System.setProperty("startpage.file", f.getPath());
|
jaroslav@1173
|
97 |
}
|
jaroslav@1173
|
98 |
}
|
jaroslav@1173
|
99 |
|
jaroslav@1043
|
100 |
httpServer = Launcher.showURL(launcher, url, startpage());
|
jaroslav@613
|
101 |
} catch (Exception ex) {
|
jaroslav@613
|
102 |
throw new MojoExecutionException("Can't open " + startpage(), ex);
|
jaroslav@613
|
103 |
}
|
jaroslav@372
|
104 |
}
|
jaroslav@357
|
105 |
System.in.read();
|
jaroslav@382
|
106 |
httpServer.close();
|
jaroslav@305
|
107 |
} catch (IOException ex) {
|
jaroslav@357
|
108 |
throw new MojoExecutionException("Can't show the browser", ex);
|
jaroslav@109
|
109 |
}
|
jaroslav@109
|
110 |
}
|
jaroslav@421
|
111 |
|
jaroslav@421
|
112 |
private String startpage() {
|
jaroslav@422
|
113 |
return startpage;
|
jaroslav@109
|
114 |
}
|
jaroslav@109
|
115 |
|
jaroslav@109
|
116 |
private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
|
jaroslav@109
|
117 |
List<URL> arr = new ArrayList<URL>();
|
jaroslav@109
|
118 |
arr.add(root.toURI().toURL());
|
jaroslav@109
|
119 |
for (Artifact a : deps) {
|
jaroslav@515
|
120 |
final File f = a.getFile();
|
jaroslav@515
|
121 |
if (f != null) {
|
jaroslav@515
|
122 |
arr.add(f.toURI().toURL());
|
jaroslav@515
|
123 |
}
|
jaroslav@109
|
124 |
}
|
jaroslav@1047
|
125 |
return new URLClassLoader(arr.toArray(new URL[0]), BrwsrMojo.class.getClassLoader());
|
jaroslav@109
|
126 |
}
|
jaroslav@109
|
127 |
}
|