ko4j/src/main/java/org/netbeans/html/ko4j/ComponentLoader.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 15 Dec 2015 23:19:35 +0100
branchComponent257162
changeset 1035 3534e15dc446
permissions -rw-r--r--
Introducing @Component annotation, adding basic annotation and providing its (reflection based) implementation in ko4j module.
jtulach@1035
     1
/**
jtulach@1035
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@1035
     3
 *
jtulach@1035
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@1035
     5
 *
jtulach@1035
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@1035
     7
 * Other names may be trademarks of their respective owners.
jtulach@1035
     8
 *
jtulach@1035
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@1035
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@1035
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@1035
    12
 * "License"). You may not use this file except in compliance with the
jtulach@1035
    13
 * License. You can obtain a copy of the License at
jtulach@1035
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@1035
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@1035
    16
 * specific language governing permissions and limitations under the
jtulach@1035
    17
 * License.  When distributing the software, include this License Header
jtulach@1035
    18
 * Notice in each file and include the License file at
jtulach@1035
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@1035
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@1035
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@1035
    22
 * accompanied this code. If applicable, add the following below the
jtulach@1035
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@1035
    24
 * your own identifying information:
jtulach@1035
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@1035
    26
 *
jtulach@1035
    27
 * Contributor(s):
jtulach@1035
    28
 *
jtulach@1035
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@1035
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@1035
    31
 *
jtulach@1035
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@1035
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@1035
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@1035
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@1035
    36
 * single choice of license, a recipient has the option to distribute
jtulach@1035
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@1035
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@1035
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@1035
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@1035
    41
 * made subject to such option by the copyright holder.
jtulach@1035
    42
 */
jtulach@1035
    43
package org.netbeans.html.ko4j;
jtulach@1035
    44
jtulach@1035
    45
import java.io.ByteArrayOutputStream;
jtulach@1035
    46
import java.io.IOException;
jtulach@1035
    47
import java.io.InputStream;
jtulach@1035
    48
import java.lang.reflect.Method;
jtulach@1035
    49
import java.net.URL;
jtulach@1035
    50
import java.util.Enumeration;
jtulach@1035
    51
import java.util.Properties;
jtulach@1035
    52
import net.java.html.BrwsrCtx;
jtulach@1035
    53
import net.java.html.js.JavaScriptBody;
jtulach@1035
    54
import net.java.html.json.Models;
jtulach@1035
    55
jtulach@1035
    56
