2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
19 package org.apidesign.bck2brwsr.mojo;
21 import java.io.BufferedReader;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.MalformedURLException;
29 import java.net.URLClassLoader;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Enumeration;
33 import java.util.HashSet;
34 import java.util.List;
36 import java.util.jar.JarEntry;
37 import java.util.jar.JarFile;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 import org.apache.maven.artifact.Artifact;
41 import org.apache.maven.plugin.AbstractMojo;
42 import org.apache.maven.plugin.MojoExecutionException;
43 import org.apache.maven.plugin.MojoFailureException;
44 import org.apache.maven.plugins.annotations.LifecyclePhase;
45 import org.apache.maven.plugins.annotations.Mojo;
46 import org.apache.maven.plugins.annotations.Parameter;
47 import org.apache.maven.plugins.annotations.ResolutionScope;
48 import org.apache.maven.project.MavenProject;
49 import org.apidesign.vm4brwsr.Bck2Brwsr;
50 import org.apidesign.vm4brwsr.ObfuscationLevel;
54 * @author Jaroslav Tulach
58 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
59 defaultPhase = LifecyclePhase.PACKAGE
61 public class AheadOfTime extends AbstractMojo {
62 @Parameter(defaultValue = "${project}")
63 private MavenProject prj;
66 * Directory where to generate ahead-of-time JavaScript files for
69 @Parameter(defaultValue = "${project.build.directory}/lib")
72 /** Root JavaScript file to generate */
73 @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
77 * The obfuscation level for the generated JavaScript file.
81 @Parameter(defaultValue = "NONE")
82 private ObfuscationLevel obfuscation;
85 public void execute() throws MojoExecutionException, MojoFailureException {
86 URLClassLoader loader;
88 loader = buildClassLoader(null, prj.getArtifacts());
89 } catch (MalformedURLException ex) {
90 throw new MojoFailureException("Can't initialize classloader");
92 for (Artifact a : prj.getArtifacts()) {
93 if (a.getFile() == null) {
96 String n = a.getFile().getName();
97 if (!n.endsWith(".jar")) {
100 if ("provided".equals(a.getScope())) {
104 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
106 aotLibrary(a, js , loader);
107 } catch (IOException ex) {
108 throw new MojoFailureException("Can't compile" + a.getFile(), ex);
113 FileWriter w = new FileWriter(vm);
114 Bck2Brwsr.newCompiler().
115 obfuscation(obfuscation).
117 resources(new Bck2Brwsr.Resources() {
120 public InputStream get(String resource) throws IOException {
127 } catch (IOException ex) {
128 throw new MojoExecutionException("Can't compile", ex);
132 private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
133 List<String> classes = new ArrayList<String>();
134 List<String> resources = new ArrayList<String>();
135 Set<String> exported = new HashSet<String>();
137 JarFile jf = new JarFile(a.getFile());
138 listJAR(jf, classes , resources, exported);
140 FileWriter w = new FileWriter(js);
141 Bck2Brwsr.newCompiler().
142 obfuscation(obfuscation).
145 addResources(resources.toArray(new String[0])).
146 addClasses(classes.toArray(new String[0])).
147 addExported(exported.toArray(new String[0])).
151 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
152 List<URL> arr = new ArrayList<URL>();
154 arr.add(root.toURI().toURL());
156 for (Artifact a : deps) {
157 if (a.getFile() != null) {
158 arr.add(a.getFile().toURI().toURL());
161 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
164 private static void listJAR(
165 JarFile j, List<String> classes,
166 List<String> resources, Set<String> exported
167 ) throws IOException {
168 Enumeration<JarEntry> en = j.entries();
169 while (en.hasMoreElements()) {
170 JarEntry e = en.nextElement();
171 final String n = e.getName();
172 if (n.endsWith("/")) {
175 int last = n.lastIndexOf('/');
176 String pkg = n.substring(0, last + 1);
177 if (n.endsWith(".class")) {
178 classes.add(n.substring(0, n.length() - 6));
181 if (n.startsWith("META-INF/services/") && exported != null) {
182 final InputStream is = j.getInputStream(e);
183 exportedServices(is, exported);
188 String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
189 if (exp != null && exported != null) {
190 for (String def : exp.split(",")) {
191 for (String sep : def.split(";")) {
192 exported.add(sep.replace('.', '/') + "/");
199 static void exportedServices(final InputStream is, Set<String> exported) throws IOException {
200 BufferedReader r = new BufferedReader(new InputStreamReader(is));
202 String l = r.readLine();
206 if (l.startsWith("#")) {
209 exported.add(l.replace('.', '/'));