1 9 package org.jboss.security.jce; 10 11 import java.security.Provider ; 12 import java.security.Security ; 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 17 import org.jboss.system.ServiceMBeanSupport; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.Node ; 20 import org.w3c.dom.NodeList ; 21 22 44 public class JCEProviderService extends ServiceMBeanSupport implements JCEProviderServiceMBean 45 { 46 private ArrayList addedProviders = new ArrayList (); 47 private Element providers; 48 49 public void setJceProviders(Element 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 getJceProviders() 63 { 64 return providers; 65 } 66 67 protected void createService() throws Exception 68 { 69 synchronized (JCEProviderService.class) 71 { 72 loadProviders(); 73 } 74 } 75 76 protected void destroyService() 77 { 78 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 (); 91 NodeList reqdProviders = providers.getElementsByTagName("provider"); 92 n = reqdProviders.getLength(); 93 95 for (int i = 0; i < n; i++) 96 { 97 Provider provider; 98 Node reqdProvider = reqdProviders.item(i); 99 100 String providerName; 101 String providerClass; 102 int requestedPosition = 0; 103 104 if (Node.ELEMENT_NODE == reqdProvider.getNodeType()) 105 { 106 Element prov = (Element ) 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 ) Class.forName(providerClass).newInstance(); 120 } 121 catch (InstantiationException e1) 122 { 123 log.warn("Unable to instantiate an instance of the JCE Provider class " + providerClass, e1); 124 continue; 125 } 126 catch (IllegalAccessException e1) 127 { 128 log.warn("No permission to access the JCE Provider class " + providerClass, e1); 129 continue; 130 } 131 catch (ClassNotFoundException e1) 132 { 133 log.warn("Could not find the JCE Provider class " + providerClass, e1); 134 continue; 135 } 136 catch (ClassCastException 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 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 iter = addedProviders.iterator(); iter.hasNext();) 203 { 204 String providerName = (String ) iter.next(); 205 try 206 { 207 Security.removeProvider(providerName); 208 } 209 catch (Exception e) 210 { 211 log.warn("Failed to remove Provider " + providerName); 212 } 213 } 214 } 215 } 216 | Popular Tags |