A way to find default context inside a browser
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 26 Apr 2013 13:45:46 +0200
changeset 39d1d08fe93ae3
parent 38 b327590acf26
child 40 fb3fa16fb606
A way to find default context inside a browser
json/src/main/java/net/java/html/json/Context.java
json/src/main/java/org/apidesign/html/json/spi/ContextProvider.java
     1.1 --- a/json/src/main/java/net/java/html/json/Context.java	Thu Apr 25 16:41:13 2013 +0200
     1.2 +++ b/json/src/main/java/net/java/html/json/Context.java	Fri Apr 26 13:45:46 2013 +0200
     1.3 @@ -20,10 +20,10 @@
     1.4   */
     1.5  package net.java.html.json;
     1.6  
     1.7 +import java.util.ServiceLoader;
     1.8  import org.apidesign.html.json.impl.ContextAccessor;
     1.9  import org.apidesign.html.json.spi.ContextBuilder;
    1.10 -import org.apidesign.html.json.spi.FunctionBinding;
    1.11 -import org.apidesign.html.json.spi.PropertyBinding;
    1.12 +import org.apidesign.html.json.spi.ContextProvider;
    1.13  import org.apidesign.html.json.spi.Technology;
    1.14  import org.apidesign.html.json.spi.Transfer;
    1.15  
    1.16 @@ -68,4 +68,23 @@
    1.17       * Useful for simple unit testing of behavior of model classes.
    1.18       */
    1.19      public static final Context EMPTY = ContextBuilder.create().build();
    1.20 +    
    1.21 +    /** Seeks for the default context that is associated with the requesting
    1.22 +     * class. If no suitable context is found, a warning message is
    1.23 +     * printed and {@link #EMPTY} context is returned.
    1.24 +     * 
    1.25 +     * @param requestor the class that makes the request
    1.26 +     * @return appropriate context for the request
    1.27 +     */
    1.28 +    public static Context findDefault(Class<?> requestor) {
    1.29 +        for (ContextProvider cp : ServiceLoader.load(ContextProvider.class)) {
    1.30 +            Context c = cp.findContext(requestor);
    1.31 +            if (c != null) {
    1.32 +                return c;
    1.33 +            }
    1.34 +        }
    1.35 +        // XXX: print out a warning
    1.36 +        return Context.EMPTY;
    1.37 +    }
    1.38 +    
    1.39  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/ContextProvider.java	Fri Apr 26 13:45:46 2013 +0200
     2.3 @@ -0,0 +1,46 @@
     2.4 +/**
     2.5 + * HTML via Java(tm) Language Bindings
     2.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details. apidesign.org
    2.16 + * designates this particular file as subject to the
    2.17 + * "Classpath" exception as provided by apidesign.org
    2.18 + * in the License file that accompanied this code.
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License
    2.21 + * along with this program. Look for COPYING file in the top folder.
    2.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    2.23 + */
    2.24 +package org.apidesign.html.json.spi;
    2.25 +
    2.26 +import net.java.html.json.Context;
    2.27 +import org.openide.util.lookup.ServiceProvider;
    2.28 +
    2.29 +/** Implementors of various {@link Technology technologies} should
    2.30 + * register their implementation via {@link ServiceProvider} so 
    2.31 + * {@link ServiceProvider} can find them, when their JARs are included
    2.32 + * on the classpath of the running application.
    2.33 + *
    2.34 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    2.35 + */
    2.36 +public interface ContextProvider {
    2.37 +    /** Identify the context suitable for provided requesting class. 
    2.38 +     * The provider should check if its own technology is available in current
    2.39 +     * scope (e.g. proper JDK, proper browser, etc.). The provider
    2.40 +     * can also find the right context depending on requestor's classloader, etc.
    2.41 +     * <p>
    2.42 +     * Providers should use {@link ContextBuilder} to construct appropriately
    2.43 +     * configured context.
    2.44 +     * 
    2.45 +     * @param requestor the application class requesting access the the HTML page
    2.46 +     * @return 
    2.47 +     */
    2.48 +    Context findContext(Class<?> requestor);
    2.49 +}