KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > keys > keyresolver > KeyResolver


1
2 /*
3  * Copyright 1999-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package com.sun.org.apache.xml.internal.security.keys.keyresolver;
19
20
21
22 import java.security.PublicKey JavaDoc;
23 import java.security.cert.X509Certificate JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.crypto.SecretKey;
28
29 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33
34 /**
35  * KeyResolver is factory class for subclass of KeyResolverSpi that
36  * represent child element of KeyInfo.
37  *
38  * @author $Author: raul $
39  * @version 1.3, 11/14/05
40  */

41 public class KeyResolver {
42
43    /** {@link java.util.logging} logging facility */
44     static java.util.logging.Logger JavaDoc log =
45         java.util.logging.Logger.getLogger(KeyResolver.class.getName());
46
47    /** Field _alreadyInitialized */
48    static boolean _alreadyInitialized = false;
49
50    /** Field _resolverVector */
51    static List JavaDoc _resolverVector = null;
52
53    /** Field _resolverSpi */
54    protected KeyResolverSpi _resolverSpi = null;
55
56    /** Field _storage */
57    protected StorageResolver _storage = null;
58
59    /**
60     * Constructor ResourceResolver
61     *
62     * @param className
63     * @throws ClassNotFoundException
64     * @throws IllegalAccessException
65     * @throws InstantiationException
66     */

67    private KeyResolver(String JavaDoc className)
68            throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc,
69                   InstantiationException JavaDoc {
70       this._resolverSpi =
71          (KeyResolverSpi) Class.forName(className).newInstance();
72    }
73
74    /**
75     * Method length
76     *
77     * @return the length of resolvers registed
78     */

79    public static int length() {
80       return KeyResolver._resolverVector.size();
81    }
82
83    /**
84     * Method item
85     *
86     * @param i
87     * @return the number i resolver registerd
88     * @throws KeyResolverException
89     */

90    public static KeyResolver item(int i) throws KeyResolverException {
91
92        KeyResolver resolver = (KeyResolver) KeyResolver._resolverVector.get(i);
93       if (resolver==null) {
94          throw new KeyResolverException("utils.resolver.noClass");
95       }
96
97       return resolver;
98    }
99
100    /**
101     * Method getInstance
102     *
103     * @param element
104     * @param BaseURI
105     * @param storage
106     * @return the instance that happends to implement the thing.
107     *
108     * @throws KeyResolverException
109     */

110    public static final KeyResolver getInstance(
111            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
112               throws KeyResolverException {
113
114       for (int i = 0; i < KeyResolver._resolverVector.size(); i++) {
115           KeyResolver resolver=
116             (KeyResolver) KeyResolver._resolverVector.get(i);
117
118           if (resolver==null) {
119             Object JavaDoc exArgs[] = {
120                (((element != null)
121                  && (element.getNodeType() == Node.ELEMENT_NODE))
122                 ? element.getTagName()
123                 : "null") };
124
125             throw new KeyResolverException("utils.resolver.noClass", exArgs);
126          }
127          if (true)
128             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "check resolvability by class " + resolver.getClass());
129
130          if (resolver.canResolve(element, BaseURI, storage)) {
131             return resolver;
132          }
133       }
134
135       Object JavaDoc exArgs[] = {
136          (((element != null) && (element.getNodeType() == Node.ELEMENT_NODE))
137           ? element.getTagName()
138           : "null") };
139
140       throw new KeyResolverException("utils.resolver.noClass", exArgs);
141    }
142
143    /**
144     * The init() function is called by com.sun.org.apache.xml.internal.security.Init.init()
145     */

146    public static void init() {
147
148       if (!KeyResolver._alreadyInitialized) {
149          KeyResolver._resolverVector = new ArrayList JavaDoc(10);
150          _alreadyInitialized = true;
151       }
152    }
153
154    /**
155     * This method is used for registering {@link KeyResolverSpi}s which are
156     * available to <I>all</I> {@link com.sun.org.apache.xml.internal.security.keys.KeyInfo} objects. This means that
157     * personalized {@link KeyResolverSpi}s should only be registered directly
158     * to the {@link com.sun.org.apache.xml.internal.security.keys.KeyInfo} using
159     * {@link com.sun.org.apache.xml.internal.security.keys.KeyInfo#registerInternalKeyResolver}.
160     *
161     * @param className
162  * @throws InstantiationException
163  * @throws IllegalAccessException
164  * @throws ClassNotFoundException
165     */

166    public static void register(String JavaDoc className) throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
167       KeyResolver._resolverVector.add(new KeyResolver(className));
168    }
169
170    /**
171     * This method is used for registering {@link KeyResolverSpi}s which are
172     * available to <I>all</I> {@link com.sun.org.apache.xml.internal.security.keys.KeyInfo} objects. This means that
173     * personalized {@link KeyResolverSpi}s should only be registered directly
174     * to the {@link com.sun.org.apache.xml.internal.security.keys.KeyInfo} using {@link com.sun.org.apache.xml.internal.security.keys.KeyInfo#registerInternalKeyResolver}.
175     *
176     * @param className
177     */

