Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind.v2.runtime; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.util.HashMap; |
ohair@286 | 29 | |
ohair@286 | 30 | import javax.xml.bind.ValidationEvent; |
ohair@286 | 31 | import javax.xml.bind.ValidationEventHandler; |
ohair@286 | 32 | import javax.xml.bind.ValidationEventLocator; |
ohair@286 | 33 | import javax.xml.bind.annotation.adapters.XmlAdapter; |
ohair@286 | 34 | import javax.xml.bind.helpers.ValidationEventImpl; |
ohair@286 | 35 | |
ohair@286 | 36 | import com.sun.xml.internal.bind.v2.ClassFactory; |
ohair@286 | 37 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
ohair@286 | 38 | |
ohair@286 | 39 | import org.xml.sax.ErrorHandler; |
ohair@286 | 40 | import org.xml.sax.SAXException; |
ohair@286 | 41 | import org.xml.sax.SAXParseException; |
ohair@286 | 42 | |
ohair@286 | 43 | /** |
ohair@286 | 44 | * Object that coordinates the marshalling/unmarshalling. |
ohair@286 | 45 | * |
ohair@286 | 46 | * <p> |
ohair@286 | 47 | * This class takes care of the logic that allows code to obtain |
ohair@286 | 48 | * {@link UnmarshallingContext} and {@link XMLSerializer} instances |
ohair@286 | 49 | * during the unmarshalling/marshalling. |
ohair@286 | 50 | * |
ohair@286 | 51 | * <p> |
ohair@286 | 52 | * This is done by using a {@link ThreadLocal}. Therefore one unmarshalling/marshalling |
ohair@286 | 53 | * episode has to be done from the beginning till end by the same thread. |
ohair@286 | 54 | * (Note that the same {@link Coordinator} can be then used by a different thread |
ohair@286 | 55 | * for an entirely different episode.) |
ohair@286 | 56 | * |
ohair@286 | 57 | * This class also maintains the user-configured instances of {@link XmlAdapter}s. |
ohair@286 | 58 | * |
ohair@286 | 59 | * <p> |
ohair@286 | 60 | * This class implements {@link ErrorHandler} and propages erros to this object |
ohair@286 | 61 | * as the {@link ValidationEventHandler}, which will be implemented in a derived class. |
ohair@286 | 62 | * |
ohair@286 | 63 | * @author Kohsuke Kawaguchi |
ohair@286 | 64 | */ |
ohair@286 | 65 | public abstract class Coordinator implements ErrorHandler, ValidationEventHandler { |
ohair@286 | 66 | |
ohair@286 | 67 | private final HashMap<Class<? extends XmlAdapter>,XmlAdapter> adapters = |
ohair@286 | 68 | new HashMap<Class<? extends XmlAdapter>,XmlAdapter>(); |
ohair@286 | 69 | |
ohair@286 | 70 | |
ohair@286 | 71 | public final XmlAdapter putAdapter(Class<? extends XmlAdapter> c, XmlAdapter a) { |
ohair@286 | 72 | if(a==null) |
ohair@286 | 73 | return adapters.remove(c); |
ohair@286 | 74 | else |
ohair@286 | 75 | return adapters.put(c,a); |
ohair@286 | 76 | } |
ohair@286 | 77 | |
ohair@286 | 78 | /** |
ohair@286 | 79 | * Gets the instance of the adapter. |
ohair@286 | 80 | * |
ohair@286 | 81 | * @return |
ohair@286 | 82 | * always non-null. |
ohair@286 | 83 | */ |
ohair@286 | 84 | public final <T extends XmlAdapter> T getAdapter(Class<T> key) { |
ohair@286 | 85 | T v = key.cast(adapters.get(key)); |
ohair@286 | 86 | if(v==null) { |
ohair@286 | 87 | v = ClassFactory.create(key); |
ohair@286 | 88 | putAdapter(key,v); |
ohair@286 | 89 | } |
ohair@286 | 90 | return v; |
ohair@286 | 91 | } |
ohair@286 | 92 | |
ohair@286 | 93 | public <T extends XmlAdapter> boolean containsAdapter(Class<T> type) { |
ohair@286 | 94 | return adapters.containsKey(type); |
ohair@286 | 95 | } |
ohair@286 | 96 | |
ohair@286 | 97 | /** |
ohair@286 | 98 | * The {@link Coordinator} in charge before this {@link Coordinator}. |
ohair@286 | 99 | */ |
ohair@286 | 100 | private Object old; |
ohair@286 | 101 | |
ohair@286 | 102 | /** |
ohair@286 | 103 | * A 'pointer' to a {@link Coordinator} that keeps track of the currently active {@link Coordinator}. |
ohair@286 | 104 | * Having this improves the runtime performance. |
ohair@286 | 105 | */ |
ohair@286 | 106 | private Object[] table; |
ohair@286 | 107 | |
ohair@286 | 108 | /** |
ohair@286 | 109 | * When we set {@link #table} to null, record who did it. |
ohair@286 | 110 | * This is for trouble-shooting a possible concurrency issue reported at: |
ohair@286 | 111 | * http://forums.java.net/jive/thread.jspa?threadID=15132 |
ohair@286 | 112 | */ |
ohair@286 | 113 | public Exception guyWhoSetTheTableToNull; |
ohair@286 | 114 | |
ohair@286 | 115 | /** |
ohair@286 | 116 | * Associates this {@link Coordinator} with the current thread. |
ohair@286 | 117 | * Should be called at the very beginning of the episode. |
ohair@286 | 118 | */ |
ohair@286 | 119 | protected final void setThreadAffinity() { |
ohair@286 | 120 | table = activeTable.get(); |
ohair@286 | 121 | assert table!=null; |
ohair@286 | 122 | } |
ohair@286 | 123 | |
ohair@286 | 124 | /** |
ohair@286 | 125 | * Dis-associate this {@link Coordinator} with the current thread. |
ohair@286 | 126 | * Sohuld be called at the end of the episode to avoid memory leak. |
ohair@286 | 127 | */ |
ohair@286 | 128 | protected final void resetThreadAffinity() { |
ohair@286 | 129 | if (activeTable != null) { |
ohair@286 | 130 | activeTable.remove(); |
ohair@286 | 131 | } |
ohair@286 | 132 | if(debugTableNPE) |
ohair@286 | 133 | guyWhoSetTheTableToNull = new Exception(); // remember that we set it to null |
ohair@286 | 134 | table = null; |
ohair@286 | 135 | } |
ohair@286 | 136 | |
ohair@286 | 137 | /** |
ohair@286 | 138 | * Called whenever an execution flow enters the realm of this {@link Coordinator}. |
ohair@286 | 139 | */ |
ohair@286 | 140 | protected final void pushCoordinator() { |
ohair@286 | 141 | old = table[0]; |
ohair@286 | 142 | table[0] = this; |
ohair@286 | 143 | } |
ohair@286 | 144 | |
ohair@286 | 145 | /** |
ohair@286 | 146 | * Called whenever an execution flow exits the realm of this {@link Coordinator}. |
ohair@286 | 147 | */ |
ohair@286 | 148 | protected final void popCoordinator() { |
ohair@286 | 149 | assert table[0]==this; |
ohair@286 | 150 | table[0] = old; |
ohair@286 | 151 | old = null; // avoid memory leak |
ohair@286 | 152 | } |
ohair@286 | 153 | |
ohair@286 | 154 | public static Coordinator _getInstance() { |
ohair@286 | 155 | return (Coordinator) activeTable.get()[0]; |
ohair@286 | 156 | } |
ohair@286 | 157 | |
ohair@286 | 158 | // this much is necessary to avoid calling get and set twice when we push. |
ohair@286 | 159 | private static final ThreadLocal<Object[]> activeTable = new ThreadLocal<Object[]>() { |
ohair@286 | 160 | @Override |
ohair@286 | 161 | public Object[] initialValue() { |
ohair@286 | 162 | return new Object[1]; |
ohair@286 | 163 | } |
ohair@286 | 164 | }; |
ohair@286 | 165 | |
ohair@286 | 166 | // |
ohair@286 | 167 | // |
ohair@286 | 168 | // ErrorHandler implementation |
ohair@286 | 169 | // |
ohair@286 | 170 | // |
ohair@286 | 171 | /** |
ohair@286 | 172 | * Gets the current location. Used for reporting the error source location. |
ohair@286 | 173 | */ |
ohair@286 | 174 | protected abstract ValidationEventLocator getLocation(); |
ohair@286 | 175 | |
ohair@286 | 176 | public final void error(SAXParseException exception) throws SAXException { |
ohair@286 | 177 | propagateEvent( ValidationEvent.ERROR, exception ); |
ohair@286 | 178 | } |
ohair@286 | 179 | |
ohair@286 | 180 | public final void warning(SAXParseException exception) throws SAXException { |
ohair@286 | 181 | propagateEvent( ValidationEvent.WARNING, exception ); |
ohair@286 | 182 | } |
ohair@286 | 183 | |
ohair@286 | 184 | public final void fatalError(SAXParseException exception) throws SAXException { |
ohair@286 | 185 | propagateEvent( ValidationEvent.FATAL_ERROR, exception ); |
ohair@286 | 186 | } |
ohair@286 | 187 | |
ohair@286 | 188 | private void propagateEvent( int severity, SAXParseException saxException ) |
ohair@286 | 189 | throws SAXException { |
ohair@286 | 190 | |
ohair@286 | 191 | ValidationEventImpl ve = |
ohair@286 | 192 | new ValidationEventImpl( severity, saxException.getMessage(), getLocation() ); |
ohair@286 | 193 | |
ohair@286 | 194 | Exception e = saxException.getException(); |
ohair@286 | 195 | if( e != null ) { |
ohair@286 | 196 | ve.setLinkedException( e ); |
ohair@286 | 197 | } else { |
ohair@286 | 198 | ve.setLinkedException( saxException ); |
ohair@286 | 199 | } |
ohair@286 | 200 | |
ohair@286 | 201 | // call the client's event handler. If it returns false, then bail-out |
ohair@286 | 202 | // and terminate the unmarshal operation. |
ohair@286 | 203 | boolean result = handleEvent( ve ); |
ohair@286 | 204 | if( ! result ) { |
ohair@286 | 205 | // bail-out of the parse with a SAX exception, but convert it into |
ohair@286 | 206 | // an UnmarshalException back in in the AbstractUnmarshaller |
ohair@286 | 207 | throw saxException; |
ohair@286 | 208 | } |
ohair@286 | 209 | } |
ohair@286 | 210 | |
ohair@286 | 211 | public static boolean debugTableNPE; |
ohair@286 | 212 | |
ohair@286 | 213 | static { |
ohair@286 | 214 | try { |
ohair@286 | 215 | debugTableNPE = Boolean.getBoolean(Coordinator.class.getName()+".debugTableNPE"); |
ohair@286 | 216 | } catch (SecurityException t) { |
ohair@286 | 217 | } |
ohair@286 | 218 | } |
ohair@286 | 219 | } |