J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * reserved comment block |
| 3 | * DO NOT REMOVE OR ALTER! |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright 1999-2004 The Apache Software Foundation. |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | */ |
| 22 | package com.sun.org.apache.xml.internal.security.utils.resolver; |
| 23 | |
| 24 | |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
| 31 | import org.w3c.dom.Attr; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * During reference validation, we have to retrieve resources from somewhere. |
| 36 | * This is done by retrieving a Resolver. The resolver needs two arguments: The |
| 37 | * URI in which the link to the new resource is defined and the BaseURI of the |
| 38 | * file/entity in which the URI occurs (the BaseURI is the same as the SystemId. |
| 39 | * |
| 40 | * <UL xml:lang="DE" LANG="DE"> |
| 41 | * <LI> Verschiedene Implementierungen k??nnen sich als Resolver registrieren. |
| 42 | * <LI> Standardm????ig werden erste Implementierungen auf dem XML config file registrirt. |
| 43 | * <LI> Der Benutzer kann bei Bedarf Implementierungen voranstellen oder anf??gen. |
| 44 | * <LI> Implementierungen k??nnen mittels Features customized werden ?? |
| 45 | * (z.B. um Proxy-Passworter ??bergeben zu k??nnen). |
| 46 | * <LI> Jede Implementierung bekommt das URI Attribut und den Base URI |
| 47 | * ??bergeben und muss antworten, ob sie aufl??sen kann. |
| 48 | * <LI> Die erste Implementierung, die die Aufgabe erf??llt, f??hrt die Aufl??sung durch. |
| 49 | * </UL> |
| 50 | * |
| 51 | * @author $Author: raul $ |
| 52 | */ |
| 53 | public class ResourceResolver { |
| 54 | |
| 55 | /** {@link java.util.logging} logging facility */ |
| 56 | static java.util.logging.Logger log = |
| 57 | java.util.logging.Logger.getLogger(ResourceResolver.class.getName()); |
| 58 | |
| 59 | /** Field _alreadyInitialized */ |
| 60 | static boolean _alreadyInitialized = false; |
| 61 | |
| 62 | /** these are the system-wide resolvers */ |
| 63 | static List _resolverVector = null; |
| 64 | |
| 65 | /** Field _individualResolverVector */ |
| 66 | List _individualResolverVector = null; |
| 67 | |
| 68 | /** Field transformSpi */ |
| 69 | protected ResourceResolverSpi _resolverSpi = null; |
| 70 | |
| 71 | /** |
| 72 | * Constructor ResourceResolver |
| 73 | * |
| 74 | * @param className |
| 75 | * @throws ClassNotFoundException |
| 76 | * @throws IllegalAccessException |
| 77 | * @throws InstantiationException |
| 78 | */ |
| 79 | private ResourceResolver(String className) |
| 80 | throws ClassNotFoundException, IllegalAccessException, |
| 81 | InstantiationException { |
| 82 | this._resolverSpi = |
| 83 | (ResourceResolverSpi) Class.forName(className).newInstance(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Constructor ResourceResolver |
| 88 | * |
| 89 | * @param resourceResolver |
| 90 | */ |
| 91 | public ResourceResolver(ResourceResolverSpi resourceResolver) { |
| 92 | this._resolverSpi = resourceResolver; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Method getInstance |
| 97 | * |
| 98 | * @param uri |
| 99 | * @param BaseURI |
| 100 | * @return the instnace |
| 101 | * |
| 102 | * @throws ResourceResolverException |
| 103 | */ |
| 104 | public static final ResourceResolver getInstance(Attr uri, String BaseURI) |
| 105 | throws ResourceResolverException { |
| 106 | int length=ResourceResolver._resolverVector.size(); |
| 107 | for (int i = 0; i < length; i++) { |
| 108 | ResourceResolver resolver = |
| 109 | (ResourceResolver) ResourceResolver._resolverVector.get(i); |
| 110 | |
| 111 | |
| 112 | if (true) |
| 113 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "check resolvability by class " + resolver.getClass().getName()); |
| 114 | |
| 115 | if ((resolver != null) && resolver.canResolve(uri, BaseURI)) { |
| 116 | return resolver; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | Object exArgs[] = { ((uri != null) |
| 121 | ? uri.getNodeValue() |
| 122 | : "null"), BaseURI }; |
| 123 | |
| 124 | throw new ResourceResolverException("utils.resolver.noClass", exArgs, |
| 125 | uri, BaseURI); |
| 126 | } |
| 127 | /** |
| 128 | * Method getInstance |
| 129 | * |
| 130 | * @param uri |
| 131 | * @param BaseURI |
| 132 | * @param individualResolvers |
| 133 | * @return the instance |
| 134 | * |
| 135 | * @throws ResourceResolverException |
| 136 | */ |
| 137 | public static final ResourceResolver getInstance( |
| 138 | Attr uri, String BaseURI, List individualResolvers) |
| 139 | throws ResourceResolverException { |
| 140 | if (true) { |
| 141 | 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()); |
| 142 | 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"); |
| 143 | } |
| 144 | |
| 145 | // first check the individual Resolvers |
| 146 | int size=0; |
| 147 | if ((individualResolvers != null) && ((size=individualResolvers.size()) > 0)) { |
| 148 | for (int i = 0; i < size; i++) { |
| 149 | ResourceResolver resolver = |
| 150 | (ResourceResolver) individualResolvers.get(i); |
| 151 | |
| 152 | if (resolver != null) { |
| 153 | String currentClass = resolver._resolverSpi.getClass().getName(); |
| 154 | if (true) |
| 155 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "check resolvability by class " + currentClass); |
| 156 | |
| 157 | if (resolver.canResolve(uri, BaseURI)) { |
| 158 | return resolver; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return getInstance(uri,BaseURI); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * The init() function is called by com.sun.org.apache.xml.internal.security.Init.init() |
| 169 | */ |
| 170 | public static void init() { |
| 171 | |
| 172 | if (!ResourceResolver._alreadyInitialized) { |
| 173 | ResourceResolver._resolverVector = new ArrayList(10); |
| 174 | _alreadyInitialized = true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Method register |
| 180 | * |
| 181 | * @param className |
| 182 | */ |
| 183 | public static void register(String className) { |
| 184 | ResourceResolver resolver = null; |
| 185 | |
| 186 | try { |
| 187 | resolver = new ResourceResolver(className); |
| 188 | ResourceResolver._resolverVector.add(resolver); |
| 189 | } catch (Exception e) { |
| 190 | // Object exArgs[] = { ((uri != null) |
| 191 | // ? uri.getNodeValue() |
| 192 | // : "null"), BaseURI }; |
| 193 | // |
| 194 | // throw new ResourceResolverException("utils.resolver.noClass", |
| 195 | // exArgs, e, uri, BaseURI); |
| 196 | log.log(java.util.logging.Level.WARNING, "Error loading resolver " + className +" disabling it"); |
| 197 | } catch (NoClassDefFoundError e) { |
| 198 | log.log(java.util.logging.Level.WARNING, "Error loading resolver " + className +" disabling it"); |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Method registerAtStart |
| 205 | * |
| 206 | * @param className |
| 207 | */ |
| 208 | public static void registerAtStart(String className) { |
| 209 | ResourceResolver._resolverVector.add(0, className); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Method resolve |
| 214 | * |
| 215 | * @param uri |
| 216 | * @param BaseURI |
| 217 | * @return the resource |
| 218 | * |
| 219 | * @throws ResourceResolverException |
| 220 | */ |
| 221 | public static XMLSignatureInput resolveStatic(Attr uri, String BaseURI) |
| 222 | throws ResourceResolverException { |
| 223 | |
| 224 | ResourceResolver myResolver = ResourceResolver.getInstance(uri, BaseURI); |
| 225 | |
| 226 | return myResolver.resolve(uri, BaseURI); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Method resolve |
| 231 | * |
| 232 | * @param uri |
| 233 | * @param BaseURI |
| 234 | * @return the resource |
| 235 | * |
| 236 | * @throws ResourceResolverException |
| 237 | */ |
| 238 | public XMLSignatureInput resolve(Attr uri, String BaseURI) |
| 239 | throws ResourceResolverException { |
| 240 | return this._resolverSpi.engineResolve(uri, BaseURI); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Method setProperty |
| 245 | * |
| 246 | * @param key |
| 247 | * @param value |
| 248 | */ |
| 249 | public void setProperty(String key, String value) { |
| 250 | this._resolverSpi.engineSetProperty(key, value); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Method getProperty |
| 255 | * |
| 256 | * @param key |
| 257 | * @return the value of the property |
| 258 | */ |
| 259 | public String getProperty(String key) { |
| 260 | return this._resolverSpi.engineGetProperty(key); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Method addProperties |
| 265 | * |
| 266 | * @param properties |
| 267 | */ |
| 268 | public void addProperties(Map properties) { |
| 269 | this._resolverSpi.engineAddProperies(properties); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Method getPropertyKeys |
| 274 | * |
| 275 | * @return all property keys. |
| 276 | */ |
| 277 | public String[] getPropertyKeys() { |
| 278 | return this._resolverSpi.engineGetPropertyKeys(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Method understandsProperty |
| 283 | * |
| 284 | * @param propertyToTest |
| 285 | * @return true if the resolver understands the property |
| 286 | */ |
| 287 | public boolean understandsProperty(String propertyToTest) { |
| 288 | return this._resolverSpi.understandsProperty(propertyToTest); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Method canResolve |
| 293 | * |
| 294 | * @param uri |
| 295 | * @param BaseURI |
| 296 | * @return true if it can resolve the uri |
| 297 | */ |
| 298 | private boolean canResolve(Attr uri, String BaseURI) { |
| 299 | return this._resolverSpi.engineCanResolve(uri, BaseURI); |
| 300 | } |
| 301 | } |