ko4j/src/main/java/org/netbeans/html/ko4j/KO4J.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 04 Dec 2014 09:21:55 +0100
changeset 886 88d62267a0b5
parent 838 bdc3d696dd4a
permissions -rw-r--r--
#248918: Introducing technology identifiers
jaroslav@443
     1
/**
jaroslav@442
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@442
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@442
     5
 *
jaroslav@442
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@442
     7
 * Other names may be trademarks of their respective owners.
jaroslav@442
     8
 *
jaroslav@442
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@442
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@442
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@442
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@442
    13
 * License. You can obtain a copy of the License at
jaroslav@442
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@442
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@442
    16
 * specific language governing permissions and limitations under the
jaroslav@442
    17
 * License.  When distributing the software, include this License Header
jaroslav@442
    18
 * Notice in each file and include the License file at
jaroslav@442
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@442
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@442
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@442
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@442
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@442
    24
 * your own identifying information:
jaroslav@442
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@442
    26
 *
jaroslav@443
    27
 * Contributor(s):
jaroslav@443
    28
 *
jaroslav@443
    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@443
    31
 *
jaroslav@442
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@442
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@442
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@442
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@442
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@442
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@442
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@442
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@442
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@442
    41
 * made subject to such option by the copyright holder.
jaroslav@442
    42
 */
jaroslav@442
    43
package org.netbeans.html.ko4j;
jaroslav@442
    44
jtulach@886
    45
import java.util.logging.Logger;
jaroslav@442
    46
import net.java.html.json.Model;
jtulach@886
    47
import net.java.html.json.OnReceive;
jtulach@838
    48
import org.netbeans.html.boot.spi.Fn;
jtulach@838
    49
import org.netbeans.html.context.spi.Contexts;
jtulach@838
    50
import org.netbeans.html.context.spi.Contexts.Provider;
jtulach@838
    51
import org.netbeans.html.json.spi.Technology;
jtulach@838
    52
import org.netbeans.html.json.spi.Transfer;
jtulach@838
    53
import org.netbeans.html.json.spi.WSTransfer;
jaroslav@442
    54
import org.openide.util.lookup.ServiceProvider;
jaroslav@442
    55
jaroslav@442
    56
/** Support for <a href="http://knockoutjs.com">knockout.js</a>
jaroslav@442
    57
 * and its Java binding via {@link Model model classes}.
jaroslav@533
    58
 * Registers {@link Provider}, so {@link java.util.ServiceLoader} can find it.
jtulach@886
    59
 * The provider registers following technologies:
jtulach@886
    60
 * <ul>
jtulach@886
    61
 * <li><b>ko4j</b> - bindings for <a href="http://knockoutjs.com">knockout.js</a>
jtulach@886
    62
 *   and the classes generated by the {@link Model} annotation.
jtulach@886
    63
 * </li>
jtulach@886
    64
 * <li><b>xhr</b> - <a href="http://www.w3.org/TR/XMLHttpRequest/">XMLHttpRequest</a>
jtulach@886
    65
 *   based implementation for <em>REST</em> calls 
jtulach@886
    66
 *   (<b>GET</b>, <b>PUT</b>, <b>POST</b>, <b>DELETE</b> methods) 
jtulach@886
    67
 *   for {@link OnReceive} annotation.
jtulach@886
    68
 * </li>
jtulach@886
    69
 * <li><b>websocket</b> - 
jtulach@886
    70
 *   native browser <a href="http://www.w3.org/TR/websockets/">websockets</a>
jtulach@886
    71
 *   based implementation for {@link OnReceive} annotation and its <b>WebSocket</b>
jtulach@886
    72
 *   subprotocol.
jtulach@886
    73
 * </li>
jtulach@886
    74
 * </ul>
jaroslav@442
    75
 *
jtulach@790
    76
 * @author Jaroslav Tulach
jaroslav@444
    77
 * @since 0.7
jaroslav@442
    78
 */