final class ComponentLoader {
jtulach@1035
    57
    @JavaScriptBody(args = {}, wait4js = false, javacall = true, body =
jtulach@1035
    58
        "var myLoader = {\n" +
jtulach@1035
    59
        "};\n" +
jtulach@1035
    60
        "myLoader.getConfig = function(name, callback) { \n" +
jtulach@1035
    61
        "  function myClbk(config) {\n" +
jtulach@1035
    62
        "    console.log('myClbk: ' + config.ko4j + ' ' + config.properties);\n" +
jtulach@1035
    63
        "    callback(config);\n" +
jtulach@1035
    64
        "  }\n" +
jtulach@1035
    65
        "  @org.netbeans.html.ko4j.ComponentLoader::loadConfig(Ljava/lang/String;Ljava/lang/Object;)(name, myClbk);\n" +
jtulach@1035
    66
        "}\n" +
jtulach@1035
    67
        "myLoader.loadComponent = function(name, componentConfig, callback) {\n" +
jtulach@1035
    68
        "  console.log('loadComponent: ' + componentConfig.ko4j + ' ' + componentConfig.properties);\n" +
jtulach@1035
    69
        " callback(null);\n" +
jtulach@1035
    70
        "}\n" +
jtulach@1035
    71
        "myLoader.loadTemplate = function(name, templateConfig, callback) {\n" +
jtulach@1035
    72
        "  console.log('loadTemplate: ' + templateConfig.ko4j + ' ' + templateConfig.properties);\n" +
jtulach@1035
    73
        "  if (templateConfig !== null) {\n" +
jtulach@1035
    74
        "    @org.netbeans.html.ko4j.ComponentLoader::loadTemplate(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)(name, templateConfig, callback);\n" +
jtulach@1035
    75
        "  } else {\n" +
jtulach@1035
    76
        "    callback(null);\n" +
jtulach@1035
    77
        "  }\n" +
jtulach@1035
    78
        "}\n" +
jtulach@1035
    79
        "myLoader.loadViewModel = function(name, config, callback) { \n" +
jtulach@1035
    80
        "  console.log('loadViewModel ' + name + ' temp: ' + config);\n" +
jtulach@1035
    81
        "  var f = function(params, info) {\n" +
jtulach@1035
    82
        "    if (!params) params = null;\n" +
jtulach@1035
    83
        "    return @org.netbeans.html.ko4j.ComponentLoader::loadViewModel(Ljava/lang/String;Ljava/util/Properties;Ljava/lang/Object;Ljava/lang/Object;)(name, config, params, info);\n" +
jtulach@1035
    84
        "  };\n" +
jtulach@1035
    85
        "  callback(f);\n" +
jtulach@1035
    86
        "}\n" +
jtulach@1035
    87
        "ko.components.loaders.unshift(myLoader);\n"
jtulach@1035
    88
    )
jtulach@1035
    89
    public static native void initialize();
jtulach@1035
    90
    
jtulach@1035
    91
    static void loadConfig(String name, Object callback) throws IOException {
jtulach@1035
    92
        System.err.println("config for " + name);
jtulach@1035
    93
        Enumeration<URL> en = ComponentLoader.class.getClassLoader().getResources("META-INF/html+java/components/" + name);
jtulach@1035
    94
        while (en.hasMoreElements()) {
jtulach@1035
    95
            URL u = en.nextElement();
jtulach@1035
    96
            InputStream is = u.openStream();
jtulach@1035
    97
            Properties p = new Properties();
jtulach@1035
    98
            p.load(is);
jtulach@1035
    99
            is.close();
jtulach@1035
   100
            callbackConfig(callback, p, p.getProperty("template"));
jtulach@1035
   101
            return;
jtulach@1035
   102
        }
jtulach@1035
   103
        callbackConfig(callback, null, null);
jtulach@1035
   104
    }
jtulach@1035
   105
    static void loadTemplate(String name, String template, Object callback) throws IOException {
jtulach@1035
   106
        InputStream is = ComponentLoader.class.getClassLoader().getResourceAsStream(template);
jtulach@1035
   107
        ByteArrayOutputStream os = new ByteArrayOutputStream();
jtulach@1035
   108
        for (;;) {
jtulach@1035
   109
            int ch = is.read();
jtulach@1035
   110
            if (ch == -1) {
jtulach@1035
   111
                break;
jtulach@1035
   112
            }
jtulach@1035
   113
            os.write(ch);
jtulach@1035
   114
        }
jtulach@1035
   115
        is.close();
jtulach@1035
   116
        String text = os.toString("UTF-8");
jtulach@1035
   117
        callbackTemplate(callback, text);
jtulach@1035
   118
    }
jtulach@1035
   119
    
jtulach@1035
   120
    @JavaScriptBody(args = { "callback", "param", "template" }, body =
jtulach@1035
   121
        "callback(param ? {\n"
jtulach@1035
   122
      + "  'ko4j': true,\n"
jtulach@1035
   123
      + "  'viewModel': param,\n"
jtulach@1035
   124
      + "  'template': template\n"
jtulach@1035
   125
      + " } : null);", wait4js = false)
jtulach@1035
   126
    private static native void callbackConfig(Object callback, Properties param, String template);
jtulach@1035
   127
jtulach@1035
   128
    @JavaScriptBody(args = { "callback", "param" }, body = "callback(ko.utils.parseHtmlFragment(param));", wait4js = false)
jtulach@1035
   129
    private static native void callbackTemplate(Object callback, String param);
jtulach@1035
   130
    
jtulach@1035
   131
    static Object loadViewModel(String name, Properties props, Object params, Object config) throws IOException {
jtulach@1035
   132
        Object res = null;
jtulach@1035
   133
        try {
jtulach@1035
   134
            String paramName = props.getProperty("paramName");
jtulach@1035
   135
            Class<?> paramType = Class.forName(paramName);
jtulach@1035
   136
            String className = props.getProperty("className");
jtulach@1035
   137
            Class<?> classType = Class.forName(className);
jtulach@1035
   138
            Method m = classType.getMethod(props.getProperty("methodName"), paramType);
jtulach@1035
   139
jtulach@1035
   140
            BrwsrCtx ctx = BrwsrCtx.findDefault(ComponentLoader.class);
jtulach@1035
   141
            Object data;
jtulach@1035
   142
            if (params == null) {
jtulach@1035
   143
                data = paramType.newInstance();
jtulach@1035
   144
            } else {
jtulach@1035
   145
                data = Models.fromRaw(ctx, paramType, params);
jtulach@1035
   146
            }
jtulach@1035
   147
            m.setAccessible(true);
jtulach@1035
   148
            res = m.invoke(null, data);
jtulach@1035
   149
            props.put(res, res);
jtulach@1035
   150
        } catch (Exception ex) {
jtulach@1035
   151
            throw new IOException(ex);
jtulach@1035
   152
        }
jtulach@1035
   153
        return Models.toRaw(res);
jtulach@1035
   154
    }
jtulach@1035
   155
}