KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > security > MuleSecurityManager


1 /*
2  * $Id: MuleSecurityManager.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * <code>MuleSecurityManager</code> is a default implementation security manager
35  * for a Mule instance.
36  */

37
38 public class MuleSecurityManager implements UMOSecurityManager
39 {
40     /**
41      * logger used by this class
42      */

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 JavaDoc iterator = providers.values().iterator(); iterator.hasNext();)
51         {
52             UMOSecurityProvider provider = (UMOSecurityProvider)iterator.next();
53             provider.initialise();
54         }
55
56         for (Iterator JavaDoc 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 JavaDoc, SecurityProviderNotFoundException
65     {
66         Iterator JavaDoc iter = providers.values().iterator();
67
68         Class JavaDoc 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 JavaDoc("Provider already registered: " + provider.getName());
98         }
99         providers.put(provider.getName(), provider);
100     }
101
102     public UMOSecurityProvider getProvider(String JavaDoc name)
103     {
104         if (name == null)
105         {
106             throw new NullPointerException JavaDoc("provider Name cannot be null");
107         }
108         return (UMOSecurityProvider)providers.get(name);
109     }
110
111     public UMOSecurityProvider removeProvider(String JavaDoc name)
112     {
113         return (UMOSecurityProvider)providers.remove(name);
114     }
115
116     public List JavaDoc getProviders()
117     {
118         return Collections.unmodifiableList(new ArrayList JavaDoc(providers.values()));
119     }
120
121     public void setProviders(List JavaDoc providers)
122     {
123         for (Iterator JavaDoc 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 JavaDoc iter = providers.values().iterator();
134
135         Class JavaDoc 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 JavaDoc name)
150     {
151         return (UMOEncryptionStrategy)cryptoStrategies.get(name);
152     }
153
154     public void addEncryptionStrategy(String JavaDoc name, UMOEncryptionStrategy strategy)
155     {
156         cryptoStrategies.put(name, strategy);
157     }
158
159     public UMOEncryptionStrategy removeEncryptionStrategy(String JavaDoc 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