001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package javax.net.ssl;
022:
023: import java.security.AccessController;
024: import java.security.InvalidAlgorithmParameterException;
025: import java.security.KeyStore;
026: import java.security.KeyStoreException;
027: import java.security.NoSuchAlgorithmException;
028: import java.security.NoSuchProviderException;
029: import java.security.Provider;
030: import java.security.Security;
031: import java.security.UnrecoverableKeyException;
032:
033: import org.apache.harmony.security.fortress.Engine;
034:
035: /**
036: * @com.intel.drl.spec_ref
037: *
038: */
039:
040: public class KeyManagerFactory {
041: // Store KeyManagerFactory service name
042: private static final String SERVICE = "KeyManagerFactory";
043:
044: // Used to access common engine functionality
045: private static Engine engine = new Engine(SERVICE);
046:
047: // Store default property name
048: private static final String PROPERTY_NAME = "ssl.KeyManagerFactory.algorithm";
049:
050: // Store used provider
051: private final Provider provider;
052:
053: // Store used KeyManagerFactorySpi implementation
054: private final KeyManagerFactorySpi spiImpl;
055:
056: // Store used algorithm
057: private final String algorithm;
058:
059: /**
060: * @com.intel.drl.spec_ref
061: *
062: */
063: protected KeyManagerFactory(KeyManagerFactorySpi factorySpi,
064: Provider provider, String algorithm) {
065: this .provider = provider;
066: this .algorithm = algorithm;
067: this .spiImpl = factorySpi;
068: }
069:
070: /**
071: * @com.intel.drl.spec_ref
072: *
073: */
074: public final String getAlgorithm() {
075: return algorithm;
076: }
077:
078: /**
079: * @com.intel.drl.spec_ref
080: *
081: * throws NullPointerException if algorithm is null (instead of
082: * NoSuchAlgorithmException as in 1.4 release)
083: */
084: public static final KeyManagerFactory getInstance(String algorithm)
085: throws NoSuchAlgorithmException {
086: if (algorithm == null) {
087: throw new NullPointerException("algorithm is null");
088: }
089: synchronized (engine) {
090: engine.getInstance(algorithm, null);
091: return new KeyManagerFactory(
092: (KeyManagerFactorySpi) engine.spi, engine.provider,
093: algorithm);
094: }
095: }
096:
097: /**
098: * @com.intel.drl.spec_ref
099: *
100: * throws NullPointerException if algorithm is null (instead of
101: * NoSuchAlgorithmException as in 1.4 release)
102: */
103: public static final KeyManagerFactory getInstance(String algorithm,
104: String provider) throws NoSuchAlgorithmException,
105: NoSuchProviderException {
106: if ((provider == null) || (provider.length() == 0)) {
107: throw new IllegalArgumentException(
108: "Provider is null or empty");
109: }
110: Provider impProvider = Security.getProvider(provider);
111: if (impProvider == null) {
112: throw new NoSuchProviderException(provider);
113: }
114: return getInstance(algorithm, impProvider);
115: }
116:
117: /**
118: * @com.intel.drl.spec_ref
119: *
120: * throws NullPointerException if algorithm is null (instead of
121: * NoSuchAlgorithmException as in 1.4 release)
122: */
123: public static final KeyManagerFactory getInstance(String algorithm,
124: Provider provider) throws NoSuchAlgorithmException {
125: if (provider == null) {
126: throw new IllegalArgumentException("Provider is null");
127: }
128: if (algorithm == null) {
129: throw new NullPointerException("algorithm is null");
130: }
131: synchronized (engine) {
132: engine.getInstance(algorithm, provider, null);
133: return new KeyManagerFactory(
134: (KeyManagerFactorySpi) engine.spi, provider,
135: algorithm);
136: }
137: }
138:
139: /**
140: * @com.intel.drl.spec_ref
141: *
142: */
143: public final Provider getProvider() {
144: return provider;
145: }
146:
147: /**
148: * @com.intel.drl.spec_ref
149: *
150: */
151: public final void init(KeyStore ks, char[] password)
152: throws KeyStoreException, NoSuchAlgorithmException,
153: UnrecoverableKeyException {
154: spiImpl.engineInit(ks, password);
155: }
156:
157: /**
158: * @com.intel.drl.spec_ref
159: *
160: */
161: public final void init(ManagerFactoryParameters spec)
162: throws InvalidAlgorithmParameterException {
163: spiImpl.engineInit(spec);
164: }
165:
166: /**
167: * @com.intel.drl.spec_ref
168: *
169: */
170: public final KeyManager[] getKeyManagers() {
171: return spiImpl.engineGetKeyManagers();
172: }
173:
174: /**
175: * @com.intel.drl.spec_ref
176: *
177: */
178: public static final String getDefaultAlgorithm() {
179: return AccessController
180: .doPrivileged(new java.security.PrivilegedAction<String>() {
181: public String run() {
182: return Security.getProperty(PROPERTY_NAME);
183: }
184: });
185: }
186: }
|