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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.bind.v2.runtime;
    28 import java.util.HashMap;
    30 import javax.xml.bind.ValidationEvent;
    31 import javax.xml.bind.ValidationEventHandler;
    32 import javax.xml.bind.ValidationEventLocator;
    33 import javax.xml.bind.annotation.adapters.XmlAdapter;
    34 import javax.xml.bind.helpers.ValidationEventImpl;
    36 import com.sun.xml.internal.bind.v2.ClassFactory;
    37 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
    39 import org.xml.sax.ErrorHandler;
    40 import org.xml.sax.SAXException;
    41 import org.xml.sax.SAXParseException;
    43 /**
    44  * Object that coordinates the marshalling/unmarshalling.
    45  *
    46  * <p>
    47  * This class takes care of the logic that allows code to obtain
    48  * {@link UnmarshallingContext} and {@link XMLSerializer} instances
    49  * during the unmarshalling/marshalling.
    50  *
    51  * <p>
    52  * This is done by using a {@link ThreadLocal}. Therefore one unmarshalling/marshalling
    53  * episode has to be done from the beginning till end by the same thread.
    54  * (Note that the same {@link Coordinator} can be then used by a different thread
    55  * for an entirely different episode.)
    56  *
    57  * This class also maintains the user-configured instances of {@link XmlAdapter}s.
    58  *
    59  * <p>
    60  * This class implements {@link ErrorHandler} and propages erros to this object
    61  * as the {@link ValidationEventHandler}, which will be implemented in a derived class.
    62  *
    63  * @author Kohsuke Kawaguchi
    64  */
    65 public abstract class Coordinator implements ErrorHandler, ValidationEventHandler {
    67     private final HashMap<Class<? extends XmlAdapter>,XmlAdapter> adapters =
    68             new HashMap<Class<? extends XmlAdapter>,XmlAdapter>();
    71     public final XmlAdapter putAdapter(Class<? extends XmlAdapter> c, XmlAdapter a) {
    72         if(a==null)
    73             return adapters.remove(c);
    74         else
    75             return adapters.put(c,a);
    76     }
    78     /**
    79      * Gets the instance of the adapter.
    80      *
    81      * @return
    82      *      always non-null.
    83      */
    84     public final <T extends XmlAdapter> T getAdapter(Class<T> key) {
    85         T v = key.cast(adapters.get(key));
    86         if(v==null) {
    87             v = ClassFactory.create(key);
    88             putAdapter(key,v);
    89         }
    90         return v;
    91     }
    93     public <T extends XmlAdapter> boolean containsAdapter(Class<T> type) {
    94         return adapters.containsKey(type);
    95     }
    97     // this much is necessary to avoid calling get and set twice when we push.
    98     private static final ThreadLocal<Coordinator> activeTable = new ThreadLocal<Coordinator>();
   100     /**
   101      * The {@link Coordinator} in charge before this {@link Coordinator}.
   102      */
   103     private Coordinator old;
   105     /**
   106      * Called whenever an execution flow enters the realm of this {@link Coordinator}.
   107      */
   108     protected final void pushCoordinator() {
   109         old = activeTable.get();
   110         activeTable.set(this);
   111     }
   113     /**
   114      * Called whenever an execution flow exits the realm of this {@link Coordinator}.
   115      */
   116     protected final void popCoordinator() {
   117         if (old != null)
   118             activeTable.set(old);
   119         else
   120             activeTable.remove();
   121         old = null; // avoid memory leak
   122     }
   124     public static Coordinator _getInstance() {
   125         return activeTable.get();
   126     }
   128 //
   129 //
   130 // ErrorHandler implementation
   131 //
   132 //
   133     /**
   134      * Gets the current location. Used for reporting the error source location.
   135      */
   136     protected abstract ValidationEventLocator getLocation();
   138     public final void error(SAXParseException exception) throws SAXException {
   139         propagateEvent( ValidationEvent.ERROR, exception );
   140     }
   142     public final void warning(SAXParseException exception) throws SAXException {
   143         propagateEvent( ValidationEvent.WARNING, exception );
   144     }
   146     public final void fatalError(SAXParseException exception) throws SAXException {
   147         propagateEvent( ValidationEvent.FATAL_ERROR, exception );
   148     }
   150     private void propagateEvent( int severity, SAXParseException saxException )
   151         throws SAXException {
   153         ValidationEventImpl ve =
   154             new ValidationEventImpl( severity, saxException.getMessage(), getLocation() );
   156         Exception e = saxException.getException();
   157         if( e != null ) {
   158             ve.setLinkedException( e );
   159         } else {
   160             ve.setLinkedException( saxException );
   161         }
   163         // call the client's event handler.  If it returns false, then bail-out
   164         // and terminate the unmarshal operation.
   165         boolean result = handleEvent( ve );
   166         if( ! result ) {
   167             // bail-out of the parse with a SAX exception, but convert it into
   168             // an UnmarshalException back in in the AbstractUnmarshaller
   169             throw saxException;
   170         }
   171     }
   172 }

mercurial