author | joehw |
Wed, 04 Oct 2017 10:54:18 -0700 | |
changeset 47312 | d4f959806fe9 |
parent 47216 | 71c04702a3d5 |
child 47359 | e1a6c0168741 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
2 |
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. |
47312 | 3 |
* @LastModified: Sep 2017 |
12005 | 4 |
*/ |
5 |
/* |
|
33349 | 6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
7 |
* contributor license agreements. See the NOTICE file distributed with |
|
8 |
* this work for additional information regarding copyright ownership. |
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
10 |
* (the "License"); you may not use this file except in compliance with |
|
11 |
* the License. You may obtain a copy of the License at |
|
12005 | 12 |
* |
33349 | 13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
12005 | 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 |
* $Id: TemplatesImpl.java,v 1.8 2007/03/26 20:12:27 spericas Exp $ |
|
23 |
*/ |
|
24 |
||
25 |
package com.sun.org.apache.xalan.internal.xsltc.trax; |
|
26 |
||
17534 | 27 |
import com.sun.org.apache.xalan.internal.XalanConstants; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
28 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
29 |
import com.sun.org.apache.xalan.internal.xsltc.DOM; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
30 |
import com.sun.org.apache.xalan.internal.xsltc.Translet; |
36520 | 31 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
32 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
33 |
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; |
12005 | 34 |
import java.io.IOException; |
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
35 |
import java.io.NotSerializableException; |
12005 | 36 |
import java.io.ObjectInputStream; |
37 |
import java.io.ObjectOutputStream; |
|
33349 | 38 |
import java.io.ObjectStreamField; |
12005 | 39 |
import java.io.Serializable; |
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
40 |
import java.lang.RuntimePermission; |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
41 |
import java.lang.module.Configuration; |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
42 |
import java.lang.module.ModuleDescriptor; |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
43 |
import java.lang.module.ModuleFinder; |
47312 | 44 |
import java.lang.module.ModuleReader; |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
45 |
import java.lang.module.ModuleReference; |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44558
diff
changeset
|
46 |
import java.lang.reflect.InvocationTargetException; |
12005 | 47 |
import java.security.AccessController; |
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
48 |
import java.security.CodeSigner; |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
49 |
import java.security.CodeSource; |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
50 |
import java.security.PermissionCollection; |
12005 | 51 |
import java.security.PrivilegedAction; |
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
52 |
import java.security.ProtectionDomain; |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
53 |
import java.util.Arrays; |
33349 | 54 |
import java.util.HashMap; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
55 |
import java.util.Map; |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
56 |
import java.util.Optional; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
57 |
import java.util.Properties; |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
58 |
import java.util.Set; |
12005 | 59 |
import javax.xml.XMLConstants; |
60 |
import javax.xml.transform.Templates; |
|
61 |
import javax.xml.transform.Transformer; |
|
62 |
import javax.xml.transform.TransformerConfigurationException; |
|
63 |
import javax.xml.transform.URIResolver; |
|
47312 | 64 |
import jdk.xml.internal.SecuritySupport; |
12005 | 65 |
|
36520 | 66 |
|
12005 | 67 |
/** |
68 |
* @author Morten Jorgensen |
|
69 |
* @author G. Todd Millerj |
|
70 |
* @author Jochen Cordes <Jochen.Cordes@t-online.de> |
|
71 |
* @author Santiago Pericas-Geertsen |
|
72 |
*/ |
|
73 |
public final class TemplatesImpl implements Templates, Serializable { |
|
74 |
static final long serialVersionUID = 673094361519270707L; |
|
16953 | 75 |
public final static String DESERIALIZE_TRANSLET = "jdk.xml.enableTemplatesImplDeserialization"; |
76 |
||
12005 | 77 |
/** |
78 |
* Name of the superclass of all translets. This is needed to |
|
79 |
* determine which, among all classes comprising a translet, |
|
80 |
* is the main one. |
|
81 |
*/ |
|
82 |
private static String ABSTRACT_TRANSLET |
|
83 |
= "com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet"; |
|
84 |
||
85 |
/** |
|
86 |
* Name of the main class or default name if unknown. |
|
87 |
*/ |
|
88 |
private String _name = null; |
|
89 |
||
90 |
/** |
|
91 |
* Contains the actual class definition for the translet class and |
|
92 |
* any auxiliary classes. |
|
93 |
*/ |
|
94 |
private byte[][] _bytecodes = null; |
|
95 |
||
96 |
/** |
|
97 |
* Contains the translet class definition(s). These are created when |
|
98 |
* this Templates is created or when it is read back from disk. |
|
99 |
*/ |
|
100 |
private Class[] _class = null; |
|
101 |
||
102 |
/** |
|
103 |
* The index of the main translet class in the arrays _class[] and |
|
104 |
* _bytecodes. |
|
105 |
*/ |
|
106 |
private int _transletIndex = -1; |
|
107 |
||
108 |
/** |
|
109 |
* Contains the list of auxiliary class definitions. |
|
110 |
*/ |
|
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
111 |
private transient Map<String, Class<?>> _auxClasses = null; |
12005 | 112 |
|
113 |
/** |
|
114 |
* Output properties of this translet. |
|
115 |
*/ |
|
116 |
private Properties _outputProperties; |
|
117 |
||
118 |
/** |
|
119 |
* Number of spaces to add for output indentation. |
|
120 |
*/ |
|
121 |
private int _indentNumber; |
|
122 |
||
123 |
/** |
|
124 |
* This URIResolver is passed to all Transformers. |
|
125 |
* Declaring it transient to fix bug 22438 |
|
126 |
*/ |
|
127 |
private transient URIResolver _uriResolver = null; |
|
128 |
||
129 |
/** |
|
130 |
* Cache the DTM for the stylesheet in a thread local variable, |
|
131 |
* which is used by the document('') function. |
|
132 |
* Use ThreadLocal because a DTM cannot be shared between |
|
133 |
* multiple threads. |
|
134 |
* Declaring it transient to fix bug 22438 |
|
135 |
*/ |
|
136 |
private transient ThreadLocal _sdom = new ThreadLocal(); |
|
137 |
||
138 |
/** |
|
139 |
* A reference to the transformer factory that this templates |
|
140 |
* object belongs to. |
|
141 |
*/ |
|
142 |
private transient TransformerFactoryImpl _tfactory = null; |
|
143 |
||
33349 | 144 |
/** |
145 |
* A flag to determine whether the Service Mechanism is used |
|
146 |
*/ |
|
147 |
private transient boolean _useServicesMechanism; |
|
12458 | 148 |
|
17534 | 149 |
/** |
150 |
* protocols allowed for external references set by the stylesheet processing instruction, Import and Include element. |
|
151 |
*/ |
|
33349 | 152 |
private transient String _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT; |
153 |
||
154 |
/** |
|
155 |
* @serialField _name String The Name of the main class |
|
156 |
* @serialField _bytecodes byte[][] Class definition |
|
157 |
* @serialField _class Class[] The translet class definition(s). |
|
158 |
* @serialField _transletIndex int The index of the main translet class |
|
159 |
* @serialField _outputProperties Properties Output properties of this translet. |
|
160 |
* @serialField _indentNumber int Number of spaces to add for output indentation. |
|
161 |
*/ |
|
162 |
private static final ObjectStreamField[] serialPersistentFields = |
|
163 |
new ObjectStreamField[] { |
|
164 |
new ObjectStreamField("_name", String.class), |
|
165 |
new ObjectStreamField("_bytecodes", byte[][].class), |
|
166 |
new ObjectStreamField("_class", Class[].class), |
|
167 |
new ObjectStreamField("_transletIndex", int.class), |
|
168 |
new ObjectStreamField("_outputProperties", Properties.class), |
|
169 |
new ObjectStreamField("_indentNumber", int.class), |
|
170 |
}; |
|
17534 | 171 |
|
12005 | 172 |
static final class TransletClassLoader extends ClassLoader { |
42247
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39801
diff
changeset
|
173 |
private final Map<String, Class<?>> _loadedExternalExtensionFunctions; |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
174 |
|
33349 | 175 |
TransletClassLoader(ClassLoader parent) { |
176 |
super(parent); |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
177 |
_loadedExternalExtensionFunctions = null; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
178 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
179 |
|
42247
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39801
diff
changeset
|
180 |
TransletClassLoader(ClassLoader parent, Map<String, Class<?>> mapEF) { |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
181 |
super(parent); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
182 |
_loadedExternalExtensionFunctions = mapEF; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
183 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
184 |
|
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
185 |
@Override |
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
186 |
public Class<?> loadClass(String name) throws ClassNotFoundException { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
187 |
Class<?> ret = null; |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
188 |
// The _loadedExternalExtensionFunctions will be empty when the |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
189 |
// SecurityManager is not set and the FSP is turned off |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
190 |
if (_loadedExternalExtensionFunctions != null) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
191 |
ret = _loadedExternalExtensionFunctions.get(name); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
192 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
193 |
if (ret == null) { |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
194 |
ret = super.loadClass(name); |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
195 |
} |
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
196 |
return ret; |
33349 | 197 |
} |
12005 | 198 |
|
199 |
/** |
|
200 |
* Access to final protected superclass member from outer class. |
|
201 |
*/ |
|
202 |
Class defineClass(final byte[] b) { |
|
203 |
return defineClass(null, b, 0, b.length); |
|
204 |
} |
|
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
205 |
|
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
206 |
Class defineClass(final byte[] b, ProtectionDomain pd) { |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
207 |
return defineClass(null, b, 0, b.length, pd); |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
208 |
} |
12005 | 209 |
} |
210 |
||
211 |
||
212 |
/** |
|
213 |
* Create an XSLTC template object from the bytecodes. |
|
214 |
* The bytecodes for the translet and auxiliary classes, plus the name of |
|
215 |
* the main translet class, must be supplied. |
|
216 |
*/ |
|
217 |
protected TemplatesImpl(byte[][] bytecodes, String transletName, |
|
218 |
Properties outputProperties, int indentNumber, |
|
219 |
TransformerFactoryImpl tfactory) |
|
220 |
{ |
|
221 |
_bytecodes = bytecodes; |
|
12458 | 222 |
init(transletName, outputProperties, indentNumber, tfactory); |
12005 | 223 |
} |
224 |
||
225 |
/** |
|
226 |
* Create an XSLTC template object from the translet class definition(s). |
|
227 |
*/ |
|
42247
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39801
diff
changeset
|
228 |
protected TemplatesImpl(Class<?>[] transletClasses, String transletName, |
12005 | 229 |
Properties outputProperties, int indentNumber, |
230 |
TransformerFactoryImpl tfactory) |
|
231 |
{ |
|
232 |
_class = transletClasses; |
|
12458 | 233 |
_transletIndex = 0; |
234 |
init(transletName, outputProperties, indentNumber, tfactory); |
|
235 |
} |
|
236 |
||
237 |
private void init(String transletName, |
|
238 |
Properties outputProperties, int indentNumber, |
|
239 |
TransformerFactoryImpl tfactory) { |
|
12005 | 240 |
_name = transletName; |
241 |
_outputProperties = outputProperties; |
|
242 |
_indentNumber = indentNumber; |
|
243 |
_tfactory = tfactory; |
|
12458 | 244 |
_useServicesMechanism = tfactory.useServicesMechnism(); |
17534 | 245 |
_accessExternalStylesheet = (String) tfactory.getAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET); |
12005 | 246 |
} |
247 |
/** |
|
248 |
* Need for de-serialization, see readObject(). |
|
249 |
*/ |
|
250 |
public TemplatesImpl() { } |
|
251 |
||
252 |
/** |
|
253 |
* Overrides the default readObject implementation since we decided |
|
254 |
* it would be cleaner not to serialize the entire tranformer |
|
255 |
* factory. [ ref bugzilla 12317 ] |
|
256 |
* We need to check if the user defined class for URIResolver also |
|
257 |
* implemented Serializable |
|
258 |
* if yes then we need to deserialize the URIResolver |
|
259 |
* Fix for bugzilla bug 22438 |
|
260 |
*/ |
|
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
261 |
@SuppressWarnings("unchecked") |
12005 | 262 |
private void readObject(ObjectInputStream is) |
263 |
throws IOException, ClassNotFoundException |
|
264 |
{ |
|
16953 | 265 |
SecurityManager security = System.getSecurityManager(); |
266 |
if (security != null){ |
|
267 |
String temp = SecuritySupport.getSystemProperty(DESERIALIZE_TRANSLET); |
|
268 |
if (temp == null || !(temp.length()==0 || temp.equalsIgnoreCase("true"))) { |
|
269 |
ErrorMsg err = new ErrorMsg(ErrorMsg.DESERIALIZE_TRANSLET_ERR); |
|
270 |
throw new UnsupportedOperationException(err.toString()); |
|
271 |
} |
|
272 |
} |
|
273 |
||
33349 | 274 |
// We have to read serialized fields first. |
275 |
ObjectInputStream.GetField gf = is.readFields(); |
|
276 |
_name = (String)gf.get("_name", null); |
|
277 |
_bytecodes = (byte[][])gf.get("_bytecodes", null); |
|
278 |
_class = (Class[])gf.get("_class", null); |
|
279 |
_transletIndex = gf.get("_transletIndex", -1); |
|
280 |
||
281 |
_outputProperties = (Properties)gf.get("_outputProperties", null); |
|
282 |
_indentNumber = gf.get("_indentNumber", 0); |
|
283 |
||
12005 | 284 |
if (is.readBoolean()) { |
285 |
_uriResolver = (URIResolver) is.readObject(); |
|
286 |
} |
|
287 |
||
288 |
_tfactory = new TransformerFactoryImpl(); |
|
289 |
} |
|
290 |
||
291 |
||
292 |
/** |
|
293 |
* This is to fix bugzilla bug 22438 |
|
294 |
* If the user defined class implements URIResolver and Serializable |
|
295 |
* then we want it to get serialized |
|
296 |
*/ |
|
297 |
private void writeObject(ObjectOutputStream os) |
|
298 |
throws IOException, ClassNotFoundException { |
|
33350
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
299 |
if (_auxClasses != null) { |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
300 |
//throw with the same message as when Hashtable was used for compatibility. |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
301 |
throw new NotSerializableException( |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
302 |
"com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable"); |
97bf4aeb431d
8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization
joehw
parents:
33349
diff
changeset
|
303 |
} |
33349 | 304 |
|
305 |
// Write serialized fields |
|
306 |
ObjectOutputStream.PutField pf = os.putFields(); |
|
307 |
pf.put("_name", _name); |
|
308 |
pf.put("_bytecodes", _bytecodes); |
|
309 |
pf.put("_class", _class); |
|
310 |
pf.put("_transletIndex", _transletIndex); |
|
311 |
pf.put("_outputProperties", _outputProperties); |
|
312 |
pf.put("_indentNumber", _indentNumber); |
|
313 |
os.writeFields(); |
|
314 |
||
12005 | 315 |
if (_uriResolver instanceof Serializable) { |
316 |
os.writeBoolean(true); |
|
317 |
os.writeObject((Serializable) _uriResolver); |
|
318 |
} |
|
319 |
else { |
|
320 |
os.writeBoolean(false); |
|
321 |
} |
|
322 |
} |
|
323 |
||
12458 | 324 |
/** |
325 |
* Return the state of the services mechanism feature. |
|
326 |
*/ |
|
327 |
public boolean useServicesMechnism() { |
|
328 |
return _useServicesMechanism; |
|
329 |
} |
|
12005 | 330 |
|
331 |
/** |
|
332 |
* Store URIResolver needed for Transformers. |
|
333 |
*/ |
|
334 |
public synchronized void setURIResolver(URIResolver resolver) { |
|
335 |
_uriResolver = resolver; |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* The TransformerFactory must pass us the translet bytecodes using this |
|
340 |
* method before we can create any translet instances |
|
341 |
* |
|
342 |
* Note: This method is private for security reasons. See |
|
343 |
* CR 6537898. When merging with Apache, we must ensure |
|
344 |
* that the privateness of this method is maintained (that |
|
345 |
* is why it wasn't removed). |
|
346 |
*/ |
|
347 |
private synchronized void setTransletBytecodes(byte[][] bytecodes) { |
|
348 |
_bytecodes = bytecodes; |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Returns the translet bytecodes stored in this template |
|
353 |
* |
|
354 |
* Note: This method is private for security reasons. See |
|
355 |
* CR 6537898. When merging with Apache, we must ensure |
|
356 |
* that the privateness of this method is maintained (that |
|
357 |
* is why it wasn't removed). |
|
358 |
*/ |
|
359 |
private synchronized byte[][] getTransletBytecodes() { |
|
360 |
return _bytecodes; |
|
361 |
} |
|
362 |
||
363 |
/** |
|
364 |
* Returns the translet bytecodes stored in this template |
|
365 |
* |
|
366 |
* Note: This method is private for security reasons. See |
|
367 |
* CR 6537898. When merging with Apache, we must ensure |
|
368 |
* that the privateness of this method is maintained (that |
|
369 |
* is why it wasn't removed). |
|
370 |
*/ |
|
371 |
private synchronized Class[] getTransletClasses() { |
|
372 |
try { |
|
373 |
if (_class == null) defineTransletClasses(); |
|
374 |
} |
|
375 |
catch (TransformerConfigurationException e) { |
|
376 |
// Falls through |
|
377 |
} |
|
378 |
return _class; |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Returns the index of the main class in array of bytecodes |
|
383 |
*/ |
|
384 |
public synchronized int getTransletIndex() { |
|
385 |
try { |
|
386 |
if (_class == null) defineTransletClasses(); |
|
387 |
} |
|
388 |
catch (TransformerConfigurationException e) { |
|
389 |
// Falls through |
|
390 |
} |
|
391 |
return _transletIndex; |
|
392 |
} |
|
393 |
||
394 |
/** |
|
395 |
* The TransformerFactory should call this method to set the translet name |
|
396 |
*/ |
|
397 |
protected synchronized void setTransletName(String name) { |
|
398 |
_name = name; |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* Returns the name of the main translet class stored in this template |
|
403 |
*/ |
|
404 |
protected synchronized String getTransletName() { |
|
405 |
return _name; |
|
406 |
} |
|
407 |
||
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
408 |
|
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
409 |
/** |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
410 |
* Creates a module layer with one module that is defined to the given class |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
411 |
* loader. |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
412 |
*/ |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
413 |
private Module createModule(ModuleDescriptor descriptor, ClassLoader loader) { |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
414 |
String mn = descriptor.name(); |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
415 |
|
42803
d78543be1ebb
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42392
diff
changeset
|
416 |
ModuleReference mref = new ModuleReference(descriptor, null) { |
d78543be1ebb
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42392
diff
changeset
|
417 |
@Override |
d78543be1ebb
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42392
diff
changeset
|
418 |
public ModuleReader open() { |
d78543be1ebb
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42392
diff
changeset
|
419 |
throw new UnsupportedOperationException(); |
d78543be1ebb
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42392
diff
changeset
|
420 |
} |
d78543be1ebb
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42392
diff
changeset
|
421 |
}; |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
422 |
|
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
423 |
ModuleFinder finder = new ModuleFinder() { |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
424 |
@Override |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
425 |
public Optional<ModuleReference> find(String name) { |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
426 |
if (name.equals(mn)) { |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
427 |
return Optional.of(mref); |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
428 |
} else { |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
429 |
return Optional.empty(); |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
430 |
} |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
431 |
} |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
432 |
@Override |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
433 |
public Set<ModuleReference> findAll() { |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
434 |
return Set.of(mref); |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
435 |
} |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
436 |
}; |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
437 |
|
44558
2ee2259a06e2
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44376
diff
changeset
|
438 |
ModuleLayer bootLayer = ModuleLayer.boot(); |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
439 |
|
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
440 |
Configuration cf = bootLayer.configuration() |
43747
9d82e9cac79e
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43254
diff
changeset
|
441 |
.resolve(finder, ModuleFinder.of(), Set.of(mn)); |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
442 |
|
44558
2ee2259a06e2
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44376
diff
changeset
|
443 |
PrivilegedAction<ModuleLayer> pa = () -> bootLayer.defineModules(cf, name -> loader); |
2ee2259a06e2
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44376
diff
changeset
|
444 |
ModuleLayer layer = AccessController.doPrivileged(pa); |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
445 |
|
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
446 |
Module m = layer.findModule(mn).get(); |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
447 |
assert m.getLayer() == layer; |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
448 |
|
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
449 |
return m; |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
450 |
} |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
451 |
|
12005 | 452 |
/** |
453 |
* Defines the translet class and auxiliary classes. |
|
454 |
* Returns a reference to the Class object that defines the main class |
|
455 |
*/ |
|
456 |
private void defineTransletClasses() |
|
457 |
throws TransformerConfigurationException { |
|
458 |
||
459 |
if (_bytecodes == null) { |
|
460 |
ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR); |
|
461 |
throw new TransformerConfigurationException(err.toString()); |
|
462 |
} |
|
463 |
||
464 |
TransletClassLoader loader = (TransletClassLoader) |
|
465 |
AccessController.doPrivileged(new PrivilegedAction() { |
|
466 |
public Object run() { |
|
29945
4912169d22d3
8040228: TransformerConfigurationException occurs with security manager, FSP and XSLT Ext
aefimov
parents:
25868
diff
changeset
|
467 |
return new TransletClassLoader(ObjectFactory.findClassLoader(),_tfactory.getExternalExtensionsMap()); |
12005 | 468 |
} |
469 |
}); |
|
470 |
||
471 |
try { |
|
472 |
final int classCount = _bytecodes.length; |
|
473 |
_class = new Class[classCount]; |
|
474 |
||
475 |
if (classCount > 1) { |
|
33349 | 476 |
_auxClasses = new HashMap<>(); |
12005 | 477 |
} |
478 |
||
36520 | 479 |
// create a module for the translet |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
480 |
|
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
481 |
String mn = "jdk.translet"; |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
482 |
|
36520 | 483 |
String pn = _tfactory.getPackageName(); |
484 |
assert pn != null && pn.length() > 0; |
|
485 |
||
43747
9d82e9cac79e
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43254
diff
changeset
|
486 |
ModuleDescriptor descriptor = |
9d82e9cac79e
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43254
diff
changeset
|
487 |
ModuleDescriptor.newModule(mn, Set.of(ModuleDescriptor.Modifier.SYNTHETIC)) |
9d82e9cac79e
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43254
diff
changeset
|
488 |
.requires("java.xml") |
44376
d1cd4860ffe4
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43747
diff
changeset
|
489 |
.exports(pn, Set.of("java.xml")) |
43747
9d82e9cac79e
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43254
diff
changeset
|
490 |
.build(); |
36520 | 491 |
|
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
492 |
Module m = createModule(descriptor, loader); |
36520 | 493 |
|
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
494 |
// the module needs access to runtime classes |
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
495 |
Module thisModule = TemplatesImpl.class.getModule(); |
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
496 |
// the module also needs permission to access each package |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
497 |
// that is exported to it |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
498 |
PermissionCollection perms = |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
499 |
new RuntimePermission("*").newPermissionCollection(); |
36520 | 500 |
Arrays.asList(Constants.PKGS_USED_BY_TRANSLET_CLASSES).forEach(p -> { |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
501 |
thisModule.addExports(p, m); |
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
502 |
perms.add(new RuntimePermission("accessClassInPackage." + p)); |
36520 | 503 |
}); |
504 |
||
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
505 |
CodeSource codeSource = new CodeSource(null, (CodeSigner[])null); |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
506 |
ProtectionDomain pd = new ProtectionDomain(codeSource, perms, |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
507 |
loader, null); |
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
508 |
|
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
509 |
// java.xml needs to instantiate the translet class |
37838
45821736044e
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36520
diff
changeset
|
510 |
thisModule.addReads(m); |
36520 | 511 |
|
12005 | 512 |
for (int i = 0; i < classCount; i++) { |
43254
0dd6e35cf667
8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents:
42803
diff
changeset
|
513 |
_class[i] = loader.defineClass(_bytecodes[i], pd); |
12005 | 514 |
final Class superClass = _class[i].getSuperclass(); |
515 |
||
516 |
// Check if this is the main class |
|
517 |
if (superClass.getName().equals(ABSTRACT_TRANSLET)) { |
|
518 |
_transletIndex = i; |
|
519 |
} |
|
520 |
else { |
|
521 |
_auxClasses.put(_class[i].getName(), _class[i]); |
|
522 |
} |
|
523 |
} |
|
524 |
||
525 |
if (_transletIndex < 0) { |
|
526 |
ErrorMsg err= new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name); |
|
527 |
throw new TransformerConfigurationException(err.toString()); |
|
528 |
} |
|
529 |
} |
|
530 |
catch (ClassFormatError e) { |
|
531 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name); |
|
39801
3d0df6c4665a
8153082: Update XSLT compiler to generate classes that invoke addReads
dfuchs
parents:
38501
diff
changeset
|
532 |
throw new TransformerConfigurationException(err.toString(), e); |
12005 | 533 |
} |
534 |
catch (LinkageError e) { |
|
535 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); |
|
36520 | 536 |
throw new TransformerConfigurationException(err.toString(), e); |
12005 | 537 |
} |
538 |
} |
|
539 |
||
540 |
/** |
|
541 |
* This method generates an instance of the translet class that is |
|
542 |
* wrapped inside this Template. The translet instance will later |
|
543 |
* be wrapped inside a Transformer object. |
|
544 |
*/ |
|
545 |
private Translet getTransletInstance() |
|
546 |
throws TransformerConfigurationException { |
|
547 |
try { |
|
548 |
if (_name == null) return null; |
|
549 |
||
550 |
if (_class == null) defineTransletClasses(); |
|
551 |
||
552 |
// The translet needs to keep a reference to all its auxiliary |
|
553 |
// class to prevent the GC from collecting them |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44558
diff
changeset
|
554 |
AbstractTranslet translet = (AbstractTranslet) |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44558
diff
changeset
|
555 |
_class[_transletIndex].getConstructor().newInstance(); |
12005 | 556 |
translet.postInitialization(); |
557 |
translet.setTemplates(this); |
|
12458 | 558 |
translet.setServicesMechnism(_useServicesMechanism); |
17534 | 559 |
translet.setAllowedProtocols(_accessExternalStylesheet); |
12005 | 560 |
if (_auxClasses != null) { |
561 |
translet.setAuxiliaryClasses(_auxClasses); |
|
562 |
} |
|
563 |
||
564 |
return translet; |
|
565 |
} |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44558
diff
changeset
|
566 |
catch (InstantiationException | IllegalAccessException | |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44558
diff
changeset
|
567 |
NoSuchMethodException | InvocationTargetException e) { |
12005 | 568 |
ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44558
diff
changeset
|
569 |
throw new TransformerConfigurationException(err.toString(), e); |
12005 | 570 |
} |
571 |
} |
|
572 |
||
573 |
/** |
|
574 |
* Implements JAXP's Templates.newTransformer() |
|
575 |
* |
|
576 |
* @throws TransformerConfigurationException |
|
577 |
*/ |
|
578 |
public synchronized Transformer newTransformer() |
|
579 |
throws TransformerConfigurationException |
|
580 |
{ |
|
581 |
TransformerImpl transformer; |
|
582 |
||
583 |
transformer = new TransformerImpl(getTransletInstance(), _outputProperties, |
|
584 |
_indentNumber, _tfactory); |
|
585 |
||
586 |
if (_uriResolver != null) { |
|
587 |
transformer.setURIResolver(_uriResolver); |
|
588 |
} |
|
589 |
||
590 |
if (_tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) { |
|
591 |
transformer.setSecureProcessing(true); |
|
592 |
} |
|
593 |
return transformer; |
|
594 |
} |
|
595 |
||
596 |
/** |
|
597 |
* Implements JAXP's Templates.getOutputProperties(). We need to |
|
598 |
* instanciate a translet to get the output settings, so |
|
599 |
* we might as well just instanciate a Transformer and use its |
|
600 |
* implementation of this method. |
|
601 |
*/ |
|
602 |
public synchronized Properties getOutputProperties() { |
|
603 |
try { |
|
604 |
return newTransformer().getOutputProperties(); |
|
605 |
} |
|
606 |
catch (TransformerConfigurationException e) { |
|
607 |
return null; |
|
608 |
} |
|
609 |
} |
|
610 |
||
611 |
/** |
|
612 |
* Return the thread local copy of the stylesheet DOM. |
|
613 |
*/ |
|
614 |
public DOM getStylesheetDOM() { |
|
615 |
return (DOM)_sdom.get(); |
|
616 |
} |
|
617 |
||
618 |
/** |
|
619 |
* Set the thread local copy of the stylesheet DOM. |
|
620 |
*/ |
|
621 |
public void setStylesheetDOM(DOM sdom) { |
|
622 |
_sdom.set(sdom); |
|
623 |
} |
|
624 |
} |