Coverage Report - org.mule.security.MuleSecurityManager
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleSecurityManager
17%
9/53
0%
0/20
2.143
 
 1  
 /*
 2  
  * $Id: MuleSecurityManager.java 11517 2008-03-31 21:34:19Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.security;
 12  
 
 13  
 import org.mule.api.EncryptionStrategy;
 14  
 import org.mule.api.lifecycle.InitialisationException;
 15  
 import org.mule.api.lifecycle.LifecycleTransitionResult;
 16  
 import org.mule.api.security.Authentication;
 17  
 import org.mule.api.security.SecurityContext;
 18  
 import org.mule.api.security.SecurityException;
 19  
 import org.mule.api.security.SecurityManager;
 20  
 import org.mule.api.security.SecurityProvider;
 21  
 import org.mule.api.security.SecurityProviderNotFoundException;
 22  
 import org.mule.api.security.UnknownAuthenticationTypeException;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collection;
 26  
 import java.util.Collections;
 27  
 import java.util.Iterator;
 28  
 import java.util.LinkedList;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 33  
 
 34  
 import org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 
 37  
 /**
 38  
  * <code>MuleSecurityManager</code> is a default implementation security manager
 39  
  * for a Mule instance.
 40  
  */
 41  
 
 42  
 public class MuleSecurityManager implements SecurityManager
 43  
 {
 44  
 
 45  
     /**
 46  
      * logger used by this class
 47  
      */
 48  2
     protected static final Log logger = LogFactory.getLog(MuleSecurityManager.class);
 49  
 
 50  1146
     private Map providers = new ConcurrentHashMap();
 51  1146
     private Map cryptoStrategies = new ConcurrentHashMap();
 52  
 
 53  
     public MuleSecurityManager()
 54  
     {
 55  1146
         super();
 56  1146
     }
 57  
 
 58  
     public void initialise() throws InitialisationException
 59  
     {
 60  1146
         List all = new LinkedList(providers.values());
 61  
         // ordering: appends
 62  1146
         all.addAll(cryptoStrategies.values());
 63  1146
         LifecycleTransitionResult.initialiseAll(all.iterator());
 64  1146
     }
 65  
 
 66  
     public Authentication authenticate(Authentication authentication)
 67  
         throws SecurityException, SecurityProviderNotFoundException
 68  
     {
 69  0
         Iterator iter = providers.values().iterator();
 70  
 
 71  0
         Class toTest = authentication.getClass();
 72  
 
 73  0
         while (iter.hasNext())
 74  
         {
 75  0
             SecurityProvider provider = (SecurityProvider) iter.next();
 76  
 
 77  0
             if (provider.supports(toTest))
 78  
             {
 79  0
                 if (logger.isDebugEnabled())
 80  
                 {
 81  0
                     logger.debug("Authentication attempt using " + provider.getClass().getName());
 82  
                 }
 83  
 
 84  0
                 Authentication result = provider.authenticate(authentication);
 85  
 
 86  0
                 if (result != null)
 87  
                 {
 88  0
                     return result;
 89  
                 }
 90  
             }
 91  0
         }
 92  
 
 93  0
         throw new SecurityProviderNotFoundException(toTest.getName());
 94  
     }
 95  
 
 96  
     public void addProvider(SecurityProvider provider)
 97  
     {
 98  0
         if (getProvider(provider.getName()) != null)
 99  
         {
 100  0
             throw new IllegalArgumentException("Provider already registered: " + provider.getName());
 101  
         }
 102  0
         providers.put(provider.getName(), provider);
 103  0
     }
 104  
 
 105  
     public SecurityProvider getProvider(String name)
 106  
     {
 107  0
         if (name == null)
 108  
         {
 109  0
             throw new IllegalArgumentException("provider Name cannot be null");
 110  
         }
 111  0
         return (SecurityProvider) providers.get(name);
 112  
     }
 113  
 
 114  
     public SecurityProvider removeProvider(String name)
 115  
     {
 116  0
         return (SecurityProvider) providers.remove(name);
 117  
     }
 118  
 
 119  
     public Collection getProviders()
 120  
     {
 121  0
         return Collections.unmodifiableCollection(new ArrayList(providers.values()));
 122  
     }
 123  
 
 124  
     public void setProviders(Collection providers)
 125  
     {
 126  0
         for (Iterator iterator = providers.iterator(); iterator.hasNext();)
 127  
         {
 128  0
             SecurityProvider provider = (SecurityProvider) iterator.next();
 129  0
             addProvider(provider);
 130  0
         }
 131  0
     }
 132  
 
 133  
     public SecurityContext createSecurityContext(Authentication authentication)
 134  
         throws UnknownAuthenticationTypeException
 135  
     {
 136  0
         Iterator iter = providers.values().iterator();
 137  
 
 138  0
         Class toTest = authentication.getClass();
 139  
 
 140  0
         while (iter.hasNext())
 141  
         {
 142  0
             SecurityProvider provider = (SecurityProvider) iter.next();
 143  
 
 144  0
             if (provider.supports(toTest))
 145  
             {
 146  0
                 return provider.createSecurityContext(authentication);
 147  
             }
 148  0
         }
 149  0
         throw new UnknownAuthenticationTypeException(authentication);
 150  
     }
 151  
 
 152  
     public EncryptionStrategy getEncryptionStrategy(String name)
 153  
     {
 154  0
         return (EncryptionStrategy) cryptoStrategies.get(name);
 155  
     }
 156  
 
 157  
     public void addEncryptionStrategy(EncryptionStrategy strategy)
 158  
     {
 159  0
         cryptoStrategies.put(strategy.getName(), strategy);
 160  0
     }
 161  
 
 162  
     public EncryptionStrategy removeEncryptionStrategy(String name)
 163  
     {
 164  0
         return (EncryptionStrategy) cryptoStrategies.remove(name);
 165  
 
 166  
     }
 167  
 
 168  
     public Collection getEncryptionStrategies()
 169  
     {
 170  0
         return Collections.unmodifiableCollection(new ArrayList(cryptoStrategies.values()));
 171  
     }
 172  
 
 173  
     public void setEncryptionStrategies(Collection strategies)
 174  
     {
 175  0
         for (Iterator iterator = strategies.iterator(); iterator.hasNext();)
 176  
         {
 177  0
             EncryptionStrategy strategy = (EncryptionStrategy) iterator.next();
 178  0
             addEncryptionStrategy(strategy);
 179  0
         }
 180  0
     }
 181  
 
 182  
 }