1 4 7 package javax.xml.crypto.dsig; 8 9 import java.security.InvalidAlgorithmParameterException ; 10 import java.security.NoSuchAlgorithmException ; 11 import java.security.NoSuchProviderException ; 12 import java.security.Provider ; 13 import java.security.Provider.Service; 14 import java.security.Security ; 15 import java.util.*; 16 import javax.xml.crypto.MarshalException; 17 import javax.xml.crypto.XMLStructure; 18 import javax.xml.crypto.XMLCryptoContext; 19 import javax.xml.crypto.dsig.spec.TransformParameterSpec; 20 21 import sun.security.jca.*; 22 import sun.security.jca.GetInstance.Instance; 23 24 90 public abstract class TransformService implements Transform { 91 92 private String algorithm; 93 private String mechanism; 94 private Provider provider; 95 96 99 protected TransformService() {} 100 101 129 public static TransformService getInstance 130 (String algorithm, String mechanismType) 131 throws NoSuchAlgorithmException { 132 if (mechanismType == null || algorithm == null) { 133 throw new NullPointerException (); 134 } 135 boolean dom = false; 136 if (mechanismType.equals("DOM")) { 137 dom = true; 138 } 139 List services = GetInstance.getServices("TransformService", algorithm); 140 for (Iterator t = services.iterator(); t.hasNext(); ) { 141 Service s = (Service)t.next(); 142 String value = s.getAttribute("MechanismType"); 143 if ((value == null && dom) || 144 (value != null && value.equals(mechanismType))) { 145 Instance instance = GetInstance.getInstance(s, null); 146 TransformService ts = (TransformService) instance.impl; 147 ts.algorithm = algorithm; 148 ts.mechanism = mechanismType; 149 ts.provider = instance.provider; 150 return ts; 151 } 152 } 153 throw new NoSuchAlgorithmException 154 (algorithm + " algorithm and " + mechanismType 155 + " mechanism not available"); 156 } 157 158 178 public static TransformService getInstance 179 (String algorithm, String mechanismType, Provider provider) 180 throws NoSuchAlgorithmException { 181 if (mechanismType == null || algorithm == null || provider == null) { 182 throw new NullPointerException (); 183 } 184 185 boolean dom = false; 186 if (mechanismType.equals("DOM")) { 187 dom = true; 188 } 189 Service s = GetInstance.getService 190 ("TransformService", algorithm, provider); 191 String value = s.getAttribute("MechanismType"); 192 if ((value == null && dom) || 193 (value != null && value.equals(mechanismType))) { 194 Instance instance = GetInstance.getInstance(s, null); 195 TransformService ts = (TransformService) instance.impl; 196 ts.algorithm = algorithm; 197 ts.mechanism = mechanismType; 198 ts.provider = instance.provider; 199 return ts; 200 } 201 throw new NoSuchAlgorithmException 202 (algorithm + " algorithm and " + mechanismType 203 + " mechanism not available"); 204 } 205 206 230 public static TransformService getInstance 231 (String algorithm, String mechanismType, String provider) 232 throws NoSuchAlgorithmException , NoSuchProviderException { 233 if (mechanismType == null || algorithm == null || provider == null) { 234 throw new NullPointerException (); 235 } else if (provider.length() == 0) { 236 throw new NoSuchProviderException (); 237 } 238 boolean dom = false; 239 if (mechanismType.equals("DOM")) { 240 dom = true; 241 } 242 Service s = GetInstance.getService 243 ("TransformService", algorithm, provider); 244 String value = s.getAttribute("MechanismType"); 245 if ((value == null && dom) || 246 (value != null && value.equals(mechanismType))) { 247 Instance instance = GetInstance.getInstance(s, null); 248 TransformService ts = (TransformService) instance.impl; 249 ts.algorithm = algorithm; 250 ts.mechanism = mechanismType; 251 ts.provider = instance.provider; 252 return ts; 253 } 254 throw new NoSuchAlgorithmException 255 (algorithm + " algorithm and " + mechanismType 256 + " mechanism not available"); 257 } 258 259 private static class MechanismMapEntry implements Map.Entry { 260 private final String mechanism; 261 private final String algorithm; 262 private final String key; 263 MechanismMapEntry(String algorithm, String mechanism) { 264 this.algorithm = algorithm; 265 this.mechanism = mechanism; 266 this.key = "TransformService." + algorithm + " MechanismType"; 267 } 268 public boolean equals(Object o) { 269 if (!(o instanceof Map.Entry)) { 270 return false; 271 } 272 Map.Entry e = (Map.Entry) o; 273 return (getKey()==null ? 274 e.getKey()==null : getKey().equals(e.getKey())) && 275 (getValue()==null ? 276 e.getValue()==null : getValue().equals(e.getValue())); 277 } 278 public Object getKey() { 279 return key; 280 } 281 public Object getValue() { 282 return mechanism; 283 } 284 public Object setValue(Object value) { 285 throw new UnsupportedOperationException (); 286 } 287 public int hashCode() { 288 return (getKey()==null ? 0 : getKey().hashCode()) ^ 289 (getValue()==null ? 0 : getValue().hashCode()); 290 } 291 } 292 293 298 public final String getMechanismType() { 299 return mechanism; 300 } 301 302 308 public final String getAlgorithm() { 309 return algorithm; 310 } 311 312 317 public final Provider getProvider() { 318 return provider; 319 } 320 321 334 public abstract void init(TransformParameterSpec params) 335 throws InvalidAlgorithmParameterException ; 336 337 351 public abstract void marshalParams 352 (XMLStructure parent, XMLCryptoContext context) 353 throws MarshalException; 354 355 370 public abstract void init(XMLStructure parent, XMLCryptoContext context) 371 throws InvalidAlgorithmParameterException ; 372 } 373 | Popular Tags |