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 Boris V. Kuznetsov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.io.IOException;
024: import java.security.spec.AlgorithmParameterSpec;
025: import java.security.spec.InvalidParameterSpecException;
026:
027: import org.apache.harmony.security.fortress.Engine;
028: import org.apache.harmony.security.internal.nls.Messages;
029:
030: /**
031: * @com.intel.drl.spec_ref
032: *
033: */
034:
035: public class AlgorithmParameters {
036: /**
037: * The service name.
038: */
039: private static final String SEVICE = "AlgorithmParameters"; //$NON-NLS-1$
040:
041: /**
042: * Used to access common engine functionality
043: */
044: private static Engine engine = new Engine(SEVICE);
045:
046: /**
047: * The provider
048: */
049: private Provider provider;
050:
051: /**
052: * The SPI implementation.
053: */
054: private AlgorithmParametersSpi spiImpl;
055:
056: /**
057: * The algorithm.
058: */
059: private String algorithm;
060:
061: /**
062: * The initialization state
063: */
064: private boolean initialized; // = false;
065:
066: /**
067: * @com.intel.drl.spec_ref
068: *
069: */
070: protected AlgorithmParameters(AlgorithmParametersSpi keyFacSpi,
071: Provider provider, String algorithm) {
072: this .provider = provider;
073: this .algorithm = algorithm;
074: this .spiImpl = keyFacSpi;
075: }
076:
077: /**
078: * @com.intel.drl.spec_ref
079: *
080: */
081: public static AlgorithmParameters getInstance(String algorithm)
082: throws NoSuchAlgorithmException {
083: if (algorithm == null) {
084: throw new NullPointerException(Messages
085: .getString("security.01")); //$NON-NLS-1$
086: }
087: synchronized (engine) {
088: engine.getInstance(algorithm, null);
089: return new AlgorithmParameters(
090: (AlgorithmParametersSpi) engine.spi,
091: engine.provider, algorithm);
092: }
093: }
094:
095: /**
096: * @com.intel.drl.spec_ref
097: *
098: */
099: public static AlgorithmParameters getInstance(String algorithm,
100: String provider) throws NoSuchAlgorithmException,
101: NoSuchProviderException {
102: if ((provider == null) || (provider.length() == 0)) {
103: throw new IllegalArgumentException(Messages
104: .getString("security.02")); //$NON-NLS-1$
105: }
106: Provider p = Security.getProvider(provider);
107: if (p == null) {
108: throw new NoSuchProviderException(Messages.getString(
109: "security.03", //$NON-NLS-1$
110: provider));
111: }
112: return getInstance(algorithm, p);
113: }
114:
115: /**
116: * @com.intel.drl.spec_ref
117: *
118: */
119: public static AlgorithmParameters getInstance(String algorithm,
120: Provider provider) throws NoSuchAlgorithmException {
121: if (provider == null) {
122: throw new IllegalArgumentException(Messages
123: .getString("security.04")); //$NON-NLS-1$
124: }
125: if (algorithm == null) {
126: throw new NullPointerException(Messages
127: .getString("security.01")); //$NON-NLS-1$
128: }
129: synchronized (engine) {
130: engine.getInstance(algorithm, provider, null);
131: return new AlgorithmParameters(
132: (AlgorithmParametersSpi) engine.spi, provider,
133: algorithm);
134: }
135: }
136:
137: /**
138: * @com.intel.drl.spec_ref
139: *
140: */
141: public final Provider getProvider() {
142: return provider;
143: }
144:
145: /**
146: * @com.intel.drl.spec_ref
147: *
148: */
149: public final String getAlgorithm() {
150: return algorithm;
151: }
152:
153: /**
154: * @com.intel.drl.spec_ref
155: *
156: */
157: public final void init(AlgorithmParameterSpec paramSpec)
158: throws InvalidParameterSpecException {
159: if (initialized) {
160: throw new InvalidParameterSpecException(Messages
161: .getString("security.1E")); //$NON-NLS-1$
162: }
163: spiImpl.engineInit(paramSpec);
164: initialized = true;
165: }
166:
167: /**
168: * @com.intel.drl.spec_ref
169: *
170: */
171: public final void init(byte[] params) throws IOException {
172: if (initialized) {
173: throw new IOException(Messages.getString("security.1E")); //$NON-NLS-1$
174: }
175: spiImpl.engineInit(params);
176: initialized = true;
177: }
178:
179: /**
180: * @com.intel.drl.spec_ref
181: *
182: */
183: public final void init(byte[] params, String format)
184: throws IOException {
185: if (initialized) {
186: throw new IOException(Messages.getString("security.1E")); //$NON-NLS-1$
187: }
188: spiImpl.engineInit(params, format);
189: initialized = true;
190: }
191:
192: /**
193: * @com.intel.drl.spec_ref
194: *
195: */
196: public final <T extends AlgorithmParameterSpec> T getParameterSpec(
197: Class<T> paramSpec) throws InvalidParameterSpecException {
198: if (!initialized) {
199: throw new InvalidParameterSpecException(Messages
200: .getString("security.1F")); //$NON-NLS-1$
201: }
202: return spiImpl.engineGetParameterSpec(paramSpec);
203: }
204:
205: /**
206: * @com.intel.drl.spec_ref
207: *
208: */
209: public final byte[] getEncoded() throws IOException {
210: if (!initialized) {
211: throw new IOException(Messages.getString("security.1F")); //$NON-NLS-1$
212: }
213: return spiImpl.engineGetEncoded();
214: }
215:
216: /**
217: * @com.intel.drl.spec_ref
218: *
219: */
220: public final byte[] getEncoded(String format) throws IOException {
221: if (!initialized) {
222: throw new IOException(Messages.getString("security.1F")); //$NON-NLS-1$
223: }
224: return spiImpl.engineGetEncoded(format);
225: }
226:
227: /**
228: * @com.intel.drl.spec_ref
229: *
230: */
231: public final String toString() {
232: if (!initialized) {
233: return null;
234: }
235: return spiImpl.engineToString();
236: }
237: }
|