165 // avoid wrapping RuntimeException to JAXBException,
166 // because it indicates a bug in this code.
167 // JAXBException re-thrown as is
168 throw x;
169 } catch (Exception x) {
170 // can't catch JAXBException because the method is hidden behind
171 // reflection. Root element collisions detected in the call to
172 // createContext() are reported as JAXBExceptions - just re-throw it
173 // some other type of exception - just wrap it
174 throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, className, x), x);
175 }
176 }
177
178 static JAXBContext newInstance(String contextPath,
179 Class[] contextPathClasses,
180 Class spFactory,
181 ClassLoader classLoader,
182 Map properties) throws JAXBException {
183
184 try {
185 /*
186 * javax.xml.bind.context.factory points to a class which has a
187 * static method called 'createContext' that
188 * returns a javax.xml.JAXBContext.
189 */
190
191 Object context = null;
192
193 // first check the method that takes Map as the third parameter.
194 // this is added in 2.0.
195 try {
196 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class);
197 // any failure in invoking this method would be considered fatal
198 Object obj = instantiateProviderIfNecessary(spFactory);
199 context = m.invoke(obj, contextPath, classLoader, properties);
200 } catch (NoSuchMethodException ignored) {
201 // it's not an error for the provider not to have this method.
202 }
203
204 if (context == null) {
205 // try the old method that doesn't take properties. compatible with 1.0.
206 // it is an error for an implementation not to have both forms of the createContext method.
207 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class);
208 Object obj = instantiateProviderIfNecessary(spFactory);
209 // any failure in invoking this method would be considered fatal
210 context = m.invoke(obj, contextPath, classLoader);
211 }
212
213 if (!(context instanceof JAXBContext)) {
214 // the cast would fail, so generate an exception with a nice message
215 throw handleClassCastException(context.getClass(), JAXBContext.class);
216 }
217
218 ModuleUtil.delegateAddOpensToImplModule(contextPathClasses, spFactory);
219
220 return (JAXBContext) context;
221 } catch (InvocationTargetException x) {
222 // throw if it is exception not to be wrapped
223 // otherwise, wrap with a JAXBException
224 Throwable e = handleInvocationTargetException(x);
225 throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
226
227 } catch (Exception x) {
228 // can't catch JAXBException because the method is hidden behind
229 // reflection. Root element collisions detected in the call to
230 // createContext() are reported as JAXBExceptions - just re-throw it
231 // some other type of exception - just wrap it
232 throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x);
233 }
234 }
235
236 private static Object instantiateProviderIfNecessary(final Class<?> implClass) throws JAXBException {
237 try {
238 if (JAXBContextFactory.class.isAssignableFrom(implClass)) {
239 return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
257
258 Class spi;
259 try {
260 spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader());
261 } catch (ClassNotFoundException e) {
262 throw new JAXBException(Messages.format(Messages.DEFAULT_PROVIDER_NOT_FOUND), e);
263 }
264
265 if (logger.isLoggable(Level.FINE)) {
266 // extra check to avoid costly which operation if not logged
267 logger.log(Level.FINE, "loaded {0} from {1}", new Object[]{className, which(spi)});
268 }
269
270 return newInstance(classes, properties, spi);
271 }
272
273 static JAXBContext newInstance(Class[] classes,
274 Map properties,
275 Class spFactory) throws JAXBException {
276 try {
277
278 Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
279 Object obj = instantiateProviderIfNecessary(spFactory);
280 Object context = m.invoke(obj, classes, properties);
281 if (!(context instanceof JAXBContext)) {
282 // the cast would fail, so generate an exception with a nice message
283 throw handleClassCastException(context.getClass(), JAXBContext.class);
284 }
285 ModuleUtil.delegateAddOpensToImplModule(classes, spFactory);
286 return (JAXBContext) context;
287
288 } catch (NoSuchMethodException | IllegalAccessException e) {
289 throw new JAXBException(e);
290 } catch (InvocationTargetException e) {
291 // throw if it is exception not to be wrapped
292 // otherwise, wrap with a JAXBException
293 Throwable x = handleInvocationTargetException(e);
294
295 throw new JAXBException(x);
296 }
297 }
298
299 static JAXBContext find(String factoryId,
300 String contextPath,
301 ClassLoader classLoader,
302 Map properties) throws JAXBException {
303
304 if (contextPath == null || contextPath.isEmpty()) {
305 // no context is specified
311
312 //first try with classloader#getResource
313 String factoryClassName = jaxbProperties(contextPath, classLoader, factoryId);
314 if (factoryClassName == null && contextPathClasses != null) {
315 //try with class#getResource
316 factoryClassName = jaxbProperties(contextPathClasses, factoryId);
317 }
318
319 if (factoryClassName != null) {
320 return newInstance(contextPath, contextPathClasses, factoryClassName, classLoader, properties);
321 }
322
323
324 String factoryName = classNameFromSystemProperties();
325 if (factoryName != null) return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);
326
327 JAXBContextFactory obj = ServiceLoaderUtil.firstByServiceLoader(
328 JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
329
330 if (obj != null) {
331 JAXBContext context = obj.createContext(contextPath, classLoader, properties);
332 ModuleUtil.delegateAddOpensToImplModule(contextPathClasses, obj.getClass());
333 return context;
334 }
335
336 // to ensure backwards compatibility
337 factoryName = firstByServiceLoaderDeprecated(JAXBContext.class, classLoader);
338 if (factoryName != null) return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);
339
340 Class ctxFactory = (Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader(
341 "javax.xml.bind.JAXBContext", logger);
342
343 if (ctxFactory != null) {
344 return newInstance(contextPath, contextPathClasses, ctxFactory, classLoader, properties);
345 }
346
347 // else no provider found
348 logger.fine("Trying to create the platform default provider");
349 return newInstance(contextPath, contextPathClasses, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties);
350 }
351
352 static JAXBContext find(Class<?>[] classes, Map<String, ?> properties) throws JAXBException {
353
368
369 if (jaxbPropertiesUrl != null) {
370
371 String factoryClassName =
372 classNameFromPackageProperties(
373 jaxbPropertiesUrl,
374 JAXBContext.JAXB_CONTEXT_FACTORY, JAXB_CONTEXT_FACTORY_DEPRECATED);
375
376 return newInstance(classes, properties, factoryClassName);
377 }
378
379 }
380
381 String factoryClassName = classNameFromSystemProperties();
382 if (factoryClassName != null) return newInstance(classes, properties, factoryClassName);
383
384 JAXBContextFactory factory =
385 ServiceLoaderUtil.firstByServiceLoader(JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
386
387 if (factory != null) {
388 JAXBContext context = factory.createContext(classes, properties);
389 ModuleUtil.delegateAddOpensToImplModule(classes, factory.getClass());
390 return context;
391 }
392
393 // to ensure backwards compatibility
394 String className = firstByServiceLoaderDeprecated(JAXBContext.class, getContextClassLoader());
395 if (className != null) return newInstance(classes, properties, className);
396
397 logger.fine("Trying to create the platform default provider");
398 Class ctxFactoryClass =
399 (Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext", logger);
400
401 if (ctxFactoryClass != null) {
402 return newInstance(classes, properties, ctxFactoryClass);
403 }
404
405 // else no provider found
406 logger.fine("Trying to create the platform default provider");
407 return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS);
408 }
409
410
|
165 // avoid wrapping RuntimeException to JAXBException,
166 // because it indicates a bug in this code.
167 // JAXBException re-thrown as is
168 throw x;
169 } catch (Exception x) {
170 // can't catch JAXBException because the method is hidden behind
171 // reflection. Root element collisions detected in the call to
172 // createContext() are reported as JAXBExceptions - just re-throw it
173 // some other type of exception - just wrap it
174 throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, className, x), x);
175 }
176 }
177
178 static JAXBContext newInstance(String contextPath,
179 Class[] contextPathClasses,
180 Class spFactory,
181 ClassLoader classLoader,
182 Map properties) throws JAXBException {
183
184 try {
185
186 ModuleUtil.delegateAddOpensToImplModule(contextPathClasses, spFactory);
187
188 /*
189 * javax.xml.bind.context.factory points to a class which has a
190 * static method called 'createContext' that
191 * returns a javax.xml.JAXBContext.
192 */
193
194 Object context = null;
195
196 // first check the method that takes Map as the third parameter.
197 // this is added in 2.0.
198 try {
199 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class);
200 // any failure in invoking this method would be considered fatal
201 Object obj = instantiateProviderIfNecessary(spFactory);
202 context = m.invoke(obj, contextPath, classLoader, properties);
203 } catch (NoSuchMethodException ignored) {
204 // it's not an error for the provider not to have this method.
205 }
206
207 if (context == null) {
208 // try the old method that doesn't take properties. compatible with 1.0.
209 // it is an error for an implementation not to have both forms of the createContext method.
210 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class);
211 Object obj = instantiateProviderIfNecessary(spFactory);
212 // any failure in invoking this method would be considered fatal
213 context = m.invoke(obj, contextPath, classLoader);
214 }
215
216 if (!(context instanceof JAXBContext)) {
217 // the cast would fail, so generate an exception with a nice message
218 throw handleClassCastException(context.getClass(), JAXBContext.class);
219 }
220
221 return (JAXBContext) context;
222 } catch (InvocationTargetException x) {
223 // throw if it is exception not to be wrapped
224 // otherwise, wrap with a JAXBException
225 Throwable e = handleInvocationTargetException(x);
226 throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
227
228 } catch (Exception x) {
229 // can't catch JAXBException because the method is hidden behind
230 // reflection. Root element collisions detected in the call to
231 // createContext() are reported as JAXBExceptions - just re-throw it
232 // some other type of exception - just wrap it
233 throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x);
234 }
235 }
236
237 private static Object instantiateProviderIfNecessary(final Class<?> implClass) throws JAXBException {
238 try {
239 if (JAXBContextFactory.class.isAssignableFrom(implClass)) {
240 return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
258
259 Class spi;
260 try {
261 spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader());
262 } catch (ClassNotFoundException e) {
263 throw new JAXBException(Messages.format(Messages.DEFAULT_PROVIDER_NOT_FOUND), e);
264 }
265
266 if (logger.isLoggable(Level.FINE)) {
267 // extra check to avoid costly which operation if not logged
268 logger.log(Level.FINE, "loaded {0} from {1}", new Object[]{className, which(spi)});
269 }
270
271 return newInstance(classes, properties, spi);
272 }
273
274 static JAXBContext newInstance(Class[] classes,
275 Map properties,
276 Class spFactory) throws JAXBException {
277 try {
278 ModuleUtil.delegateAddOpensToImplModule(classes, spFactory);
279
280 Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
281 Object obj = instantiateProviderIfNecessary(spFactory);
282 Object context = m.invoke(obj, classes, properties);
283 if (!(context instanceof JAXBContext)) {
284 // the cast would fail, so generate an exception with a nice message
285 throw handleClassCastException(context.getClass(), JAXBContext.class);
286 }
287 return (JAXBContext) context;
288
289 } catch (NoSuchMethodException | IllegalAccessException e) {
290 throw new JAXBException(e);
291 } catch (InvocationTargetException e) {
292 // throw if it is exception not to be wrapped
293 // otherwise, wrap with a JAXBException
294 Throwable x = handleInvocationTargetException(e);
295
296 throw new JAXBException(x);
297 }
298 }
299
300 static JAXBContext find(String factoryId,
301 String contextPath,
302 ClassLoader classLoader,
303 Map properties) throws JAXBException {
304
305 if (contextPath == null || contextPath.isEmpty()) {
306 // no context is specified
312
313 //first try with classloader#getResource
314 String factoryClassName = jaxbProperties(contextPath, classLoader, factoryId);
315 if (factoryClassName == null && contextPathClasses != null) {
316 //try with class#getResource
317 factoryClassName = jaxbProperties(contextPathClasses, factoryId);
318 }
319
320 if (factoryClassName != null) {
321 return newInstance(contextPath, contextPathClasses, factoryClassName, classLoader, properties);
322 }
323
324
325 String factoryName = classNameFromSystemProperties();
326 if (factoryName != null) return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);
327
328 JAXBContextFactory obj = ServiceLoaderUtil.firstByServiceLoader(
329 JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
330
331 if (obj != null) {
332 ModuleUtil.delegateAddOpensToImplModule(contextPathClasses, obj.getClass());
333 return obj.createContext(contextPath, classLoader, properties);
334 }
335
336 // to ensure backwards compatibility
337 factoryName = firstByServiceLoaderDeprecated(JAXBContext.class, classLoader);
338 if (factoryName != null) return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);
339
340 Class ctxFactory = (Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader(
341 "javax.xml.bind.JAXBContext", logger);
342
343 if (ctxFactory != null) {
344 return newInstance(contextPath, contextPathClasses, ctxFactory, classLoader, properties);
345 }
346
347 // else no provider found
348 logger.fine("Trying to create the platform default provider");
349 return newInstance(contextPath, contextPathClasses, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties);
350 }
351
352 static JAXBContext find(Class<?>[] classes, Map<String, ?> properties) throws JAXBException {
353
368
369 if (jaxbPropertiesUrl != null) {
370
371 String factoryClassName =
372 classNameFromPackageProperties(
373 jaxbPropertiesUrl,
374 JAXBContext.JAXB_CONTEXT_FACTORY, JAXB_CONTEXT_FACTORY_DEPRECATED);
375
376 return newInstance(classes, properties, factoryClassName);
377 }
378
379 }
380
381 String factoryClassName = classNameFromSystemProperties();
382 if (factoryClassName != null) return newInstance(classes, properties, factoryClassName);
383
384 JAXBContextFactory factory =
385 ServiceLoaderUtil.firstByServiceLoader(JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
386
387 if (factory != null) {
388 ModuleUtil.delegateAddOpensToImplModule(classes, factory.getClass());
389 return factory.createContext(classes, properties);
390 }
391
392 // to ensure backwards compatibility
393 String className = firstByServiceLoaderDeprecated(JAXBContext.class, getContextClassLoader());
394 if (className != null) return newInstance(classes, properties, className);
395
396 logger.fine("Trying to create the platform default provider");
397 Class ctxFactoryClass =
398 (Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext", logger);
399
400 if (ctxFactoryClass != null) {
401 return newInstance(classes, properties, ctxFactoryClass);
402 }
403
404 // else no provider found
405 logger.fine("Trying to create the platform default provider");
406 return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS);
407 }
408
409
|