author | duke |
Wed, 05 Jul 2017 21:00:03 +0200 | |
changeset 33642 | d16d6d82a16a |
parent 31109 | a542f8dcbbf8 |
child 35413 | 56a695bbebe8 |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
12009 | 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 javax.xml.bind; |
|
27 |
||
28 |
import java.io.BufferedReader; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.InputStreamReader; |
|
32 |
import java.io.UnsupportedEncodingException; |
|
33 |
import java.lang.reflect.InvocationTargetException; |
|
34 |
import java.lang.reflect.Method; |
|
35 |
import java.net.URL; |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
36 |
import java.security.AccessController; |
12009 | 37 |
import java.util.Map; |
38 |
import java.util.Properties; |
|
39 |
import java.util.StringTokenizer; |
|
40 |
import java.util.logging.ConsoleHandler; |
|
41 |
import java.util.logging.Level; |
|
42 |
import java.util.logging.Logger; |
|
43 |
||
44 |
||
45 |
/** |
|
46 |
* This class is package private and therefore is not exposed as part of the |
|
47 |
* JAXB API. |
|
48 |
* |
|
49 |
* This code is designed to implement the JAXB 1.0 spec pluggability feature |
|
50 |
* |
|
51 |
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul> |
|
52 |
* @see JAXBContext |
|
53 |
*/ |
|
54 |
class ContextFinder { |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
55 |
|
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
56 |
/** |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
57 |
* When JAXB is in J2SE, rt.jar has to have a JAXB implementation. |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
58 |
* However, rt.jar cannot have META-INF/services/javax.xml.bind.JAXBContext |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
59 |
* because if it has, it will take precedence over any file that applications have |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
60 |
* in their jar files. |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
61 |
* |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
62 |
* <p> |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
63 |
* When the user bundles his own JAXB implementation, we'd like to use it, and we |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
64 |
* want the platform default to be used only when there's no other JAXB provider. |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
65 |
* |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
66 |
* <p> |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
67 |
* For this reason, we have to hard-code the class name into the API. |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
68 |
*/ |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
69 |
private static final String PLATFORM_DEFAULT_FACTORY_CLASS = "com.sun.xml.internal.bind.v2.ContextFactory"; |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
70 |
|
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
71 |
// previous value of JAXBContext.JAXB_CONTEXT_FACTORY, using also this to ensure backwards compatibility |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
72 |
private static final String JAXB_CONTEXT_FACTORY_DEPRECATED = "javax.xml.bind.context.factory"; |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
73 |
|
12009 | 74 |
private static final Logger logger; |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
75 |
|
12009 | 76 |
static { |
77 |
logger = Logger.getLogger("javax.xml.bind"); |
|
78 |
try { |
|
79 |
if (AccessController.doPrivileged(new GetPropertyAction("jaxb.debug")) != null) { |
|
80 |
// disconnect the logger from a bigger framework (if any) |
|
81 |
// and take the matters into our own hands |
|
82 |
logger.setUseParentHandlers(false); |
|
83 |
logger.setLevel(Level.ALL); |
|
84 |
ConsoleHandler handler = new ConsoleHandler(); |
|
85 |
handler.setLevel(Level.ALL); |
|
86 |
logger.addHandler(handler); |
|
87 |
} else { |
|
88 |
// don't change the setting of this logger |
|
89 |
// to honor what other frameworks |
|
90 |
// have done on configurations. |
|
91 |
} |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
92 |
} catch (Throwable t) { |
12009 | 93 |
// just to be extra safe. in particular System.getProperty may throw |
94 |
// SecurityException. |
|
95 |
} |
|
96 |
} |
|
97 |
||
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
98 |
private static ServiceLoaderUtil.ExceptionHandler<JAXBException> EXCEPTION_HANDLER = |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
99 |
new ServiceLoaderUtil.ExceptionHandler<JAXBException>() { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
100 |
@Override |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
101 |
public JAXBException createException(Throwable throwable, String message) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
102 |
return new JAXBException(message, throwable); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
103 |
} |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
104 |
}; |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
105 |
|
12009 | 106 |
/** |
107 |
* If the {@link InvocationTargetException} wraps an exception that shouldn't be wrapped, |
|
108 |
* throw the wrapped exception. |
|
109 |
*/ |
|
110 |
private static void handleInvocationTargetException(InvocationTargetException x) throws JAXBException { |
|
111 |
Throwable t = x.getTargetException(); |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
112 |
if (t != null) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
113 |
if (t instanceof JAXBException) |
12009 | 114 |
// one of our exceptions, just re-throw |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
115 |
throw (JAXBException) t; |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
116 |
if (t instanceof RuntimeException) |
12009 | 117 |
// avoid wrapping exceptions unnecessarily |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
118 |
throw (RuntimeException) t; |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
119 |
if (t instanceof Error) |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
120 |
throw (Error) t; |
12009 | 121 |
} |
122 |
} |
|
123 |
||
124 |
||
125 |
/** |
|
126 |
* Determine if two types (JAXBContext in this case) will generate a ClassCastException. |
|
127 |
* |
|
128 |
* For example, (targetType)originalType |
|
129 |
* |
|
130 |
* @param originalType |
|
131 |
* The Class object of the type being cast |
|
132 |
* @param targetType |
|
133 |
* The Class object of the type that is being cast to |
|
134 |
* @return JAXBException to be thrown. |
|
135 |
*/ |
|
136 |
private static JAXBException handleClassCastException(Class originalType, Class targetType) { |
|
137 |
final URL targetTypeURL = which(targetType); |
|
138 |
||
139 |
return new JAXBException(Messages.format(Messages.ILLEGAL_CAST, |
|
140 |
// we don't care where the impl class is, we want to know where JAXBContext lives in the impl |
|
141 |
// class' ClassLoader |
|
142 |
getClassClassLoader(originalType).getResource("javax/xml/bind/JAXBContext.class"), |
|
143 |
targetTypeURL)); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Create an instance of a class using the specified ClassLoader |
|
148 |
*/ |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
149 |
static JAXBContext newInstance(String contextPath, |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
150 |
String className, |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
151 |
ClassLoader classLoader, |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
152 |
Map properties) throws JAXBException { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
153 |
|
12009 | 154 |
try { |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
155 |
Class spFactory = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader); |
12009 | 156 |
return newInstance(contextPath, spFactory, classLoader, properties); |
157 |
} catch (ClassNotFoundException x) { |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
158 |
throw new JAXBException(Messages.format(Messages.PROVIDER_NOT_FOUND, className), x); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
159 |
|
12009 | 160 |
} catch (RuntimeException x) { |
161 |
// avoid wrapping RuntimeException to JAXBException, |
|
162 |
// because it indicates a bug in this code. |
|
163 |
throw x; |
|
164 |
} catch (Exception x) { |
|
165 |
// can't catch JAXBException because the method is hidden behind |
|
166 |
// reflection. Root element collisions detected in the call to |
|
167 |
// createContext() are reported as JAXBExceptions - just re-throw it |
|
168 |
// some other type of exception - just wrap it |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
169 |
throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, className, x), x); |
12009 | 170 |
} |
171 |
} |
|
172 |
||
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
173 |
static JAXBContext newInstance(String contextPath, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
174 |
Class spFactory, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
175 |
ClassLoader classLoader, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
176 |
Map properties) throws JAXBException { |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
177 |
|
12009 | 178 |
try { |
179 |
/* |
|
180 |
* javax.xml.bind.context.factory points to a class which has a |
|
181 |
* static method called 'createContext' that |
|
182 |
* returns a javax.xml.JAXBContext. |
|
183 |
*/ |
|
184 |
||
185 |
Object context = null; |
|
186 |
||
187 |
// first check the method that takes Map as the third parameter. |
|
188 |
// this is added in 2.0. |
|
189 |
try { |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
190 |
Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class); |
12009 | 191 |
// any failure in invoking this method would be considered fatal |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
192 |
context = m.invoke(null, contextPath, classLoader, properties); |
12009 | 193 |
} catch (NoSuchMethodException e) { |
194 |
// it's not an error for the provider not to have this method. |
|
195 |
} |
|
196 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
197 |
if (context == null) { |
12009 | 198 |
// try the old method that doesn't take properties. compatible with 1.0. |
199 |
// it is an error for an implementation not to have both forms of the createContext method. |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
200 |
Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class); |
12009 | 201 |
// any failure in invoking this method would be considered fatal |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
202 |
context = m.invoke(null, contextPath, classLoader); |
12009 | 203 |
} |
204 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
205 |
if (!(context instanceof JAXBContext)) { |
12009 | 206 |
// the cast would fail, so generate an exception with a nice message |
207 |
throw handleClassCastException(context.getClass(), JAXBContext.class); |
|
208 |
} |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
209 |
return (JAXBContext) context; |
12009 | 210 |
} catch (InvocationTargetException x) { |
211 |
handleInvocationTargetException(x); |
|
212 |
// for other exceptions, wrap the internal target exception |
|
213 |
// with a JAXBException |
|
214 |
Throwable e = x; |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
215 |
if (x.getTargetException() != null) |
12009 | 216 |
e = x.getTargetException(); |
217 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
218 |
throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e); |
12009 | 219 |
} catch (RuntimeException x) { |
220 |
// avoid wrapping RuntimeException to JAXBException, |
|
221 |
// because it indicates a bug in this code. |
|
222 |
throw x; |
|
223 |
} catch (Exception x) { |
|
224 |
// can't catch JAXBException because the method is hidden behind |
|
225 |
// reflection. Root element collisions detected in the call to |
|
226 |
// createContext() are reported as JAXBExceptions - just re-throw it |
|
227 |
// some other type of exception - just wrap it |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
228 |
throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x); |
12009 | 229 |
} |
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
* Create an instance of a class using the thread context ClassLoader |
|
234 |
*/ |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
235 |
static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
236 |
|
12009 | 237 |
Class spi; |
238 |
try { |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
239 |
spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader()); |
12009 | 240 |
} catch (ClassNotFoundException e) { |
241 |
throw new JAXBException(e); |
|
242 |
} |
|
243 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
244 |
if (logger.isLoggable(Level.FINE)) { |
12009 | 245 |
// extra check to avoid costly which operation if not logged |
246 |
logger.log(Level.FINE, "loaded {0} from {1}", new Object[]{className, which(spi)}); |
|
247 |
} |
|
248 |
||
249 |
return newInstance(classes, properties, spi); |
|
250 |
} |
|
251 |
||
252 |
static JAXBContext newInstance(Class[] classes, |
|
253 |
Map properties, |
|
254 |
Class spFactory) throws JAXBException { |
|
255 |
try { |
|
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
256 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
257 |
Method m = spFactory.getMethod("createContext", Class[].class, Map.class); |
12009 | 258 |
Object context = m.invoke(null, classes, properties); |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
259 |
if (!(context instanceof JAXBContext)) { |
12009 | 260 |
// the cast would fail, so generate an exception with a nice message |
261 |
throw handleClassCastException(context.getClass(), JAXBContext.class); |
|
262 |
} |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
263 |
return (JAXBContext) context; |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
264 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
265 |
} catch (NoSuchMethodException | IllegalAccessException e) { |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
266 |
throw new JAXBException(e); |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
267 |
|
12009 | 268 |
} catch (InvocationTargetException e) { |
269 |
handleInvocationTargetException(e); |
|
270 |
||
271 |
Throwable x = e; |
|
272 |
if (e.getTargetException() != null) |
|
273 |
x = e.getTargetException(); |
|
274 |
||
275 |
throw new JAXBException(x); |
|
276 |
} |
|
277 |
} |
|
278 |
||
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
279 |
static JAXBContext find(String factoryId, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
280 |
String contextPath, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
281 |
ClassLoader classLoader, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
282 |
Map properties) throws JAXBException { |
12009 | 283 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
284 |
StringTokenizer packages = new StringTokenizer(contextPath, ":"); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
285 |
if (!packages.hasMoreTokens()) { |
12009 | 286 |
// no context is specified |
287 |
throw new JAXBException(Messages.format(Messages.NO_PACKAGE_IN_CONTEXTPATH)); |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
288 |
} |
12009 | 289 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
290 |
// search for jaxb.properties in the class loader of each class first |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
291 |
logger.fine("Searching jaxb.properties"); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
292 |
while (packages.hasMoreTokens()) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
293 |
// com.acme.foo - > com/acme/foo/jaxb.properties |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
294 |
String factoryClassName = |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
295 |
classNameFromPackageProperties( |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
296 |
classLoader, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
297 |
packages.nextToken(":").replace('.', '/'), |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
298 |
factoryId, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
299 |
JAXB_CONTEXT_FACTORY_DEPRECATED); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
300 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
301 |
if (factoryClassName != null) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
302 |
return newInstance(contextPath, factoryClassName, classLoader, properties); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
303 |
} |
12009 | 304 |
} |
305 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
306 |
String factoryName = classNameFromSystemProperties(); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
307 |
if (factoryName != null) return newInstance(contextPath, factoryName, classLoader, properties); |
12009 | 308 |
|
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
309 |
JAXBContextFactory obj = ServiceLoaderUtil.firstByServiceLoader( |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
310 |
JAXBContextFactory.class, logger, EXCEPTION_HANDLER); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
311 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
312 |
if (obj != null) return obj.createContext(contextPath, classLoader, properties); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
313 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
314 |
// to ensure backwards compatibility |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
315 |
factoryName = firstByServiceLoaderDeprecated(JAXBContext.class, classLoader); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
316 |
if (factoryName != null) return newInstance(contextPath, factoryName, classLoader, properties); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
317 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
318 |
Class ctxFactory = (Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader( |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
319 |
"javax.xml.bind.JAXBContext", logger); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
320 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
321 |
if (ctxFactory != null) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
322 |
return newInstance(contextPath, ctxFactory, classLoader, properties); |
12009 | 323 |
} |
324 |
||
325 |
// else no provider found |
|
326 |
logger.fine("Trying to create the platform default provider"); |
|
327 |
return newInstance(contextPath, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties); |
|
328 |
} |
|
329 |
||
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
330 |
static JAXBContext find(Class<?>[] classes, Map<String, ?> properties) throws JAXBException { |
12009 | 331 |
|
332 |
// search for jaxb.properties in the class loader of each class first |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
333 |
logger.fine("Searching jaxb.properties"); |
12009 | 334 |
for (final Class c : classes) { |
335 |
// this classloader is used only to load jaxb.properties, so doing this should be safe. |
|
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
336 |
// this is possible for primitives, arrays, and classes that are |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
337 |
// loaded by poorly implemented ClassLoaders |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
338 |
if (c.getPackage() == null) continue; |
12009 | 339 |
|
340 |
// TODO: do we want to optimize away searching the same package? org.Foo, org.Bar, com.Baz |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
341 |
// classes from the same package might come from different class loades, so it might be a bad idea |
12009 | 342 |
// TODO: it's easier to look things up from the class |
343 |
// c.getResourceAsStream("jaxb.properties"); |
|
344 |
||
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
345 |
String factoryClassName = |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
346 |
classNameFromPackageProperties( |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
347 |
getClassClassLoader(c), |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
348 |
c.getPackage().getName().replace('.', '/'), |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
349 |
JAXBContext.JAXB_CONTEXT_FACTORY, JAXB_CONTEXT_FACTORY_DEPRECATED); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
350 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
351 |
if (factoryClassName != null) return newInstance(classes, properties, factoryClassName); |
12009 | 352 |
} |
353 |
||
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
354 |
String factoryClassName = classNameFromSystemProperties(); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
355 |
if (factoryClassName != null) return newInstance(classes, properties, factoryClassName); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
356 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
357 |
JAXBContextFactory factory = |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
358 |
ServiceLoaderUtil.firstByServiceLoader(JAXBContextFactory.class, logger, EXCEPTION_HANDLER); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
359 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
360 |
if (factory != null) return factory.createContext(classes, properties); |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
361 |
|
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
362 |
// to ensure backwards compatibility |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
363 |
String className = firstByServiceLoaderDeprecated(JAXBContext.class, getContextClassLoader()); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
364 |
if (className != null) return newInstance(classes, properties, className); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
365 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
366 |
logger.fine("Trying to create the platform default provider"); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
367 |
Class ctxFactoryClass = |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
368 |
(Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext", logger); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
369 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
370 |
if (ctxFactoryClass != null) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
371 |
return newInstance(classes, properties, ctxFactoryClass); |
12009 | 372 |
} |
373 |
||
374 |
// else no provider found |
|
375 |
logger.fine("Trying to create the platform default provider"); |
|
376 |
return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS); |
|
377 |
} |
|
378 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
379 |
|
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
380 |
/** |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
381 |
* first factoryId should be the preffered one, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
382 |
* more of those can be provided to support backwards compatibility |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
383 |
*/ |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
384 |
private static String classNameFromPackageProperties(ClassLoader classLoader, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
385 |
String packageName, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
386 |
String ... factoryIds) throws JAXBException { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
387 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
388 |
String resourceName = packageName + "/jaxb.properties"; |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
389 |
logger.log(Level.FINE, "Trying to locate {0}", resourceName); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
390 |
Properties props = loadJAXBProperties(classLoader, resourceName); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
391 |
if (props != null) { |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
392 |
for(String factoryId : factoryIds) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
393 |
if (props.containsKey(factoryId)) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
394 |
return props.getProperty(factoryId); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
395 |
} |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
396 |
} |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
397 |
throw new JAXBException(Messages.format(Messages.MISSING_PROPERTY, packageName, factoryIds[0])); |
12009 | 398 |
} |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
399 |
return null; |
12009 | 400 |
} |
401 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
402 |
private static String classNameFromSystemProperties() throws JAXBException { |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
403 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
404 |
String factoryClassName = getSystemProperty(JAXBContext.JAXB_CONTEXT_FACTORY); |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
405 |
if (factoryClassName != null) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
406 |
return factoryClassName; |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
407 |
} |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
408 |
// leave this here to assure compatibility |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
409 |
factoryClassName = getDeprecatedSystemProperty(JAXB_CONTEXT_FACTORY_DEPRECATED); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
410 |
if (factoryClassName != null) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
411 |
return factoryClassName; |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
412 |
} |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
413 |
// leave this here to assure compatibility |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
414 |
factoryClassName = getDeprecatedSystemProperty(JAXBContext.class.getName()); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
415 |
if (factoryClassName != null) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
416 |
return factoryClassName; |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
417 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
418 |
return null; |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
419 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
420 |
|
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
421 |
private static String getDeprecatedSystemProperty(String property) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
422 |
String value = getSystemProperty(property); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
423 |
if (value != null) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
424 |
logger.log(Level.WARNING, "Using non-standard property: {0}. Property {1} should be used instead.", |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
425 |
new Object[] {property, JAXBContext.JAXB_CONTEXT_FACTORY}); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
426 |
} |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
427 |
return value; |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
428 |
} |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
429 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
430 |
private static String getSystemProperty(String property) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
431 |
logger.log(Level.FINE, "Checking system property {0}", property); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
432 |
String value = AccessController.doPrivileged(new GetPropertyAction(property)); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
433 |
if (value != null) { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
434 |
logger.log(Level.FINE, " found {0}", value); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
435 |
} else { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
436 |
logger.log(Level.FINE, " not found"); |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
437 |
} |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
438 |
return value; |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
439 |
} |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
440 |
|
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
441 |
private static Properties loadJAXBProperties(ClassLoader classLoader, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
442 |
String propFileName) throws JAXBException { |
12009 | 443 |
|
444 |
Properties props = null; |
|
445 |
try { |
|
446 |
URL url; |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
447 |
if (classLoader == null) |
12009 | 448 |
url = ClassLoader.getSystemResource(propFileName); |
449 |
else |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
450 |
url = classLoader.getResource(propFileName); |
12009 | 451 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
452 |
if (url != null) { |
12009 | 453 |
logger.log(Level.FINE, "loading props from {0}", url); |
454 |
props = new Properties(); |
|
455 |
InputStream is = url.openStream(); |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
456 |
props.load(is); |
12009 | 457 |
is.close(); |
458 |
} |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
459 |
} catch (IOException ioe) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
460 |
logger.log(Level.FINE, "Unable to load " + propFileName, ioe); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
461 |
throw new JAXBException(ioe.toString(), ioe); |
12009 | 462 |
} |
463 |
||
464 |
return props; |
|
465 |
} |
|
466 |
||
467 |
||
468 |
/** |
|
469 |
* Search the given ClassLoader for an instance of the specified class and |
|
470 |
* return a string representation of the URL that points to the resource. |
|
471 |
* |
|
472 |
* @param clazz |
|
473 |
* The class to search for |
|
474 |
* @param loader |
|
475 |
* The ClassLoader to search. If this parameter is null, then the |
|
476 |
* system class loader will be searched |
|
477 |
* @return |
|
478 |
* the URL for the class or null if it wasn't found |
|
479 |
*/ |
|
480 |
static URL which(Class clazz, ClassLoader loader) { |
|
481 |
||
482 |
String classnameAsResource = clazz.getName().replace('.', '/') + ".class"; |
|
483 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
484 |
if (loader == null) { |
16791 | 485 |
loader = getSystemClassLoader(); |
12009 | 486 |
} |
487 |
||
488 |
return loader.getResource(classnameAsResource); |
|
489 |
} |
|
490 |
||
491 |
/** |
|
492 |
* Get the URL for the Class from it's ClassLoader. |
|
493 |
* |
|
494 |
* Convenience method for {@link #which(Class, ClassLoader)}. |
|
495 |
* |
|
496 |
* Equivalent to calling: which(clazz, clazz.getClassLoader()) |
|
497 |
* |
|
498 |
* @param clazz |
|
499 |
* The class to search for |
|
500 |
* @return |
|
501 |
* the URL for the class or null if it wasn't found |
|
502 |
*/ |
|
503 |
static URL which(Class clazz) { |
|
504 |
return which(clazz, getClassClassLoader(clazz)); |
|
505 |
} |
|
506 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
507 |
@SuppressWarnings("unchecked") |
12009 | 508 |
private static ClassLoader getContextClassLoader() { |
509 |
if (System.getSecurityManager() == null) { |
|
510 |
return Thread.currentThread().getContextClassLoader(); |
|
511 |
} else { |
|
512 |
return (ClassLoader) java.security.AccessController.doPrivileged( |
|
513 |
new java.security.PrivilegedAction() { |
|
514 |
public java.lang.Object run() { |
|
515 |
return Thread.currentThread().getContextClassLoader(); |
|
516 |
} |
|
517 |
}); |
|
518 |
} |
|
519 |
} |
|
520 |
||
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
521 |
@SuppressWarnings("unchecked") |
12009 | 522 |
private static ClassLoader getClassClassLoader(final Class c) { |
523 |
if (System.getSecurityManager() == null) { |
|
524 |
return c.getClassLoader(); |
|
525 |
} else { |
|
526 |
return (ClassLoader) java.security.AccessController.doPrivileged( |
|
527 |
new java.security.PrivilegedAction() { |
|
528 |
public java.lang.Object run() { |
|
529 |
return c.getClassLoader(); |
|
530 |
} |
|
531 |
}); |
|
532 |
} |
|
533 |
} |
|
534 |
||
16791 | 535 |
private static ClassLoader getSystemClassLoader() { |
536 |
if (System.getSecurityManager() == null) { |
|
537 |
return ClassLoader.getSystemClassLoader(); |
|
538 |
} else { |
|
539 |
return (ClassLoader) java.security.AccessController.doPrivileged( |
|
540 |
new java.security.PrivilegedAction() { |
|
541 |
public java.lang.Object run() { |
|
542 |
return ClassLoader.getSystemClassLoader(); |
|
543 |
} |
|
544 |
}); |
|
545 |
} |
|
546 |
} |
|
547 |
||
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
548 |
// ServiceLoaderUtil.firstByServiceLoaderDeprecated should be used instead. |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
549 |
@Deprecated |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
550 |
static String firstByServiceLoaderDeprecated(Class spiClass, |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
551 |
ClassLoader classLoader) throws JAXBException { |
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
552 |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
553 |
final String jaxbContextFQCN = spiClass.getName(); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
554 |
|
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
555 |
logger.fine("Searching META-INF/services"); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
556 |
|
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
557 |
// search META-INF services next |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
558 |
BufferedReader r = null; |
31109
a542f8dcbbf8
8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents:
29839
diff
changeset
|
559 |
final String resource = "META-INF/services/" + jaxbContextFQCN; |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
560 |
try { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
561 |
final InputStream resourceStream = |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
562 |
(classLoader == null) ? |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
563 |
ClassLoader.getSystemResourceAsStream(resource) : |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
564 |
classLoader.getResourceAsStream(resource); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
565 |
|
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
566 |
if (resourceStream != null) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
567 |
r = new BufferedReader(new InputStreamReader(resourceStream, "UTF-8")); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
568 |
String factoryClassName = r.readLine(); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
569 |
if (factoryClassName != null) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
570 |
factoryClassName = factoryClassName.trim(); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
571 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
572 |
r.close(); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
573 |
logger.log(Level.FINE, "Configured factorty class:{0}", factoryClassName); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
574 |
return factoryClassName; |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
575 |
} else { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
576 |
logger.log(Level.FINE, "Unable to load:{0}", resource); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
577 |
return null; |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
578 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
579 |
} catch (IOException e) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
580 |
throw new JAXBException(e); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
581 |
} finally { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
582 |
try { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
583 |
if (r != null) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
584 |
r.close(); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
585 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
586 |
} catch (IOException ex) { |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
587 |
logger.log(Level.SEVERE, "Unable to close resource: " + resource, ex); |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
588 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
589 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
590 |
} |
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
591 |
|
12009 | 592 |
} |