ko-osgi-test/src/main/java/org/netbeans/html/ko/osgi/test/KnockoutEquinoxTCKImpl.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 444 03eeeb863fa1
child 574 7958c9c205d9
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@370
     1
/**
jaroslav@370
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@370
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@370
     5
 *
jaroslav@370
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@370
     7
 * Other names may be trademarks of their respective owners.
jaroslav@370
     8
 *
jaroslav@370
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@370
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@370
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@370
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@370
    13
 * License. You can obtain a copy of the License at
jaroslav@370
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@370
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@370
    16
 * specific language governing permissions and limitations under the
jaroslav@370
    17
 * License.  When distributing the software, include this License Header
jaroslav@370
    18
 * Notice in each file and include the License file at
jaroslav@370
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@370
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@370
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@370
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@370
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@370
    24
 * your own identifying information:
jaroslav@370
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@370
    26
 *
jaroslav@370
    27
 * Contributor(s):
jaroslav@370
    28
 *
jaroslav@370
    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@370
    31
 *
jaroslav@370
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@370
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@370
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@370
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@370
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@370
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@370
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@370
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@370
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@370
    41
 * made subject to such option by the copyright holder.
jaroslav@370
    42
 */
jaroslav@370
    43
package org.netbeans.html.ko.osgi.test;
jaroslav@370
    44
jaroslav@371
    45
import java.io.BufferedReader;
jaroslav@370
    46
import java.io.IOException;
jaroslav@371
    47
import java.io.InputStreamReader;
jaroslav@444
    48
import java.lang.reflect.Constructor;
jaroslav@371
    49
import java.lang.reflect.Method;
jaroslav@371
    50
import java.net.URI;
jaroslav@371
    51
import java.net.URISyntaxException;
jaroslav@371
    52
import java.net.URL;
jaroslav@371
    53
import java.net.URLConnection;
jaroslav@370
    54
import java.util.Map;
jaroslav@371
    55
import java.util.concurrent.Callable;
jaroslav@396
    56
import java.util.concurrent.Executors;
jaroslav@370
    57
import net.java.html.BrwsrCtx;
jaroslav@396
    58
import net.java.html.boot.BrowserBuilder;
jaroslav@371
    59
import net.java.html.js.JavaScriptBody;
jaroslav@371
    60
import org.apidesign.html.boot.spi.Fn;
jaroslav@371
    61
import org.apidesign.html.context.spi.Contexts;
jaroslav@371
    62
import org.apidesign.html.json.spi.Technology;
jaroslav@371
    63
import org.apidesign.html.json.spi.Transfer;
jaroslav@371
    64
import org.apidesign.html.json.tck.KnockoutTCK;
jaroslav@371
    65
import org.json.JSONException;
jaroslav@371
    66
import org.json.JSONObject;
jaroslav@371
    67
import org.openide.util.lookup.ServiceProvider;
jaroslav@396
    68
import org.osgi.framework.Bundle;
jaroslav@396
    69
import org.osgi.framework.BundleContext;
jaroslav@396
    70
import org.osgi.framework.FrameworkUtil;
jaroslav@370
    71
jaroslav@370
    72
/**
jaroslav@370
    73
 *
jaroslav@370
    74
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@370
    75
 */
jaroslav@371
    76
@ServiceProvider(service = KnockoutTCK.class)
jaroslav@391
    77
