1 10 11 package org.mule.impl.security; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.mule.umo.UMOEncryptionStrategy; 18 import org.mule.umo.lifecycle.InitialisationException; 19 import org.mule.umo.security.SecurityException; 20 import org.mule.umo.security.SecurityProviderNotFoundException; 21 import org.mule.umo.security.UMOAuthentication; 22 import org.mule.umo.security.UMOSecurityContext; 23 import org.mule.umo.security.UMOSecurityManager; 24 import org.mule.umo.security.UMOSecurityProvider; 25 import org.mule.umo.security.UnknownAuthenticationTypeException; 26 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 37 38 public class MuleSecurityManager implements UMOSecurityManager 39 { 40 43 protected static final Log logger = LogFactory.getLog(MuleSecurityManager.class); 44 45 private Map providers = new ConcurrentHashMap(); 46 private Map cryptoStrategies = new ConcurrentHashMap(); 47 48 public void initialise() throws InitialisationException 49 { 50 for (Iterator iterator = providers.values().iterator(); iterator.hasNext();) 51 { 52 UMOSecurityProvider provider = (UMOSecurityProvider)iterator.next(); 53 provider.initialise(); 54 } 55 56 for (Iterator iterator = cryptoStrategies.values().iterator(); iterator.hasNext();) 57 { 58 UMOEncryptionStrategy strategy = (UMOEncryptionStrategy)iterator.next(); 59 strategy.initialise(); 60 } 61 } 62 63 public UMOAuthentication authenticate(UMOAuthentication authentication) 64 throws SecurityException , SecurityProviderNotFoundException 65 { 66 Iterator iter = providers.values().iterator(); 67 68 Class toTest = authentication.getClass(); 69 70 while (iter.hasNext()) 71 { 72 UMOSecurityProvider provider = (UMOSecurityProvider)iter.next(); 73 74 if (provider.supports(toTest)) 75 { 76 if (logger.isDebugEnabled()) 77 { 78 logger.debug("Authentication attempt using " + provider.getClass().getName()); 79 } 80 81 UMOAuthentication result = provider.authenticate(authentication); 82 83 if (result != null) 84 { 85 return result; 86 } 87 } 88 } 89 90 throw new SecurityProviderNotFoundException(toTest.getName()); 91 } 92 93 public void addProvider(UMOSecurityProvider provider) 94 { 95 if (getProvider(provider.getName()) != null) 96 { 97 throw new IllegalArgumentException ("Provider already registered: " + provider.getName()); 98 } 99 providers.put(provider.getName(), provider); 100 } 101 102 public UMOSecurityProvider getProvider(String name) 103 { 104 if (name == null) 105 { 106 throw new NullPointerException ("provider Name cannot be null"); 107 } 108 return (UMOSecurityProvider)providers.get(name); 109 } 110 111 public UMOSecurityProvider removeProvider(String name) 112 { 113 return (UMOSecurityProvider)providers.remove(name); 114 } 115 116 public List getProviders() 117 { 118 return Collections.unmodifiableList(new ArrayList (providers.values())); 119 } 120 121 public void setProviders(List providers) 122 { 123 for (Iterator iterator = providers.iterator(); iterator.hasNext();) 124 { 125 UMOSecurityProvider provider = (UMOSecurityProvider)iterator.next(); 126 addProvider(provider); 127 } 128 } 129 130 public UMOSecurityContext createSecurityContext(UMOAuthentication authentication) 131 throws UnknownAuthenticationTypeException 132 { 133 Iterator iter = providers.values().iterator(); 134 135 Class toTest = authentication.getClass(); 136 137 while (iter.hasNext()) 138 { 139 UMOSecurityProvider provider = (UMOSecurityProvider)iter.next(); 140 141 if (provider.supports(toTest)) 142 { 143 return provider.createSecurityContext(authentication); 144 } 145 } 146 throw new UnknownAuthenticationTypeException(authentication); 147 } 148 149 public UMOEncryptionStrategy getEncryptionStrategy(String name) 150 { 151 return (UMOEncryptionStrategy)cryptoStrategies.get(name); 152 } 153 154 public void addEncryptionStrategy(String name, UMOEncryptionStrategy strategy) 155 { 156 cryptoStrategies.put(name, strategy); 157 } 158 159 public UMOEncryptionStrategy removeEncryptionStrategy(String name) 160 { 161 return (UMOEncryptionStrategy)cryptoStrategies.remove(name); 162 163 } 164 165 public void setEncryptionStrategies(Map strategies) 166 { 167 cryptoStrategies.putAll(strategies); 168 } 169 } 170 | Popular Tags |