author | joehw |
Wed, 18 Oct 2017 13:25:49 -0700 | |
changeset 47359 | e1a6c0168741 |
parent 47312 | d4f959806fe9 |
child 48409 | 5ab69533994b |
permissions | -rw-r--r-- |
12458 | 1 |
/* |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47312
diff
changeset
|
3 |
* @LastModified: Oct 2017 |
12458 | 4 |
*/ |
5 |
/* |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
42806
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
42806
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
42806
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
42806
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
42806
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
42806
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
12458 | 12 |
* |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
42806
diff
changeset
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
12458 | 14 |
* |
15 |
* Unless required by applicable law or agreed to in writing, software |
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 |
* See the License for the specific language governing permissions and |
|
19 |
* limitations under the License. |
|
20 |
*/ |
|
21 |
||
22 |
package com.sun.org.apache.xalan.internal.utils; |
|
23 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
24 |
import java.util.function.Supplier; |
47312 | 25 |
import jdk.xml.internal.SecuritySupport; |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
26 |
|
12458 | 27 |
/** |
28 |
* This class is duplicated for each JAXP subpackage so keep it in sync. |
|
29 |
* It is package private and therefore is not exposed as part of the JAXP |
|
30 |
* API. |
|
31 |
* <p> |
|
32 |
* This class was moved from the <code>javax.xml.parsers.ObjectFactory</code> |
|
33 |
* class and modified to be used as a general utility for creating objects |
|
34 |
* dynamically. |
|
35 |
* |
|
36 |
*/ |
|
37 |
public class ObjectFactory { |
|
38 |
||
39 |
// |
|
40 |
// Constants |
|
41 |
// |
|
25590 | 42 |
private static final String JAXP_INTERNAL = "com.sun.org.apache"; |
43 |
private static final String STAX_INTERNAL = "com.sun.xml.internal"; |
|
12458 | 44 |
|
45 |
/** Set to true for debugging */ |
|
46 |
private static final boolean DEBUG = false; |
|
47 |
||
48 |
||
49 |
/** Prints a message to standard error if debugging is enabled. */ |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
50 |
private static void debugPrintln(Supplier<String> msgGen) { |
12458 | 51 |
if (DEBUG) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
52 |
System.err.println("JAXP: " + msgGen.get()); |
12458 | 53 |
} |
54 |
} // debugPrintln(String) |
|
55 |
||
56 |
/** |
|
57 |
* Figure out which ClassLoader to use. For JDK 1.2 and later use |
|
58 |
* the context ClassLoader. |
|
59 |
*/ |
|
60 |
public static ClassLoader findClassLoader() |
|
61 |
{ |
|
62 |
if (System.getSecurityManager()!=null) { |
|
63 |
//this will ensure bootclassloader is used |
|
64 |
return null; |
|
65 |
} |
|
66 |
||
67 |
// Figure out which ClassLoader to use for loading the provider |
|
68 |
// class. If there is a Context ClassLoader then use it. |
|
69 |
ClassLoader context = SecuritySupport.getContextClassLoader(); |
|
70 |
ClassLoader system = SecuritySupport.getSystemClassLoader(); |
|
71 |
||
72 |
ClassLoader chain = system; |
|
73 |
while (true) { |
|
74 |
if (context == chain) { |
|
75 |
// Assert: we are on JDK 1.1 or we have no Context ClassLoader |
|
76 |
// or any Context ClassLoader in chain of system classloader |
|
77 |
// (including extension ClassLoader) so extend to widest |
|
78 |
// ClassLoader (always look in system ClassLoader if Xalan |
|
79 |
// is in boot/extension/system classpath and in current |
|
80 |
// ClassLoader otherwise); normal classloaders delegate |
|
81 |
// back to system ClassLoader first so this widening doesn't |
|
82 |
// change the fact that context ClassLoader will be consulted |
|
83 |
ClassLoader current = ObjectFactory.class.getClassLoader(); |
|
84 |
||
85 |
chain = system; |
|
86 |
while (true) { |
|
87 |
if (current == chain) { |
|
88 |
// Assert: Current ClassLoader in chain of |
|
89 |
// boot/extension/system ClassLoaders |
|
90 |
return system; |
|
91 |
} |
|
92 |
if (chain == null) { |
|
93 |
break; |
|
94 |
} |
|
95 |
chain = SecuritySupport.getParentClassLoader(chain); |
|
96 |
} |
|
97 |
||
98 |
// Assert: Current ClassLoader not in chain of |
|
99 |
// boot/extension/system ClassLoaders |
|
100 |
return current; |
|
101 |
} |
|
102 |
||
103 |
if (chain == null) { |
|
104 |
// boot ClassLoader reached |
|
105 |
break; |
|
106 |
} |
|
107 |
||
108 |
// Check for any extension ClassLoaders in chain up to |
|
109 |
// boot ClassLoader |
|
110 |
chain = SecuritySupport.getParentClassLoader(chain); |
|
111 |
} |
|
112 |
||
113 |
// Assert: Context ClassLoader not in chain of |
|
114 |
// boot/extension/system ClassLoaders |
|
115 |
return context; |
|
116 |
} // findClassLoader():ClassLoader |
|
117 |
||
118 |
/** |
|
18352
a1e183c996d6
8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents:
16953
diff
changeset
|
119 |
* Create an instance of a class using the same class loader for the ObjectFactory by default |
a1e183c996d6
8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents:
16953
diff
changeset
|
120 |
* or boot class loader when Security Manager is in place |
12458 | 121 |
*/ |
122 |
public static Object newInstance(String className, boolean doFallback) |
|
123 |
throws ConfigurationError |
|
124 |
{ |
|
31497 | 125 |
ClassLoader cl = System.getSecurityManager()!=null ? null : findClassLoader(); |
12458 | 126 |
try{ |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47312
diff
changeset
|
127 |
Class<?> providerClass = findProviderClass(className, cl, doFallback); |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
128 |
Object instance = providerClass.getConstructor().newInstance(); |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
129 |
debugPrintln(()->"created new instance of " + providerClass + |
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
130 |
" using ClassLoader: " + cl); |
12458 | 131 |
return instance; |
132 |
} catch (ClassNotFoundException x) { |
|
133 |
throw new ConfigurationError( |
|
134 |
"Provider " + className + " not found", x); |
|
135 |
} catch (Exception x) { |
|
136 |
throw new ConfigurationError( |
|
137 |
"Provider " + className + " could not be instantiated: " + x, |
|
138 |
x); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
18352
a1e183c996d6
8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents:
16953
diff
changeset
|
143 |
* Find a Class using the same class loader for the ObjectFactory by default |
a1e183c996d6
8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents:
16953
diff
changeset
|
144 |
* or boot class loader when Security Manager is in place |
12458 | 145 |
*/ |
18352
a1e183c996d6
8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents:
16953
diff
changeset
|
146 |
public static Class<?> findProviderClass(String className, boolean doFallback) |
12458 | 147 |
throws ClassNotFoundException, ConfigurationError |
148 |
{ |
|
25590 | 149 |
return findProviderClass (className, |
12458 | 150 |
findClassLoader (), doFallback); |
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* Find a Class using the specified ClassLoader |
|
155 |
*/ |
|
18352
a1e183c996d6
8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents:
16953
diff
changeset
|
156 |
private static Class<?> findProviderClass(String className, ClassLoader cl, |
12458 | 157 |
boolean doFallback) |
158 |
throws ClassNotFoundException, ConfigurationError |
|
159 |
{ |
|
160 |
//throw security exception if the calling thread is not allowed to access the |
|
161 |
//class. Restrict the access to the package classes as specified in java.security policy. |
|
162 |
SecurityManager security = System.getSecurityManager(); |
|
163 |
try{ |
|
16953 | 164 |
if (security != null){ |
25590 | 165 |
if (className.startsWith(JAXP_INTERNAL) || |
166 |
className.startsWith(STAX_INTERNAL)) { |
|
16953 | 167 |
cl = null; |
168 |
} else { |
|
12458 | 169 |
final int lastDot = className.lastIndexOf("."); |
170 |
String packageName = className; |
|
171 |
if (lastDot != -1) packageName = className.substring(0, lastDot); |
|
172 |
security.checkPackageAccess(packageName); |
|
16953 | 173 |
} |
174 |
} |
|
12458 | 175 |
}catch(SecurityException e){ |
176 |
throw e; |
|
177 |
} |
|
178 |
||
18352
a1e183c996d6
8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents:
16953
diff
changeset
|
179 |
Class<?> providerClass; |
12458 | 180 |
if (cl == null) { |
25590 | 181 |
providerClass = Class.forName(className, false, ObjectFactory.class.getClassLoader()); |
12458 | 182 |
} else { |
183 |
try { |
|
184 |
providerClass = cl.loadClass(className); |
|
185 |
} catch (ClassNotFoundException x) { |
|
186 |
if (doFallback) { |
|
187 |
// Fall back to current classloader |
|
188 |
ClassLoader current = ObjectFactory.class.getClassLoader(); |
|
189 |
if (current == null) { |
|
190 |
providerClass = Class.forName(className); |
|
191 |
} else if (cl != current) { |
|
192 |
cl = current; |
|
193 |
providerClass = cl.loadClass(className); |
|
194 |
} else { |
|
195 |
throw x; |
|
196 |
} |
|
197 |
} else { |
|
198 |
throw x; |
|
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
return providerClass; |
|
204 |
} |
|
205 |
||
206 |
} // class ObjectFactory |