History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/org/apache/xml/internal/security/Init.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/org/apache/xml/internal/security/Init.java+413−0
@@ -0,0 +1,413 @@
1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21package com.sun.org.apache.xml.internal.security;
22
23import java.io.InputStream;
24import java.security.AccessController;
25import java.security.PrivilegedAction;
26import javax.xml.parsers.DocumentBuilder;
27import javax.xml.parsers.DocumentBuilderFactory;
28
29import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
30import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
31import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
32import com.sun.org.apache.xml.internal.security.keys.KeyInfo;
33import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolver;
34import com.sun.org.apache.xml.internal.security.transforms.Transform;
35import com.sun.org.apache.xml.internal.security.utils.I18n;
36//import com.sun.org.apache.xml.internal.security.utils.PRNG;
37import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
38import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver;
39import org.w3c.dom.Attr;
40import org.w3c.dom.Document;
41import org.w3c.dom.Element;
42import org.w3c.dom.Node;
43
44
45/**
46 * This class does the configuration of the library. This includes creating
47 * the mapping of Canonicalization and Transform algorithms. Initialization is
48 * done by calling {@link Init#init} which should be done in any static block
49 * of the files of this library. We ensure that this call is only executed once.
50 *
51 * @author $Author: mullan $
52 */
53public final class Init {
54
55 /** {@link java.util.logging} logging facility */
56 static java.util.logging.Logger log =
57 java.util.logging.Logger.getLogger(Init.class.getName());
58
59 /** Field _initialized */
60 private static boolean _alreadyInitialized = false;
61
62 /** The namespace for CONF file **/
63 public static final String CONF_NS="http://www.xmlsecurity.org/NS/#configuration";
64
65 /**
66 * Method isInitialized
67 * @return true if the librairy is already initialized.
68 *
69 */
70 public static final boolean isInitialized() {
71 return Init._alreadyInitialized;
72 }
73
74 /**
75 * Method init
76 *
77 */
78 public synchronized static void init() {
79
80 if (_alreadyInitialized) {
81 return;
82 }
83 long XX_configure_i18n_end=0;
84 long XX_configure_reg_c14n_start=0;
85 long XX_configure_reg_c14n_end=0;
86 long XX_configure_reg_jcemapper_end=0;
87 long XX_configure_reg_keyInfo_start=0;
88 long XX_configure_reg_keyResolver_end=0;
89 long XX_configure_reg_prefixes_start=0;
90 long XX_configure_reg_resourceresolver_start=0;
91 long XX_configure_reg_sigalgos_end=0;
92 long XX_configure_reg_transforms_end=0;
93 long XX_configure_reg_keyInfo_end=0;
94 long XX_configure_reg_keyResolver_start=0;
95 _alreadyInitialized = true;
96
97 try {
98 long XX_init_start = System.currentTimeMillis();
99 long XX_prng_start = System.currentTimeMillis();
100
101 //PRNG.init(new java.security.SecureRandom());
102
103 long XX_prng_end = System.currentTimeMillis();
104
105 /* read library configuration file */
106 long XX_parsing_start = System.currentTimeMillis();
107 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
108
109 dbf.setNamespaceAware(true);
110 dbf.setValidating(false);
111
112 DocumentBuilder db = dbf.newDocumentBuilder();
113 // We don't allow users to override the Apache XML Security
114 // configuration in the JRE. Users should use the standard security
115 // provider mechanism instead if implementing their own
116 // transform or canonicalization algorithms.
117 // InputStream is = Class.forName("com.sun.org.apache.xml.internal.security.Init").getResourceAsStream("resource/config.xml");
118 InputStream is = (InputStream) AccessController.doPrivileged(
119 new PrivilegedAction() {
120 public Object run() {
121// String cfile = System.getProperty
122// ("com.sun.org.apache.xml.internal.security.resource.config");
123 return getClass().getResourceAsStream
124// (cfile != null ? cfile : "resource/config.xml");
125 ("resource/config.xml");
126 }
127 });
128
129 Document doc = db.parse(is);
130 long XX_parsing_end = System.currentTimeMillis();
131 long XX_configure_i18n_start = 0;
132
133 {
134 XX_configure_reg_keyInfo_start = System.currentTimeMillis();
135 try {
136 KeyInfo.init();
137 } catch (Exception e) {
138 e.printStackTrace();
139
140 throw e;
141 }
142 XX_configure_reg_keyInfo_end = System.currentTimeMillis();
143 }
144
145 long XX_configure_reg_transforms_start=0;
146 long XX_configure_reg_jcemapper_start=0;
147 long XX_configure_reg_sigalgos_start=0;
148 long XX_configure_reg_resourceresolver_end=0;
149 long XX_configure_reg_prefixes_end=0;
150 Node config=doc.getFirstChild();
151 for (;config!=null;config=config.getNextSibling()) {
152 if ("Configuration".equals(config.getLocalName())) {
153 break;
154 }
155 }
156 for (Node el=config.getFirstChild();el!=null;el=el.getNextSibling()) {
157 if (!(el instanceof Element)) {
158 continue;
159 }
160 String tag=el.getLocalName();
161//
162// Commented out: not supported in the JDK. We use the default locale.
163//
164// if (tag.equals("ResourceBundles")){
165// XX_configure_i18n_start = System.currentTimeMillis();
166// Element resource=(Element)el;
167// /* configure internationalization */
168// Attr langAttr = resource.getAttributeNode("defaultLanguageCode");
169// Attr countryAttr = resource.getAttributeNode("defaultCountryCode");
170// String languageCode = (langAttr == null)
171// ? null
172// : langAttr.getNodeValue();
173// String countryCode = (countryAttr == null)
174// ? null
175// : countryAttr.getNodeValue();
176//
177// I18n.init(languageCode, countryCode);
178// XX_configure_i18n_end = System.currentTimeMillis();
179// }
180
181 if (tag.equals("CanonicalizationMethods")){
182 XX_configure_reg_c14n_start = System.currentTimeMillis();
183 Canonicalizer.init();
184 Element[] list=XMLUtils.selectNodes(el.getFirstChild(),CONF_NS,"CanonicalizationMethod");
185
186 for (int i = 0; i < list.length; i++) {
187 String URI = list[i].getAttributeNS(null,
188 "URI");
189 String JAVACLASS =
190 list[i].getAttributeNS(null,
191 "JAVACLASS");
192 try {
193 Class.forName(JAVACLASS);
194/* Method methods[] = c.getMethods();
195
196 for (int j = 0; j < methods.length; j++) {
197 Method currMeth = methods[j];
198
199 if (currMeth.getDeclaringClass().getName()
200 .equals(JAVACLASS)) {
201 log.log(java.util.logging.Level.FINE, currMeth.getDe claringClass().toString());
202 }
203 }*/
204 if (log.isLoggable(java.util.logging.Level.FINE))
205 log.log(java.util.logging.Level.FINE, "Canonicalizer.register(" + URI + ", "
206 + JAVACLASS + ")");
207 Canonicalizer.register(URI, JAVACLASS);
208 } catch (ClassNotFoundException e) {
209 Object exArgs[] = { URI, JAVACLASS };
210
211 log.log(java.util.logging.Level.SEVERE, I18n.translate("algorithm.classDoesNotExist",
212 exArgs));
213 }
214 }
215 XX_configure_reg_c14n_end = System.currentTimeMillis();
216 }
217
218 if (tag.equals("TransformAlgorithms")){
219 XX_configure_reg_transforms_start = System.currentTimeMillis();
220 Transform.init();
221
222 Element[] tranElem = XMLUtils.selectNodes(el.getFirstChild(),CONF_NS,"TransformAlgorithm");
223
224 for (int i = 0; i < tranElem.length; i++) {
225 String URI = tranElem[i].getAttributeNS(null,
226 "URI");
227 String JAVACLASS =
228 tranElem[i].getAttributeNS(null,
229 "JAVACLASS");
230 try {
231 Class.forName(JAVACLASS);
232 if (log.isLoggable(java.util.logging.Level.FINE))
233 log.log(java.util.logging.Level.FINE, "Transform.register(" + URI + ", " + JAVACLASS + ")");
234 Transform.register(URI, JAVACLASS);
235 } catch (ClassNotFoundException e) {
236 Object exArgs[] = { URI, JAVACLASS };
237
238 log.log(java.util.logging.Level.SEVERE, I18n.translate("algorithm.classDoesNotExist",
239 exArgs));
240
241 } catch (NoClassDefFoundError ex) {
242 log.log(java.util.logging.Level.WARNING, "Not able to found dependecies for algorithm, I'm keep working.");
243 }
244 }
245 XX_configure_reg_transforms_end = System.currentTimeMillis();
246 }
247
248
249 if ("JCEAlgorithmMappings".equals(tag)){
250 XX_configure_reg_jcemapper_start = System.currentTimeMillis();
251 JCEMapper.init((Element)el);
252 XX_configure_reg_jcemapper_end = System.currentTimeMillis();
253 }
254
255
256
257 if (tag.equals("SignatureAlgorithms")){
258 XX_configure_reg_sigalgos_start = System.currentTimeMillis();
259 SignatureAlgorithm.providerInit();
260
261 Element[] sigElems = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS,
262 "SignatureAlgorithm");
263
264 for (int i = 0; i < sigElems.length; i++) {
265 String URI = sigElems[i].getAttributeNS(null,
266 "URI");
267 String JAVACLASS =
268 sigElems[i].getAttributeNS(null,
269 "JAVACLASS");
270
271 /** $todo$ handle registering */
272
273 try {
274 Class.forName(JAVACLASS);
275 // Method methods[] = c.getMethods();
276
277// for (int j = 0; j < methods.length; j++) {
278// Method currMeth = methods[j];
279//
280// if (currMeth.getDeclaringClass().getName()
281// .equals(JAVACLASS)) {
282// log.log(java.util.logging.Level.FINE, currMeth.getDe claringClass().toString());
283// }
284// }
285 if (log.isLoggable(java.util.logging.Level.FINE))
286 log.log(java.util.logging.Level.FINE, "SignatureAlgorithm.register(" + URI + ", " + JAVACLASS + ")");
287 SignatureAlgorithm.register(URI, JAVACLASS);
288 } catch (ClassNotFoundException e) {
289 Object exArgs[] = { URI, JAVACLASS };
290
291 log.log(java.util.logging.Level.SEVERE, I18n.translate("algorithm.classDoesNotExist",
292 exArgs));
293
294 }
295 }
296 XX_configure_reg_sigalgos_end = System.currentTimeMillis();
297 }
298
299
300
301 if (tag.equals("ResourceResolvers")){
302 XX_configure_reg_resourceresolver_start = System.currentTimeMillis();
303 ResourceResolver.init();
304
305 Element[]resolverElem = XMLUtils.selectNodes(el.getFirstChild(),CONF_NS,
306 "Resolver");
307
308 for (int i = 0; i < resolverElem.length; i++) {
309 String JAVACLASS =
310 resolverElem[i].getAttributeNS(null,
311 "JAVACLASS");
312 String Description =
313 resolverElem[i].getAttributeNS(null,
314 "DESCRIPTION");
315
316 if ((Description != null) && (Description.length() > 0)) {
317 if (log.isLoggable(java.util.logging.Level.FINE))
318 log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS + ": " + Description);
319 } else {
320 if (log.isLoggable(java.util.logging.Level.FINE))
321 log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS + ": For unknown purposes");
322 }
323 try {
324 ResourceResolver.register(JAVACLASS);
325 } catch (Throwable e) {
326 log.log(java.util.logging.Level.WARNING, "Cannot register:"+JAVACLASS+" perhaps some needed jars are not installed",e);
327 }
328 XX_configure_reg_resourceresolver_end =
329 System.currentTimeMillis();
330 }
331
332 }
333
334
335
336
337
338
339 if (tag.equals("KeyResolver")){
340 XX_configure_reg_keyResolver_start =System.currentTimeMillis();
341 KeyResolver.init();
342
343 Element[] resolverElem = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS,"Resolver");
344
345 for (int i = 0; i < resolverElem.length; i++) {
346 String JAVACLASS =
347 resolverElem[i].getAttributeNS(null,
348 "JAVACLASS");
349 String Description =
350 resolverElem[i].getAttributeNS(null,
351 "DESCRIPTION");
352
353 if ((Description != null) && (Description.length() > 0)) {
354 if (log.isLoggable(java.util.logging.Level.FINE))
355 log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS + ": " + Description);
356 } else {
357 if (log.isLoggable(java.util.logging.Level.FINE))
358 log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS + ": For unknown purposes");
359 }
360
361 KeyResolver.register(JAVACLASS);
362 }
363 XX_configure_reg_keyResolver_end = System.currentTimeMillis();
364 }
365
366
367 if (tag.equals("PrefixMappings")){
368 XX_configure_reg_prefixes_start = System.currentTimeMillis();
369 if (log.isLoggable(java.util.logging.Level.FINE))
370 log.log(java.util.logging.Level.FINE, "Now I try to bind prefixes:");
371
372 Element[] nl = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS,"PrefixMapping");
373
374 for (int i = 0; i < nl.length; i++) {
375 String namespace = nl[i].getAttributeNS(null,
376 "namespace");
377 String prefix = nl[i].getAttributeNS(null,
378 "prefix");
379 if (log.isLoggable(java.util.logging.Level.FINE))
380 log.log(java.util.logging.Level.FINE, "Now I try to bind " + prefix + " to " + namespace);
381 com.sun.org.apache.xml.internal.security.utils.ElementProxy
382 .setDefaultPrefix(namespace, prefix);
383 }
384 XX_configure_reg_prefixes_end = System.currentTimeMillis();
385 }
386 }
387
388 long XX_init_end = System.currentTimeMillis();
389
390 //J-
391 if (log.isLoggable(java.util.logging.Level.FINE)) {
392 log.log(java.util.logging.Level.FINE, "XX_init " + ((int)(XX_init_end - XX_init_start)) + " ms");
393 log.log(java.util.logging.Level.FINE, " XX_prng " + ((int)(XX_prng_end - XX_prng_start)) + " ms");
394 log.log(java.util.logging.Level.FINE, " XX_parsing " + ((int)(XX_parsing_end - XX_parsing_start)) + " ms");
395 log.log(java.util.logging.Level.FINE, " XX_configure_i18n " + ((int)(XX_configure_i18n_end- XX_configure_i18n_start)) + " ms");
396 log.log(java.util.logging.Level.FINE, " XX_configure_reg_c14n " + ((int)(XX_configure_reg_c14n_end- XX_configure_reg_c14n_start)) + " ms");
397 log.log(java.util.logging.Level.FINE, " XX_configure_reg_jcemapper " + ((int)(XX_configure_reg_jcemapper_end- XX_configure_reg_jcemapper_start)) + " ms");
398 log.log(java.util.logging.Level.FINE, " XX_configure_reg_keyInfo " + ((int)(XX_configure_reg_keyInfo_end- XX_configure_reg_keyInfo_start)) + " ms");
399 log.log(java.util.logging.Level.FINE, " XX_configure_reg_keyResolver " + ((int)(XX_configure_reg_keyResolver_end- XX_configure_reg_keyResolver_start)) + " ms");
400 log.log(java.util.logging.Level.FINE, " XX_configure_reg_prefixes " + ((int)(XX_configure_reg_prefixes_end- XX_configure_reg_prefixes_start)) + " ms");
401 log.log(java.util.logging.Level.FINE, " XX_configure_reg_resourceresolver " + ((int)(XX_configure_reg_resourceresolver_end- XX_configure_reg_resourceresolver_start)) + " ms");
402 log.log(java.util.logging.Level.FINE, " XX_configure_reg_sigalgos " + ((int)(XX_configure_reg_sigalgos_end- XX_configure_reg_sigalgos_start)) + " ms");
403 log.log(java.util.logging.Level.FINE, " XX_configure_reg_transforms " + ((int)(XX_configure_reg_transforms_end- XX_configure_reg_transforms_start)) + " ms");
404 }
405 } catch (Exception e) {
406 log.log(java.util.logging.Level.SEVERE, "Bad: ", e);
407 e.printStackTrace();
408 }
409
410 }
411
412
413}