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:
030: import org.apache.harmony.security.fortress.Engine;
031: import org.apache.harmony.security.internal.nls.Messages;
032:
033: /**
034: * @com.intel.drl.spec_ref
035: *
036: */
037:
038: public class CertPathBuilder {
039:
040: // Store CertPathBuilder service name
041: private static final String SERVICE = "CertPathBuilder"; //$NON-NLS-1$
042:
043: // Used to access common engine functionality
044: private static Engine engine = new Engine(SERVICE);
045:
046: // Store default property name
047: private static final String PROPERTYNAME = "certpathbuilder.type"; //$NON-NLS-1$
048:
049: // Default value of CertPathBuilder type. It returns if certpathbuild.type
050: // property is not defined in java.security file
051: private static final String DEFAULTPROPERTY = "PKIX"; //$NON-NLS-1$
052:
053: // Store used provider
054: private final Provider provider;
055:
056: // Store spi implementation
057: private CertPathBuilderSpi spiImpl;
058:
059: // Store algorithm name
060: private final String algorithm;
061:
062: /**
063: * @com.intel.drl.spec_ref
064: */
065: protected CertPathBuilder(CertPathBuilderSpi builderSpi,
066: Provider provider, String algorithm) {
067: this .provider = provider;
068: this .algorithm = algorithm;
069: this .spiImpl = builderSpi;
070: }
071:
072: /**
073: * @com.intel.drl.spec_ref
074: */
075: public final String getAlgorithm() {
076: return algorithm;
077: }
078:
079: /**
080: * @com.intel.drl.spec_ref
081: */
082: public final Provider getProvider() {
083: return provider;
084: }
085:
086: /**
087: * @com.intel.drl.spec_ref
088: *
089: * throws NullPointerException if algorithm is null (instead of
090: * NoSuchAlgorithmException as in 1.4 release)
091: */
092: public static CertPathBuilder getInstance(String algorithm)
093: throws NoSuchAlgorithmException {
094: if (algorithm == null) {
095: throw new NullPointerException(Messages
096: .getString("security.01")); //$NON-NLS-1$
097: }
098: synchronized (engine) {
099: engine.getInstance(algorithm, null);
100: return new CertPathBuilder((CertPathBuilderSpi) engine.spi,
101: engine.provider, algorithm);
102: }
103: }
104:
105: /**
106: * @com.intel.drl.spec_ref
107: *
108: * throws NullPointerException if algorithm is null (instead of
109: * NoSuchAlgorithmException as in 1.4 release)
110: *
111: * FIXME: jrockit-j2re1.4.2_04 throws IllegalArgumentException when provider
112: * is empty
113: */
114: public static CertPathBuilder getInstance(String algorithm,
115: String provider) throws NoSuchAlgorithmException,
116: NoSuchProviderException {
117: if ((provider == null) || (provider.length() == 0)) {
118: throw new IllegalArgumentException(Messages
119: .getString("security.02")); //$NON-NLS-1$
120: }
121: Provider impProvider = Security.getProvider(provider);
122: if (impProvider == null) {
123: throw new NoSuchProviderException(provider);
124: }
125: return getInstance(algorithm, impProvider);
126:
127: }
128:
129: /**
130: * @com.intel.drl.spec_ref
131: *
132: * throws NullPointerException if algorithm is null (instead of
133: * NoSuchAlgorithmException as in 1.4 release)
134: */
135: public static CertPathBuilder getInstance(String algorithm,
136: Provider provider) throws NoSuchAlgorithmException {
137: if (provider == null) {
138: throw new IllegalArgumentException(Messages
139: .getString("security.04")); //$NON-NLS-1$
140: }
141: if (algorithm == null) {
142: throw new NullPointerException(Messages
143: .getString("security.01")); //$NON-NLS-1$
144: }
145: synchronized (engine) {
146: engine.getInstance(algorithm, provider, null);
147: return new CertPathBuilder((CertPathBuilderSpi) engine.spi,
148: provider, algorithm);
149: }
150: }
151:
152: /**
153: * @com.intel.drl.spec_ref
154: */
155: public final CertPathBuilderResult build(CertPathParameters params)
156: throws CertPathBuilderException,
157: InvalidAlgorithmParameterException {
158: return spiImpl.engineBuild(params);
159: }
160:
161: /**
162: * @com.intel.drl.spec_ref
163: */
164: public static final String getDefaultType() {
165: String defaultType = AccessController
166: .doPrivileged(new java.security.PrivilegedAction<String>() {
167: public String run() {
168: return Security.getProperty(PROPERTYNAME);
169: }
170: });
171: return (defaultType != null ? defaultType : DEFAULTPROPERTY);
172: }
173: }
|