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 org.apache.harmony.security.fortress;
022:
023: import java.security.NoSuchAlgorithmException;
024: import java.security.Provider;
025:
026: import org.apache.harmony.security.Util;
027: import org.apache.harmony.security.internal.nls.Messages;
028:
029: /**
030: *
031: * This class implements common functionality for all engine classes
032: *
033: */
034: public class Engine {
035:
036: // Service name
037: private String serviceName;
038:
039: // for getInstance(String algorithm, Object param) optimization:
040: // previous result
041: private Provider.Service returnedService;
042:
043: // previous parameter
044: private String lastAlgorithm;
045:
046: private int refreshNumber;
047:
048: /**
049: * Provider
050: */
051: public Provider provider;
052:
053: /**
054: * SPI instance
055: */
056: public Object spi;
057:
058: /**
059: * Access to package visible api in java.security
060: */
061: public static SecurityAccess door;
062:
063: /**
064: * Creates a Engine object
065: *
066: * @param service
067: */
068: public Engine(String service) {
069: this .serviceName = service;
070: }
071:
072: /**
073: *
074: * Finds the appropriate service implementation and creates instance of the
075: * class that implements corresponding Service Provider Interface.
076: *
077: * @param algorithm
078: * @param service
079: * @throws NoSuchAlgorithmException
080: */
081: public synchronized void getInstance(String algorithm, Object param)
082: throws NoSuchAlgorithmException {
083: Provider.Service serv;
084:
085: if (algorithm == null) {
086: throw new NoSuchAlgorithmException(Messages
087: .getString("security.149")); //$NON-NLS-1$
088: }
089: Services.refresh();
090: if (returnedService != null
091: && Util.equalsIgnoreCase(algorithm, lastAlgorithm)
092: && refreshNumber == Services.refreshNumber) {
093: serv = returnedService;
094: } else {
095: if (Services.isEmpty()) {
096: throw new NoSuchAlgorithmException(Messages.getString(
097: "security.14A", //$NON-NLS-1$
098: serviceName, algorithm));
099: }
100: serv = Services.getService(new StringBuffer(128).append(
101: serviceName).append(".").append( //$NON-NLS-1$
102: Util.toUpperCase(algorithm)).toString());
103: if (serv == null) {
104: throw new NoSuchAlgorithmException(Messages.getString(
105: "security.14A", //$NON-NLS-1$
106: serviceName, algorithm));
107: }
108: returnedService = serv;
109: lastAlgorithm = algorithm;
110: refreshNumber = Services.refreshNumber;
111: }
112: spi = serv.newInstance(param);
113: this .provider = serv.getProvider();
114: }
115:
116: /**
117: *
118: * Finds the appropriate service implementation and creates instance of the
119: * class that implements corresponding Service Provider Interface.
120: *
121: * @param algorithm
122: * @param service
123: * @param provider
124: * @throws NoSuchAlgorithmException
125: */
126: public synchronized void getInstance(String algorithm,
127: Provider provider, Object param)
128: throws NoSuchAlgorithmException {
129:
130: Provider.Service serv = null;
131: if (algorithm == null) {
132: throw new NoSuchAlgorithmException(Messages.getString(
133: "security.14B", serviceName)); //$NON-NLS-1$
134: }
135: serv = provider.getService(serviceName, algorithm);
136: if (serv == null) {
137: throw new NoSuchAlgorithmException(Messages.getString(
138: "security.14A", //$NON-NLS-1$
139: serviceName, algorithm));
140: }
141: spi = serv.newInstance(param);
142: this.provider = provider;
143: }
144:
145: }
|