1 21 package oracle.toplink.essentials.platform.xml; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 27 import oracle.toplink.essentials.exceptions.ValidationException; 28 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 29 import oracle.toplink.essentials.internal.security.PrivilegedGetClassLoaderForClass; 30 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass; 31 32 public class XMLPlatformFactory { 33 public static final String XML_PLATFORM_PROPERTY = "toplink.xml.platform"; 34 public static final String XDK_PLATFORM_CLASS_NAME = "oracle.toplink.essentials.platform.xml.xdk.XDKPlatform"; 35 public static final String JAXP_PLATFORM_CLASS_NAME = "oracle.toplink.essentials.platform.xml.jaxp.JAXPPlatform"; 36 private static XMLPlatformFactory instance; 37 private Class xmlPlatformClass; 38 39 private XMLPlatformFactory() { 40 super(); 41 } 42 43 49 public static XMLPlatformFactory getInstance() throws XMLPlatformException { 50 if (null == instance) { 51 instance = new XMLPlatformFactory(); 52 } 53 return instance; 54 } 55 56 62 public Class getXMLPlatformClass() throws XMLPlatformException { 63 if (null != xmlPlatformClass) { 64 return xmlPlatformClass; 65 } 66 67 String newXMLPlatformClassName = System.getProperty(XML_PLATFORM_PROPERTY); 68 if (null == newXMLPlatformClassName) { 69 newXMLPlatformClassName = JAXP_PLATFORM_CLASS_NAME; 70 } 71 72 try { 73 ClassLoader classLoader = null; 74 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 75 try{ 76 classLoader = (ClassLoader )AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(this.getClass())); 77 } catch (PrivilegedActionException exc){ 78 } 80 } else { 81 classLoader = PrivilegedAccessHelper.getClassLoaderForClass(this.getClass()); 82 } 83 Class newXMLPlatformClass = classLoader.loadClass(newXMLPlatformClassName); 86 setXMLPlatformClass(newXMLPlatformClass); 87 return xmlPlatformClass; 88 } catch (ClassNotFoundException e) { 89 throw XMLPlatformException.xmlPlatformClassNotFound(newXMLPlatformClassName, e); 90 } 91 } 92 93 97 public void setXMLPlatformClass(Class xmlPlatformClass) { 98 this.xmlPlatformClass = xmlPlatformClass; 99 } 100 101 107 public XMLPlatform getXMLPlatform() throws XMLPlatformException { 108 try { 109 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 110 try { 111 return (XMLPlatform)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(getXMLPlatformClass())); 112 } catch (PrivilegedActionException exception) { 113 Exception throwableException = exception.getException(); 114 if (throwableException instanceof InstantiationException ) { 115 throw XMLPlatformException.xmlPlatformCouldNotInstantiate(getXMLPlatformClass().getName(), throwableException); 116 } else { 117 throw XMLPlatformException.xmlPlatformCouldNotInstantiate(getXMLPlatformClass().getName(), throwableException); 118 } 119 } 120 } else { 121 return (XMLPlatform)PrivilegedAccessHelper.newInstanceFromClass(getXMLPlatformClass()); 122 } 123 } catch (IllegalAccessException e) { 124 throw XMLPlatformException.xmlPlatformCouldNotInstantiate(getXMLPlatformClass().getName(), e); 125 } catch (InstantiationException e) { 126 throw XMLPlatformException.xmlPlatformCouldNotInstantiate(getXMLPlatformClass().getName(), e); 127 } 128 } 129 } 130 | Popular Tags |