context/src/main/java/org/apidesign/html/context/spi/Contexts.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 28 May 2013 13:31:42 +0200
branchcontext
changeset 110 21bf4126e3a9
child 256 59202f396b49
permissions -rw-r--r--
Moving Context into its own separate module, renaming to BrwsrCtx and making it flexible
     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 org.apidesign.html.context.spi;
    22 
    23 import net.java.html.BrwsrCtx;
    24 import org.apidesign.html.context.impl.CtxImpl;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    29  */
    30 public final class Contexts {
    31     private Contexts() {
    32     }
    33 
    34     /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the
    35      * end call the {@link #build()} method to generate the context.
    36      *
    37      * @return new instance of the builder
    38      */
    39     public static Builder newBuilder() {
    40         return new Builder();
    41     }
    42 
    43     /** Seeks for the specified technology in the provided context.
    44      * 
    45      * @param <Tech> type of the technology
    46      * @param context the context to seek in 
    47      *    (previously filled with ({@link Builder#register(java.lang.Class, java.lang.Object, int)})
    48      * @param technology class that identifies the technology
    49      * @return 
    50      */
    51     public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
    52         return CtxImpl.find(context, technology);
    53     }
    54 
    55     /** Implementors of various HTML technologies should
    56      * register their implementation via {@link ServiceProvider} so
    57      * {@link ServiceProvider} can find them, when their JARs are included
    58      * on the classpath of the running application.
    59      *
    60      * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    61      */
    62     public static interface Provider {
    63 
    64         /** Register into the context if suitable technology is
    65          * available for the requesting class.
    66          * The provider should check if its own technology is available in current
    67          * scope (e.g. proper JDK, proper browser, etc.). The provider
    68          * can also find the right context depending on requestor's classloader, etc.
    69          * <p>
    70          * Providers should use {@link Builder} to enrich appropriately
    71          * the context.
    72          *
    73          * @param context the context builder to fill with technologies
    74          * @param requestor the application class requesting access the the HTML page
    75          * @see BrwsrCtx#findDefault(java.lang.Class)
    76          */
    77         void fillContext(Builder context, Class<?> requestor);
    78     }
    79 
    80     /** Support for providers of new {@link BrwsrCtx}. Providers of different
    81      * technologies should be of particular interest in this class. End users
    82      * designing their application with existing technologies should rather
    83      * point their attention to {@link BrwsrCtx} and co.
    84      *
    85      * @author Jaroslav Tulach <jtulach@netbeans.org>
    86      */
    87     public static final class Builder {
    88         private CtxImpl impl = new CtxImpl();
    89 
    90         Builder() {
    91         }
    92         
    93         /** Registers new technology into the context. Each technology is
    94          * exactly identified by its implementation class and can be associated
    95          * with (positive) priority. In case of presence of multiple technologies
    96          * with the same class, the one with higher lower priority takes precedence.
    97          */
    98         public <Tech> Builder register(Class<Tech> type, Tech impl, int priority) {
    99             if (priority <= 0) {
   100                 throw new IllegalStateException();
   101             }
   102             this.impl.register(type, impl, priority);
   103             return this;
   104         }
   105 
   106         /** Generates context based on values previously inserted into
   107          * this builder.
   108          *
   109          * @return new, immutable instance of {@link BrwsrCtx}
   110          */
   111         public BrwsrCtx build() {
   112             return impl.build();
   113         }
   114     }
   115 }