context/src/main/java/org/netbeans/html/context/spi/Contexts.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 29 Feb 2016 05:25:31 +0100
branchxhr4j
changeset 1057 b547f8f663f5
parent 927 3eb54c43c5c3
permissions -rw-r--r--
#257849: xhr4j module uses java.net package to handle @OnReceive requests to workaround CORS limitations
jaroslav@110
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@110
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@110
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@110
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@110
    42
 */
jtulach@838
    43
package org.netbeans.html.context.spi;
jaroslav@110
    44
jtulach@1057
    45
import java.lang.annotation.Documented;
jtulach@886
    46
import java.lang.annotation.ElementType;
jtulach@886
    47
import java.lang.annotation.Retention;
jtulach@886
    48
import java.lang.annotation.RetentionPolicy;
jtulach@886
    49
import java.lang.annotation.Target;
jtulach@927
    50
import java.util.HashSet;
jaroslav@574
    51
import java.util.ServiceLoader;
jtulach@927
    52
import java.util.Set;
jaroslav@110
    53
import net.java.html.BrwsrCtx;
jaroslav@362
    54
import org.netbeans.html.context.impl.CtxImpl;
jaroslav@110
    55
jaroslav@538
    56
/** Factory class to assign various technologies 
jaroslav@538
    57
 * to a {@link BrwsrCtx browser context}. Start with {@link #newBuilder()}
jaroslav@538
    58
 * and then assign technologies with {@link Builder#register(java.lang.Class, java.lang.Object, int)}
jaroslav@538
    59
 * method.
jaroslav@110
    60
 *
jtulach@655
    61
 * @author Jaroslav Tulach
jaroslav@110
    62
 */
jaroslav@110
    63
