author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Wed, 26 Jun 2013 17:21:10 +0200 | |
branch | classloader |
changeset 167 | 10005a498972 |
parent 121 | 81e976eb0fc2 |
child 301 | 74e862ea824e |
child 320 | 719062a62824 |
permissions | -rw-r--r-- |
jaroslav@5 | 1 |
/** |
jaroslav@5 | 2 |
* HTML via Java(tm) Language Bindings |
jaroslav@5 | 3 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
jaroslav@5 | 4 |
* |
jaroslav@5 | 5 |
* This program is free software: you can redistribute it and/or modify |
jaroslav@5 | 6 |
* it under the terms of the GNU General Public License as published by |
jaroslav@5 | 7 |
* the Free Software Foundation, version 2 of the License. |
jaroslav@5 | 8 |
* |
jaroslav@5 | 9 |
* This program is distributed in the hope that it will be useful, |
jaroslav@5 | 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
jaroslav@5 | 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
jaroslav@5 | 12 |
* GNU General Public License for more details. apidesign.org |
jaroslav@5 | 13 |
* designates this particular file as subject to the |
jaroslav@5 | 14 |
* "Classpath" exception as provided by apidesign.org |
jaroslav@5 | 15 |
* in the License file that accompanied this code. |
jaroslav@5 | 16 |
* |
jaroslav@5 | 17 |
* You should have received a copy of the GNU General Public License |
jaroslav@5 | 18 |
* along with this program. Look for COPYING file in the top folder. |
jaroslav@5 | 19 |
* If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException |
jaroslav@5 | 20 |
*/ |
jaroslav@110 | 21 |
package net.java.html; |
jaroslav@5 | 22 |
|
jaroslav@39 | 23 |
import java.util.ServiceLoader; |
jaroslav@110 | 24 |
import org.apidesign.html.context.impl.CtxAccssr; |
jaroslav@110 | 25 |
import org.apidesign.html.context.impl.CtxImpl; |
jaroslav@110 | 26 |
import org.apidesign.html.context.spi.Contexts; |
jaroslav@6 | 27 |
|
jaroslav@167 | 28 |
/** Represents context where the <code>net.java.html.json.Model</code> |
jaroslav@167 | 29 |
* and other objects |
jaroslav@5 | 30 |
* operate in. The context is usually a particular HTML page in a browser. |
jaroslav@167 | 31 |
* The context is also associated with the actual HTML technology |
jaroslav@5 | 32 |
* in the HTML page - there is likely to be different context for |
jaroslav@5 | 33 |
* <a href="http://knockoutjs.com">knockout.js</a> and different one |
jaroslav@5 | 34 |
* for <a href="http://angularjs.org">angular</a>. |
jaroslav@5 | 35 |
* |
jaroslav@5 | 36 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jaroslav@5 | 37 |
*/ |
jaroslav@110 | 38 |
public final class BrwsrCtx { |
jaroslav@110 | 39 |
private final CtxImpl impl; |
jaroslav@110 | 40 |
private BrwsrCtx(CtxImpl impl) { |
jaroslav@110 | 41 |
this.impl = impl; |
jaroslav@5 | 42 |
} |
jaroslav@6 | 43 |
static { |
jaroslav@110 | 44 |
new CtxAccssr() { |
jaroslav@6 | 45 |
@Override |
jaroslav@110 | 46 |
protected BrwsrCtx newContext(CtxImpl impl) { |
jaroslav@110 | 47 |
return new BrwsrCtx(impl); |
jaroslav@6 | 48 |
} |
jaroslav@6 | 49 |
|
jaroslav@6 | 50 |
@Override |
jaroslav@110 | 51 |
protected CtxImpl find(BrwsrCtx context) { |
jaroslav@110 | 52 |
return context.impl; |
jaroslav@6 | 53 |
} |
jaroslav@6 | 54 |
}; |
jaroslav@6 | 55 |
} |
jaroslav@5 | 56 |
/** Dummy context without binding to any real browser or technology. |
jaroslav@110 | 57 |
* Useful for simple unit testing of behavior of various business logic |
jaroslav@110 | 58 |
* code. |
jaroslav@5 | 59 |
*/ |
jaroslav@110 | 60 |
public static final BrwsrCtx EMPTY = Contexts.newBuilder().build(); |
jaroslav@39 | 61 |
|
jaroslav@39 | 62 |
/** Seeks for the default context that is associated with the requesting |
jaroslav@39 | 63 |
* class. If no suitable context is found, a warning message is |
jaroslav@39 | 64 |
* printed and {@link #EMPTY} context is returned. |
jaroslav@39 | 65 |
* |
jaroslav@39 | 66 |
* @param requestor the class that makes the request |
jaroslav@39 | 67 |
* @return appropriate context for the request |
jaroslav@39 | 68 |
*/ |
jaroslav@110 | 69 |
public static BrwsrCtx findDefault(Class<?> requestor) { |
jaroslav@110 | 70 |
org.apidesign.html.context.spi.Contexts.Builder cb = Contexts.newBuilder(); |
jaroslav@110 | 71 |
boolean found = false; |
jaroslav@121 | 72 |
|
jaroslav@121 | 73 |
ClassLoader l; |
jaroslav@121 | 74 |
try { |
jaroslav@121 | 75 |
l = requestor.getClassLoader(); |
jaroslav@121 | 76 |
} catch (SecurityException ex) { |
jaroslav@121 | 77 |
l = null; |
jaroslav@121 | 78 |
} |
jaroslav@121 | 79 |
|
jaroslav@121 | 80 |
for (org.apidesign.html.context.spi.Contexts.Provider cp : ServiceLoader.load( |
jaroslav@121 | 81 |
org.apidesign.html.context.spi.Contexts.Provider.class, l |
jaroslav@121 | 82 |
)) { |
jaroslav@110 | 83 |
cp.fillContext(cb, requestor); |
jaroslav@110 | 84 |
found = true; |
jaroslav@39 | 85 |
} |
jaroslav@113 | 86 |
try { |
jaroslav@113 | 87 |
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())) { |
jaroslav@113 | 88 |
cp.fillContext(cb, requestor); |
jaroslav@113 | 89 |
found = true; |
jaroslav@113 | 90 |
} |
jaroslav@113 | 91 |
} catch (SecurityException ex) { |
jaroslav@113 | 92 |
if (!found) { |
jaroslav@113 | 93 |
throw ex; |
jaroslav@113 | 94 |
} |
jaroslav@113 | 95 |
// if we have some data from regular provides, go on |
jaroslav@41 | 96 |
} |
jaroslav@110 | 97 |
if (!found) { |
jaroslav@110 | 98 |
// XXX: print out a warning |
jaroslav@110 | 99 |
return EMPTY; |
jaroslav@110 | 100 |
} |
jaroslav@110 | 101 |
return cb.build(); |
jaroslav@39 | 102 |
} |
jaroslav@39 | 103 |
|
jaroslav@5 | 104 |
} |