KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > platform > xml > XMLPlatformFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.platform.xml;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
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 JavaDoc XML_PLATFORM_PROPERTY = "toplink.xml.platform";
34     public static final String JavaDoc XDK_PLATFORM_CLASS_NAME = "oracle.toplink.essentials.platform.xml.xdk.XDKPlatform";
35     public static final String JavaDoc JAXP_PLATFORM_CLASS_NAME = "oracle.toplink.essentials.platform.xml.jaxp.JAXPPlatform";
36     private static XMLPlatformFactory instance;
37     private Class JavaDoc xmlPlatformClass;
38
39     private XMLPlatformFactory() {
40         super();
41     }
42
43     /**
44      * INTERNAL:
45      * Return the singleton instance of XMLPlatformContext.
46      * @return the the singleton instance of XMLPlatformContext.
47      * @throws XMLPlatformException
48      */

49     public static XMLPlatformFactory getInstance() throws XMLPlatformException {
50         if (null == instance) {
51             instance = new XMLPlatformFactory();
52         }
53         return instance;
54     }
55
56     /**
57      * INTERNAL:
58      * Return the implementation class for the XMLPlatform.
59      * @return the implementation class for the XMLPlatform.
60      * @throws XMLPlatformException
61      */

62     public Class JavaDoc getXMLPlatformClass() throws XMLPlatformException {
63         if (null != xmlPlatformClass) {
64             return xmlPlatformClass;
65         }
66
67         String JavaDoc newXMLPlatformClassName = System.getProperty(XML_PLATFORM_PROPERTY);
68         if (null == newXMLPlatformClassName) {
69             newXMLPlatformClassName = JAXP_PLATFORM_CLASS_NAME;
70         }
71
72         try {
73             ClassLoader JavaDoc classLoader = null;
74             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
75                 try{
76                     classLoader = (ClassLoader JavaDoc)AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(this.getClass()));
77                 } catch (PrivilegedActionException JavaDoc exc){
78                     // will not be thrown
79
}
80             } else {
81                 classLoader = PrivilegedAccessHelper.getClassLoaderForClass(this.getClass());
82             }
83             // ClassLoader classLoader = ClassLoader.getSystemClassLoader();
84
// ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
85
Class JavaDoc newXMLPlatformClass = classLoader.loadClass(newXMLPlatformClassName);
86             setXMLPlatformClass(newXMLPlatformClass);
87             return xmlPlatformClass;
88         } catch (ClassNotFoundException JavaDoc e) {
89             throw XMLPlatformException.xmlPlatformClassNotFound(newXMLPlatformClassName, e);
90         }
91     }
92
93     /**
94      * PUBLIC:
95      * Set the implementation of XMLPlatform.
96      */

97     public void setXMLPlatformClass(Class JavaDoc xmlPlatformClass) {
98         this.xmlPlatformClass = xmlPlatformClass;
99     }
100
101     /**
102      * INTERNAL:
103      * Return the XMLPlatform based on the toplink.xml.platform System property.
104      * @return an instance of XMLPlatform
105      * @throws XMLPlatformException
106      */

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 JavaDoc exception) {
113                     Exception JavaDoc throwableException = exception.getException();
114                     if (throwableException instanceof InstantiationException JavaDoc) {
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 JavaDoc e) {
124             throw XMLPlatformException.xmlPlatformCouldNotInstantiate(getXMLPlatformClass().getName(), e);
125         } catch (InstantiationException JavaDoc e) {
126             throw XMLPlatformException.xmlPlatformCouldNotInstantiate(getXMLPlatformClass().getName(), e);
127         }
128     }
129 }
130
Popular Tags