context/src/main/java/net/java/html/BrwsrCtx.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 26 Jun 2013 17:21:10 +0200
branchclassloader
changeset 167 10005a498972
parent 121 81e976eb0fc2
child 301 74e862ea824e
child 320 719062a62824
permissions -rw-r--r--
Javadoc for the context API
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    16  *
    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
    20  */
    21 package net.java.html;
    22 
    23 import java.util.ServiceLoader;
    24 import org.apidesign.html.context.impl.CtxAccssr;
    25 import org.apidesign.html.context.impl.CtxImpl;
    26 import org.apidesign.html.context.spi.Contexts;
    27 
    28 /** Represents context where the <code>net.java.html.json.Model</code>
    29  * and other objects
    30  * operate in. The context is usually a particular HTML page in a browser.
    31  * The context is also associated with the actual HTML technology
    32  * in the HTML page - there is likely to be different context for 
    33  * <a href="http://knockoutjs.com">knockout.js</a> and different one
    34  * for <a href="http://angularjs.org">angular</a>.
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 public final class BrwsrCtx {
    39     private final CtxImpl impl;
    40     private BrwsrCtx(CtxImpl impl) {
    41         this.impl = impl;
    42     }
    43     static {
    44         new CtxAccssr() {
    45             @Override
    46             protected BrwsrCtx newContext(CtxImpl impl) {
    47                 return new BrwsrCtx(impl);
    48             }
    49 
    50             @Override
    51             protected CtxImpl find(BrwsrCtx context) {
    52                 return context.impl;
    53             }
    54         };
    55     }
    56     /** Dummy context without binding to any real browser or technology. 
    57      * Useful for simple unit testing of behavior of various business logic
    58      * code.
    59      */
    60     public static final BrwsrCtx EMPTY = Contexts.newBuilder().build();
    61     
    62     /** Seeks for the default context that is associated with the requesting
    63      * class. If no suitable context is found, a warning message is
    64      * printed and {@link #EMPTY} context is returned.
    65      * 
    66      * @param requestor the class that makes the request
    67      * @return appropriate context for the request
    68      */
    69     public static BrwsrCtx findDefault(Class<?> requestor) {
    70         org.apidesign.html.context.spi.Contexts.Builder cb = Contexts.newBuilder();
    71         boolean found = false;
    72         
    73         ClassLoader l;
    74         try {
    75             l = requestor.getClassLoader();
    76         } catch (SecurityException ex) {
    77             l = null;
    78         }
    79         
    80         for (org.apidesign.html.context.spi.Contexts.Provider cp : ServiceLoader.load(
    81             org.apidesign.html.context.spi.Contexts.Provider.class, l
    82         )) {
    83             cp.fillContext(cb, requestor);
    84             found = true;
    85         }
    86         try {
    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())) {
    88                 cp.fillContext(cb, requestor);
    89                 found = true;
    90             }
    91         } catch (SecurityException ex) {
    92             if (!found) {
    93                 throw ex;
    94             }
    95             // if we have some data from regular provides, go on
    96         }
    97         if (!found) {
    98             // XXX: print out a warning
    99             return EMPTY;
   100         }
   101         return cb.build();
   102     }
   103     
   104 }