KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > jce > JCEProviderService


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  *
7  * Created on Mar 19, 2004
8  */

9 package org.jboss.security.jce;
10
11 import java.security.Provider JavaDoc;
12 import java.security.Security JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.jboss.system.ServiceMBeanSupport;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21
22 /**
23  * The JCEProviderService is capable of loading one or more third party
24  * JCE providers at runtime. The configuration looks something like:<br>
25  * <pre>
26  * &lt;mbean code="org.jboss.security.jce.JCEProviderService"
27  * name="Security:service=JCEProviderService"&gt;
28  * &lt;attribute name="JceProviders"&gt;
29  * &lt;providers&gt;
30  * &lt;provider class="org.bouncycastle.jce.provider.BouncyCastleProvider" position="3"/&gt;
31  * &lt;provider class="cryptix.provider.Cryptix"/&gt;
32  * &lt;/providers&gt;
33  * &lt;/attribute&gt;
34  * &lt;/mbean&gt;
35  * </pre>
36  * The required class attribute is the class name of the Provider to load. The optional position attribute
37  * is the position in the provider list that this provider would like to be loaded.<p>
38  * Any provider that already exists in the provider list will not be loaded again, and it will not be removed from
39  * the list when the JCEProviderService is destroyed.
40  *
41  * @author <a HREF="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
42  * @version $Revision: 1.1 $
43  */

44 public class JCEProviderService extends ServiceMBeanSupport implements JCEProviderServiceMBean
45 {
46    private ArrayList JavaDoc addedProviders = new ArrayList JavaDoc();
47    private Element JavaDoc providers;
48
49    public void setJceProviders(Element JavaDoc element)
50    {
51       providers = element;
52       if (getState() != UNREGISTERED)
53       {
54          synchronized (JCEProviderService.class)
55          {
56             removeProviders();
57             loadProviders();
58          }
59       }
60    }
61
62    public Element JavaDoc getJceProviders()
63    {
64       return providers;
65    }
66
67    protected void createService() throws Exception JavaDoc
68    {
69       // Install the requested JCE providers
70
synchronized (JCEProviderService.class)
71       {
72          loadProviders();
73       }
74    }
75
76    protected void destroyService()
77    {
78       // Uninstall any JCE Providers that we actually loaded
79
synchronized (JCEProviderService.class)
80       {
81          removeProviders();
82       }
83    }
84
85    private void loadProviders()
86    {
87       int n = 0;
88       if (providers != null)
89       {
90          addedProviders = new ArrayList JavaDoc();
91          NodeList JavaDoc reqdProviders = providers.getElementsByTagName("provider");
92          n = reqdProviders.getLength();
93          //int providersLoaded = 0;
94

95          for (int i = 0; i < n; i++)
96          {
97             Provider JavaDoc provider;
98             Node JavaDoc reqdProvider = reqdProviders.item(i);
99
100             String JavaDoc providerName;
101             String JavaDoc providerClass;
102             int requestedPosition = 0;
103
104             if (Node.ELEMENT_NODE == reqdProvider.getNodeType())
105             {
106                Element JavaDoc prov = (Element JavaDoc) reqdProvider;
107                if (prov.hasAttribute("class"))
108                {
109                   providerClass = prov.getAttribute("class");
110                }
111                else
112                {
113                   log.warn("A provider element must, at the very least, have a class attribute: " + prov);
114                   continue;
115                }
116
117                try
118                {
119                   provider = (Provider JavaDoc) Class.forName(providerClass).newInstance();
120                }
121                catch (InstantiationException JavaDoc e1)
122                {
123                   log.warn("Unable to instantiate an instance of the JCE Provider class " + providerClass, e1);
124                   continue;
125                }
126                catch (IllegalAccessException JavaDoc e1)
127                {
128                   log.warn("No permission to access the JCE Provider class " + providerClass, e1);
129                   continue;
130                }
131                catch (ClassNotFoundException JavaDoc e1)
132                {
133                   log.warn("Could not find the JCE Provider class " + providerClass, e1);
134                   continue;
135                }
136                catch (ClassCastException JavaDoc e1)
137                {
138                   log.warn("The Class " + providerClass + " is not a java.security.Provider");
139                   continue;
140                }
141
142                providerName = provider.getName();
143
144                if (prov.hasAttribute("position"))
145                {
146                   try
147                   {
148                      requestedPosition = Integer.parseInt(prov.getAttribute("position"));
149                   }
150                   catch (NumberFormatException JavaDoc e)
151                   {
152                      log.warn("the position '" + prov.getAttribute("position")
153                            + "' is not a valid number. This provider has to go to the end of the line. " + prov);
154                   }
155                }
156
157                int pos;
158                if (requestedPosition < 1)
159                {
160                   pos = Security.addProvider(provider);
161                }
162                else
163                {
164                   pos = Security.insertProviderAt(provider, requestedPosition);
165                }
166
167                if (pos == -1)
168                {
169                   int exPos = Arrays.asList(Security.getProviders()).indexOf(provider);
170                   log.info("The provider " + providerName + " already exists at position " + exPos);
171                }
172                else if (requestedPosition >= 1 && pos != requestedPosition)
173                {
174                   log.info("The position " + requestedPosition + " was requested for Provider " + providerName
175                         + " but it was added at position " + pos);
176                   addedProviders.add(providerName);
177                }
178                else
179                {
180                   log.info("The Provider " + providerName + " was added at position " + pos);
181                   addedProviders.add(providerName);
182                }
183             }
184             else
185             {
186                if (log.isDebugEnabled())
187                   log.debug("Ignoring node" + reqdProvider);
188             }
189          }
190
191          if (addedProviders.size() == 1)
192             log.info(addedProviders.size() + " JCE Provider was actually loaded.");
193          else
194             log.info(addedProviders.size() + " JCE Providers were actually loaded.");
195
196       }
197       if (n < 1)
198          log.info("No JCE Providers were requested.");
199    }
200    private void removeProviders()
201    {
202       for (Iterator JavaDoc iter = addedProviders.iterator(); iter.hasNext();)
203       {
204          String JavaDoc providerName = (String JavaDoc) iter.next();
205          try
206          {
207             Security.removeProvider(providerName);
208          }
209          catch (Exception JavaDoc e)
210          {
211             log.warn("Failed to remove Provider " + providerName);
212          }
213       }
214    }
215 }
216
Popular Tags