KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > resolver > ResourceResolver


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.utils.resolver;
19
20
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
27 import org.w3c.dom.Attr JavaDoc;
28
29
30 /**
31  * During reference validation, we have to retrieve resources from somewhere.
32  * This is done by retrieving a Resolver. The resolver needs two arguments: The
33  * URI in which the link to the new resource is defined and the BaseURI of the
34  * file/entity in which the URI occurs (the BaseURI is the same as the SystemId.
35  *
36  * <UL xml:lang="DE" LANG="DE">
37  * <LI> Verschiedene Implementierungen k??nnen sich als Resolver registrieren.
38  * <LI> Standardm????ig werden erste Implementierungen auf dem XML config file registrirt.
39  * <LI> Der Benutzer kann bei Bedarf Implementierungen voranstellen oder anf??gen.
40  * <LI> Implementierungen k??nnen mittels Features customized werden ??
41  * (z.B. um Proxy-Passworter ??bergeben zu k??nnen).
42  * <LI> Jede Implementierung bekommt das URI Attribut und den Base URI
43  * ??bergeben und muss antworten, ob sie aufl??sen kann.
44  * <LI> Die erste Implementierung, die die Aufgabe erf??llt, f??hrt die Aufl??sung durch.
45  * </UL>
46  *
47  * @author $Author: raul $
48  */

49 public class ResourceResolver {
50
51    /** {@link java.util.logging} logging facility */
52     static java.util.logging.Logger JavaDoc log =
53         java.util.logging.Logger.getLogger(ResourceResolver.class.getName());
54
55    /** Field _alreadyInitialized */
56    static boolean _alreadyInitialized = false;
57
58    /** these are the system-wide resolvers */
59    static List JavaDoc _resolverVector = null;
60
61    /** Field _individualResolverVector */
62    List JavaDoc _individualResolverVector = null;
63
64    /** Field transformSpi */
65    protected ResourceResolverSpi _resolverSpi = null;
66
67    /**
68     * Constructor ResourceResolver
69     *
70     * @param className
71     * @throws ClassNotFoundException
72     * @throws IllegalAccessException
73     * @throws InstantiationException
74     */

75    private ResourceResolver(String JavaDoc className)
76            throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc,
77                   InstantiationException JavaDoc {
78       this._resolverSpi =
79          (ResourceResolverSpi) Class.forName(className).newInstance();
80    }
81
82    /**
83     * Constructor ResourceResolver
84     *
85     * @param resourceResolver
86     */

87    public ResourceResolver(ResourceResolverSpi resourceResolver) {
88       this._resolverSpi = resourceResolver;
89    }
90
91    /**
92     * Method getInstance
93     *
94     * @param uri
95     * @param BaseURI
96     * @return the instnace
97     *
98     * @throws ResourceResolverException
99     */

100    public static final ResourceResolver getInstance(Attr JavaDoc uri, String JavaDoc BaseURI)
101            throws ResourceResolverException {
102       int length=ResourceResolver._resolverVector.size();
103       for (int i = 0; i < length; i++) {
104           ResourceResolver resolver =
105             (ResourceResolver) ResourceResolver._resolverVector.get(i);
106          
107
108          if (true)
109             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "check resolvability by class " + resolver.getClass().getName());
110
111          if ((resolver != null) && resolver.canResolve(uri, BaseURI)) {
112             return resolver;
113          }
114       }
115
116       Object JavaDoc exArgs[] = { ((uri != null)
117                            ? uri.getNodeValue()
118                            : "null"), BaseURI };
119
120       throw new ResourceResolverException("utils.resolver.noClass", exArgs,
121                                           uri, BaseURI);
122    }
123    /**
124     * Method getInstance
125     *
126     * @param uri
127     * @param BaseURI
128     * @param individualResolvers
129     * @return the instance
130     *
131     * @throws ResourceResolverException
132     */

