Coverage Report - org.openpermis.security.SecurityProviderInitializer
 
Classes in this File Line Coverage Branch Coverage Complexity
SecurityProviderInitializer
57%
15/26
75%
3/4
1.8
SecurityProviderInitializer$Initializer
0%
0/3
N/A
1.8
 
 1  
 /*
 2  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 3  
  * All rights reserved.
 4  
  * 
 5  
  * Licensed under the Open Permis License which accompanies this distribution, 
 6  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 7  
  */
 8  
 package org.openpermis.security;
 9  
 
 10  
 import java.io.IOException;
 11  
 import java.io.InputStreamReader;
 12  
 import java.io.Reader;
 13  
 import java.net.URL;
 14  
 import java.security.Provider;
 15  
 import java.security.Security;
 16  
 
 17  
 import org.picocontainer.DefaultPicoContainer;
 18  
 import org.picocontainer.PicoContainer;
 19  
 import org.picocontainer.script.ContainerBuilder;
 20  
 import org.picocontainer.script.xml.XMLContainerBuilder;
 21  
 import org.slf4j.Logger;
 22  
 import org.slf4j.LoggerFactory;
 23  
 
 24  
 
 25  
 /**
 26  
  * Static hook to initialize the security provider from a Pico configuration.
 27  
  * @since 0.3.0
 28  
  */
 29  
 public final class SecurityProviderInitializer {
 30  
 
 31  
         //---- Static
 32  
 
 33  
         /**
 34  
          * The logger object of this class.
 35  
          * @since 0.3.0
 36  
          */
 37  1
         private static final Logger LOGGER = 
 38  
                 LoggerFactory.getLogger(SecurityProviderInitializer.class);
 39  
 
 40  
         /**
 41  
          * JCE pico container configuration file name.
 42  
          * @since 0.3.0
 43  
          */
 44  
         private static final String JCE_CONFIGURATION = "openpermis-jce.xml";
 45  
 
 46  
         /**
 47  
          * Reads the <tt>openpermis-jce.xml</tt> in the top level folder of the classpath and 
 48  
          * initializes the security provider according to its contents.
 49  
          * @since 0.3.0
 50  
          */
 51  
         public static void initializeSecurityProvider () {
 52  0
                 initializeSecurityProvider(new Initializer());
 53  0
         }
 54  
         
 55  
         /**
 56  
          * Reads the <tt>openpermis-jce.xml</tt> in the top level folder of the classpath and 
 57  
          * initializes the security provider according to its contents.
 58  
          * @param initializer the actual initializer to use.
 59  
          * @since 0.3.0
 60  
          */
 61  
         protected static void initializeSecurityProvider (Initializer initializer) {
 62  1
                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
 63  1
                 if (cl == null) {
 64  0
                         cl = SecurityProviderInitializer.class.getClassLoader();
 65  
                 }
 66  1
                 initializeSecurityProvider(cl.getResource(JCE_CONFIGURATION), cl, initializer);
 67  1
         }
 68  
         
 69  
         /**
 70  
          * Initializes the security provider using the configuration and class loader specified.
 71  
          * @param url the configuration URL that points to a Pico XML configuration.
 72  
          * @param cl the class loader to load the configuration classes from.
 73  
          * @param initializer the actual initializer to use.
 74  
          * @since 0.3.0
 75  
          */
 76  
         protected static void initializeSecurityProvider (
 77  
                 URL url, ClassLoader cl, Initializer initializer
 78  
         ) {
 79  
                 try {
 80  3
                         final Reader rd = new InputStreamReader(url.openStream());
 81  
                         try {
 82  3
                                 final ContainerBuilder builder = new XMLContainerBuilder(rd, cl);
 83  3
                                 final PicoContainer container = builder.buildContainer(
 84  
                                         new DefaultPicoContainer(), null, true
 85  
                                 );
 86  3
                                 for (Provider provider : container.getComponents(java.security.Provider.class)) {
 87  4
                                         LOGGER.debug("Adding security provider [{}].", provider.getClass().getName());
 88  4
                                         initializer.addProvider(provider);
 89  
                                 }
 90  
                         } finally {
 91  0
                                 try {
 92  3
                                         rd.close();
 93  0
                                 } catch (IOException e) {
 94  0
                                         LOGGER.debug("Failed to close the JCE configuration file.", e);
 95  3
                                 }
 96  0
                         }
 97  0
                 } catch (IOException e) {
 98  0
                         LOGGER.warn("Cannot read JCE configuration [" + url.toString() + "].", e);
 99  3
                 }
 100  3
         }
 101  
         
 102  
         //---- Constructors
 103  
         
 104  
         /**
 105  
          * Objects of this class cannot be instantiated.
 106  
          * @since 0.3.0
 107  
          */
 108  
         private SecurityProviderInitializer () {
 109  0
                 super();
 110  0
         }
 111  
         
 112  
         //---- Initializer
 113  
         
 114  
         /**
 115  
          * Initializer class that sets the actual security provider.
 116  
          * @since 0.3.0
 117  
          */
 118  0
         protected static class Initializer {
 119  
                 
 120  
                 /**
 121  
                  * Adds a security provider using {@link Security#addProvider(Provider)}.
 122  
                  * @param provider the provider to add.
 123  
                  * @since 0.3.0
 124  
                  */
 125  
                 public void addProvider (Provider provider) {
 126  0
                         Security.addProvider(provider);
 127  0
                 }
 128  
                 
 129  
         }
 130  
         
 131  
 }