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
jaroslav@110
     1
/**
jaroslav@110
     2
 * HTML via Java(tm) Language Bindings
jaroslav@110
     3
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@110
     4
 *
jaroslav@110
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@110
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@110
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@110
     8
 *
jaroslav@110
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@110
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@110
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@110
    12
 * GNU General Public License for more details. apidesign.org
jaroslav@110
    13
 * designates this particular file as subject to the
jaroslav@110
    14
 * "Classpath" exception as provided by apidesign.org
jaroslav@110
    15
 * in the License file that accompanied this code.
jaroslav@110
    16
 *
jaroslav@110
    17
 * You should have received a copy of the GNU General Public License
jaroslav@110
    18
 * along with this program. Look for COPYING file in the top folder.
jaroslav@110
    19
 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
jaroslav@110
    20
 */
jaroslav@110
    21
package org.apidesign.html.context.spi;
jaroslav@110
    22
jaroslav@110
    23
import net.java.html.BrwsrCtx;
jaroslav@110
    24
import org.apidesign.html.context.impl.CtxImpl;
jaroslav@110
    25
jaroslav@110
    26
/**
jaroslav@110
    27
 *
jaroslav@110
    28
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@110
    29
 */
jaroslav@110
    30
public final class Contexts {
jaroslav@110
    31
    private Contexts() {
jaroslav@110
    32
    }
jaroslav@110
    33
jaroslav@110
    34
    /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the
jaroslav@110
    35
     * end call the {@link #build()} method to generate the context.
jaroslav@110
    36
     *
jaroslav@110
    37
     * @return new instance of the builder
jaroslav@110
    38
     */
jaroslav@110
    39
    public static Builder newBuilder() {
jaroslav@110
    40
        return new Builder();
jaroslav@110
    41
    }
jaroslav@110
    42
jaroslav@110
    43
    /** Seeks for the specified technology in the provided context.
jaroslav@110
    44
     * 
jaroslav@110
    45
     * @param <Tech> type of the technology
jaroslav@110
    46
     * @param context the context to seek in 
jaroslav@110
    47
     *    (previously filled with ({@link Builder#register(java.lang.Class, java.lang.Object, int)})
jaroslav@110
    48
     * @param technology class that identifies the technology
jaroslav@110
    49
     * @return 
jaroslav@110
    50
     */
jaroslav@110
    51
    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
jaroslav@110
    52
        return CtxImpl.find(context, technology);
jaroslav@110
    53
    }
jaroslav@110
    54
jaroslav@110
    55
    /** Implementors of various HTML technologies should
jaroslav@110
    56
     * register their implementation via {@link ServiceProvider} so
jaroslav@110
    57
     * {@link ServiceProvider} can find them, when their JARs are included
jaroslav@110
    58
     * on the classpath of the running application.
jaroslav@110
    59
     *
jaroslav@110
    60
     * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@110
    61
     */
jaroslav@110
    62
    public static interface Provider {
jaroslav@110
    63
jaroslav@110
    64
        /** Register into the context if suitable technology is
jaroslav@110
    65
         * available for the requesting class.
jaroslav@110
    66
         * The provider should check if its own technology is available in current
jaroslav@110
    67
         * scope (e.g. proper JDK, proper browser, etc.). The provider
jaroslav@110
    68
         * can also find the right context depending on requestor's classloader, etc.
jaroslav@110
    69
         * <p>
jaroslav@110
    70
         * Providers should use {@link Builder} to enrich appropriately
jaroslav@110
    71
         * the context.
jaroslav@110
    72
         *
jaroslav@110
    73
         * @param context the context builder to fill with technologies
jaroslav@110
    74
         * @param requestor the application class requesting access the the HTML page
jaroslav@110
    75
         * @see BrwsrCtx#findDefault(java.lang.Class)
jaroslav@110
    76
         */
jaroslav@110
    77
        void fillContext(Builder context, Class<?> requestor);
jaroslav@110
    78
    }
jaroslav@110
    79
jaroslav@110
    80
    /** Support for providers of new {@link BrwsrCtx}. Providers of different
jaroslav@110
    81
     * technologies should be of particular interest in this class. End users
jaroslav@110
    82
     * designing their application with existing technologies should rather
jaroslav@110
    83
     * point their attention to {@link BrwsrCtx} and co.
jaroslav@110
    84
     *
jaroslav@110
    85
     * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@110
    86
     */
jaroslav@110
    87
    public static final class Builder {
jaroslav@110
    88
        private CtxImpl impl = new CtxImpl();
jaroslav@110
    89
jaroslav@110
    90
        Builder() {
jaroslav@110
    91
        }
jaroslav@110
    92
        
jaroslav@110
    93
        /** Registers new technology into the context. Each technology is
jaroslav@110
    94
         * exactly identified by its implementation class and can be associated
jaroslav@110
    95
         * with (positive) priority. In case of presence of multiple technologies
jaroslav@110
    96
         * with the same class, the one with higher lower priority takes precedence.
jaroslav@110
    97
         */
jaroslav@110
    98
        public <Tech> Builder register(Class<Tech> type, Tech impl, int priority) {
jaroslav@110
    99
            if (priority <= 0) {
jaroslav@110
   100
                throw new IllegalStateException();
jaroslav@110
   101
            }
jaroslav@110
   102
            this.impl.register(type, impl, priority);
jaroslav@110
   103
            return this;
jaroslav@110
   104
        }
jaroslav@110
   105
jaroslav@110
   106
        /** Generates context based on values previously inserted into
jaroslav@110
   107
         * this builder.
jaroslav@110
   108
         *
jaroslav@110
   109
         * @return new, immutable instance of {@link BrwsrCtx}
jaroslav@110
   110
         */
jaroslav@110
   111
        public BrwsrCtx build() {
jaroslav@110
   112
            return impl.build();
jaroslav@110
   113
        }
jaroslav@110
   114
    }
jaroslav@110
   115
}