Exposing Fn.activate and Fn.activePresenter so they are available from exported packages
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 05 Nov 2013 23:06:32 +0100
changeset 3224a93f2679691
parent 321 3b55d5d459fb
child 323 86aabecda7a3
Exposing Fn.activate and Fn.activePresenter so they are available from exported packages
boot-fx/pom.xml
boot/pom.xml
boot/src/main/java/org/apidesign/html/boot/impl/FnContext.java
boot/src/main/java/org/apidesign/html/boot/impl/JavaScriptProcesor.java
boot/src/main/java/org/apidesign/html/boot/spi/Fn.java
json/pom.xml
ko-fx/pom.xml
ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java
ko-fx/src/test/java/org/apidesign/html/kofx/KOFx.java
     1.1 --- a/boot-fx/pom.xml	Tue Nov 05 13:05:36 2013 +0100
     1.2 +++ b/boot-fx/pom.xml	Tue Nov 05 23:06:32 2013 +0100
     1.3 @@ -29,6 +29,15 @@
     1.4                    <forkMode>always</forkMode>
     1.5                </configuration>
     1.6            </plugin>
     1.7 +         <plugin>
     1.8 +            <groupId>org.apache.maven.plugins</groupId>
     1.9 +            <artifactId>maven-compiler-plugin</artifactId>
    1.10 +            <version>2.3.2</version>
    1.11 +            <configuration>
    1.12 +               <source>1.7</source>
    1.13 +               <target>1.7</target>
    1.14 +            </configuration>
    1.15 +         </plugin>
    1.16        </plugins>
    1.17    </build>
    1.18    <dependencies>
     2.1 --- a/boot/pom.xml	Tue Nov 05 13:05:36 2013 +0100
     2.2 +++ b/boot/pom.xml	Tue Nov 05 23:06:32 2013 +0100
     2.3 @@ -22,6 +22,15 @@
     2.4                <groupId>org.apache.felix</groupId>
     2.5                <artifactId>maven-bundle-plugin</artifactId>
     2.6            </plugin>
     2.7 +         <plugin>
     2.8 +            <groupId>org.apache.maven.plugins</groupId>
     2.9 +            <artifactId>maven-compiler-plugin</artifactId>
    2.10 +            <version>2.3.2</version>
    2.11 +            <configuration>
    2.12 +               <source>1.7</source>
    2.13 +               <target>1.7</target>
    2.14 +            </configuration>
    2.15 +         </plugin>
    2.16        </plugins>
    2.17    </build>
    2.18    <dependencies>
     3.1 --- a/boot/src/main/java/org/apidesign/html/boot/impl/FnContext.java	Tue Nov 05 13:05:36 2013 +0100
     3.2 +++ b/boot/src/main/java/org/apidesign/html/boot/impl/FnContext.java	Tue Nov 05 23:06:32 2013 +0100
     3.3 @@ -20,15 +20,42 @@
     3.4   */
     3.5  package org.apidesign.html.boot.impl;
     3.6  
     3.7 +import java.io.Closeable;
     3.8 +import java.io.IOException;
     3.9 +import java.util.logging.Logger;
    3.10  import org.apidesign.html.boot.spi.Fn;
    3.11  
    3.12  /**
    3.13   *
    3.14   * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.15   */
    3.16 -public final class FnContext {
    3.17 -    private FnContext() {
    3.18 +public final class FnContext implements Closeable {
    3.19 +    private static final Logger LOG = Logger.getLogger(FnContext.class.getName());
    3.20 +
    3.21 +    private Fn.Presenter prev;
    3.22 +    private FnContext(Fn.Presenter p) {
    3.23 +        this.prev = p;
    3.24      }
    3.25 +
    3.26 +    @Override
    3.27 +    public void close() throws IOException {
    3.28 +        if (prev != null) {
    3.29 +            currentPresenter(prev);
    3.30 +            prev = null;
    3.31 +        }
    3.32 +    }
    3.33 +/*
    3.34 +    @Override
    3.35 +    protected void finalize() throws Throwable {
    3.36 +        if (prev != null) {
    3.37 +            LOG.warning("Unclosed context!");
    3.38 +        }
    3.39 +    }
    3.40 +*/
    3.41 +    public static Closeable activate(Fn.Presenter newP) {
    3.42 +        return new FnContext(currentPresenter(newP));
    3.43 +    }
    3.44 +    
    3.45      
    3.46      private static final ThreadLocal<Fn.Presenter> CURRENT = new ThreadLocal<Fn.Presenter>();
    3.47  
     4.1 --- a/boot/src/main/java/org/apidesign/html/boot/impl/JavaScriptProcesor.java	Tue Nov 05 13:05:36 2013 +0100
     4.2 +++ b/boot/src/main/java/org/apidesign/html/boot/impl/JavaScriptProcesor.java	Tue Nov 05 23:06:32 2013 +0100
     4.3 @@ -226,7 +226,7 @@
     4.4              source.append("    this.p = p;\n");
     4.5              source.append("  }\n");
     4.6              source.append("  final $JsCallbacks$ current() {\n");
     4.7 -            source.append("    org.apidesign.html.boot.spi.Fn.Presenter now = org.apidesign.html.boot.impl.FnContext.currentPresenter();\n");
     4.8 +            source.append("    org.apidesign.html.boot.spi.Fn.Presenter now = org.apidesign.html.boot.spi.Fn.activePresenter();\n");
     4.9              source.append("    if (now == p) return this;\n");
    4.10              source.append("    if (last != null && now == last.p) return last;\n");
    4.11              source.append("    return last = new $JsCallbacks$(now);\n");
    4.12 @@ -254,14 +254,8 @@
    4.13                      source.append(" arg").append(++cnt);
    4.14                      sep = ", ";
    4.15                  }
    4.16 -                source.append(")");
    4.17 -                sep = "\n throws ";
    4.18 -                for (TypeMirror thrwn : m.getThrownTypes()) {
    4.19 -                    source.append(sep).append(thrwn.toString());
    4.20 -                    sep = ",";
    4.21 -                }
    4.22 -                source.append(" {\n");
    4.23 -                source.append("    org.apidesign.html.boot.spi.Fn.Presenter $$prev = org.apidesign.html.boot.impl.FnContext.currentPresenter(p); try { \n");
    4.24 +                source.append(") throws Throwable {\n");
    4.25 +                source.append("    try (java.io.Closeable a = org.apidesign.html.boot.spi.Fn.activate(p)) { \n");
    4.26                  source.append("    ");
    4.27                  if (m.getReturnType().getKind() != TypeKind.VOID) {
    4.28                      source.append("return ");
    4.29 @@ -285,7 +279,7 @@
    4.30                  if (m.getReturnType().getKind() == TypeKind.VOID) {
    4.31                      source.append("    return null;\n");
    4.32                  }
    4.33 -                source.append("    } finally { org.apidesign.html.boot.impl.FnContext.currentPresenter($$prev); }\n");
    4.34 +                source.append("    }\n");
    4.35                  source.append("  }\n");
    4.36              }
    4.37              source.append("}\n");
     5.1 --- a/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java	Tue Nov 05 13:05:36 2013 +0100
     5.2 +++ b/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java	Tue Nov 05 23:06:32 2013 +0100
     5.3 @@ -20,8 +20,10 @@
     5.4   */
     5.5  package org.apidesign.html.boot.spi;
     5.6  
     5.7 +import java.io.Closeable;
     5.8  import java.io.Reader;
     5.9  import java.net.URL;
    5.10 +import net.java.html.js.JavaScriptBody;
    5.11  import org.apidesign.html.boot.impl.FnContext;
    5.12  
    5.13  /** Represents single JavaScript function that can be invoked. 
    5.14 @@ -60,6 +62,31 @@
    5.15          return FnContext.currentPresenter() == presenter;
    5.16      }
    5.17      
    5.18 +    /** The currently active presenter.
    5.19 +     * 
    5.20 +     * @return the currently active presenter or <code>null</code>
    5.21 +     * @since 0.7
    5.22 +     */
    5.23 +    public static Presenter activePresenter() {
    5.24 +        return FnContext.currentPresenter();
    5.25 +    }
    5.26 +    
    5.27 +    /** Activates given presenter. Used by the code generated by 
    5.28 +     * {@link JavaScriptBody} annotation: 
    5.29 +     * <pre>
    5.30 +     * try ({@link Closeable} c = Fn.activate(presenter)) {
    5.31 +     *   doCallsInPresenterContext();
    5.32 +     * }
    5.33 +     * </pre>
    5.34 +     * 
    5.35 +     * @param p the presenter that should be active until closable is closed
    5.36 +     * @return the closable to close
    5.37 +     * @since 0.7
    5.38 +     */
    5.39 +    public static Closeable activate(Presenter p) {
    5.40 +        return FnContext.activate(p);
    5.41 +    }
    5.42 +    
    5.43      /** Invokes the defined function with specified <code>this</code> and
    5.44       * appropriate arguments.
    5.45       * 
     6.1 --- a/json/pom.xml	Tue Nov 05 13:05:36 2013 +0100
     6.2 +++ b/json/pom.xml	Tue Nov 05 23:06:32 2013 +0100
     6.3 @@ -14,7 +14,7 @@
     6.4    <url>http://maven.apache.org</url>
     6.5    <properties>
     6.6      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     6.7 -    <publicPackages>net.java.html.json</publicPackages>
     6.8 +    <publicPackages>net.java.html.json,org.apidesign.html.json.spi</publicPackages>
     6.9    </properties>
    6.10    <build>
    6.11        <plugins>
    6.12 @@ -36,6 +36,15 @@
    6.13                    </execution>
    6.14                </executions>
    6.15            </plugin>
    6.16 +          <plugin>
    6.17 +              <groupId>org.apache.maven.plugins</groupId>
    6.18 +              <artifactId>maven-javadoc-plugin</artifactId>
    6.19 +              <version>2.9</version>
    6.20 +              <configuration>
    6.21 +                  <subpackages>net.java.html.json</subpackages>
    6.22 +                  <skip>false</skip>
    6.23 +              </configuration>
    6.24 +          </plugin>
    6.25        </plugins>
    6.26    </build>
    6.27    <dependencies>
     7.1 --- a/ko-fx/pom.xml	Tue Nov 05 13:05:36 2013 +0100
     7.2 +++ b/ko-fx/pom.xml	Tue Nov 05 23:06:32 2013 +0100
     7.3 @@ -28,6 +28,15 @@
     7.4                    <skip>false</skip>
     7.5                </configuration>
     7.6            </plugin>
     7.7 +         <plugin>
     7.8 +            <groupId>org.apache.maven.plugins</groupId>
     7.9 +            <artifactId>maven-compiler-plugin</artifactId>
    7.10 +            <version>2.3.2</version>
    7.11 +            <configuration>
    7.12 +               <source>1.7</source>
    7.13 +               <target>1.7</target>
    7.14 +            </configuration>
    7.15 +         </plugin>
    7.16        </plugins>
    7.17    </build>
    7.18    <dependencies>
    7.19 @@ -39,10 +48,9 @@
    7.20          <systemPath>${jfxrt.jar}</systemPath>
    7.21      </dependency>
    7.22      <dependency>
    7.23 -      <groupId>org.json</groupId>
    7.24 -      <artifactId>json</artifactId>
    7.25 -      <version>20090211</version>
    7.26 -      <type>jar</type>
    7.27 +        <groupId>de.twentyeleven.skysail</groupId>
    7.28 +        <artifactId>org.json-osgi</artifactId>
    7.29 +        <version>20080701</version>
    7.30      </dependency>
    7.31      <dependency>
    7.32        <groupId>org.apidesign.html</groupId>
     8.1 --- a/ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java	Tue Nov 05 13:05:36 2013 +0100
     8.2 +++ b/ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java	Tue Nov 05 23:06:32 2013 +0100
     8.3 @@ -20,6 +20,7 @@
     8.4   */
     8.5  package org.apidesign.html.kofx;
     8.6  
     8.7 +import java.io.Closeable;
     8.8  import java.io.IOException;
     8.9  import java.io.InputStream;
    8.10  import java.util.ServiceLoader;
    8.11 @@ -27,7 +28,6 @@
    8.12  import javafx.application.Platform;
    8.13  import net.java.html.js.JavaScriptBody;
    8.14  import netscape.javascript.JSObject;
    8.15 -import org.apidesign.html.boot.impl.FnContext;
    8.16  import org.apidesign.html.boot.spi.Fn;
    8.17  import org.apidesign.html.context.spi.Contexts;
    8.18  import org.apidesign.html.json.spi.FunctionBinding;
    8.19 @@ -151,13 +151,11 @@
    8.20      public void runSafe(final Runnable r) {
    8.21          class Wrap implements Runnable {
    8.22              @Override public void run() {
    8.23 -                Fn.Presenter prev = FnContext.currentPresenter(browserContext);
    8.24 -                try {
    8.25 +                try (Closeable c = Fn.activate(browserContext)) {
    8.26                      r.run();
    8.27 -                } finally {
    8.28 -                    FnContext.currentPresenter(prev);
    8.29 +                } catch (IOException ex) {
    8.30 +                    // cannot be thrown
    8.31                  }
    8.32 -                
    8.33              }
    8.34          }
    8.35          Wrap w = new Wrap();
    8.36 @@ -189,7 +187,7 @@
    8.37          @Override
    8.38          public void fillContext(Contexts.Builder context, Class<?> requestor) {
    8.39              if (isJavaScriptEnabled()) {
    8.40 -                FXContext c = new FXContext(FnContext.currentPresenter());
    8.41 +                FXContext c = new FXContext(Fn.activePresenter());
    8.42                  
    8.43                  context.register(Technology.class, c, 100);
    8.44                  context.register(Transfer.class, c, 100);
     9.1 --- a/ko-fx/src/test/java/org/apidesign/html/kofx/KOFx.java	Tue Nov 05 13:05:36 2013 +0100
     9.2 +++ b/ko-fx/src/test/java/org/apidesign/html/kofx/KOFx.java	Tue Nov 05 23:06:32 2013 +0100
     9.3 @@ -20,10 +20,10 @@
     9.4   */
     9.5  package org.apidesign.html.kofx;
     9.6  
     9.7 +import java.io.Closeable;
     9.8  import java.lang.reflect.InvocationTargetException;
     9.9  import java.lang.reflect.Method;
    9.10  import javafx.application.Platform;
    9.11 -import org.apidesign.html.boot.impl.FnContext;
    9.12  import org.apidesign.html.boot.spi.Fn;
    9.13  import org.testng.ITest;
    9.14  import org.testng.annotations.Test;
    9.15 @@ -66,8 +66,7 @@
    9.16      @Override
    9.17      public synchronized void run() {
    9.18          boolean notify = true;
    9.19 -        try {
    9.20 -            FnContext.currentPresenter(p);
    9.21 +        try (Closeable a = Fn.activate(p)) {
    9.22              if (inst == null) {
    9.23                  inst = m.getDeclaringClass().newInstance();
    9.24              }
    9.25 @@ -91,7 +90,6 @@
    9.26              if (notify) {
    9.27                  notifyAll();
    9.28              }
    9.29 -            FnContext.currentPresenter(null);
    9.30          }
    9.31      }
    9.32