public class KnockoutEquinoxTCKImpl extends KnockoutTCK implements Callable<Class[]> {
jaroslav@371
    78
    
jaroslav@371
    79
    private static Fn.Presenter browserContext;
jaroslav@371
    80
jaroslav@396
    81
    public static Class loadOSGiClass(String name, BundleContext ctx) throws Exception {
jaroslav@396
    82
        for (Bundle b : ctx.getBundles()) {
jaroslav@396
    83
            try {
jaroslav@396
    84
                Class<?> osgiClass = b.loadClass(name);
jaroslav@396
    85
                if (osgiClass != null && osgiClass.getClassLoader() != ClassLoader.getSystemClassLoader()) {
jaroslav@396
    86
                    return osgiClass;
jaroslav@396
    87
                }
jaroslav@396
    88
            } catch (ClassNotFoundException cnfe) {
jaroslav@396
    89
                // go on
jaroslav@396
    90
            }
jaroslav@396
    91
        }
jaroslav@396
    92
        throw new IllegalStateException("Cannot load " + name + " from the OSGi container!");
jaroslav@396
    93
    }
jaroslav@396
    94
jaroslav@391
    95
    @Override
jaroslav@391
    96
    public Class[] call() throws Exception {
jaroslav@391
    97
        return testClasses();
jaroslav@371
    98
    }
jaroslav@396
    99
    
jaroslav@396
   100
    public static void start(URI server) throws Exception {
jaroslav@396
   101
        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(KnockoutEquinoxTCKImpl.class).
jaroslav@396
   102
            loadPage(server.toString()).
jaroslav@396
   103
            invoke("initialized");
jaroslav@396
   104
jaroslav@396
   105
        Executors.newSingleThreadExecutor().submit(new Runnable() {
jaroslav@396
   106
            @Override
jaroslav@396
   107
            public void run() {
jaroslav@396
   108
                try {
jaroslav@396
   109
                    final ClassLoader osgiClassLoader = BrowserBuilder.class.getClassLoader();
jaroslav@396
   110
                    Thread.currentThread().setContextClassLoader(osgiClassLoader);
jaroslav@396
   111
                    bb.showAndWait();
jaroslav@396
   112
                } catch (Throwable t) {
jaroslav@396
   113
                    t.printStackTrace();
jaroslav@396
   114
                }
jaroslav@396
   115
            }
jaroslav@396
   116
        });
jaroslav@396
   117
    }
jaroslav@371
   118
jaroslav@391
   119
    public static void initialized() throws Exception {
jaroslav@398
   120
        Bundle bundle = FrameworkUtil.getBundle(KnockoutEquinoxTCKImpl.class);
jaroslav@398
   121
        if (bundle == null) {
jaroslav@398
   122
            throw new IllegalStateException(
jaroslav@398
   123
                "Should be loaded from a bundle. But was: " + KnockoutEquinoxTCKImpl.class.getClassLoader()
jaroslav@398
   124
            );
jaroslav@398
   125
        }
jaroslav@391
   126
        Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(
jaroslav@391
   127
            "org.netbeans.html.ko.osgi.test.KnockoutEquinoxIT"
jaroslav@391
   128
        );
jaroslav@396
   129
        Method m = classpathClass.getMethod("initialized", Class.class, Object.class);
jaroslav@391
   130
        browserContext = Fn.activePresenter();
jaroslav@396
   131
        m.invoke(null, KnockoutEquinoxTCKImpl.class, browserContext);
jaroslav@371
   132
    }
jaroslav@371
   133
    
jaroslav@371
   134
    @Override
jaroslav@371
   135
    public BrwsrCtx createContext() {
jaroslav@391
   136
        try {
jaroslav@396
   137
            Class<?> fxCls = loadOSGiClass(
jaroslav@444
   138
                "org.netbeans.html.ko4j.FXContext",
jaroslav@396
   139
                FrameworkUtil.getBundle(KnockoutEquinoxTCKImpl.class).getBundleContext()
jaroslav@396
   140
            );
jaroslav@444
   141
            final Constructor<?> cnstr = fxCls.getConstructor(Fn.Presenter.class);
jaroslav@444
   142
            cnstr.setAccessible(true);
jaroslav@444
   143
            Object fx = cnstr.newInstance(browserContext);
jaroslav@391
   144
            Contexts.Builder cb = Contexts.newBuilder().
jaroslav@391
   145
                register(Technology.class, (Technology)fx, 10).
jaroslav@391
   146
                register(Transfer.class, (Transfer)fx, 10);
jaroslav@371
   147
//        if (fx.areWebSocketsSupported()) {
jaroslav@371
   148
//            cb.register(WSTransfer.class, fx, 10);
jaroslav@371
   149
//        }
jaroslav@391
   150
            return cb.build();
jaroslav@391
   151
        } catch (Exception ex) {
jaroslav@391
   152
            throw new IllegalStateException(ex);
jaroslav@391
   153
        }
jaroslav@371
   154
    }
jaroslav@371
   155
jaroslav@371
   156
    @Override
jaroslav@371
   157
    public Object createJSON(Map<String, Object> values) {
jaroslav@371
   158
        JSONObject json = new JSONObject();
jaroslav@371
   159
        for (Map.Entry<String, Object> entry : values.entrySet()) {
jaroslav@371
   160
            try {
jaroslav@371
   161
                json.put(entry.getKey(), entry.getValue());
jaroslav@371
   162
            } catch (JSONException ex) {
jaroslav@371
   163
                throw new IllegalStateException(ex);
jaroslav@371
   164
            }
jaroslav@371
   165
        }
jaroslav@371
   166
        return json;
jaroslav@371
   167
    }
jaroslav@371
   168
jaroslav@371
   169
    @Override
jaroslav@371
   170
    @JavaScriptBody(args = { "s", "args" }, body = ""
jaroslav@371
   171
        + "var f = new Function(s); "
jaroslav@371
   172
        + "return f.apply(null, args);"
jaroslav@371
   173
    )
jaroslav@371
   174
    public native Object executeScript(String script, Object[] arguments);
jaroslav@371
   175
jaroslav@371
   176
    @JavaScriptBody(args = {  }, body = 
jaroslav@371
   177
          "var h;"
jaroslav@371
   178
        + "if (!!window && !!window.location && !!window.location.href)\n"
jaroslav@371
   179
        + "  h = window.location.href;\n"
jaroslav@371
   180
        + "else "
jaroslav@371
   181
        + "  h = null;"
jaroslav@371
   182
        + "return h;\n"
jaroslav@371
   183
    )
jaroslav@371
   184
    private static native String findBaseURL();
jaroslav@371
   185
    
jaroslav@371
   186
    @Override
jaroslav@371
   187
    public URI prepareURL(String content, String mimeType, String[] parameters) {
jaroslav@371
   188
        try {
jaroslav@371
   189
            final URL baseURL = new URL(findBaseURL());
jaroslav@371
   190
            StringBuilder sb = new StringBuilder();
jaroslav@371
   191
            sb.append("/dynamic?mimeType=").append(mimeType);
jaroslav@371
   192
            for (int i = 0; i < parameters.length; i++) {
jaroslav@371
   193
                sb.append("&param" + i).append("=").append(parameters[i]);
jaroslav@371
   194
            }
jaroslav@371
   195
            String mangle = content.replace("\n", "%0a")
jaroslav@371
   196
                .replace("\"", "\\\"").replace(" ", "%20");
jaroslav@371
   197
            sb.append("&content=").append(mangle);
jaroslav@371
   198
jaroslav@371
   199
            URL query = new URL(baseURL, sb.toString());
jaroslav@371
   200
            URLConnection c = query.openConnection();
jaroslav@371
   201
            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
jaroslav@371
   202
            URI connectTo = new URI(br.readLine());
jaroslav@371
   203
            return connectTo;
jaroslav@371
   204
        } catch (IOException ex) {
jaroslav@371
   205
            throw new IllegalStateException(ex);
jaroslav@371
   206
        } catch (URISyntaxException ex) {
jaroslav@371
   207
            throw new IllegalStateException(ex);
jaroslav@371
   208
        }
jaroslav@371
   209
    }
jaroslav@371
   210
jaroslav@371
   211
    @Override
jaroslav@371
   212
    public boolean canFailWebSocketTest() {
jaroslav@371
   213
        try {
jaroslav@371
   214
            Class.forName("java.util.function.Function");
jaroslav@371
   215
            return false;
jaroslav@371
   216
        } catch (ClassNotFoundException ex) {
jaroslav@371
   217
            // running on JDK7, FX WebView WebSocket impl does not work
jaroslav@371
   218
            return true;
jaroslav@371
   219
        }
jaroslav@371
   220
    }
jaroslav@391
   221
jaroslav@370
   222
}