133    public static final ResourceResolver getInstance(
134            Attr JavaDoc uri, String JavaDoc BaseURI, List JavaDoc individualResolvers)
135               throws ResourceResolverException {
136       if (true) {
137         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I was asked to create a ResourceResolver and got " + individualResolvers.size());
138         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, " extra resolvers to my existing " + ResourceResolver._resolverVector.size() + " system-wide resolvers");
139       }
140
141       // first check the individual Resolvers
142
int size=0;
143       if ((individualResolvers != null) && ((size=individualResolvers.size()) > 0)) {
144          for (int i = 0; i < size; i++) {
145             ResourceResolver resolver =
146                (ResourceResolver) individualResolvers.get(i);
147
148             if (resolver != null) {
149                String JavaDoc currentClass = resolver._resolverSpi.getClass().getName();
150                if (true)
151                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "check resolvability by class " + currentClass);
152
153                if (resolver.canResolve(uri, BaseURI)) {
154                   return resolver;
155                }
156             }
157          }
158       }
159
160       return getInstance(uri,BaseURI);
161    }
162
163    /**
164     * The init() function is called by com.sun.org.apache.xml.internal.security.Init.init()
165     */

166    public static void init() {
167
168       if (!ResourceResolver._alreadyInitialized) {
169          ResourceResolver._resolverVector = new ArrayList JavaDoc(10);
170          _alreadyInitialized = true;
171       }
172    }
173
174    /**
175     * Method register
176     *
177     * @param className
178     */

179    public static void register(String JavaDoc className) {
180         ResourceResolver resolver = null;
181
182         try {
183            resolver = new ResourceResolver(className);
184            ResourceResolver._resolverVector.add(resolver);
185         } catch (Exception JavaDoc e) {
186 // Object exArgs[] = { ((uri != null)
187
// ? uri.getNodeValue()
188
// : "null"), BaseURI };
189
//
190
// throw new ResourceResolverException("utils.resolver.noClass",
191
// exArgs, e, uri, BaseURI);
192
log.log(java.util.logging.Level.WARNING, "Error loading resolver " + className +" disabling it");
193         } catch (NoClassDefFoundError JavaDoc e) {
194             log.log(java.util.logging.Level.WARNING, "Error loading resolver " + className +" disabling it");
195         }
196       
197    }
198
199    /**
200     * Method registerAtStart
201     *
202     * @param className
203     */

204    public static void registerAtStart(String JavaDoc className) {
205       ResourceResolver._resolverVector.add(0, className);
206    }
207
208    /**
209     * Method resolve
210     *
211     * @param uri
212     * @param BaseURI
213     * @return the resource
214     *
215     * @throws ResourceResolverException
216     */

217    public static XMLSignatureInput resolveStatic(Attr JavaDoc uri, String JavaDoc BaseURI)
218            throws ResourceResolverException {
219
220       ResourceResolver myResolver = ResourceResolver.getInstance(uri, BaseURI);
221
222       return myResolver.resolve(uri, BaseURI);
223    }
224
225    /**
226     * Method resolve
227     *
228     * @param uri
229     * @param BaseURI
230     * @return the resource
231     *
232     * @throws ResourceResolverException
233     */

234    public XMLSignatureInput resolve(Attr JavaDoc uri, String JavaDoc BaseURI)
235            throws ResourceResolverException {
236       return this._resolverSpi.engineResolve(uri, BaseURI);
237    }
238
239    /**
240     * Method setProperty
241     *
242     * @param key
243     * @param value
244     */

245    public void setProperty(String JavaDoc key, String JavaDoc value) {
246       this._resolverSpi.engineSetProperty(key, value);
247    }
248
249    /**
250     * Method getProperty
251     *
252     * @param key
253     * @return the value of the property
254     */

255    public String JavaDoc getProperty(String JavaDoc key) {
256       return this._resolverSpi.engineGetProperty(key);
257    }
258
259    /**
260     * Method addProperties
261     *
262     * @param properties
263     */

264    public void addProperties(Map JavaDoc properties) {
265       this._resolverSpi.engineAddProperies(properties);
266    }
267
268    /**
269     * Method getPropertyKeys
270     *
271     * @return all property keys.
272     */

273    public String JavaDoc[] getPropertyKeys() {
274       return this._resolverSpi.engineGetPropertyKeys();
275    }
276
277    /**
278     * Method understandsProperty
279     *
280     * @param propertyToTest
281     * @return true if the resolver understands the property
282     */

283    public boolean understandsProperty(String JavaDoc propertyToTest) {
284       return this._resolverSpi.understandsProperty(propertyToTest);
285    }
286
287    /**
288     * Method canResolve
289     *
290     * @param uri
291     * @param BaseURI
292     * @return true if it can resolve the uri
293     */

294    private boolean canResolve(Attr JavaDoc uri, String JavaDoc BaseURI) {
295       return this._resolverSpi.engineCanResolve(uri, BaseURI);
296    }
297 }
298
Popular Tags