jaroslav@309
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@309
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@309
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@309
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@309
|
42 |
*/
|
jaroslav@362
|
43 |
package org.netbeans.html.boot.impl;
|
jaroslav@309
|
44 |
|
jaroslav@322
|
45 |
import java.io.Closeable;
|
jaroslav@582
|
46 |
import java.io.Flushable;
|
jaroslav@322
|
47 |
import java.io.IOException;
|
jtulach@884
|
48 |
import java.util.logging.Level;
|
jaroslav@322
|
49 |
import java.util.logging.Logger;
|
jtulach@838
|
50 |
import org.netbeans.html.boot.spi.Fn;
|
jtulach@884
|
51 |
import org.objectweb.asm.Opcodes;
|
jaroslav@309
|
52 |
|
jaroslav@309
|
53 |
/**
|
jaroslav@309
|
54 |
*
|
jtulach@790
|
55 |
* @author Jaroslav Tulach
|
jaroslav@309
|
56 |
*/
|
jaroslav@322
|
57 |
public final class FnContext implements Closeable {
|
jaroslav@322
|
58 |
private static final Logger LOG = Logger.getLogger(FnContext.class.getName());
|
jaroslav@582
|
59 |
private static final FnContext DUMMY;
|
jaroslav@582
|
60 |
static {
|
jaroslav@582
|
61 |
DUMMY = new FnContext(null, null);
|
jaroslav@582
|
62 |
DUMMY.prev = DUMMY;
|
jaroslav@582
|
63 |
}
|
jaroslav@322
|
64 |
|
jtulach@884
|
65 |
public static boolean isJavaScriptCapable(ClassLoader l) {
|
jtulach@884
|
66 |
if (l instanceof JsClassLoader) {
|
jtulach@884
|
67 |
return true;
|
jtulach@884
|
68 |
}
|
jtulach@884
|
69 |
if (l.getResource("META-INF/net.java.html.js.classes") != null) {
|
jtulach@884
|
70 |
return false;
|
jtulach@884
|
71 |
}
|
jtulach@884
|
72 |
return true;
|
jtulach@884
|
73 |
}
|
jtulach@884
|
74 |
|
jtulach@884
|
75 |
public static boolean isAsmPresent() {
|
jtulach@884
|
76 |
Throwable t;
|
jtulach@884
|
77 |
try {
|
jtulach@884
|
78 |
Class.forName(Opcodes.class.getName());
|
jtulach@884
|
79 |
return true;
|
jtulach@884
|
80 |
} catch (LinkageError ex) {
|
jtulach@884
|
81 |
t = ex;
|
jtulach@884
|
82 |
} catch (ClassNotFoundException ex) {
|
jtulach@884
|
83 |
t = ex;
|
jtulach@884
|
84 |
}
|
jtulach@884
|
85 |
LOG.log(Level.SEVERE, "When using @JavaScriptBody methods, one needs to either:");
|
jtulach@884
|
86 |
LOG.log(Level.SEVERE, " - include asm-5.0.jar on runtime classpath");
|
jtulach@884
|
87 |
LOG.log(Level.SEVERE, " - post process classes, see http://bits.netbeans.org/html+java/dev/net/java/html/js/package-summary.html#post-process");
|
jtulach@884
|
88 |
LOG.log(Level.SEVERE, "Cannot initialize asm-5.0.jar!", t);
|
jtulach@884
|
89 |
return false;
|
jtulach@884
|
90 |
}
|
jtulach@884
|
91 |
|
jaroslav@323
|
92 |
private Object prev;
|
jaroslav@582
|
93 |
private final Fn.Presenter current;
|
jaroslav@582
|
94 |
private FnContext(Fn.Presenter prevP, Fn.Presenter newP) {
|
jaroslav@582
|
95 |
this.current = newP;
|
jaroslav@582
|
96 |
this.prev = prevP;
|
jaroslav@309
|
97 |
}
|
jaroslav@322
|
98 |
|
jaroslav@322
|
99 |
@Override
|
jaroslav@322
|
100 |
public void close() throws IOException {
|
jaroslav@323
|
101 |
if (prev != this) {
|
jaroslav@323
|
102 |
currentPresenter((Fn.Presenter)prev);
|
jaroslav@323
|
103 |
prev = this;
|
jaroslav@582
|
104 |
if (current instanceof Flushable) {
|
jaroslav@582
|
105 |
((Flushable)current).flush();
|
jaroslav@582
|
106 |
}
|
jaroslav@322
|
107 |
}
|
jaroslav@322
|
108 |
}
|
jaroslav@322
|
109 |
/*
|
jaroslav@322
|
110 |
@Override
|
jaroslav@322
|
111 |
protected void finalize() throws Throwable {
|
jaroslav@322
|
112 |
if (prev != null) {
|
jaroslav@322
|
113 |
LOG.warning("Unclosed context!");
|
jaroslav@322
|
114 |
}
|
jaroslav@322
|
115 |
}
|
jaroslav@322
|
116 |
*/
|
jaroslav@322
|
117 |
public static Closeable activate(Fn.Presenter newP) {
|
jaroslav@582
|
118 |
final Fn.Presenter oldP = currentPresenter(newP);
|
jaroslav@582
|
119 |
if (oldP == newP) {
|
jaroslav@582
|
120 |
return DUMMY;
|
jaroslav@582
|
121 |
}
|
jaroslav@582
|
122 |
return new FnContext(oldP, newP);
|
jaroslav@322
|
123 |
}
|
jaroslav@322
|
124 |
|
jaroslav@309
|
125 |
|
jaroslav@309
|
126 |
private static final ThreadLocal<Fn.Presenter> CURRENT = new ThreadLocal<Fn.Presenter>();
|
jaroslav@309
|
127 |
|
jaroslav@309
|
128 |
public static Fn.Presenter currentPresenter(Fn.Presenter p) {
|
jaroslav@309
|
129 |
Fn.Presenter prev = CURRENT.get();
|
jaroslav@309
|
130 |
CURRENT.set(p);
|
jaroslav@309
|
131 |
return prev;
|
jaroslav@309
|
132 |
}
|
jaroslav@309
|
133 |
|
jaroslav@424
|
134 |
public static Fn.Presenter currentPresenter(boolean fail) {
|
jaroslav@309
|
135 |
Fn.Presenter p = CURRENT.get();
|
jaroslav@424
|
136 |
if (p == null && fail) {
|
jaroslav@309
|
137 |
throw new IllegalStateException("No current WebView context around!");
|
jaroslav@309
|
138 |
}
|
jaroslav@309
|
139 |
return p;
|
jaroslav@309
|
140 |
}
|
jaroslav@309
|
141 |
|
jaroslav@309
|
142 |
}
|