jaroslav@255: /** jaroslav@358: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. jaroslav@255: * jaroslav@551: * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. jaroslav@255: * jaroslav@358: * Oracle and Java are registered trademarks of Oracle and/or its affiliates. jaroslav@358: * Other names may be trademarks of their respective owners. jaroslav@255: * jaroslav@358: * The contents of this file are subject to the terms of either the GNU jaroslav@358: * General Public License Version 2 only ("GPL") or the Common jaroslav@358: * Development and Distribution License("CDDL") (collectively, the jaroslav@358: * "License"). You may not use this file except in compliance with the jaroslav@358: * License. You can obtain a copy of the License at jaroslav@358: * http://www.netbeans.org/cddl-gplv2.html jaroslav@358: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the jaroslav@358: * specific language governing permissions and limitations under the jaroslav@358: * License. When distributing the software, include this License Header jaroslav@358: * Notice in each file and include the License file at jaroslav@358: * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this jaroslav@358: * particular file as subject to the "Classpath" exception as provided jaroslav@358: * by Oracle in the GPL Version 2 section of the License file that jaroslav@358: * accompanied this code. If applicable, add the following below the jaroslav@358: * License Header, with the fields enclosed by brackets [] replaced by jaroslav@358: * your own identifying information: jaroslav@358: * "Portions Copyrighted [year] [name of copyright owner]" jaroslav@358: * jaroslav@358: * Contributor(s): jaroslav@358: * jaroslav@358: * The Original Software is NetBeans. The Initial Developer of the Original jaroslav@551: * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. jaroslav@358: * jaroslav@358: * If you wish your version of this file to be governed by only the CDDL jaroslav@358: * or only the GPL Version 2, indicate your decision by adding jaroslav@358: * "[Contributor] elects to include this software in this distribution jaroslav@358: * under the [CDDL or GPL Version 2] license." If you do not indicate a jaroslav@358: * single choice of license, a recipient has the option to distribute jaroslav@358: * your version of this file under either the CDDL, the GPL Version 2 or jaroslav@358: * to extend the choice of license to its licensees as provided above. jaroslav@358: * However, if you add GPL Version 2 code and therefore, elected the GPL jaroslav@358: * Version 2 license, then the option applies only if the new code is jaroslav@358: * made subject to such option by the copyright holder. jaroslav@255: */ jaroslav@362: package org.netbeans.html.json.impl; jaroslav@255: jtulach@1057: import java.util.concurrent.Callable; jaroslav@255: jaroslav@255: /** Super type for those who wish to receive JSON messages. jaroslav@255: * jtulach@790: * @author Jaroslav Tulach jaroslav@255: */ jaroslav@255: public abstract class RcvrJSON { jaroslav@255: protected void onOpen(MsgEvnt msg) {} jaroslav@255: protected abstract void onMessage(MsgEvnt msg); jaroslav@255: protected void onClose(MsgEvnt msg) {} jaroslav@255: protected abstract void onError(MsgEvnt msg); jaroslav@255: jaroslav@255: public abstract static class MsgEvnt { jaroslav@255: MsgEvnt() { jaroslav@255: } jaroslav@255: jaroslav@255: public Throwable getError() { jaroslav@255: return null; jaroslav@255: } jaroslav@255: jaroslav@255: public final Exception getException() { jaroslav@255: Throwable t = getError(); jaroslav@255: if (t instanceof Exception) { jaroslav@255: return (Exception)t; jaroslav@255: } jaroslav@255: if (t == null) { jaroslav@255: return null; jaroslav@255: } jaroslav@255: return new Exception(t); jaroslav@255: } jaroslav@255: jaroslav@386: public Object[] getValues() { jaroslav@386: return null; jaroslav@255: } jaroslav@255: jaroslav@255: public abstract void dispatch(RcvrJSON r); jaroslav@255: jaroslav@255: public static MsgEvnt createError(final Throwable t) { jaroslav@255: return new MsgEvnt() { jaroslav@255: @Override jaroslav@255: public Throwable getError() { jaroslav@255: return t; jaroslav@255: } jaroslav@255: jaroslav@255: @Override jaroslav@255: public void dispatch(RcvrJSON r) { jaroslav@255: r.onError(this); jaroslav@255: } jaroslav@255: }; jaroslav@255: } jaroslav@255: jtulach@1057: public static MsgEvnt createMessage(final Object value) { jaroslav@255: return new MsgEvnt() { jtulach@1057: private Object val = value; jtulach@1057: jaroslav@255: @Override jaroslav@386: public Object[] getValues() { jtulach@1057: if (val instanceof Callable) { jtulach@1057: try { jtulach@1057: val = ((Callable)val).call(); jtulach@1057: } catch (Exception ex) { jtulach@1057: throw new IllegalStateException("Cannot compute " + val, ex); jtulach@1057: } jtulach@1057: } jtulach@1057: return val instanceof Object[] ? (Object[])val : new Object[] { val }; jaroslav@255: } jaroslav@255: jaroslav@255: @Override jaroslav@255: public void dispatch(RcvrJSON r) { jaroslav@255: r.onMessage(this); jaroslav@255: } jaroslav@255: }; jaroslav@255: } jaroslav@255: jaroslav@255: public static MsgEvnt createOpen() { jaroslav@255: return new MsgEvnt() { jaroslav@255: @Override jaroslav@255: public void dispatch(RcvrJSON r) { jaroslav@255: r.onOpen(this); jaroslav@255: } jaroslav@255: }; jaroslav@255: } jaroslav@255: jaroslav@255: public static MsgEvnt createClose() { jaroslav@255: return new MsgEvnt() { jaroslav@255: @Override jaroslav@255: public void dispatch(RcvrJSON r) { jaroslav@255: r.onClose(this); jaroslav@255: } jaroslav@255: }; jaroslav@255: } jaroslav@386: } }