src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/Coordinator.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/Coordinator.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,172 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime;
    1.30 +
    1.31 +import java.util.HashMap;
    1.32 +
    1.33 +import javax.xml.bind.ValidationEvent;
    1.34 +import javax.xml.bind.ValidationEventHandler;
    1.35 +import javax.xml.bind.ValidationEventLocator;
    1.36 +import javax.xml.bind.annotation.adapters.XmlAdapter;
    1.37 +import javax.xml.bind.helpers.ValidationEventImpl;
    1.38 +
    1.39 +import com.sun.xml.internal.bind.v2.ClassFactory;
    1.40 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
    1.41 +
    1.42 +import org.xml.sax.ErrorHandler;
    1.43 +import org.xml.sax.SAXException;
    1.44 +import org.xml.sax.SAXParseException;
    1.45 +
    1.46 +/**
    1.47 + * Object that coordinates the marshalling/unmarshalling.
    1.48 + *
    1.49 + * <p>
    1.50 + * This class takes care of the logic that allows code to obtain
    1.51 + * {@link UnmarshallingContext} and {@link XMLSerializer} instances
    1.52 + * during the unmarshalling/marshalling.
    1.53 + *
    1.54 + * <p>
    1.55 + * This is done by using a {@link ThreadLocal}. Therefore one unmarshalling/marshalling
    1.56 + * episode has to be done from the beginning till end by the same thread.
    1.57 + * (Note that the same {@link Coordinator} can be then used by a different thread
    1.58 + * for an entirely different episode.)
    1.59 + *
    1.60 + * This class also maintains the user-configured instances of {@link XmlAdapter}s.
    1.61 + *
    1.62 + * <p>
    1.63 + * This class implements {@link ErrorHandler} and propages erros to this object
    1.64 + * as the {@link ValidationEventHandler}, which will be implemented in a derived class.
    1.65 + *
    1.66 + * @author Kohsuke Kawaguchi
    1.67 + */
    1.68 +public abstract class Coordinator implements ErrorHandler, ValidationEventHandler {
    1.69 +
    1.70 +    private final HashMap<Class<? extends XmlAdapter>,XmlAdapter> adapters =
    1.71 +            new HashMap<Class<? extends XmlAdapter>,XmlAdapter>();
    1.72 +
    1.73 +
    1.74 +    public final XmlAdapter putAdapter(Class<? extends XmlAdapter> c, XmlAdapter a) {
    1.75 +        if(a==null)
    1.76 +            return adapters.remove(c);
    1.77 +        else
    1.78 +            return adapters.put(c,a);
    1.79 +    }
    1.80 +
    1.81 +    /**
    1.82 +     * Gets the instance of the adapter.
    1.83 +     *
    1.84 +     * @return
    1.85 +     *      always non-null.
    1.86 +     */
    1.87 +    public final <T extends XmlAdapter> T getAdapter(Class<T> key) {
    1.88 +        T v = key.cast(adapters.get(key));
    1.89 +        if(v==null) {
    1.90 +            v = ClassFactory.create(key);
    1.91 +            putAdapter(key,v);
    1.92 +        }
    1.93 +        return v;
    1.94 +    }
    1.95 +
    1.96 +    public <T extends XmlAdapter> boolean containsAdapter(Class<T> type) {
    1.97 +        return adapters.containsKey(type);
    1.98 +    }
    1.99 +
   1.100 +    // this much is necessary to avoid calling get and set twice when we push.
   1.101 +    private static final ThreadLocal<Coordinator> activeTable = new ThreadLocal<Coordinator>();
   1.102 +
   1.103 +    /**
   1.104 +     * The {@link Coordinator} in charge before this {@link Coordinator}.
   1.105 +     */
   1.106 +    private Coordinator old;
   1.107 +
   1.108 +    /**
   1.109 +     * Called whenever an execution flow enters the realm of this {@link Coordinator}.
   1.110 +     */
   1.111 +    protected final void pushCoordinator() {
   1.112 +        old = activeTable.get();
   1.113 +        activeTable.set(this);
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Called whenever an execution flow exits the realm of this {@link Coordinator}.
   1.118 +     */
   1.119 +    protected final void popCoordinator() {
   1.120 +        if (old != null)
   1.121 +            activeTable.set(old);
   1.122 +        else
   1.123 +            activeTable.remove();
   1.124 +        old = null; // avoid memory leak
   1.125 +    }
   1.126 +
   1.127 +    public static Coordinator _getInstance() {
   1.128 +        return activeTable.get();
   1.129 +    }
   1.130 +
   1.131 +//
   1.132 +//
   1.133 +// ErrorHandler implementation
   1.134 +//
   1.135 +//
   1.136 +    /**
   1.137 +     * Gets the current location. Used for reporting the error source location.
   1.138 +     */
   1.139 +    protected abstract ValidationEventLocator getLocation();
   1.140 +
   1.141 +    public final void error(SAXParseException exception) throws SAXException {
   1.142 +        propagateEvent( ValidationEvent.ERROR, exception );
   1.143 +    }
   1.144 +
   1.145 +    public final void warning(SAXParseException exception) throws SAXException {
   1.146 +        propagateEvent( ValidationEvent.WARNING, exception );
   1.147 +    }
   1.148 +
   1.149 +    public final void fatalError(SAXParseException exception) throws SAXException {
   1.150 +        propagateEvent( ValidationEvent.FATAL_ERROR, exception );
   1.151 +    }
   1.152 +
   1.153 +    private void propagateEvent( int severity, SAXParseException saxException )
   1.154 +        throws SAXException {
   1.155 +
   1.156 +        ValidationEventImpl ve =
   1.157 +            new ValidationEventImpl( severity, saxException.getMessage(), getLocation() );
   1.158 +
   1.159 +        Exception e = saxException.getException();
   1.160 +        if( e != null ) {
   1.161 +            ve.setLinkedException( e );
   1.162 +        } else {
   1.163 +            ve.setLinkedException( saxException );
   1.164 +        }
   1.165 +
   1.166 +        // call the client's event handler.  If it returns false, then bail-out
   1.167 +        // and terminate the unmarshal operation.
   1.168 +        boolean result = handleEvent( ve );
   1.169 +        if( ! result ) {
   1.170 +            // bail-out of the parse with a SAX exception, but convert it into
   1.171 +            // an UnmarshalException back in in the AbstractUnmarshaller
   1.172 +            throw saxException;
   1.173 +        }
   1.174 +    }
   1.175 +}

mercurial