jaroslav@533
    79
@ServiceProvider(service = Provider.class)
jaroslav@533
    80
public final class KO4J implements Provider {
jtulach@886
    81
    static final Logger LOG = Logger.getLogger(KOSockets.class.getName());
jtulach@886
    82
    private KOTech ko4j;
jtulach@886
    83
    private KOTransfer trans;
jtulach@886
    84
    private KOSockets socks;
jaroslav@444
    85
    
jaroslav@444
    86
    public KO4J() {
jaroslav@444
    87
        this(null);
jaroslav@444
    88
    }
jtulach@886
    89
jtulach@886
    90
    @Deprecated
jaroslav@444
    91
    public KO4J(Fn.Presenter presenter) {
jaroslav@444
    92
    }
jaroslav@444
    93
    
jaroslav@444
    94
    /** Return instance of the knockout.js for Java technology.
jaroslav@444
    95
     * @return non-null instance
jaroslav@444
    96
     */
jaroslav@444
    97
    public Technology knockout() {
jtulach@886
    98
        if (ko4j == null) {
jtulach@886
    99
            ko4j = new KOTech();
jtulach@886
   100
        }
jtulach@886
   101
        return ko4j;
jaroslav@444
   102
    }
jaroslav@444
   103
    
jaroslav@446
   104
    /** Browser based implementation of transfer interface. Uses
jaroslav@446
   105
     * browser method to convert string to JSON.
jaroslav@446
   106
     * 
jaroslav@446
   107
     * @return non-null instance
jaroslav@444
   108
     */
jaroslav@446
   109
    public Transfer transfer() {
jtulach@886
   110
        if (trans == null) {
jtulach@886
   111
            trans = new KOTransfer();
jtulach@886
   112
        }
jtulach@886
   113
        return trans;
jaroslav@444
   114
    }
jaroslav@444
   115
    
jaroslav@444
   116
    /** Returns browser based implementation of websocket transfer.
jaroslav@446
   117
     * If available (for example JavaFX WebView on JDK7 does not have
jaroslav@446
   118
     * this implementation).
jaroslav@444
   119
     * 
jaroslav@444
   120
     * @return an instance or <code>null</code>, if there is no
jaroslav@444
   121
     *   <code>WebSocket</code> object in the browser
jaroslav@444
   122
     */
jaroslav@446
   123
    public WSTransfer<?> websockets() {
jtulach@886
   124
        if (!KOSockets.areWebSocketsSupported()) {
jtulach@886
   125
            return null;
jtulach@886
   126
        }
jtulach@886
   127
        if (socks == null) {
jtulach@886
   128
            socks = new KOSockets();
jtulach@886
   129
        }
jtulach@886
   130
        return socks;
jaroslav@444
   131
    }
jaroslav@442
   132
jaroslav@444
   133
    /** Registers technologies at position 100:
jaroslav@444
   134
     * <ul>
jaroslav@444
   135
     *   <li>{@link #knockout()}</li>
jaroslav@446
   136
     *   <li>{@link #transfer()}</li>
jaroslav@446
   137
     *   <li>{@link #websockets()()} - if browser supports web sockets</li>
jaroslav@444
   138
     * </ul>
jaroslav@444
   139
     * @param context the context to register to
jaroslav@444
   140
     * @param requestor the class requesting the registration
jaroslav@444
   141
     */
jaroslav@442
   142
    @Override
jaroslav@442
   143
    public void fillContext(Contexts.Builder context, Class<?> requestor) {
jaroslav@575
   144
        context.register(Technology.class, knockout(), 100);
jaroslav@575
   145
        context.register(Transfer.class, transfer(), 100);
jaroslav@589
   146
        context.register(WSTransfer.class, websockets(), 100);
jaroslav@442
   147
    }
jaroslav@442
   148
    
jaroslav@442
   149
}