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 net.java.html;
23 import java.util.HashMap;
25 import java.util.ServiceLoader;
26 import org.apidesign.html.context.impl.CtxAccssr;
27 import org.apidesign.html.context.impl.CtxImpl;
28 import org.apidesign.html.context.spi.Contexts;
30 /** Represents context where the <code>net.java.html.json.Model</code>
32 * operate in. The context is usually a particular HTML page in a browser.
33 * The context is also associated with the actual HTML technology
34 * in the HTML page - there is likely to be different context for
35 * <a href="http://knockoutjs.com">knockout.js</a> and different one
36 * for <a href="http://angularjs.org">angular</a>.
38 * @author Jaroslav Tulach <jtulach@netbeans.org>
40 public final class BrwsrCtx {
41 private final CtxImpl impl;
42 private BrwsrCtx(CtxImpl impl) {
48 protected BrwsrCtx newContext(CtxImpl impl) {
49 return new BrwsrCtx(impl);
53 protected CtxImpl find(BrwsrCtx context) {
58 /** Dummy context without binding to any real browser or technology.
59 * Useful for simple unit testing of behavior of various business logic
62 public static final BrwsrCtx EMPTY = Contexts.newBuilder().build();
63 /** map of known classloaders and their contexts */
64 private static final Map<ClassLoader,BrwsrCtx> CTXS = new HashMap<ClassLoader, BrwsrCtx>();
66 /** Seeks for the default context that is associated with the requesting
67 * class. If no suitable context is found, a warning message is
68 * printed and {@link #EMPTY} context is returned.
70 * @param requestor the class that makes the request
71 * @return appropriate context for the request
73 public static BrwsrCtx findDefault(Class<?> requestor) {
74 org.apidesign.html.context.spi.Contexts.Builder cb = Contexts.newBuilder();
75 boolean found = false;
79 l = requestor.getClassLoader();
80 } catch (SecurityException ex) {
85 BrwsrCtx c = CTXS.get(l);
91 for (org.apidesign.html.context.spi.Contexts.Provider cp : ServiceLoader.load(
92 org.apidesign.html.context.spi.Contexts.Provider.class, l
94 cp.fillContext(cb, requestor);
98 for (org.apidesign.html.context.spi.Contexts.Provider cp : ServiceLoader.load(org.apidesign.html.context.spi.Contexts.Provider.class, org.apidesign.html.context.spi.Contexts.Provider.class.getClassLoader())) {
99 cp.fillContext(cb, requestor);
102 } catch (SecurityException ex) {
106 // if we have some data from regular provides, go on
109 // XXX: print out a warning
112 BrwsrCtx c = cb.build();
114 synchronized (CTXS) {