2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 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. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.boot.impl;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.List;
31 import org.apidesign.html.boot.spi.Fn;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 public final class FnUtils {
38 private static final ThreadLocal<Fn.Presenter> CURRENT = new ThreadLocal<Fn.Presenter>();
39 private static final ThreadLocal<List<Runnable>> LATER = new ThreadLocal<List<Runnable>>();
44 public static Fn define(Class<?> caller, String code, String... names) {
45 return currentPresenter().defineFn(code, names);
48 public static boolean isJavaScriptCapable(ClassLoader l) {
49 return l instanceof JsClassLoader;
52 public static boolean isValid(Fn fn) {
53 return fn != null && fn.isValid();
56 public static ClassLoader newLoader(final FindResources f, final Fn.Presenter d, ClassLoader parent) {
57 return new JsClassLoader(parent) {
59 protected URL findResource(String name) {
60 List<URL> l = res(name, true);
61 return l.isEmpty() ? null : l.get(0);
65 protected Enumeration<URL> findResources(String name) {
66 return Collections.enumeration(res(name, false));
69 private List<URL> res(String name, boolean oneIsEnough) {
70 List<URL> l = new ArrayList<URL>();
71 f.findResources(name, l, oneIsEnough);
76 protected Fn defineFn(String code, String... names) {
77 return d.defineFn(code, names);
81 protected void loadScript(Reader code) throws Exception {
87 static String callback(final String body) {
88 return new JsCallback() {
90 protected CharSequence callMethod(
91 String ident, String fqn, String method, String params
93 StringBuilder sb = new StringBuilder();
94 sb.append("vm.").append(mangle(fqn, method, params));
105 static void loadScript(JsClassLoader jcl, String resource) {
106 final InputStream script = jcl.getResourceAsStream(resource);
107 if (script == null) {
108 throw new NullPointerException("Can't find " + resource);
113 isr = new InputStreamReader(script, "UTF-8");
120 } catch (Exception ex) {
121 throw new IllegalStateException("Can't execute " + resource, ex);
125 public static Fn.Presenter currentPresenter() {
126 Fn.Presenter p = CURRENT.get();
128 throw new IllegalStateException("No current WebView context around!");
133 public static Fn.Presenter currentPresenter(Fn.Presenter p) {
134 List<Runnable> list = LATER.get();
136 if (list != null) for (Runnable runnable : list) {
139 } catch (Throwable t) {
144 Fn.Presenter prev = CURRENT.get();
149 public static void runLater(Runnable r) {
151 List<Runnable> list = LATER.get();
153 list = new ArrayList<Runnable>();