The static calculator demo needs to reference just a single application .js file from its main HTML page. The rest is loaded based on classpath attribute.
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.
18 package org.apidesign.bck2brwsr.aot;
20 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.jar.JarEntry;
32 import java.util.jar.JarFile;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import java.util.zip.ZipEntry;
36 import org.apidesign.vm4brwsr.Bck2Brwsr;
38 /** Utilities to process JAR files and set a compiler
42 * @author Jaroslav Tulach
44 public final class Bck2BrwsrJars {
45 private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
47 private Bck2BrwsrJars() {
50 /** Creates new compiler pre-configured from the content of
51 * provided JAR file. The compiler will compile all classes.
52 * The system understands OSGi manifest entries and will export
53 * all packages that are exported in the JAR file. The system
54 * also recognizes META-INF/services and makes sure the file names
57 * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
58 * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
59 * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
60 * Can be <code>null</code> - in such case an
61 * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
62 * @param jar the file to process
63 * @return newly configured compiler
64 * @throws IOException if something goes wrong
66 public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
67 final JarFile jf = new JarFile(jar);
68 List<String> classes = new ArrayList<>();
69 List<String> resources = new ArrayList<>();
70 Set<String> exported = new HashSet<>();
72 listJAR(jf, classes, resources, exported);
74 String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
75 String[] classpath = cp == null ? new String[0] : cp.split(" ");
77 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
80 public InputStream get(String resource) throws IOException {
81 InputStream is = jf.getInputStream(new ZipEntry(resource));
82 return is == null ? super.get(resource) : is;
85 return Bck2Brwsr.newCompiler()
87 .addClasses(classes.toArray(new String[classes.size()]))
88 .addExported(exported.toArray(new String[exported.size()]))
89 .addResources(resources.toArray(new String[resources.size()]))
90 .resources(new JarRes());
93 private static void listJAR(
94 JarFile j, List<String> classes,
95 List<String> resources, Set<String> keep
96 ) throws IOException {
97 Enumeration<JarEntry> en = j.entries();
98 while (en.hasMoreElements()) {
99 JarEntry e = en.nextElement();
100 final String n = e.getName();
101 if (n.endsWith("/")) {
104 if (n.startsWith("META-INF/maven/")) {
107 int last = n.lastIndexOf('/');
108 String pkg = n.substring(0, last + 1);
109 if (pkg.startsWith("java/")) {
112 if (n.endsWith(".class")) {
113 classes.add(n.substring(0, n.length() - 6));
116 if (n.startsWith("META-INF/services/") && keep != null) {
117 BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
119 String l = r.readLine();
123 if (l.startsWith("#")) {
126 keep.add(l.replace('.', '/'));
131 String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
132 if (exp != null && keep != null) {
133 for (String def : exp.split(",")) {
134 for (String sep : def.split(";")) {
135 keep.add(sep.replace('.', '/') + "/");
142 static class EmulationResources implements Bck2Brwsr.Resources {
145 public InputStream get(String name) throws IOException {
146 Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
148 while (en.hasMoreElements()) {
149 u = en.nextElement();
152 LOG.log(Level.WARNING, "Cannot find {0}", name);
155 if (u.toExternalForm().contains("/rt.jar!")) {
156 LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
159 return u.openStream();