|
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 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2.runtime; |
|
27 |
|
28 import java.util.HashMap; |
|
29 |
|
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; |
|
35 |
|
36 import com.sun.xml.internal.bind.v2.ClassFactory; |
|
37 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
|
38 |
|
39 import org.xml.sax.ErrorHandler; |
|
40 import org.xml.sax.SAXException; |
|
41 import org.xml.sax.SAXParseException; |
|
42 |
|
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 { |
|
66 |
|
67 private final HashMap<Class<? extends XmlAdapter>,XmlAdapter> adapters = |
|
68 new HashMap<Class<? extends XmlAdapter>,XmlAdapter>(); |
|
69 |
|
70 |
|
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 } |
|
77 |
|
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 } |
|
92 |
|
93 public <T extends XmlAdapter> boolean containsAdapter(Class<T> type) { |
|
94 return adapters.containsKey(type); |
|
95 } |
|
96 |
|
97 /** |
|
98 * The {@link Coordinator} in charge before this {@link Coordinator}. |
|
99 */ |
|
100 private Object old; |
|
101 |
|
102 /** |
|
103 * A 'pointer' to a {@link Coordinator} that keeps track of the currently active {@link Coordinator}. |
|
104 * Having this improves the runtime performance. |
|
105 */ |
|
106 private Object[] table; |
|
107 |
|
108 /** |
|
109 * When we set {@link #table} to null, record who did it. |
|
110 * This is for trouble-shooting a possible concurrency issue reported at: |
|
111 * http://forums.java.net/jive/thread.jspa?threadID=15132 |
|
112 */ |
|
113 public Exception guyWhoSetTheTableToNull; |
|
114 |
|
115 /** |
|
116 * Associates this {@link Coordinator} with the current thread. |
|
117 * Should be called at the very beginning of the episode. |
|
118 */ |
|
119 protected final void setThreadAffinity() { |
|
120 table = activeTable.get(); |
|
121 assert table!=null; |
|
122 } |
|
123 |
|
124 /** |
|
125 * Dis-associate this {@link Coordinator} with the current thread. |
|
126 * Sohuld be called at the end of the episode to avoid memory leak. |
|
127 */ |
|
128 protected final void resetThreadAffinity() { |
|
129 if (activeTable != null) { |
|
130 activeTable.remove(); |
|
131 } |
|
132 if(debugTableNPE) |
|
133 guyWhoSetTheTableToNull = new Exception(); // remember that we set it to null |
|
134 table = null; |
|
135 } |
|
136 |
|
137 /** |
|
138 * Called whenever an execution flow enters the realm of this {@link Coordinator}. |
|
139 */ |
|
140 protected final void pushCoordinator() { |
|
141 old = table[0]; |
|
142 table[0] = this; |
|
143 } |
|
144 |
|
145 /** |
|
146 * Called whenever an execution flow exits the realm of this {@link Coordinator}. |
|
147 */ |
|
148 protected final void popCoordinator() { |
|
149 assert table[0]==this; |
|
150 table[0] = old; |
|
151 old = null; // avoid memory leak |
|
152 } |
|
153 |
|
154 public static Coordinator _getInstance() { |
|
155 return (Coordinator) activeTable.get()[0]; |
|
156 } |
|
157 |
|
158 // this much is necessary to avoid calling get and set twice when we push. |
|
159 private static final ThreadLocal<Object[]> activeTable = new ThreadLocal<Object[]>() { |
|
160 @Override |
|
161 public Object[] initialValue() { |
|
162 return new Object[1]; |
|
163 } |
|
164 }; |
|
165 |
|
166 // |
|
167 // |
|
168 // ErrorHandler implementation |
|
169 // |
|
170 // |
|
171 /** |
|
172 * Gets the current location. Used for reporting the error source location. |
|
173 */ |
|
174 protected abstract ValidationEventLocator getLocation(); |
|
175 |
|
176 public final void error(SAXParseException exception) throws SAXException { |
|
177 propagateEvent( ValidationEvent.ERROR, exception ); |
|
178 } |
|
179 |
|
180 public final void warning(SAXParseException exception) throws SAXException { |
|
181 propagateEvent( ValidationEvent.WARNING, exception ); |
|
182 } |
|
183 |
|
184 public final void fatalError(SAXParseException exception) throws SAXException { |
|
185 propagateEvent( ValidationEvent.FATAL_ERROR, exception ); |
|
186 } |
|
187 |
|
188 private void propagateEvent( int severity, SAXParseException saxException ) |
|
189 throws SAXException { |
|
190 |
|
191 ValidationEventImpl ve = |
|
192 new ValidationEventImpl( severity, saxException.getMessage(), getLocation() ); |
|
193 |
|
194 Exception e = saxException.getException(); |
|
195 if( e != null ) { |
|
196 ve.setLinkedException( e ); |
|
197 } else { |
|
198 ve.setLinkedException( saxException ); |
|
199 } |
|
200 |
|
201 // call the client's event handler. If it returns false, then bail-out |
|
202 // and terminate the unmarshal operation. |
|
203 boolean result = handleEvent( ve ); |
|
204 if( ! result ) { |
|
205 // bail-out of the parse with a SAX exception, but convert it into |
|
206 // an UnmarshalException back in in the AbstractUnmarshaller |
|
207 throw saxException; |
|
208 } |
|
209 } |
|
210 |
|
211 public static boolean debugTableNPE; |
|
212 |
|
213 static { |
|
214 try { |
|
215 debugTableNPE = Boolean.getBoolean(Coordinator.class.getName()+".debugTableNPE"); |
|
216 } catch (SecurityException t) { |
|
217 } |
|
218 } |
|
219 } |