author | mkos |
Fri, 23 Aug 2013 09:57:21 +0100 | |
changeset 19645 | 36f707905f2b |
parent 12009 | 4abb694f273a |
child 22679 | d785acd84a14 |
permissions | -rw-r--r-- |
12009 | 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 |
||
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
97 |
// this much is necessary to avoid calling get and set twice when we push. |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
98 |
private static final ThreadLocal<Coordinator> activeTable = new ThreadLocal<Coordinator>(); |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
99 |
|
12009 | 100 |
/** |
101 |
* The {@link Coordinator} in charge before this {@link Coordinator}. |
|
102 |
*/ |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
103 |
private Coordinator old; |
12009 | 104 |
|
105 |
/** |
|
106 |
* Called whenever an execution flow enters the realm of this {@link Coordinator}. |
|
107 |
*/ |
|
108 |
protected final void pushCoordinator() { |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
109 |
old = activeTable.get(); |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
110 |
activeTable.set(this); |
12009 | 111 |
} |
112 |
||
113 |
/** |
|
114 |
* Called whenever an execution flow exits the realm of this {@link Coordinator}. |
|
115 |
*/ |
|
116 |
protected final void popCoordinator() { |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
117 |
if (old != null) |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
118 |
activeTable.set(old); |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
119 |
else |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
120 |
activeTable.remove(); |
12009 | 121 |
old = null; // avoid memory leak |
122 |
} |
|
123 |
||
124 |
public static Coordinator _getInstance() { |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
125 |
return activeTable.get(); |
12009 | 126 |
} |
127 |
||
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(); |
|
137 |
||
138 |
public final void error(SAXParseException exception) throws SAXException { |
|
139 |
propagateEvent( ValidationEvent.ERROR, exception ); |
|
140 |
} |
|
141 |
||
142 |
public final void warning(SAXParseException exception) throws SAXException { |
|
143 |
propagateEvent( ValidationEvent.WARNING, exception ); |
|
144 |
} |
|
145 |
||
146 |
public final void fatalError(SAXParseException exception) throws SAXException { |
|
147 |
propagateEvent( ValidationEvent.FATAL_ERROR, exception ); |
|
148 |
} |
|
149 |
||
150 |
private void propagateEvent( int severity, SAXParseException saxException ) |
|
151 |
throws SAXException { |
|
152 |
||
153 |
ValidationEventImpl ve = |
|
154 |
new ValidationEventImpl( severity, saxException.getMessage(), getLocation() ); |
|
155 |
||
156 |
Exception e = saxException.getException(); |
|
157 |
if( e != null ) { |
|
158 |
ve.setLinkedException( e ); |
|
159 |
} else { |
|
160 |
ve.setLinkedException( saxException ); |
|
161 |
} |
|
162 |
||
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 |
} |