View Javadoc

1   /*
2    * $Id: MuleSecurityManager.java 7963 2007-08-21 08:53:15Z 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.impl.security;
12  
13  import org.mule.umo.UMOEncryptionStrategy;
14  import org.mule.umo.lifecycle.InitialisationException;
15  import org.mule.umo.security.SecurityException;
16  import org.mule.umo.security.SecurityProviderNotFoundException;
17  import org.mule.umo.security.UMOAuthentication;
18  import org.mule.umo.security.UMOSecurityContext;
19  import org.mule.umo.security.UMOSecurityManager;
20  import org.mule.umo.security.UMOSecurityProvider;
21  import org.mule.umo.security.UnknownAuthenticationTypeException;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
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 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 IllegalArgumentException("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 }