KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > Init


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security;
18
19
20
21 import java.io.InputStream JavaDoc;
22 import java.security.AccessController JavaDoc;
23 import java.security.PrivilegedAction JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27
28 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
29 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
30 import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
31 import com.sun.org.apache.xml.internal.security.keys.KeyInfo;
32 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolver;
33 import com.sun.org.apache.xml.internal.security.transforms.Transform;
34 import com.sun.org.apache.xml.internal.security.utils.I18n;
35 //import com.sun.org.apache.xml.internal.security.utils.PRNG;
36
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
37 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver;
38 import org.w3c.dom.Attr JavaDoc;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42
43
44 /**
45  * This class does the configuration of the library. This includes creating
46  * the mapping of Canonicalization and Transform algorithms. Initialization is
47  * done by calling {@link Init#init} which should be done in any static block
48  * of the files of this library. We ensure that this call is only executed once.
49  *
50  * @author $Author: raul $
51  */

52 public final class Init {
53
54   /** {@link java.util.logging} logging facility */
55   static java.util.logging.Logger JavaDoc log =
56         java.util.logging.Logger.getLogger(Init.class.getName());
57
58    /** Field _initialized */
59    private static boolean _alreadyInitialized = false;
60    
61    /** The namespace for CONF file **/
62    public static final String JavaDoc CONF_NS="http://www.xmlsecurity.org/NS/#configuration";
63
64    /**
65     * Method isInitialized
66     * @return true if the librairy is already initialized.
67     *
68     */

69    public static final boolean isInitialized() {
70       return Init._alreadyInitialized;
71    }
72
73    /**
74     * Method init
75     *
76     */

77    public synchronized static void init() {
78
79       if (_alreadyInitialized) {
80         return;
81       }
82       long XX_configure_i18n_end=0;
83       long XX_configure_reg_c14n_start=0;
84       long XX_configure_reg_c14n_end=0;
85       long XX_configure_reg_jcemapper_end=0;
86       long XX_configure_reg_keyInfo_start=0;
87       long XX_configure_reg_keyResolver_end=0;
88       long XX_configure_reg_prefixes_start=0;
89       long XX_configure_reg_resourceresolver_start=0;
90       long XX_configure_reg_sigalgos_end=0;
91       long XX_configure_reg_transforms_end=0;
92       long XX_configure_reg_keyInfo_end=0;
93       long XX_configure_reg_keyResolver_start=0;
94          _alreadyInitialized = true;
95
96          try {
97             long XX_init_start = System.currentTimeMillis();
98             long XX_prng_start = System.currentTimeMillis();
99
100             //PRNG.init(new java.security.SecureRandom());
101

102             long XX_prng_end = System.currentTimeMillis();
103
104             /* read library configuration file */
105             long XX_parsing_start = System.currentTimeMillis();
106             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
107
108             dbf.setNamespaceAware(true);
109             dbf.setValidating(false);
110
111             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
112         
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
// String cfile = System.getProperty("com.sun.org.apache.xml.internal.security.resource.config");
118
// InputStream is =
119
// Class.forName("com.sun.org.apache.xml.internal.security.Init")
120
// .getResourceAsStream(cfile != null ? cfile : "resource/config.xml");
121
InputStream JavaDoc is = (InputStream JavaDoc) AccessController.doPrivileged(
122         new PrivilegedAction JavaDoc() {
123             public Object JavaDoc run() {
124             return getClass().getResourceAsStream
125                     ("resource/config.xml");
126             }
127         });
128
129             Document JavaDoc 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 JavaDoc 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 JavaDoc config=doc.getFirstChild();
151             for (;config!=null;config=config.getNextSibling()) {
152                 if ("Configuration".equals(config.getLocalName())) {
153                     break;
154                 }
155             }
156             for (Node JavaDoc el=config.getFirstChild();el!=null;el=el.getNextSibling()) {
157                 if (!(el instanceof Element JavaDoc)) {
158                     continue;
159                 }
160                 String JavaDoc 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 JavaDoc[] list=XMLUtils.selectNodes(el.getFirstChild(),CONF_NS,"CanonicalizationMethod");
185
186                for (int i = 0; i < list.length; i++) {
187                   String JavaDoc URI = list[i].getAttributeNS(null,
188                                   "URI");
189                   String JavaDoc 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.getDeclaringClass().toString());
202                         }
203                      }*/

204                       if (true)
205                         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Canonicalizer.register(" + URI + ", "
206                             + JAVACLASS + ")");
207                      Canonicalizer.register(URI, JAVACLASS);
208                   } catch (ClassNotFoundException JavaDoc e) {
209                      Object JavaDoc 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 JavaDoc[] tranElem = XMLUtils.selectNodes(el.getFirstChild(),CONF_NS,"TransformAlgorithm");
223
224                for (int i = 0; i < tranElem.length; i++) {
225                   String JavaDoc URI = tranElem[i].getAttributeNS(null,
226                                   "URI");
227                   String JavaDoc JAVACLASS =
228                      tranElem[i].getAttributeNS(null,
229                         "JAVACLASS");
230                   try {
231                      Class.forName(JAVACLASS);
232                      if (true)
233                         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Transform.register(" + URI + ", " + JAVACLASS
234                             + ")");
235                      Transform.register(URI, JAVACLASS);
236                   } catch (ClassNotFoundException JavaDoc e) {
237                      Object JavaDoc exArgs[] = { URI, JAVACLASS };
238
239                      log.log(java.util.logging.Level.SEVERE, I18n.translate("algorithm.classDoesNotExist",
240                                               exArgs));
241
242                   } catch (NoClassDefFoundError JavaDoc ex) {
243                       log.log(java.util.logging.Level.WARNING, "Not able to found dependecies for algorithm, I'm keep working.");
244                   }
245                }
246                XX_configure_reg_transforms_end = System.currentTimeMillis();
247             }
248                         
249
250             if ("JCEAlgorithmMappings".equals(tag)){
251                XX_configure_reg_jcemapper_start = System.currentTimeMillis();
252                JCEMapper.init((Element JavaDoc)el);
253                XX_configure_reg_jcemapper_end = System.currentTimeMillis();
254             }
255
256                         
257
258             if (tag.equals("SignatureAlgorithms")){
259                XX_configure_reg_sigalgos_start = System.currentTimeMillis();
260                SignatureAlgorithm.providerInit();
261
262                Element JavaDoc[] sigElems = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS,
263                   "SignatureAlgorithm");
264
265                for (int i = 0; i < sigElems.length; i++) {
266                   String JavaDoc URI = sigElems[i].getAttributeNS(null,
267                                   "URI");
268                   String JavaDoc JAVACLASS =
269                     sigElems[i].getAttributeNS(null,
270                         "JAVACLASS");
271
272                   /** $todo$ handle registering */
273
274                   try {
275                       Class.forName(JAVACLASS);
276  // Method methods[] = c.getMethods();
277

278 // for (int j = 0; j < methods.length; j++) {
279
// Method currMeth = methods[j];
280
//
281
// if (currMeth.getDeclaringClass().getName()
282
// .equals(JAVACLASS)) {
283
// log.log(java.util.logging.Level.FINE, currMeth.getDeclaringClass().toString());
284
// }
285
// }
286
if (true)
287                         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "SignatureAlgorithm.register(" + URI + ", "
288                             + JAVACLASS + ")");
289                      SignatureAlgorithm.register(URI, JAVACLASS);
290                   } catch (ClassNotFoundException JavaDoc e) {
291                      Object JavaDoc exArgs[] = { URI, JAVACLASS };
292
293                      log.log(java.util.logging.Level.SEVERE, I18n.translate("algorithm.classDoesNotExist",
294                                               exArgs));
295
296                   }
297                }
298                XX_configure_reg_sigalgos_end = System.currentTimeMillis();
299             }
300
301             
302             
303             if (tag.equals("ResourceResolvers")){
304                XX_configure_reg_resourceresolver_start = System.currentTimeMillis();
305                ResourceResolver.init();
306
307                Element JavaDoc[]resolverElem = XMLUtils.selectNodes(el.getFirstChild(),CONF_NS,
308                   "Resolver");
309
310                for (int i = 0; i < resolverElem.length; i++) {
311                   String JavaDoc JAVACLASS =
312                       resolverElem[i].getAttributeNS(null,
313                         "JAVACLASS");
314                   String JavaDoc Description =
315                      resolverElem[i].getAttributeNS(null,
316                         "DESCRIPTION");
317
318                   if ((Description != null) && (Description.length() > 0)) {
319                     if (true)
320                         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS + ": "
321                                + Description);
322                   } else {
323                     if (true)
324                         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS
325                                + ": For unknown purposes");
326                   }
327                   try {
328                       ResourceResolver.register(JAVACLASS);
329                   } catch (Throwable JavaDoc e) {
330                       log.log(java.util.logging.Level.WARNING, "Cannot register:"+JAVACLASS+" perhaps some needed jars are not installed",e);
331                   }
332                   XX_configure_reg_resourceresolver_end =
333                     System.currentTimeMillis();
334                }
335
336             }
337
338             
339
340             
341
342                         
343             if (tag.equals("KeyResolver")){
344                XX_configure_reg_keyResolver_start =System.currentTimeMillis();
345                KeyResolver.init();
346
347                Element JavaDoc[] resolverElem = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS,"Resolver");
348
349                for (int i = 0; i < resolverElem.length; i++) {
350                   String JavaDoc JAVACLASS =
351                      resolverElem[i].getAttributeNS(null,
352                         "JAVACLASS");
353                   String JavaDoc Description =
354                      resolverElem[i].getAttributeNS(null,
355                         "DESCRIPTION");
356
357                   if ((Description != null) && (Description.length() > 0)) {
358                     if (true)
359                         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS + ": "
360                                + Description);
361                   } else {
362                     if (true)
363                         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Register Resolver: " + JAVACLASS
364                                + ": For unknown purposes");
365                   }
366
367                   KeyResolver.register(JAVACLASS);
368                }
369                XX_configure_reg_keyResolver_end = System.currentTimeMillis();
370             }
371
372                         
373             if (tag.equals("PrefixMappings")){
374                 XX_configure_reg_prefixes_start = System.currentTimeMillis();
375                 if (true)
376                     if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Now I try to bind prefixes:");
377
378                Element JavaDoc[] nl = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS,"PrefixMapping");
379
380                for (int i = 0; i < nl.length; i++) {
381                   String JavaDoc namespace = nl[i].getAttributeNS(null,
382                                         "namespace");
383                   String JavaDoc prefix = nl[i].getAttributeNS(null,
384                                      "prefix");
385                   if (true)
386                     if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Now I try to bind " + prefix + " to " + namespace);
387                   com.sun.org.apache.xml.internal.security.utils.ElementProxy
388                      .setDefaultPrefix(namespace, prefix);
389                }
390                XX_configure_reg_prefixes_end = System.currentTimeMillis();
391             }
392             }
393             
394             long XX_init_end = System.currentTimeMillis();
395
396             //J-
397
if (true) {
398                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "XX_init " + ((int)(XX_init_end - XX_init_start)) + " ms");
399                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_prng " + ((int)(XX_prng_end - XX_prng_start)) + " ms");
400                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_parsing " + ((int)(XX_parsing_end - XX_parsing_start)) + " ms");
401                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_i18n " + ((int)(XX_configure_i18n_end- XX_configure_i18n_start)) + " ms");
402                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_c14n " + ((int)(XX_configure_reg_c14n_end- XX_configure_reg_c14n_start)) + " ms");
403                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_jcemapper " + ((int)(XX_configure_reg_jcemapper_end- XX_configure_reg_jcemapper_start)) + " ms");
404                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_keyInfo " + ((int)(XX_configure_reg_keyInfo_end- XX_configure_reg_keyInfo_start)) + " ms");
405                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_keyResolver " + ((int)(XX_configure_reg_keyResolver_end- XX_configure_reg_keyResolver_start)) + " ms");
406                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_prefixes " + ((int)(XX_configure_reg_prefixes_end- XX_configure_reg_prefixes_start)) + " ms");
407                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_resourceresolver " + ((int)(XX_configure_reg_resourceresolver_end- XX_configure_reg_resourceresolver_start)) + " ms");
408                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_sigalgos " + ((int)(XX_configure_reg_sigalgos_end- XX_configure_reg_sigalgos_start)) + " ms");
409                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " XX_configure_reg_transforms " + ((int)(XX_configure_reg_transforms_end- XX_configure_reg_transforms_start)) + " ms");
410             }
411          } catch (Exception JavaDoc e) {
412             log.log(java.util.logging.Level.SEVERE, "Bad: ", e);
413             e.printStackTrace();
414          }
415       
416    }
417
418   
419 }
420
421
Popular Tags