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