Static compilator demo is capable to load itself from individual extension files
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 org.apache.maven.artifact.Artifact;
39 import org.apache.maven.plugin.AbstractMojo;
40 import org.apache.maven.plugin.MojoExecutionException;
41 import org.apache.maven.plugin.MojoFailureException;
42 import org.apache.maven.plugins.annotations.LifecyclePhase;
43 import org.apache.maven.plugins.annotations.Mojo;
44 import org.apache.maven.plugins.annotations.Parameter;
45 import org.apache.maven.plugins.annotations.ResolutionScope;
46 import org.apache.maven.project.MavenProject;
47 import org.apidesign.vm4brwsr.Bck2Brwsr;
48 import org.apidesign.vm4brwsr.ObfuscationLevel;
52 * @author Jaroslav Tulach
56 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
57 defaultPhase = LifecyclePhase.PACKAGE
59 public class AheadOfTime extends AbstractMojo {
60 @Parameter(defaultValue = "${project}")
61 private MavenProject prj;
64 * Directory where to generate ahead-of-time JavaScript files for
67 @Parameter(defaultValue = "${project.build.directory}/lib")
70 /** Root JavaScript file to generate */
71 @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
75 * The obfuscation level for the generated JavaScript file.
79 @Parameter(defaultValue = "NONE")
80 private ObfuscationLevel obfuscation;
83 public void execute() throws MojoExecutionException, MojoFailureException {
85 URLClassLoader loader = buildClassLoader(null, prj.getArtifacts());
86 for (Artifact a : prj.getArtifacts()) {
87 if (a.getFile() == null) {
90 String n = a.getFile().getName();
91 if (!n.endsWith(".jar")) {
95 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
96 aotLibrary(a, js , loader);
99 FileWriter w = new FileWriter(vm);
100 Bck2Brwsr.newCompiler().
101 obfuscation(obfuscation).
103 resources(new Bck2Brwsr.Resources() {
106 public InputStream get(String resource) throws IOException {
113 } catch (IOException ex) {
114 throw new MojoExecutionException("Can't compile", ex);
118 private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
119 List<String> classes = new ArrayList<String>();
120 List<String> resources = new ArrayList<String>();
121 Set<String> exported = new HashSet<String>();
123 JarFile jf = new JarFile(a.getFile());
124 listJAR(jf, classes , resources, exported);
126 FileWriter w = new FileWriter(js);
127 Bck2Brwsr.newCompiler().
128 obfuscation(obfuscation).
131 addResources(resources.toArray(new String[0])).
132 addClasses(classes.toArray(new String[0])).
133 addExported(exported.toArray(new String[0])).
137 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
138 List<URL> arr = new ArrayList<URL>();
140 arr.add(root.toURI().toURL());
142 for (Artifact a : deps) {
143 if (a.getFile() != null) {
144 arr.add(a.getFile().toURI().toURL());
147 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
150 private static void listJAR(
151 JarFile j, List<String> classes,
152 List<String> resources, Set<String> exported
153 ) throws IOException {
154 Enumeration<JarEntry> en = j.entries();
155 while (en.hasMoreElements()) {
156 JarEntry e = en.nextElement();
157 final String n = e.getName();
158 if (n.endsWith("/")) {
161 int last = n.lastIndexOf('/');
162 String pkg = n.substring(0, last + 1);
163 if (n.endsWith(".class")) {
164 classes.add(n.substring(0, n.length() - 6));
167 if (n.startsWith("META-INF/services/") && exported != null) {
168 final InputStream is = j.getInputStream(e);
169 exportedServices(is, exported);
174 String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
175 if (exp != null && exported != null) {
176 for (String def : exp.split(",")) {
177 for (String sep : def.split(";")) {
178 exported.add(sep.replace('.', '/') + "/");
185 static void exportedServices(final InputStream is, Set<String> exported) throws IOException {
186 BufferedReader r = new BufferedReader(new InputStreamReader(is));
188 String l = r.readLine();
192 if (l.startsWith("#")) {
195 exported.add(l.replace('.', '/'));