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

package org.springframework.beans.factory.xml;

19
import java.io.FileNotFoundException;
J
Juergen Hoeller 已提交
20
import java.io.IOException;
21
import java.util.Map;
22
import java.util.Properties;
23
import java.util.concurrent.ConcurrentHashMap;
24 25 26 27 28 29 30 31 32

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
33
import org.springframework.lang.Nullable;
34
import org.springframework.util.Assert;
35
import org.springframework.util.CollectionUtils;
36 37 38 39 40

/**
 * {@link EntityResolver} implementation that attempts to resolve schema URLs into
 * local {@link ClassPathResource classpath resources} using a set of mappings files.
 *
J
Juergen Hoeller 已提交
41 42 43
 * <p>By default, this class will look for mapping files in the classpath using the
 * pattern: {@code META-INF/spring.schemas} allowing for multiple files to exist on
 * the classpath at any one time.
44
 *
J
Juergen Hoeller 已提交
45 46
 * <p>The format of {@code META-INF/spring.schemas} is a properties file where each line
 * should be of the form {@code systemId=schema-location} where {@code schema-location}
J
Juergen Hoeller 已提交
47 48
 * should also be a schema file in the classpath. Since {@code systemId} is commonly a
 * URL, one must be careful to escape any ':' characters which are treated as delimiters
J
Juergen Hoeller 已提交
49
 * in properties files.
50
 *
J
Juergen Hoeller 已提交
51 52
 * <p>The pattern for the mapping files can be overridden using the
 * {@link #PluggableSchemaResolver(ClassLoader, String)} constructor.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @since 2.0
 */
public class PluggableSchemaResolver implements EntityResolver {

	/**
	 * The location of the file that defines schema mappings.
	 * Can be present in multiple JAR files.
	 */
	public static final String DEFAULT_SCHEMA_MAPPINGS_LOCATION = "META-INF/spring.schemas";


	private static final Log logger = LogFactory.getLog(PluggableSchemaResolver.class);

69
	@Nullable
70 71 72 73
	private final ClassLoader classLoader;

	private final String schemaMappingsLocation;

P
Phillip Webb 已提交
74
	/** Stores the mapping of schema URL -> local schema path. */
75
	@Nullable
76
	private volatile Map<String, String> schemaMappings;
77 78 79 80 81 82


	/**
	 * Loads the schema URL -> schema file location mappings using the default
	 * mapping file pattern "META-INF/spring.schemas".
	 * @param classLoader the ClassLoader to use for loading
83
	 * (can be {@code null}) to use the default ClassLoader)
84 85
	 * @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
	 */
86
	public PluggableSchemaResolver(@Nullable ClassLoader classLoader) {
87 88 89 90 91 92 93 94
		this.classLoader = classLoader;
		this.schemaMappingsLocation = DEFAULT_SCHEMA_MAPPINGS_LOCATION;
	}

	/**
	 * Loads the schema URL -> schema file location mappings using the given
	 * mapping file pattern.
	 * @param classLoader the ClassLoader to use for loading
95
	 * (can be {@code null}) to use the default ClassLoader)
96 97 98 99
	 * @param schemaMappingsLocation the location of the file that defines schema mappings
	 * (must not be empty)
	 * @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
	 */
100
	public PluggableSchemaResolver(@Nullable ClassLoader classLoader, String schemaMappingsLocation) {
101 102 103 104 105
		Assert.hasText(schemaMappingsLocation, "'schemaMappingsLocation' must not be empty");
		this.classLoader = classLoader;
		this.schemaMappingsLocation = schemaMappingsLocation;
	}

J
Juergen Hoeller 已提交
106

107
	@Override
108
	@Nullable
109
	public InputSource resolveEntity(@Nullable String publicId, @Nullable String systemId) throws IOException {
110 111 112 113
		if (logger.isTraceEnabled()) {
			logger.trace("Trying to resolve XML entity with public id [" + publicId +
					"] and system id [" + systemId + "]");
		}
114

115
		if (systemId != null) {
116
			String resourceLocation = getSchemaMappings().get(systemId);
117 118 119 120
			if (resourceLocation == null && systemId.startsWith("https:")) {
				// Retrieve canonical http schema mapping even for https declaration
				resourceLocation = getSchemaMappings().get("http:" + systemId.substring(6));
			}
121 122
			if (resourceLocation != null) {
				Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
123 124 125 126
				try {
					InputSource source = new InputSource(resource.getInputStream());
					source.setPublicId(publicId);
					source.setSystemId(systemId);
127 128
					if (logger.isTraceEnabled()) {
						logger.trace("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
129 130 131 132 133
					}
					return source;
				}
				catch (FileNotFoundException ex) {
					if (logger.isDebugEnabled()) {
134
						logger.debug("Could not find XML schema [" + systemId + "]: " + resource, ex);
135
					}
136 137 138
				}
			}
		}
J
Juergen Hoeller 已提交
139 140

		// Fall back to the parser's default behavior.
141 142 143
		return null;
	}

144 145 146 147
	/**
	 * Load the specified schema mappings lazily.
	 */
	private Map<String, String> getSchemaMappings() {
148 149
		Map<String, String> schemaMappings = this.schemaMappings;
		if (schemaMappings == null) {
150
			synchronized (this) {
151 152
				schemaMappings = this.schemaMappings;
				if (schemaMappings == null) {
153 154
					if (logger.isTraceEnabled()) {
						logger.trace("Loading schema mappings from [" + this.schemaMappingsLocation + "]");
155 156 157 158
					}
					try {
						Properties mappings =
								PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation, this.classLoader);
159 160
						if (logger.isTraceEnabled()) {
							logger.trace("Loaded schema mappings: " + mappings);
161
						}
J
Juergen Hoeller 已提交
162 163
						schemaMappings = new ConcurrentHashMap<>(mappings.size());
						CollectionUtils.mergePropertiesIntoMap(mappings, schemaMappings);
164 165 166 167 168 169
						this.schemaMappings = schemaMappings;
					}
					catch (IOException ex) {
						throw new IllegalStateException(
								"Unable to load schema mappings from location [" + this.schemaMappingsLocation + "]", ex);
					}
170 171 172
				}
			}
		}
173
		return schemaMappings;
174 175 176 177 178
	}


	@Override
	public String toString() {
J
Juergen Hoeller 已提交
179
		return "EntityResolver using schema mappings " + getSchemaMappings();
180 181 182
	}

}