src/share/jaxws_classes/javax/xml/bind/ContextFinder.java

changeset 397
b99d7e355d4b
parent 368
0989ad8c0860
child 637
9c07ef4934dd
equal deleted inserted replaced
393:6cdc6ed98780 397:b99d7e355d4b
1 /* 1 /*
2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
308 if( factoryClassName != null ) { 308 if( factoryClassName != null ) {
309 return newInstance( contextPath, factoryClassName, classLoader, properties ); 309 return newInstance( contextPath, factoryClassName, classLoader, properties );
310 } 310 }
311 } 311 }
312 312
313 if (getContextClassLoader() == classLoader) { 313 // OSGi search
314 Class factory = lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext"); 314 Class jaxbContext = lookupJaxbContextUsingOsgiServiceLoader();
315 if (factory != null) { 315 if (jaxbContext != null) {
316 logger.fine("OSGi environment detected"); 316 logger.fine("OSGi environment detected");
317 return newInstance(contextPath, factory, classLoader, properties); 317 return newInstance(contextPath, jaxbContext, classLoader, properties);
318 }
319 } 318 }
320 319
321 logger.fine("Searching META-INF/services"); 320 logger.fine("Searching META-INF/services");
322 // search META-INF services next 321 // search META-INF services next
323 BufferedReader r; 322 BufferedReader r = null;
324 try { 323 try {
325 final StringBuilder resource = new StringBuilder().append("META-INF/services/").append(jaxbContextFQCN); 324 final StringBuilder resource = new StringBuilder().append("META-INF/services/").append(jaxbContextFQCN);
326 final InputStream resourceStream = 325 final InputStream resourceStream =
327 classLoader.getResourceAsStream(resource.toString()); 326 classLoader.getResourceAsStream(resource.toString());
328 327
329 if (resourceStream != null) { 328 if (resourceStream != null) {
330 r = new BufferedReader(new InputStreamReader(resourceStream, "UTF-8")); 329 r = new BufferedReader(new InputStreamReader(resourceStream, "UTF-8"));
331 factoryClassName = r.readLine().trim(); 330 factoryClassName = r.readLine();
331 if (factoryClassName != null) {
332 factoryClassName = factoryClassName.trim();
333 }
332 r.close(); 334 r.close();
333 return newInstance(contextPath, factoryClassName, classLoader, properties); 335 return newInstance(contextPath, factoryClassName, classLoader, properties);
334 } else { 336 } else {
335 logger.log(Level.FINE, "Unable to load:{0}", resource.toString()); 337 logger.log(Level.FINE, "Unable to load:{0}", resource.toString());
336 } 338 }
337 } catch (UnsupportedEncodingException e) { 339 } catch (UnsupportedEncodingException e) {
338 // should never happen 340 // should never happen
339 throw new JAXBException(e); 341 throw new JAXBException(e);
340 } catch (IOException e) { 342 } catch (IOException e) {
341 throw new JAXBException(e); 343 throw new JAXBException(e);
344 } finally {
345 try {
346 if (r != null) {
347 r.close();
348 }
349 } catch (IOException ex) {
350 Logger.getLogger(ContextFinder.class.getName()).log(Level.SEVERE, null, ex);
351 }
342 } 352 }
343 353
344 // else no provider found 354 // else no provider found
345 logger.fine("Trying to create the platform default provider"); 355 logger.fine("Trying to create the platform default provider");
346 return newInstance(contextPath, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties); 356 return newInstance(contextPath, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties);
400 } else { 410 } else {
401 logger.fine(" not found"); 411 logger.fine(" not found");
402 } 412 }
403 } 413 }
404 414
405 Class factory = lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext"); 415 // OSGi search
406 if (factory != null) { 416 Class jaxbContext = lookupJaxbContextUsingOsgiServiceLoader();
417 if (jaxbContext != null) {
407 logger.fine("OSGi environment detected"); 418 logger.fine("OSGi environment detected");
408 return newInstance(classes, properties, factory); 419 return newInstance(classes, properties, jaxbContext);
409 } 420 }
410 421
411 // search META-INF services next 422 // search META-INF services next
412 logger.fine("Checking META-INF/services"); 423 logger.fine("Checking META-INF/services");
413 BufferedReader r; 424 BufferedReader r = null;
414 try { 425 try {
415 final String resource = new StringBuilder("META-INF/services/").append(jaxbContextFQCN).toString(); 426 final String resource = new StringBuilder("META-INF/services/").append(jaxbContextFQCN).toString();
416 ClassLoader classLoader = getContextClassLoader(); 427 ClassLoader classLoader = getContextClassLoader();
417 URL resourceURL; 428 URL resourceURL;
418 if(classLoader==null) 429 if(classLoader==null)
421 resourceURL = classLoader.getResource(resource); 432 resourceURL = classLoader.getResource(resource);
422 433
423 if (resourceURL != null) { 434 if (resourceURL != null) {
424 logger.log(Level.FINE, "Reading {0}", resourceURL); 435 logger.log(Level.FINE, "Reading {0}", resourceURL);
425 r = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "UTF-8")); 436 r = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "UTF-8"));
426 factoryClassName = r.readLine().trim(); 437 factoryClassName = r.readLine();
438 if (factoryClassName != null) {
439 factoryClassName = factoryClassName.trim();
440 }
427 return newInstance(classes, properties, factoryClassName); 441 return newInstance(classes, properties, factoryClassName);
428 } else { 442 } else {
429 logger.log(Level.FINE, "Unable to find: {0}", resource); 443 logger.log(Level.FINE, "Unable to find: {0}", resource);
430 } 444 }
431 } catch (UnsupportedEncodingException e) { 445 } catch (UnsupportedEncodingException e) {
432 // should never happen 446 // should never happen
433 throw new JAXBException(e); 447 throw new JAXBException(e);
434 } catch (IOException e) { 448 } catch (IOException e) {
435 throw new JAXBException(e); 449 throw new JAXBException(e);
450 } finally {
451 if (r != null) {
452 try {
453 r.close();
454 } catch (IOException ex) {
455 logger.log(Level.FINE, "Unable to close stream", ex);
456 }
457 }
436 } 458 }
437 459
438 // else no provider found 460 // else no provider found
439 logger.fine("Trying to create the platform default provider"); 461 logger.fine("Trying to create the platform default provider");
440 return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS); 462 return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS);
441 } 463 }
442 464
443 private static Class lookupUsingOSGiServiceLoader(String factoryId) { 465 private static Class lookupJaxbContextUsingOsgiServiceLoader() {
444 try { 466 try {
445 // Use reflection to avoid having any dependendcy on ServiceLoader class 467 // Use reflection to avoid having any dependency on ServiceLoader class
446 Class serviceClass = Class.forName(factoryId);
447 Class target = Class.forName("com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader"); 468 Class target = Class.forName("com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader");
448 Method m = target.getMethod("lookupProviderClasses", Class.class); 469 Method m = target.getMethod("lookupProviderClasses", Class.class);
449 Iterator iter = ((Iterable) m.invoke(null, serviceClass)).iterator(); 470 Iterator iter = ((Iterable) m.invoke(null, JAXBContext.class)).iterator();
450 return iter.hasNext() ? (Class)iter.next() : null; 471 return iter.hasNext() ? (Class)iter.next() : null;
451 } catch(Exception e) { 472 } catch(Exception e) {
452 logger.log(Level.FINE, "Unable to find from OSGi: {0}", factoryId); 473 logger.log(Level.FINE, "Unable to find from OSGi: javax.xml.bind.JAXBContext");
453 return null; 474 return null;
454 } 475 }
455 } 476 }
456 477
457 private static Properties loadJAXBProperties( ClassLoader classLoader, 478 private static Properties loadJAXBProperties( ClassLoader classLoader,

mercurial