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 java.security.cert;
022:
023: import java.security.AccessController;
024: import java.security.InvalidAlgorithmParameterException;
025: import java.security.NoSuchAlgorithmException;
026: import java.security.NoSuchProviderException;
027: import java.security.Provider;
028: import java.security.Security;
029: import java.util.Collection;
030:
031: import org.apache.harmony.security.fortress.Engine;
032: import org.apache.harmony.security.internal.nls.Messages;
033:
034: /**
035: * @com.intel.drl.spec_ref
036: *
037: */
038:
039: public class CertStore {
040:
041: // Store spi implementation service name
042: private static final String SERVICE = "CertStore"; //$NON-NLS-1$
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 PROPERTYNAME = "certstore.type"; //$NON-NLS-1$
049:
050: // Default value of CertStore type. It returns if certpathbuild.type
051: // property is not defined in java.security file
052: private static final String DEFAULTPROPERTY = "LDAP"; //$NON-NLS-1$
053:
054: // Store used provider
055: private final Provider provider;
056:
057: // Store CertStoreSpi implementation
058: private final CertStoreSpi spiImpl;
059:
060: // Store used type
061: private final String type;
062:
063: // Store used parameters
064: private final CertStoreParameters certStoreParams;
065:
066: /**
067: * @com.intel.drl.spec_ref
068: */
069: protected CertStore(CertStoreSpi storeSpi, Provider provider,
070: String type, CertStoreParameters params) {
071: this .provider = provider;
072: this .type = type;
073: this .spiImpl = storeSpi;
074: this .certStoreParams = params;
075: }
076:
077: /**
078: * @com.intel.drl.spec_ref
079: *
080: * throws NullPointerException if type is null (instead of
081: * NoSuchAlgorithmException as in 1.4 release)
082: */
083: public static CertStore getInstance(String type,
084: CertStoreParameters params)
085: throws InvalidAlgorithmParameterException,
086: NoSuchAlgorithmException {
087: if (type == null) {
088: throw new NullPointerException(Messages
089: .getString("security.07")); //$NON-NLS-1$
090: }
091: try {
092: synchronized (engine) {
093: engine.getInstance(type, params);
094: return new CertStore((CertStoreSpi) engine.spi,
095: engine.provider, type, params);
096: }
097: } catch (NoSuchAlgorithmException e) {
098: Throwable th = e.getCause();
099: if (th == null) {
100: throw e;
101: } else {
102: throw new InvalidAlgorithmParameterException(e
103: .getMessage(), th);
104: }
105: }
106: }
107:
108: /**
109: * @com.intel.drl.spec_ref
110: *
111: * throws NullPointerException if type is null (instead of
112: * NoSuchAlgorithmException as in 1.4 release)
113: *
114: * FIXME: IllegalArgumentException when provider is empty
115: */
116: public static CertStore getInstance(String type,
117: CertStoreParameters params, String provider)
118: throws InvalidAlgorithmParameterException,
119: NoSuchAlgorithmException, NoSuchProviderException {
120: if ((provider == null) || (provider.length() == 0)) {
121: throw new IllegalArgumentException(Messages
122: .getString("security.02")); //$NON-NLS-1$
123: }
124: Provider impProvider = Security.getProvider(provider);
125: if (impProvider == null) {
126: throw new NoSuchProviderException(provider);
127: }
128: return getInstance(type, params, impProvider);
129: }
130:
131: /**
132: * @com.intel.drl.spec_ref
133: *
134: * throws NullPointerException if type is null (instead of
135: * NoSuchAlgorithmException as in 1.4 release)
136: */
137: public static CertStore getInstance(String type,
138: CertStoreParameters params, Provider provider)
139: throws NoSuchAlgorithmException,
140: InvalidAlgorithmParameterException {
141: if (provider == null) {
142: throw new IllegalArgumentException(Messages
143: .getString("security.04")); //$NON-NLS-1$
144: }
145: if (type == null) {
146: throw new NullPointerException(Messages
147: .getString("security.07")); //$NON-NLS-1$
148: }
149: try {
150: synchronized (engine) {
151: engine.getInstance(type, provider, params);
152: return new CertStore((CertStoreSpi) engine.spi,
153: provider, type, params);
154: }
155: } catch (NoSuchAlgorithmException e) {
156: Throwable th = e.getCause();
157: if (th == null) {
158: throw e;
159: } else {
160: throw new InvalidAlgorithmParameterException(e
161: .getMessage(), th);
162: }
163: }
164: }
165:
166: /**
167: * @com.intel.drl.spec_ref
168: */
169: public final String getType() {
170: return type;
171: }
172:
173: /**
174: * @com.intel.drl.spec_ref
175: */
176: public final Provider getProvider() {
177: return provider;
178: }
179:
180: /**
181: * @com.intel.drl.spec_ref
182: */
183: public final CertStoreParameters getCertStoreParameters() {
184: if (certStoreParams == null) {
185: return null;
186: } else {
187: return (CertStoreParameters) certStoreParams.clone();
188: }
189: }
190:
191: /**
192: * @com.intel.drl.spec_ref
193: */
194: public final Collection<? extends Certificate> getCertificates(
195: CertSelector selector) throws CertStoreException {
196: return spiImpl.engineGetCertificates(selector);
197: }
198:
199: /**
200: * @com.intel.drl.spec_ref
201: */
202: public final Collection<? extends CRL> getCRLs(CRLSelector selector)
203: throws CertStoreException {
204: return spiImpl.engineGetCRLs(selector);
205: }
206:
207: /**
208: * @com.intel.drl.spec_ref
209: */
210: public static final String getDefaultType() {
211: String defaultType = AccessController
212: .doPrivileged(new java.security.PrivilegedAction<String>() {
213: public String run() {
214: return Security.getProperty(PROPERTYNAME);
215: }
216: });
217: return (defaultType == null ? DEFAULTPROPERTY : defaultType);
218: }
219: }
|