public final class Contexts {
jaroslav@110
    64
    private Contexts() {
jaroslav@110
    65
    }
jaroslav@110
    66
jaroslav@110
    67
    /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the
jaroslav@534
    68
     * end call the {@link Builder#build()} method to generate the context.
jtulach@886
    69
     * 
jtulach@886
    70
     * @param context instances of various classes or names of {@link Id technologies} 
jtulach@886
    71
     *    to be preferred and used in the built {@link BrwsrCtx context}.
jtulach@886
    72
     * @return new instance of the builder
jtulach@886
    73
     * @since 1.1
jtulach@886
    74
     */
jtulach@886
    75
    public static Builder newBuilder(Object... context) {
jtulach@886
    76
        return new Builder(context);
jtulach@886
    77
    }
jtulach@886
    78
    /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the
jtulach@886
    79
     * end call the {@link Builder#build()} method to generate the context.
jtulach@886
    80
     * Simply calls {@link #newBuilder(java.lang.Object...) newBuilder(new Object[0])}.
jtulach@886
    81
     * 
jaroslav@110
    82
     * @return new instance of the builder
jaroslav@110
    83
     */
jaroslav@110
    84
    public static Builder newBuilder() {
jtulach@886
    85
        return newBuilder(new Object[0]);
jaroslav@110
    86
    }
jaroslav@110
    87
jaroslav@110
    88
    /** Seeks for the specified technology in the provided context.
jaroslav@110
    89
     * 
jaroslav@110
    90
     * @param <Tech> type of the technology
jaroslav@110
    91
     * @param context the context to seek in 
jaroslav@110
    92
     *    (previously filled with ({@link Builder#register(java.lang.Class, java.lang.Object, int)})
jaroslav@110
    93
     * @param technology class that identifies the technology
jaroslav@534
    94
     * @return the technology in the context or <code>null</code>
jaroslav@110
    95
     */
jaroslav@110
    96
    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
jaroslav@110
    97
        return CtxImpl.find(context, technology);
jaroslav@110
    98
    }
jaroslav@110
    99
jaroslav@574
   100
    /** Seeks {@link ServiceLoader} for all registered instances of
jtulach@838
   101
     * {@link Provider} and asks them to {@link Provider#fillContext(org.netbeans.html.context.spi.Contexts.Builder, java.lang.Class) fill
jaroslav@574
   102
     * the builder}.
jaroslav@574
   103
     * 
jaroslav@574
   104
     * @param requestor the application class for which to find the context
jaroslav@574
   105
     * @param cb the context builder to register technologies into
jaroslav@574
   106
     * @return <code>true</code>, if some instances of the provider were
jaroslav@574
   107
     *    found, <code>false</code> otherwise
jaroslav@574
   108
     * @since 0.7.6
jaroslav@574
   109
     */
jaroslav@574
   110
    public static boolean fillInByProviders(Class<?> requestor, Contexts.Builder cb) {
jaroslav@574
   111
        boolean found = false;
jaroslav@574
   112
        ClassLoader l;
jaroslav@574
   113
        try {
jaroslav@574
   114
            l = requestor.getClassLoader();
jaroslav@574
   115
        } catch (SecurityException ex) {
jaroslav@574
   116
            l = null;
jaroslav@574
   117
        }
jtulach@927
   118
        Set<Class<?>> classes = new HashSet<Class<?>>();
jaroslav@574
   119
        for (Provider cp : ServiceLoader.load(Provider.class, l)) {
jtulach@927
   120
            if (!classes.add(cp.getClass())) {
jtulach@927
   121
                continue;
jtulach@927
   122
            }
jaroslav@574
   123
            cp.fillContext(cb, requestor);
jaroslav@574
   124
            found = true;
jaroslav@574
   125
        }
jaroslav@574
   126
        try {
jaroslav@574
   127
            for (Provider cp : ServiceLoader.load(Provider.class, Provider.class.getClassLoader())) {
jtulach@927
   128
                if (!classes.add(cp.getClass())) {
jtulach@927
   129
                    continue;
jtulach@927
   130
                }
jaroslav@574
   131
                cp.fillContext(cb, requestor);
jaroslav@574
   132
                found = true;
jaroslav@574
   133
            }
jaroslav@574
   134
        } catch (SecurityException ex) {
jaroslav@574
   135
            if (!found) {
jaroslav@574
   136
                throw ex;
jaroslav@574
   137
            }
jaroslav@574
   138
        }
jaroslav@574
   139
        if (!found) {
jaroslav@574
   140
            for (Provider cp : ServiceLoader.load(Provider.class)) {
jtulach@927
   141
                if (!classes.add(cp.getClass())) {
jtulach@927
   142
                    continue;
jtulach@927
   143
                }
jaroslav@574
   144
                cp.fillContext(cb, requestor);
jaroslav@574
   145
                found = true;
jaroslav@574
   146
            }
jaroslav@574
   147
        }
jaroslav@574
   148
        return found;
jaroslav@574
   149
    }
jtulach@886
   150
    
jtulach@886
   151
    /** Identifies the technologies passed to {@link Builder context builder}
jtulach@886
   152
     * by a name. Each implementation of a technology 
jtulach@886
   153
     * {@link Builder#register(java.lang.Class, java.lang.Object, int) registered into a context}
jtulach@886
   154
     * can be annotated with a name (or multiple names). Such implementation
jtulach@886
   155
     * will later be 
jtulach@886
   156
     * {@link Contexts#fillInByProviders(java.lang.Class, org.netbeans.html.context.spi.Contexts.Builder)  preferred during lookup}
jtulach@886
   157
     * if their name(s) has been requested in when 
jtulach@886
   158
     * {@link Contexts#newBuilder(java.lang.Object...)  creating a context}.
jtulach@886
   159
     * @since 1.1
jtulach@886
   160
     */
jtulach@886
   161
    @Retention(RetentionPolicy.RUNTIME)
jtulach@886
   162
    @Target(ElementType.TYPE)
jtulach@1057
   163
    @Documented
jtulach@886
   164
    public @interface Id {
jtulach@904
   165
        /** Identifier(s) for the implementation. 
jtulach@904
   166
         * 
jtulach@904
   167
         * @return one of more identifiers giving this implementation name(s)
jtulach@904
   168
         */
jtulach@886
   169
        public String[] value();
jtulach@886
   170
    }
jaroslav@574
   171
jaroslav@110
   172
    /** Implementors of various HTML technologies should
jtulach@790
   173
     * register their implementation via <code>org.openide.util.lookup.ServiceProvider</code>, so
jtulach@790
   174
     * {@link ServiceLoader} can find them, when their JARs are included
jaroslav@110
   175
     * on the classpath of the running application.
jaroslav@110
   176
     *
jtulach@655
   177
     * @author Jaroslav Tulach
jaroslav@110
   178
     */
jaroslav@110
   179
    public static interface Provider {
jaroslav@110
   180
jaroslav@110
   181
        /** Register into the context if suitable technology is
jaroslav@110
   182
         * available for the requesting class.
jaroslav@110
   183
         * The provider should check if its own technology is available in current
jaroslav@110
   184
         * scope (e.g. proper JDK, proper browser, etc.). The provider
jaroslav@110
   185
         * can also find the right context depending on requestor's classloader, etc.
jaroslav@110
   186
         * <p>
jaroslav@110
   187
         * Providers should use {@link Builder} to enrich appropriately
jaroslav@110
   188
         * the context.
jaroslav@110
   189
         *
jaroslav@110
   190
         * @param context the context builder to fill with technologies
jaroslav@110
   191
         * @param requestor the application class requesting access the the HTML page
jaroslav@110
   192
         * @see BrwsrCtx#findDefault(java.lang.Class)
jaroslav@110
   193
         */
jaroslav@110
   194
        void fillContext(Builder context, Class<?> requestor);
jaroslav@110
   195
    }
jaroslav@110
   196
jaroslav@110
   197
    /** Support for providers of new {@link BrwsrCtx}. Providers of different
jaroslav@110
   198
     * technologies should be of particular interest in this class. End users
jaroslav@110
   199
     * designing their application with existing technologies should rather
jaroslav@110
   200
     * point their attention to {@link BrwsrCtx} and co.
jaroslav@110
   201
     *
jtulach@655
   202
     * @author Jaroslav Tulach
jaroslav@110
   203
     */
jaroslav@110
   204
    public static final class Builder {
jtulach@886
   205
        private final CtxImpl impl;
jaroslav@110
   206
jtulach@886
   207
        public Builder(Object[] context) {
jtulach@886
   208
            this.impl = new CtxImpl(context);
jaroslav@110
   209
        }
jaroslav@110
   210
        
jaroslav@110
   211
        /** Registers new technology into the context. Each technology is
jaroslav@110
   212
         * exactly identified by its implementation class and can be associated
jaroslav@110
   213
         * with (positive) priority. In case of presence of multiple technologies
jaroslav@110
   214
         * with the same class, the one with higher lower priority takes precedence.
jaroslav@256
   215
         * @param <Tech> type of technology to register
jaroslav@256
   216
         * @param type the real class of the technology type
jaroslav@256
   217
         * @param impl an instance of the technology class
jaroslav@256
   218
         * @param position the lower position, the more important implementation 
jaroslav@256
   219
         *    which will be consulted sooner when seeking for a {@link Contexts#find(net.java.html.BrwsrCtx, java.lang.Class)}
jaroslav@256
   220
         *    an implementation
jaroslav@256
   221
         * @return this builder
jaroslav@110
   222
         */
jaroslav@256
   223
        public <Tech> Builder register(Class<Tech> type, Tech impl, int position) {
jaroslav@589
   224
            if (impl == null) {
jaroslav@589
   225
                return this;
jaroslav@589
   226
            }
jaroslav@256
   227
            if (position <= 0) {
jaroslav@110
   228
                throw new IllegalStateException();
jaroslav@110
   229
            }
jaroslav@256
   230
            this.impl.register(type, impl, position);
jaroslav@110
   231
            return this;
jaroslav@110
   232
        }
jaroslav@110
   233
jaroslav@110
   234
        /** Generates context based on values previously inserted into
jaroslav@110
   235
         * this builder.
jaroslav@110
   236
         *
jaroslav@110
   237
         * @return new, immutable instance of {@link BrwsrCtx}
jaroslav@110
   238
         */
jaroslav@110
   239
        public BrwsrCtx build() {
jaroslav@110
   240
            return impl.build();
jaroslav@110
   241
        }
jaroslav@110
   242
    }
jaroslav@110
   243
}