178    public static void registerAtStart(String JavaDoc className) {
179       KeyResolver._resolverVector.add(0, className);
180    }
181
182    /*
183     * Method resolve
184     *
185     * @param element
186     *
187     * @throws KeyResolverException
188     */

189
190    /**
191     * Method resolveStatic
192     *
193     * @param element
194     * @param BaseURI
195     * @param storage
196     * @return resolve from the static register an element
197     *
198     * @throws KeyResolverException
199     */

200    public static PublicKey JavaDoc resolveStatic(
201            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
202               throws KeyResolverException {
203
204       KeyResolver myResolver = KeyResolver.getInstance(element, BaseURI,
205                                   storage);
206
207       return myResolver.resolvePublicKey(element, BaseURI, storage);
208    }
209
210    /**
211     * Method resolve
212     *
213     * @param element
214     * @param BaseURI
215     * @param storage
216     * @return resolved public key from the registered from the elements
217     *
218     * @throws KeyResolverException
219     */

220    public PublicKey JavaDoc resolvePublicKey(
221            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
222               throws KeyResolverException {
223       return this._resolverSpi.engineResolvePublicKey(element, BaseURI, storage);
224    }
225
226    /**
227     * Method resolveX509Certificate
228     *
229     * @param element
230     * @param BaseURI
231     * @param storage
232     * @return resolved X509certificate key from the registered from the elements
233     *
234     * @throws KeyResolverException
235     */

236    public X509Certificate JavaDoc resolveX509Certificate(
237            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
238               throws KeyResolverException {
239       return this._resolverSpi.engineResolveX509Certificate(element, BaseURI,
240               storage);
241    }
242
243    /**
244     * @param element
245     * @param BaseURI
246     * @param storage
247     * @return resolved SecretKey key from the registered from the elements
248     * @throws KeyResolverException
249     */

250    public SecretKey resolveSecretKey(
251            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
252               throws KeyResolverException {
253       return this._resolverSpi.engineResolveSecretKey(element, BaseURI,
254               storage);
255    }
256
257    /**
258     * Method setProperty
259     *
260     * @param key
261     * @param value
262     */

263    public void setProperty(String JavaDoc key, String JavaDoc value) {
264       this._resolverSpi.engineSetProperty(key, value);
265    }
266
267    /**
268     * Method getProperty
269     *
270     * @param key
271     * @return the property setted for this resolver
272     */

273    public String JavaDoc getProperty(String JavaDoc key) {
274       return this._resolverSpi.engineGetProperty(key);
275    }
276
277    /**
278     * Method getPropertyKeys
279     *
280     * @return the properties key registerd in this resolver
281     */

282    public String JavaDoc[] getPropertyKeys() {
283       return this._resolverSpi.engineGetPropertyKeys();
284    }
285
286    /**
287     * Method understandsProperty
288     *
289     * @param propertyToTest
290     * @return true if the resolver understands property propertyToTest
291     */

292    public boolean understandsProperty(String JavaDoc propertyToTest) {
293       return this._resolverSpi.understandsProperty(propertyToTest);
294    }
295
296    /**
297     * Method canResolve
298     *
299     * @param element
300     * @param BaseURI
301     * @param storage
302     * @return true if can resolve the key in the element
303     */

304    public boolean canResolve(Element JavaDoc element, String JavaDoc BaseURI,
305                              StorageResolver storage) {
306       return this._resolverSpi.engineCanResolve(element, BaseURI, storage);
307    }
308
309    /**
310     * Method resolverClassName
311     *
312     * @return the name of the resolver.
313     */

314    public String JavaDoc resolverClassName() {
315       return this._resolverSpi.getClass().getName();
316    }
317 }
318
Popular Tags