context/src/main/java/org/apidesign/html/context/spi/Contexts.java
branchBrwsrCtxExec
changeset 574 7958c9c205d9
parent 551 7ca2253fa86d
child 575 3e6c5f24b12d
     1.1 --- a/context/src/main/java/org/apidesign/html/context/spi/Contexts.java	Fri Feb 07 07:44:34 2014 +0100
     1.2 +++ b/context/src/main/java/org/apidesign/html/context/spi/Contexts.java	Tue Mar 04 17:09:22 2014 +0100
     1.3 @@ -42,6 +42,7 @@
     1.4   */
     1.5  package org.apidesign.html.context.spi;
     1.6  
     1.7 +import java.util.ServiceLoader;
     1.8  import net.java.html.BrwsrCtx;
     1.9  import org.netbeans.html.context.impl.CtxImpl;
    1.10  
    1.11 @@ -77,6 +78,47 @@
    1.12          return CtxImpl.find(context, technology);
    1.13      }
    1.14  
    1.15 +    /** Seeks {@link ServiceLoader} for all registered instances of
    1.16 +     * {@link Provider} and asks them to {@link Provider#fillContext(org.apidesign.html.context.spi.Contexts.Builder, java.lang.Class) fill
    1.17 +     * the builder}.
    1.18 +     * 
    1.19 +     * @param requestor the application class for which to find the context
    1.20 +     * @param cb the context builder to register technologies into
    1.21 +     * @return <code>true</code>, if some instances of the provider were
    1.22 +     *    found, <code>false</code> otherwise
    1.23 +     * @since 0.7.6
    1.24 +     */
    1.25 +    public static boolean fillInByProviders(Class<?> requestor, Contexts.Builder cb) {
    1.26 +        boolean found = false;
    1.27 +        ClassLoader l;
    1.28 +        try {
    1.29 +            l = requestor.getClassLoader();
    1.30 +        } catch (SecurityException ex) {
    1.31 +            l = null;
    1.32 +        }
    1.33 +        for (Provider cp : ServiceLoader.load(Provider.class, l)) {
    1.34 +            cp.fillContext(cb, requestor);
    1.35 +            found = true;
    1.36 +        }
    1.37 +        try {
    1.38 +            for (Provider cp : ServiceLoader.load(Provider.class, Provider.class.getClassLoader())) {
    1.39 +                cp.fillContext(cb, requestor);
    1.40 +                found = true;
    1.41 +            }
    1.42 +        } catch (SecurityException ex) {
    1.43 +            if (!found) {
    1.44 +                throw ex;
    1.45 +            }
    1.46 +        }
    1.47 +        if (!found) {
    1.48 +            for (Provider cp : ServiceLoader.load(Provider.class)) {
    1.49 +                cp.fillContext(cb, requestor);
    1.50 +                found = true;
    1.51 +            }
    1.52 +        }
    1.53 +        return found;
    1.54 +    }
    1.55 +
    1.56      /** Implementors of various HTML technologies should
    1.57       * register their implementation via {@link java.util.ServiceProvider} so
    1.58       * {@link java.util.ServiceProvider} can find them, when their JARs are included
    1.59 @@ -144,4 +186,18 @@
    1.60              return impl.build();
    1.61          }
    1.62      }
    1.63 +    
    1.64 +    /** Injects a {@link BrwsrCtx} into a technology registered by
    1.65 +     * {@link Builder#register(java.lang.Class, java.lang.Object, int)} when
    1.66 +     * appropriate context is created.
    1.67 +     * 
    1.68 +     * @since 0.7.6
    1.69 +     */
    1.70 +    public interface CtxAware {
    1.71 +        /** Injects associated context into an technology.
    1.72 +         * 
    1.73 +         * @param ctx the context this technology is registed in
    1.74 +         */
    1.75 +        public void injectCtx(BrwsrCtx ctx);
    1.76 +    }
    1.77  }