1.1 --- a/context/src/main/java/org/apidesign/html/context/spi/Contexts.java Tue Aug 26 17:43:37 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,192 +0,0 @@
1.4 -/**
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
1.8 - *
1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 - * Other names may be trademarks of their respective owners.
1.11 - *
1.12 - * The contents of this file are subject to the terms of either the GNU
1.13 - * General Public License Version 2 only ("GPL") or the Common
1.14 - * Development and Distribution License("CDDL") (collectively, the
1.15 - * "License"). You may not use this file except in compliance with the
1.16 - * License. You can obtain a copy of the License at
1.17 - * http://www.netbeans.org/cddl-gplv2.html
1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 - * specific language governing permissions and limitations under the
1.20 - * License. When distributing the software, include this License Header
1.21 - * Notice in each file and include the License file at
1.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 - * particular file as subject to the "Classpath" exception as provided
1.24 - * by Oracle in the GPL Version 2 section of the License file that
1.25 - * accompanied this code. If applicable, add the following below the
1.26 - * License Header, with the fields enclosed by brackets [] replaced by
1.27 - * your own identifying information:
1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
1.29 - *
1.30 - * Contributor(s):
1.31 - *
1.32 - * The Original Software is NetBeans. The Initial Developer of the Original
1.33 - * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
1.34 - *
1.35 - * If you wish your version of this file to be governed by only the CDDL
1.36 - * or only the GPL Version 2, indicate your decision by adding
1.37 - * "[Contributor] elects to include this software in this distribution
1.38 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 - * single choice of license, a recipient has the option to distribute
1.40 - * your version of this file under either the CDDL, the GPL Version 2 or
1.41 - * to extend the choice of license to its licensees as provided above.
1.42 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 - * Version 2 license, then the option applies only if the new code is
1.44 - * made subject to such option by the copyright holder.
1.45 - */
1.46 -package org.apidesign.html.context.spi;
1.47 -
1.48 -import java.util.ServiceLoader;
1.49 -import net.java.html.BrwsrCtx;
1.50 -import org.netbeans.html.context.impl.CtxImpl;
1.51 -
1.52 -/** Factory class to assign various technologies
1.53 - * to a {@link BrwsrCtx browser context}. Start with {@link #newBuilder()}
1.54 - * and then assign technologies with {@link Builder#register(java.lang.Class, java.lang.Object, int)}
1.55 - * method.
1.56 - *
1.57 - * @author Jaroslav Tulach
1.58 - */
1.59 -public final class Contexts {
1.60 - private Contexts() {
1.61 - }
1.62 -
1.63 - /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the
1.64 - * end call the {@link Builder#build()} method to generate the context.
1.65 - *
1.66 - * @return new instance of the builder
1.67 - */
1.68 - public static Builder newBuilder() {
1.69 - return new Builder();
1.70 - }
1.71 -
1.72 - /** Seeks for the specified technology in the provided context.
1.73 - *
1.74 - * @param <Tech> type of the technology
1.75 - * @param context the context to seek in
1.76 - * (previously filled with ({@link Builder#register(java.lang.Class, java.lang.Object, int)})
1.77 - * @param technology class that identifies the technology
1.78 - * @return the technology in the context or <code>null</code>
1.79 - */
1.80 - public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
1.81 - return CtxImpl.find(context, technology);
1.82 - }
1.83 -
1.84 - /** Seeks {@link ServiceLoader} for all registered instances of
1.85 - * {@link Provider} and asks them to {@link Provider#fillContext(org.apidesign.html.context.spi.Contexts.Builder, java.lang.Class) fill
1.86 - * the builder}.
1.87 - *
1.88 - * @param requestor the application class for which to find the context
1.89 - * @param cb the context builder to register technologies into
1.90 - * @return <code>true</code>, if some instances of the provider were
1.91 - * found, <code>false</code> otherwise
1.92 - * @since 0.7.6
1.93 - */
1.94 - public static boolean fillInByProviders(Class<?> requestor, Contexts.Builder cb) {
1.95 - boolean found = false;
1.96 - ClassLoader l;
1.97 - try {
1.98 - l = requestor.getClassLoader();
1.99 - } catch (SecurityException ex) {
1.100 - l = null;
1.101 - }
1.102 - for (Provider cp : ServiceLoader.load(Provider.class, l)) {
1.103 - cp.fillContext(cb, requestor);
1.104 - found = true;
1.105 - }
1.106 - try {
1.107 - for (Provider cp : ServiceLoader.load(Provider.class, Provider.class.getClassLoader())) {
1.108 - cp.fillContext(cb, requestor);
1.109 - found = true;
1.110 - }
1.111 - } catch (SecurityException ex) {
1.112 - if (!found) {
1.113 - throw ex;
1.114 - }
1.115 - }
1.116 - if (!found) {
1.117 - for (Provider cp : ServiceLoader.load(Provider.class)) {
1.118 - cp.fillContext(cb, requestor);
1.119 - found = true;
1.120 - }
1.121 - }
1.122 - return found;
1.123 - }
1.124 -
1.125 - /** Implementors of various HTML technologies should
1.126 - * register their implementation via <code>org.openide.util.lookup.ServiceProvider</code>, so
1.127 - * {@link ServiceLoader} can find them, when their JARs are included
1.128 - * on the classpath of the running application.
1.129 - *
1.130 - * @author Jaroslav Tulach
1.131 - */
1.132 - public static interface Provider {
1.133 -
1.134 - /** Register into the context if suitable technology is
1.135 - * available for the requesting class.
1.136 - * The provider should check if its own technology is available in current
1.137 - * scope (e.g. proper JDK, proper browser, etc.). The provider
1.138 - * can also find the right context depending on requestor's classloader, etc.
1.139 - * <p>
1.140 - * Providers should use {@link Builder} to enrich appropriately
1.141 - * the context.
1.142 - *
1.143 - * @param context the context builder to fill with technologies
1.144 - * @param requestor the application class requesting access the the HTML page
1.145 - * @see BrwsrCtx#findDefault(java.lang.Class)
1.146 - */
1.147 - void fillContext(Builder context, Class<?> requestor);
1.148 - }
1.149 -
1.150 - /** Support for providers of new {@link BrwsrCtx}. Providers of different
1.151 - * technologies should be of particular interest in this class. End users
1.152 - * designing their application with existing technologies should rather
1.153 - * point their attention to {@link BrwsrCtx} and co.
1.154 - *
1.155 - * @author Jaroslav Tulach
1.156 - */
1.157 - public static final class Builder {
1.158 - private final CtxImpl impl = new CtxImpl();
1.159 -
1.160 - Builder() {
1.161 - }
1.162 -
1.163 - /** Registers new technology into the context. Each technology is
1.164 - * exactly identified by its implementation class and can be associated
1.165 - * with (positive) priority. In case of presence of multiple technologies
1.166 - * with the same class, the one with higher lower priority takes precedence.
1.167 - * @param <Tech> type of technology to register
1.168 - * @param type the real class of the technology type
1.169 - * @param impl an instance of the technology class
1.170 - * @param position the lower position, the more important implementation
1.171 - * which will be consulted sooner when seeking for a {@link Contexts#find(net.java.html.BrwsrCtx, java.lang.Class)}
1.172 - * an implementation
1.173 - * @return this builder
1.174 - */
1.175 - public <Tech> Builder register(Class<Tech> type, Tech impl, int position) {
1.176 - if (impl == null) {
1.177 - return this;
1.178 - }
1.179 - if (position <= 0) {
1.180 - throw new IllegalStateException();
1.181 - }
1.182 - this.impl.register(type, impl, position);
1.183 - return this;
1.184 - }
1.185 -
1.186 - /** Generates context based on values previously inserted into
1.187 - * this builder.
1.188 - *
1.189 - * @return new, immutable instance of {@link BrwsrCtx}
1.190 - */
1.191 - public BrwsrCtx build() {
1.192 - return impl.build();
1.193 - }
1.194 - }
1.195 -}