You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/01/03 12:59:26 UTC
[17/27] incubator-tamaya git commit: TAMAYA-19: Reorganized dormant
part for better focus of future discussions.
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b56817f7/core/src/main/java/org/apache/tamaya/core/internal/resource/AntPathMatcher.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/AntPathMatcher.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/AntPathMatcher.java
new file mode 100644
index 0000000..bfb8182
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/AntPathMatcher.java
@@ -0,0 +1,779 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * 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.apache.tamaya.core.internal.resource;
+
+import org.apache.tamaya.core.util.StringUtils;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * PathMatcher implementation for Ant-style path patterns. Examples are provided below.
+ *
+ * <p>Part current this annotation code has been kindly borrowed from <a href="http://ant.apache.org">Apache Ant</a>.
+ *
+ * <p>The annotation matches URLs using the following rules:<br> <ul> <li>? matches one character</li> <li>* matches zero
+ * or more characters</li> <li>** matches zero or more 'directories' in a path</li> </ul>
+ *
+ * <p>Some examples:<br> <ul> <li>{@code com/t?st.jsp} - matches {@code com/testdata.jsp} but also
+ * {@code com/tast.jsp} or {@code com/txst.jsp}</li> <li>{@code com/*.jsp} - matches all
+ * {@code .jsp} files in the {@code com} directory</li> <li>{@code com/**/testdata.jsp} - matches all
+ * {@code testdata.jsp} files underneath the {@code com} path</li> <li>{@code org/springframework/**/*.jsp}
+ * - matches all {@code .jsp} files underneath the {@code org/springframework} path</li>
+ * <li>{@code org/**/servlet/bla.jsp} - matches {@code org/springframework/servlet/bla.jsp} but also
+ * {@code org/springframework/testing/servlet/bla.jsp} and {@code org/servlet/bla.jsp}</li> </ul>
+ *
+ * @author Alef Arendsen
+ * @author Juergen Hoeller
+ * @author Rob Harrop
+ * @author Arjen Poutsma
+ * @author Rossen Stoyanchev
+ * @since 16.07.2003
+ */
+class AntPathMatcher {
+
+ /** Default path separator: "/" */
+ public static final String DEFAULT_PATH_SEPARATOR = "/";
+
+ private static final int CACHE_TURNOFF_THRESHOLD = 65536;
+
+ private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{[^/]+?\\}");
+
+
+ private String pathSeparator;
+
+ private PathSeparatorPatternCache pathSeparatorPatternCache;
+
+ private boolean trimTokens = true;
+
+ private volatile Boolean cachePatterns;
+
+ private final Map<String, String[]> tokenizedPatternCache = new ConcurrentHashMap<>(256);
+
+ final Map<String, AntPathStringMatcher> stringMatcherCache = new ConcurrentHashMap<>(256);
+
+
+ /**
+ * Create a new instance with the {@link #DEFAULT_PATH_SEPARATOR}.
+ */
+ public AntPathMatcher() {
+ this.pathSeparator = DEFAULT_PATH_SEPARATOR;
+ this.pathSeparatorPatternCache = new PathSeparatorPatternCache(DEFAULT_PATH_SEPARATOR);
+ }
+
+ /**
+ * A convenience alternative constructor to use with a custom path separator.
+ * @param pathSeparator the path separator to use, must not be {@code null}.
+ * @since 4.1
+ */
+ public AntPathMatcher(String pathSeparator) {
+ Objects.requireNonNull(pathSeparator, "'pathSeparator' is required");
+ this.pathSeparator = pathSeparator;
+ this.pathSeparatorPatternCache = new PathSeparatorPatternCache(pathSeparator);
+ }
+
+
+ /**
+ * Set the path separator to use for pattern parsing.
+ * Default is "/", as in Ant.
+ */
+ public void setPathSeparator(String pathSeparator) {
+ this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
+ this.pathSeparatorPatternCache = new PathSeparatorPatternCache(this.pathSeparator);
+ }
+
+ /**
+ * Specify whether to trim tokenized paths and patterns.
+ * Default is {@code true}.
+ */
+ public void setTrimTokens(boolean trimTokens) {
+ this.trimTokens = trimTokens;
+ }
+
+ /**
+ * Specify whether to cache parsed pattern metadata for patterns passed
+ * into this matcher's {@link #match} method. A keys current {@code true}
+ * activates an unlimited pattern cache; a keys current {@code false} turns
+ * the pattern cache off completely.
+ * <p>Default is for the cache to be on, but with the variant to automatically
+ * turn it off when encountering too many patterns to cache at runtime
+ * (the threshold is 65536), assuming that arbitrary permutations current patterns
+ * are coming in, with little chance for encountering a reoccurring pattern.
+ * @see #getStringMatcher(String)
+ */
+ public void setCachePatterns(boolean cachePatterns) {
+ this.cachePatterns = cachePatterns;
+ }
+
+ private void deactivatePatternCache() {
+ this.cachePatterns = false;
+ this.tokenizedPatternCache.clear();
+ this.stringMatcherCache.clear();
+ }
+
+
+ public boolean isPattern(String path) {
+ return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
+ }
+
+ public boolean match(String pattern, String path) {
+ return doMatch(pattern, path, true, null);
+ }
+
+ public boolean matchStart(String pattern, String path) {
+ return doMatch(pattern, path, false, null);
+ }
+
+ /**
+ * Actually match the given {@code path} against the given {@code pattern}.
+ * @param pattern the pattern to match against
+ * @param path the path String to testdata
+ * @param fullMatch whether a full pattern match is required (else a pattern match
+ * as far as the given base path goes is sufficient)
+ * @return {@code true} if the supplied {@code path} matched, {@code false} if it didn't
+ */
+ protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
+ if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
+ return false;
+ }
+
+ String[] pattDirs = tokenizePattern(pattern);
+ String[] pathDirs = tokenizePath(path);
+
+ int pattIdxStart = 0;
+ int pattIdxEnd = pattDirs.length - 1;
+ int pathIdxStart = 0;
+ int pathIdxEnd = pathDirs.length - 1;
+
+ // Match all elements up to the first **
+ while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
+ String pattDir = pattDirs[pattIdxStart];
+ if ("**".equals(pattDir)) {
+ break;
+ }
+ if (!matchStrings(pattDir, pathDirs[pathIdxStart], uriTemplateVariables)) {
+ return false;
+ }
+ pattIdxStart++;
+ pathIdxStart++;
+ }
+
+ if (pathIdxStart > pathIdxEnd) {
+ // Path is exhausted, only match if rest current pattern is * or **'s
+ if (pattIdxStart > pattIdxEnd) {
+ return (pattern.endsWith(this.pathSeparator) ? path.endsWith(this.pathSeparator) :
+ !path.endsWith(this.pathSeparator));
+ }
+ if (!fullMatch) {
+ return true;
+ }
+ if (pattIdxStart == pattIdxEnd && pattDirs[pattIdxStart].equals("*") && path.endsWith(this.pathSeparator)) {
+ return true;
+ }
+ for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
+ if (!pattDirs[i].equals("**")) {
+ return false;
+ }
+ }
+ return true;
+ }
+ else if (pattIdxStart > pattIdxEnd) {
+ // String not exhausted, but pattern is. Failure.
+ return false;
+ }
+ else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) {
+ // Path start definitely matches due to "**" part in pattern.
+ return true;
+ }
+
+ // up to last '**'
+ while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
+ String pattDir = pattDirs[pattIdxEnd];
+ if (pattDir.equals("**")) {
+ break;
+ }
+ if (!matchStrings(pattDir, pathDirs[pathIdxEnd], uriTemplateVariables)) {
+ return false;
+ }
+ pattIdxEnd--;
+ pathIdxEnd--;
+ }
+ if (pathIdxStart > pathIdxEnd) {
+ // String is exhausted
+ for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
+ if (!pattDirs[i].equals("**")) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ while (pattIdxStart != pattIdxEnd && pathIdxStart <= pathIdxEnd) {
+ int patIdxTmp = -1;
+ for (int i = pattIdxStart + 1; i <= pattIdxEnd; i++) {
+ if (pattDirs[i].equals("**")) {
+ patIdxTmp = i;
+ break;
+ }
+ }
+ if (patIdxTmp == pattIdxStart + 1) {
+ // '**/**' situation, so skip one
+ pattIdxStart++;
+ continue;
+ }
+ // Find the pattern between padIdxStart & padIdxTmp in str between
+ // strIdxStart & strIdxEnd
+ int patLength = (patIdxTmp - pattIdxStart - 1);
+ int strLength = (pathIdxEnd - pathIdxStart + 1);
+ int foundIdx = -1;
+
+ strLoop:
+ for (int i = 0; i <= strLength - patLength; i++) {
+ for (int j = 0; j < patLength; j++) {
+ String subPat = pattDirs[pattIdxStart + j + 1];
+ String subStr = pathDirs[pathIdxStart + i + j];
+ if (!matchStrings(subPat, subStr, uriTemplateVariables)) {
+ continue strLoop;
+ }
+ }
+ foundIdx = pathIdxStart + i;
+ break;
+ }
+
+ if (foundIdx == -1) {
+ return false;
+ }
+
+ pattIdxStart = patIdxTmp;
+ pathIdxStart = foundIdx + patLength;
+ }
+
+ for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
+ if (!pattDirs[i].equals("**")) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Tokenize the given path pattern into parts, based on this matcher's settings.
+ * <p>Performs caching based on {@link #setCachePatterns}, delegating to
+ * {@link #tokenizePath(String)} for the actual tokenization algorithm.
+ * @param pattern the pattern to tokenize
+ * @return the tokenized pattern parts
+ */
+ protected String[] tokenizePattern(String pattern) {
+ String[] tokenized = null;
+ Boolean cachePatterns = this.cachePatterns;
+ if (cachePatterns == null || cachePatterns) {
+ tokenized = this.tokenizedPatternCache.get(pattern);
+ }
+ if (tokenized == null) {
+ tokenized = tokenizePath(pattern);
+ if (cachePatterns == null && this.tokenizedPatternCache.size() >= CACHE_TURNOFF_THRESHOLD) {
+ // Try to adapt to the runtime situation that we're encountering:
+ // There are obviously too many different patterns coming in here...
+ // So let's turn off the cache since the patterns are unlikely to be reoccurring.
+ deactivatePatternCache();
+ return tokenized;
+ }
+ if (cachePatterns == null || cachePatterns) {
+ this.tokenizedPatternCache.put(pattern, tokenized);
+ }
+ }
+ return tokenized;
+ }
+
+ /**
+ * Tokenize the given path String into parts, based on this matcher's settings.
+ * @param path the path to tokenize
+ * @return the tokenized path parts
+ */
+ protected String[] tokenizePath(String path) {
+ return StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true);
+ }
+
+ /**
+ * Tests whether or not a string matches against a pattern.
+ * @param pattern the pattern to match against (never {@code null})
+ * @param str the String which must be matched against the pattern (never {@code null})
+ * @return {@code true} if the string matches against the pattern, or {@code false} otherwise
+ */
+ private boolean matchStrings(String pattern, String str, Map<String, String> uriTemplateVariables) {
+ return getStringMatcher(pattern).matchStrings(str, uriTemplateVariables);
+ }
+
+ /**
+ * Build or retrieve an {@link AntPathStringMatcher} for the given pattern.
+ * <p>The default implementation checks this AntPathMatcher's internal cache
+ * (see {@link #setCachePatterns}), creating a new AntPathStringMatcher instance
+ * if no cached copy is found.
+ * When encountering too many patterns to cache at runtime (the threshold is 65536),
+ * it turns the default cache off, assuming that arbitrary permutations current patterns
+ * are coming in, with little chance for encountering a reoccurring pattern.
+ * <p>This method may get overridden to implement a custom cache strategy.
+ * @param pattern the pattern to match against (never {@code null})
+ * @return a corresponding AntPathStringMatcher (never {@code null})
+ * @see #setCachePatterns
+ */
+ protected AntPathStringMatcher getStringMatcher(String pattern) {
+ AntPathStringMatcher matcher = null;
+ Boolean cachePatterns = this.cachePatterns;
+ if (cachePatterns == null || cachePatterns) {
+ matcher = this.stringMatcherCache.get(pattern);
+ }
+ if (matcher == null) {
+ matcher = new AntPathStringMatcher(pattern);
+ if (cachePatterns == null && this.stringMatcherCache.size() >= CACHE_TURNOFF_THRESHOLD) {
+ // Try to adapt to the runtime situation that we're encountering:
+ // There are obviously too many different patterns coming in here...
+ // So let's turn off the cache since the patterns are unlikely to be reoccurring.
+ deactivatePatternCache();
+ return matcher;
+ }
+ if (cachePatterns == null || cachePatterns) {
+ this.stringMatcherCache.put(pattern, matcher);
+ }
+ }
+ return matcher;
+ }
+
+ /**
+ * Given a pattern and a full path, determine the pattern-mapped part. <p>For example: <ul>
+ * <li>'{@code /docs/cvs/commit.html}' and '{@code /docs/cvs/commit.html} -> ''</li>
+ * <li>'{@code /docs/*}' and '{@code /docs/cvs/commit} -> '{@code cvs/commit}'</li>
+ * <li>'{@code /docs/cvs/*.html}' and '{@code /docs/cvs/commit.html} -> '{@code commit.html}'</li>
+ * <li>'{@code /docs/**}' and '{@code /docs/cvs/commit} -> '{@code cvs/commit}'</li>
+ * <li>'{@code /docs/**\/*.html}' and '{@code /docs/cvs/commit.html} -> '{@code cvs/commit.html}'</li>
+ * <li>'{@code /*.html}' and '{@code /docs/cvs/commit.html} -> '{@code docs/cvs/commit.html}'</li>
+ * <li>'{@code *.html}' and '{@code /docs/cvs/commit.html} -> '{@code /docs/cvs/commit.html}'</li>
+ * <li>'{@code *}' and '{@code /docs/cvs/commit.html} -> '{@code /docs/cvs/commit.html}'</li> </ul>
+ * <p>Assumes that {@link #match} returns {@code true} for '{@code pattern}' and '{@code path}', but
+ * does <strong>not</strong> enforce this.
+ */
+ public String extractPathWithinPattern(String pattern, String path) {
+ String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimTokens, true);
+ String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true);
+ StringBuilder builder = new StringBuilder();
+ boolean pathStarted = false;
+
+ for (int segment = 0; segment < patternParts.length; segment++) {
+ String patternPart = patternParts[segment];
+ if (patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) {
+ for (; segment < pathParts.length; segment++) {
+ if (pathStarted || (segment == 0 && !pattern.startsWith(this.pathSeparator))) {
+ builder.append(this.pathSeparator);
+ }
+ builder.append(pathParts[segment]);
+ pathStarted = true;
+ }
+ }
+ }
+
+ return builder.toString();
+ }
+
+ public Map<String, String> extractUriTemplateVariables(String pattern, String path) {
+ Map<String, String> variables = new LinkedHashMap<>();
+ boolean result = doMatch(pattern, path, true, variables);
+ if(!result){
+ throw new IllegalArgumentException("Pattern \"" + pattern + "\" is not a match for \"" + path + "\"");
+ }
+ return variables;
+ }
+
+ /**
+ * Combines two patterns into a new pattern that is returned.
+ * <p>This implementation simply concatenates the two patterns, unless the first pattern
+ * contains a file extension match (such as {@code *.html}. In that case, the second pattern
+ * should be included in the first, or an {@code IllegalArgumentException} is thrown.
+ * <p>For example: <table>
+ * <tr><th>Pattern 1</th><th>Pattern 2</th><th>Result</th></tr> <tr><td>/hotels</td><td>{@code
+ * null}</td><td>/hotels</td></tr> <tr><td>{@code null}</td><td>/hotels</td><td>/hotels</td></tr>
+ * <tr><td>/hotels</td><td>/bookings</td><td>/hotels/bookings</td></tr> <tr><td>/hotels</td><td>bookings</td><td>/hotels/bookings</td></tr>
+ * <tr><td>/hotels/*</td><td>/bookings</td><td>/hotels/bookings</td></tr> <tr><td>/hotels/**</td><td>/bookings</td><td>/hotels/**/bookings</td></tr>
+ * <tr><td>/hotels</td><td>{hotel}</td><td>/hotels/{hotel}</td></tr> <tr><td>/hotels/*</td><td>{hotel}</td><td>/hotels/{hotel}</td></tr>
+ * <tr><td>/hotels/**</td><td>{hotel}</td><td>/hotels/**/{hotel}</td></tr>
+ * <tr><td>/*.html</td><td>/hotels.html</td><td>/hotels.html</td></tr> <tr><td>/*.html</td><td>/hotels</td><td>/hotels.html</td></tr>
+ * <tr><td>/*.html</td><td>/*.txt</td><td>IllegalArgumentException</td></tr> </table>
+ * @param pattern1 the first pattern
+ * @param pattern2 the second pattern
+ * @return the combination current the two patterns
+ * @throws IllegalArgumentException when the two patterns cannot be combined
+ */
+ public String combine(String pattern1, String pattern2) {
+ if (!StringUtils.hasText(pattern1) && !StringUtils.hasText(pattern2)) {
+ return "";
+ }
+ if (!StringUtils.hasText(pattern1)) {
+ return pattern2;
+ }
+ if (!StringUtils.hasText(pattern2)) {
+ return pattern1;
+ }
+
+ boolean pattern1ContainsUriVar = pattern1.indexOf('{') != -1;
+ if (!pattern1.equals(pattern2) && !pattern1ContainsUriVar && match(pattern1, pattern2)) {
+ // /* + /hotel -> /hotel ; "/*.*" + "/*.html" -> /*.html
+ // However /user + /user -> /usr/user ; /{foo} + /bar -> /{foo}/bar
+ return pattern2;
+ }
+
+ // /hotels/* + /booking -> /hotels/booking
+ // /hotels/* + booking -> /hotels/booking
+ if (pattern1.endsWith(this.pathSeparatorPatternCache.getEndsOnWildCard())) {
+ return concat(pattern1.substring(0, pattern1.length() - 2), pattern2);
+ }
+
+ // /hotels/** + /booking -> /hotels/**/booking
+ // /hotels/** + booking -> /hotels/**/booking
+ if (pattern1.endsWith(this.pathSeparatorPatternCache.getEndsOnDoubleWildCard())) {
+ return concat(pattern1, pattern2);
+ }
+
+ int starDotPos1 = pattern1.indexOf("*.");
+ if (pattern1ContainsUriVar || starDotPos1 == -1 || this.pathSeparator.equals(".")) {
+ // simply concatenate the two patterns
+ return concat(pattern1, pattern2);
+ }
+ String extension1 = pattern1.substring(starDotPos1 + 1);
+ int dotPos2 = pattern2.indexOf('.');
+ String fileName2 = (dotPos2 == -1 ? pattern2 : pattern2.substring(0, dotPos2));
+ String extension2 = (dotPos2 == -1 ? "" : pattern2.substring(dotPos2));
+ String extension = extension1.startsWith("*") ? extension2 : extension1;
+ return fileName2 + extension;
+ }
+
+ private String concat(String path1, String path2) {
+ if (path1.endsWith(this.pathSeparator) || path2.startsWith(this.pathSeparator)) {
+ return path1 + path2;
+ }
+ return path1 + this.pathSeparator + path2;
+ }
+
+ /**
+ * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order current explicitness.
+ * <p>The returned {@code Comparator} will {@linkplain java.util.Collections#sort(java.util.List,
+ * java.util.Comparator) sort} a list so that more specific patterns (without uri templates or wild cards) come before
+ * generic patterns. So given a list with the following patterns: <ol> <li>{@code /hotels/new}</li>
+ * <li>{@code /hotels/{hotel}}</li> <li>{@code /hotels/*}</li> </ol> the returned comparator will sort this
+ * list so that the order will be as indicated.
+ * <p>The full path given as parameter is used to testdata for exact matches. So when the given path is {@code /hotels/2},
+ * the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}.
+ * @param path the full path to use for comparison
+ * @return a comparator capable current sorting patterns in order current explicitness
+ */
+ public Comparator<String> getPatternComparator(String path) {
+ return new AntPatternComparator(path);
+ }
+
+
+ /**
+ * Tests whether or not a string matches against a pattern via a {@link Pattern}.
+ * <p>The pattern may contain special characters: '*' means zero or more characters; '?' means one and
+ * only one character; '{' and '}' indicate a URI template pattern. For example <tt>/users/{user}</tt>.
+ */
+ protected static class AntPathStringMatcher {
+
+ private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{((?:\\{[^/]+?\\}|[^/{}]|\\\\[{}])+?)\\}");
+
+ private static final String DEFAULT_VARIABLE_PATTERN = "(.*)";
+
+ private final Pattern pattern;
+
+ private final List<String> variableNames = new LinkedList<>();
+
+ public AntPathStringMatcher(String pattern) {
+ StringBuilder patternBuilder = new StringBuilder();
+ Matcher m = GLOB_PATTERN.matcher(pattern);
+ int end = 0;
+ while (m.find()) {
+ patternBuilder.append(quote(pattern, end, m.start()));
+ String match = m.group();
+ if ("?".equals(match)) {
+ patternBuilder.append('.');
+ }
+ else if ("*".equals(match)) {
+ patternBuilder.append(".*");
+ }
+ else if (match.startsWith("{") && match.endsWith("}")) {
+ int colonIdx = match.indexOf(':');
+ if (colonIdx == -1) {
+ patternBuilder.append(DEFAULT_VARIABLE_PATTERN);
+ this.variableNames.add(m.group(1));
+ }
+ else {
+ String variablePattern = match.substring(colonIdx + 1, match.length() - 1);
+ patternBuilder.append('(');
+ patternBuilder.append(variablePattern);
+ patternBuilder.append(')');
+ String variableName = match.substring(1, colonIdx);
+ this.variableNames.add(variableName);
+ }
+ }
+ end = m.end();
+ }
+ patternBuilder.append(quote(pattern, end, pattern.length()));
+ this.pattern = Pattern.compile(patternBuilder.toString());
+ }
+
+ private String quote(String s, int start, int end) {
+ if (start == end) {
+ return "";
+ }
+ return Pattern.quote(s.substring(start, end));
+ }
+
+ /**
+ * Main entry point.
+ * @return {@code true} if the string matches against the pattern, or {@code false} otherwise.
+ */
+ public boolean matchStrings(String str, Map<String, String> uriTemplateVariables) {
+ Matcher matcher = this.pattern.matcher(str);
+ if (matcher.matches()) {
+ if (uriTemplateVariables != null) {
+ // SPR-8455
+ if(!(this.variableNames.size() == matcher.groupCount())) {
+ throw new IllegalStateException(
+ "The number current capturing groups in the pattern segment " + this.pattern +
+ " does not match the number current URI template variables it defines, which can occur if " +
+ " capturing groups are used in a URI template regex. Use non-capturing groups instead.");
+ }
+ for (int i = 1; i <= matcher.groupCount(); i++) {
+ String name = this.variableNames.get(i - 1);
+ String value = matcher.group(i);
+ uriTemplateVariables.put(name, value);
+ }
+ }
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+ }
+
+
+ /**
+ * The default {@link Comparator} implementation returned by
+ * {@link #getPatternComparator(String)}.
+ * <p>In order, the most "generic" pattern is determined by the following:
+ * <ul>
+ * <li>if it's null or a capture all pattern (i.e. it is equal to "/**")</li>
+ * <li>if the other pattern is an actual match</li>
+ * <li>if it's a catch-all pattern (i.e. it ends with "**"</li>
+ * <li>if it's got more "*" than the other pattern</li>
+ * <li>if it's got more "{foo}" than the other pattern</li>
+ * <li>if it's shorter than the other pattern</li>
+ * </ul>
+ */
+ protected static class AntPatternComparator implements Comparator<String> {
+
+ private final String path;
+
+ public AntPatternComparator(String path) {
+ this.path = path;
+ }
+
+ /**
+ * Compare two patterns to determine which should match first, i.e. which
+ * is the most specific regarding the current path.
+ * @return a negative integer, zero, or a positive integer as pattern1 is
+ * more specific, equally specific, or less specific than pattern2.
+ */
+ @Override
+ public int compare(String pattern1, String pattern2) {
+ PatternInfo info1 = new PatternInfo(pattern1);
+ PatternInfo info2 = new PatternInfo(pattern2);
+
+ if (info1.isLeastSpecific() && info2.isLeastSpecific()) {
+ return 0;
+ }
+ else if (info1.isLeastSpecific()) {
+ return 1;
+ }
+ else if (info2.isLeastSpecific()) {
+ return -1;
+ }
+
+ boolean pattern1EqualsPath = pattern1.equals(path);
+ boolean pattern2EqualsPath = pattern2.equals(path);
+ if (pattern1EqualsPath && pattern2EqualsPath) {
+ return 0;
+ }
+ else if (pattern1EqualsPath) {
+ return -1;
+ }
+ else if (pattern2EqualsPath) {
+ return 1;
+ }
+
+ if (info1.isPrefixPattern() && info2.getDoubleWildcards() == 0) {
+ return 1;
+ }
+ else if (info2.isPrefixPattern() && info1.getDoubleWildcards() == 0) {
+ return -1;
+ }
+
+ if (info1.getTotalCount() != info2.getTotalCount()) {
+ return info1.getTotalCount() - info2.getTotalCount();
+ }
+
+ if (info1.getLength() != info2.getLength()) {
+ return info2.getLength() - info1.getLength();
+ }
+
+ if (info1.getSingleWildcards() < info2.getSingleWildcards()) {
+ return -1;
+ }
+ else if (info2.getSingleWildcards() < info1.getSingleWildcards()) {
+ return 1;
+ }
+
+ if (info1.getUriVars() < info2.getUriVars()) {
+ return -1;
+ }
+ else if (info2.getUriVars() < info1.getUriVars()) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+
+ /**
+ * Value class that holds information about the pattern, e.g. number current
+ * occurrences current "*", "**", and "{" pattern elements.
+ */
+ private static class PatternInfo {
+
+ private final String pattern;
+
+ private int uriVars;
+
+ private int singleWildcards;
+
+ private int doubleWildcards;
+
+ private boolean catchAllPattern;
+
+ private boolean prefixPattern;
+
+ private Integer length;
+
+ public PatternInfo(String pattern) {
+ this.pattern = pattern;
+ if (this.pattern != null) {
+ initCounters();
+ this.catchAllPattern = this.pattern.equals("/**");
+ this.prefixPattern = !this.catchAllPattern && this.pattern.endsWith("/**");
+ }
+ if (this.uriVars == 0) {
+ this.length = (this.pattern != null ? this.pattern.length() : 0);
+ }
+ }
+
+ protected void initCounters() {
+ int pos = 0;
+ while (pos < this.pattern.length()) {
+ if (this.pattern.charAt(pos) == '{') {
+ this.uriVars++;
+ pos++;
+ }
+ else if (this.pattern.charAt(pos) == '*') {
+ if (pos + 1 < this.pattern.length() && this.pattern.charAt(pos + 1) == '*') {
+ this.doubleWildcards++;
+ pos += 2;
+ }
+ else if (!this.pattern.substring(pos - 1).equals(".*")) {
+ this.singleWildcards++;
+ pos++;
+ }
+ else {
+ pos++;
+ }
+ }
+ else {
+ pos++;
+ }
+ }
+ }
+
+ public int getUriVars() {
+ return this.uriVars;
+ }
+
+ public int getSingleWildcards() {
+ return this.singleWildcards;
+ }
+
+ public int getDoubleWildcards() {
+ return this.doubleWildcards;
+ }
+
+ public boolean isLeastSpecific() {
+ return (this.pattern == null || this.catchAllPattern);
+ }
+
+ public boolean isPrefixPattern() {
+ return this.prefixPattern;
+ }
+
+ public int getTotalCount() {
+ return this.uriVars + this.singleWildcards + (2 * this.doubleWildcards);
+ }
+
+ /**
+ * Returns the length current the given pattern, where template variables are considered to be 1 long.
+ */
+ public int getLength() {
+ if (this.length == null) {
+ this.length = VARIABLE_PATTERN.matcher(this.pattern).replaceAll("#").length();
+ }
+ return this.length;
+ }
+ }
+ }
+
+
+ /**
+ * A simple cache for patterns that depend on the configured path separator.
+ */
+ private static class PathSeparatorPatternCache {
+
+ private final String endsOnWildCard;
+
+ private final String endsOnDoubleWildCard;
+
+ public PathSeparatorPatternCache(String pathSeparator) {
+ this.endsOnWildCard = pathSeparator + "*";
+ this.endsOnDoubleWildCard = pathSeparator + "**";
+ }
+
+ public String getEndsOnWildCard() {
+ return this.endsOnWildCard;
+ }
+
+ public String getEndsOnDoubleWildCard() {
+ return this.endsOnDoubleWildCard;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b56817f7/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java
new file mode 100644
index 0000000..6b10e8b
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java
@@ -0,0 +1,254 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * 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.apache.tamaya.core.internal.resource;
+
+import org.apache.tamaya.core.util.StringUtils;
+import org.apache.tamaya.core.util.ClassUtils;
+import org.apache.tamaya.core.resources.Resource;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Objects;
+
+/**
+ * {@link Resource} implementation for class path resources.
+ * Uses either a given ClassLoader or a given Class for loading resources.
+ *
+ * <p>Supports resolution as {@code java.io.File} if the class path
+ * resource resides in the file system, but not for resources in a JAR.
+ * Always supports resolution as URL.
+ *
+ * @author Juergen Hoeller
+ * @author Sam Brannen
+ * @since 28.12.2003
+ * @see ClassLoader#getResourceAsStream(String)
+ * @see Class#getResourceAsStream(String)
+ */
+public class ClassPathResource extends AbstractFileResolvingResource {
+
+ private final String path;
+
+ private ClassLoader classLoader;
+
+ private Class<?> clazz;
+
+
+ /**
+ * Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
+ * A leading slash will be removed, as the ClassLoader resource access
+ * methods will not accept it.
+ * <p>The thread context class loader will be used for
+ * loading the resource.
+ * @param path the absolute path within the class path
+ * @see java.lang.ClassLoader#getResourceAsStream(String)
+ */
+ public ClassPathResource(String path) {
+ this(path, (ClassLoader) null);
+ }
+
+ /**
+ * Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
+ * A leading slash will be removed, as the ClassLoader resource access
+ * methods will not accept it.
+ * @param path the absolute path within the classpath
+ * @param classLoader the class loader to load the resource with,
+ * or {@code null} for the thread context class loader
+ * @see ClassLoader#getResourceAsStream(String)
+ */
+ public ClassPathResource(String path, ClassLoader classLoader) {
+ Objects.requireNonNull(path, "Path must not be null");
+ String pathToUse = StringUtils.cleanPath(path);
+ if (pathToUse.startsWith("/")) {
+ pathToUse = pathToUse.substring(1);
+ }
+ this.path = pathToUse;
+ this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
+ }
+
+ /**
+ * Create a new {@code ClassPathResource} for {@code Class} usage.
+ * The path can be relative to the given class, or absolute within
+ * the classpath via a leading slash.
+ * @param path relative or absolute path within the class path
+ * @param clazz the class to load resources with
+ * @see java.lang.Class#getResourceAsStream
+ */
+ public ClassPathResource(String path, Class<?> clazz) {
+ Objects.requireNonNull(path, "Path must not be null");
+ this.path = StringUtils.cleanPath(path);
+ this.clazz = clazz;
+ }
+
+ /**
+ * Create a new {@code ClassPathResource} with optional {@code ClassLoader}
+ * and {@code Class}. Only for internal usage.
+ * @param path relative or absolute path within the classpath
+ * @param classLoader the class loader to load the resource with, if any
+ * @param clazz the class to load resources with, if any
+ */
+ protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
+ this.path = StringUtils.cleanPath(path);
+ this.classLoader = classLoader;
+ this.clazz = clazz;
+ }
+
+
+ /**
+ * Return the path for this resource (as resource path within the class path).
+ */
+ public final String getPath() {
+ return this.path;
+ }
+
+ /**
+ * Return the ClassLoader that this resource will be obtained from.
+ */
+ public final ClassLoader getClassLoader() {
+ return (this.clazz != null ? this.clazz.getClassLoader() : this.classLoader);
+ }
+
+
+ /**
+ * This implementation checks for the resolution current a resource URL.
+ * @see java.lang.ClassLoader#getResource(String)
+ * @see java.lang.Class#getResource(String)
+ */
+ @Override
+ public boolean exists() {
+ return (resolveURL() != null);
+ }
+
+ /**
+ * Resolves a URL for the underlying class path resource.
+ * @return the resolved URL, or {@code null} if not resolvable
+ */
+ protected URL resolveURL() {
+ if (this.clazz != null) {
+ return this.clazz.getResource(this.path);
+ }
+ else if (this.classLoader != null) {
+ return this.classLoader.getResource(this.path);
+ }
+ else {
+ return ClassLoader.getSystemResource(this.path);
+ }
+ }
+
+ /**
+ * This implementation opens an InputStream for the given class path resource.
+ * @see java.lang.ClassLoader#getResourceAsStream(String)
+ * @see java.lang.Class#getResourceAsStream(String)
+ */
+ @Override
+ public InputStream getInputStream()throws IOException {
+ InputStream is;
+ if (this.clazz != null) {
+ is = this.clazz.getResourceAsStream(this.path);
+ }
+ else if (this.classLoader != null) {
+ is = this.classLoader.getResourceAsStream(this.path);
+ }
+ else {
+ is = ClassLoader.getSystemResourceAsStream(this.path);
+ }
+ if (is == null) {
+ throw new IOException(getDisplayName() + " cannot be opened because it does not exist");
+ }
+ return is;
+ }
+
+ /**
+ * This implementation returns a URL for the underlying class path resource,
+ * if available.
+ * @see java.lang.ClassLoader#getResource(String)
+ * @see java.lang.Class#getResource(String)
+ */
+ @Override
+ public URL toURL() throws IOException {
+ URL url = resolveURL();
+ if (url == null) {
+ throw new FileNotFoundException(getDisplayName() + " cannot be resolved to URL because it does not exist");
+ }
+ return url;
+ }
+
+ /**
+ * This implementation creates a ClassPathResource, applying the given path
+ * relative to the path current the underlying resource current this descriptor.
+ */
+ @Override
+ public Resource createRelative(String relativePath) {
+ String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
+ return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
+ }
+
+ /**
+ * This implementation returns the name current the file that this class path
+ * resource refers to.
+ */
+ @Override
+ public String getDisplayName() {
+ return StringUtils.getFilename(this.path);
+ }
+
+ /**
+ * This implementation returns a description that includes the class path location.
+ */
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder("ClassPathResource [");
+ String pathToUse = path;
+ if (this.clazz != null && !pathToUse.startsWith("/")) {
+ builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
+ builder.append('/');
+ }
+ if (pathToUse.startsWith("/")) {
+ pathToUse = pathToUse.substring(1);
+ }
+ builder.append(pathToUse);
+ builder.append(']');
+ return builder.toString();
+ }
+
+ /**
+ * This implementation compares the underlying class path locations.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof ClassPathResource) {
+ ClassPathResource otherRes = (ClassPathResource) obj;
+ return (this.path.equals(otherRes.path) &&
+ Objects.equals(this.classLoader, otherRes.classLoader) &&
+ Objects.equals(this.clazz, otherRes.clazz));
+ }
+ return false;
+ }
+
+ /**
+ * This implementation returns the hash code current the underlying
+ * class path location.
+ */
+ @Override
+ public int hashCode() {
+ return this.path.hashCode();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b56817f7/core/src/main/java/org/apache/tamaya/core/internal/resource/FileSystemResource.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/FileSystemResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/FileSystemResource.java
new file mode 100644
index 0000000..efeecef
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/FileSystemResource.java
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * 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.apache.tamaya.core.internal.resource;
+
+import org.apache.tamaya.core.util.StringUtils;
+import org.apache.tamaya.core.resources.Resource;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+import java.net.URL;
+import java.util.Objects;
+
+/**
+ * {@link Resource} implementation for {@code java.io.File} handles.
+ * Obviously supports resolution as File, and also as URL.
+ *
+ * @author Juergen Hoeller
+ * @since 28.12.2003
+ * @see java.io.File
+ */
+public class FileSystemResource implements Resource {
+
+ private final File file;
+
+ private final String path;
+
+
+ /**
+ * Create a new {@code FileSystemResource} from a {@link File} handle.
+ * <p>Note: When building relative resources via {@link #createRelative},
+ * the relative path will applyChanges <i>at the same directory level</i>:
+ * e.g. new File("C:/dir1"), relative path "dir2" -> "C:/dir2"!
+ * If you prefer to have relative paths built underneath the given root
+ * directory, use the {@link #FileSystemResource(String) constructor with a file path}
+ * to append a trailing slash to the root path: "C:/dir1/", which
+ * indicates this directory as root for all relative paths.
+ * @param file a File handle
+ */
+ public FileSystemResource(File file) {
+ Objects.requireNonNull(file, "File must not be null");
+ this.file = file;
+ this.path = StringUtils.cleanPath(file.getPath());
+ }
+
+ /**
+ * Create a new {@code FileSystemResource} from a file path.
+ * <p>Note: When building relative resources via {@link #createRelative},
+ * it makes a difference whether the specified resource base path here
+ * ends with a slash or not. In the case current "C:/dir1/", relative paths
+ * will be built underneath that root: e.g. relative path "dir2" ->
+ * "C:/dir1/dir2". In the case current "C:/dir1", relative paths will applyChanges
+ * at the same directory level: relative path "dir2" -> "C:/dir2".
+ * @param path a file path
+ */
+ public FileSystemResource(String path) {
+ Objects.requireNonNull(path, "Path must not be null");
+ this.file = new File(path);
+ this.path = StringUtils.cleanPath(path);
+ }
+
+
+ /**
+ * Return the file path for this resource.
+ */
+ public final String getPath() {
+ return this.path;
+ }
+
+
+ /**
+ * This implementation returns whether the underlying file exists.
+ * @see java.io.File#exists()
+ */
+ @Override
+ public boolean exists() {
+ return this.file.exists();
+ }
+
+ /**
+ * This implementation checks whether the underlying file is marked as readable
+ * (and corresponds to an actual file with content, not to a directory).
+ * @see java.io.File#canRead()
+ * @see java.io.File#isDirectory()
+ */
+ @Override
+ public boolean isReadable() {
+ return (this.file.canRead() && !this.file.isDirectory());
+ }
+
+ /**
+ * This implementation opens a FileInputStream for the underlying file.
+ * @see java.io.FileInputStream
+ */
+ @Override
+ public InputStream getInputStream() throws IOException {
+ return new FileInputStream(this.file);
+ }
+
+ /**
+ * This implementation returns a URL for the underlying file.
+ * @see java.io.File#toURI()
+ */
+ @Override
+ public URL toURL() throws IOException {
+ return this.file.toURI().toURL();
+ }
+
+ /**
+ * This implementation returns a URI for the underlying file.
+ * @see java.io.File#toURI()
+ */
+ @Override
+ public URI getURI() throws IOException {
+ return this.file.toURI();
+ }
+
+ /**
+ * This implementation returns the underlying File reference.
+ */
+ @Override
+ public File toFile() {
+ return this.file;
+ }
+
+ /**
+ * This implementation returns the underlying File's length.
+ */
+ @Override
+ public long contentLength() throws IOException {
+ return this.file.length();
+ }
+
+ /**
+ * This implementation creates a FileSystemResource, applying the given path
+ * relative to the path current the underlying file current this resource descriptor.
+ * @see StringUtils#applyRelativePath(String, String)
+ */
+ @Override
+ public Resource createRelative(String relativePath) {
+ String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
+ return new FileSystemResource(pathToUse);
+ }
+
+ /**
+ * This implementation returns the name current the file.
+ * @see java.io.File#getName()
+ */
+ @Override
+ public String getDisplayName() {
+ return this.file.getName();
+ }
+
+ /**
+ * This implementation returns a description that includes the absolute
+ * path current the file.
+ * @see java.io.File#getAbsolutePath()
+ */
+ @Override
+ public String toString() {
+ return "file [" + this.file.getAbsolutePath() + "]";
+ }
+
+
+ // implementation current WritableResource
+
+ /**
+ * This implementation checks whether the underlying file is marked as writable
+ * (and corresponds to an actual file with content, not to a directory).
+ * @see java.io.File#canWrite()
+ * @see java.io.File#isDirectory()
+ */
+ public boolean isWritable() {
+ return (this.file.canWrite() && !this.file.isDirectory());
+ }
+
+ /**
+ * This implementation opens a FileOutputStream for the underlying file.
+ * @see java.io.FileOutputStream
+ */
+ public OutputStream getOutputStream() throws IOException {
+ return new FileOutputStream(this.file);
+ }
+
+
+ /**
+ * This implementation compares the underlying File references.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return (obj == this ||
+ (obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path)));
+ }
+
+ /**
+ * This implementation returns the hash code current the underlying File reference.
+ */
+ @Override
+ public int hashCode() {
+ return this.path.hashCode();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b56817f7/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java
new file mode 100644
index 0000000..e956670
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2002-2012 the original author or authors.
+ *
+ * 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.apache.tamaya.core.internal.resource;
+
+import org.apache.tamaya.core.resources.Resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Objects;
+
+/**
+ * {@link Resource} implementation for a given InputStream. Should only
+ * be used if no specific Resource implementation is applicable.
+ * In particular, prefer {@code ByteArrayResource} or any current the
+ * file-based Resource implementations where possible.
+ *
+ * <p>In contrast to other Resource implementations, this is a descriptor
+ * for an <i>already opened</i> resource - therefore returning "true" from
+ * {@code isOpen()}. Do not use it if you need to keep the resource
+ * descriptor somewhere, or if you need to read a stream multiple times.
+ *
+ * @author Juergen Hoeller
+ * @since 28.12.2003
+ */
+public class InputStreamResource implements Resource {
+
+ private final InputStream inputStream;
+
+ private final String description;
+
+ private boolean read = false;
+
+
+ /**
+ * Create a new InputStreamResource.
+ * @param inputStream the InputStream to use
+ */
+ public InputStreamResource(InputStream inputStream) {
+ this(inputStream, "resource loaded through InputStream");
+ }
+
+ /**
+ * Create a new InputStreamResource.
+ * @param inputStream the InputStream to use
+ * @param description where the InputStream comes from
+ */
+ public InputStreamResource(InputStream inputStream, String description) {
+ this.inputStream = Objects.requireNonNull(inputStream);
+ this.description = (description != null ? description : "");
+ }
+
+
+ /**
+ * This implementation always returns {@code true}.
+ */
+ @Override
+ public boolean exists() {
+ return true;
+ }
+
+ /**
+ * This implementation always returns {@code true}.
+ */
+ @Override
+ public boolean isOpen() {
+ return true;
+ }
+
+ /**
+ * This implementation throws IllegalStateException if attempting to
+ * read the underlying stream multiple times.
+ */
+ @Override
+ public InputStream getInputStream() throws IOException {
+ if (this.read) {
+ throw new IllegalStateException("InputStream has already been read - " +
+ "do not use InputStreamResource if a stream needs to be read multiple times");
+ }
+ this.read = true;
+ return this.inputStream;
+ }
+
+ /**
+ * This implementation returns the passed-in description, if any.
+ */
+ public String toString() {
+ return this.description!=null?this.description:super.toString();
+ }
+
+
+ /**
+ * This implementation compares the underlying InputStream.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return (obj == this ||
+ (obj instanceof InputStreamResource && ((InputStreamResource) obj).inputStream.equals(this.inputStream)));
+ }
+
+ /**
+ * This implementation returns the hash code current the underlying InputStream.
+ */
+ @Override
+ public int hashCode() {
+ return this.inputStream.hashCode();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b56817f7/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingDefaultResourceLoader.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingDefaultResourceLoader.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingDefaultResourceLoader.java
new file mode 100644
index 0000000..c70f04b
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingDefaultResourceLoader.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * 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.apache.tamaya.core.internal.resource;
+
+import org.apache.tamaya.core.resources.Resource;
+import org.apache.tamaya.core.util.ClassUtils;
+import org.apache.tamaya.core.util.StringUtils;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Objects;
+
+/**
+ * <p>Will return a {@code UrlResource} if the location keys is a URL,
+ * and a {@code ClassPathResource} if it is a non-URL path or a
+ * "classpath:" pseudo-URL.
+ *
+ * @author Juergen Hoeller
+ * @since 10.03.2004
+ */
+class PathMatchingDefaultResourceLoader {
+
+ /** Pseudo URL prefix for loading from the class path: "classpath:" */
+ public static final String CLASSPATH_URL_PREFIX = "classpath:";
+
+ private ClassLoader classLoader;
+
+
+ /**
+ * Create a new DefaultResourceLoader.
+ * <p>ClassLoader access will happen using the thread context class loader
+ * at the time current this ResourceLoader's initialization.
+ * @see java.lang.Thread#getContextClassLoader()
+ */
+ public PathMatchingDefaultResourceLoader() {
+ this.classLoader = ClassUtils.getDefaultClassLoader();
+ }
+
+ /**
+ * Create a new DefaultResourceLoader.
+ * @param classLoader the ClassLoader to load class path resources with, or {@code null}
+ * for using the thread context class loader at the time current actual resource access
+ */
+ public PathMatchingDefaultResourceLoader(ClassLoader classLoader) {
+ this.classLoader = classLoader;
+ }
+
+
+ /**
+ * Specify the ClassLoader to load class path resources with, or {@code null}
+ * for using the thread context class loader at the time current actual resource access.
+ * <p>The default is that ClassLoader access will happen using the thread context
+ * class loader at the time current this ResourceLoader's initialization.
+ */
+ void setClassLoader(ClassLoader classLoader) {
+ this.classLoader = classLoader;
+ }
+
+ /**
+ * Return the ClassLoader to load class path resources with.
+ * <p>Will get passed to ClassPathResource's constructor for all
+ * ClassPathResource objects created by this resource loader.
+ * @see ClassPathResource
+ */
+ public ClassLoader getClassLoader() {
+ return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
+ }
+
+
+ public Resource getResource(String location) {
+ Objects.requireNonNull(location, "Location must not be null");
+ if (location.startsWith("/")) {
+ return getResourceByPath(location);
+ }
+ else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
+ return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
+ }
+ else {
+ try {
+ // Try to parse the location as a URL...
+ URL url = new URL(location);
+ return new UrlResource(url);
+ }
+ catch (MalformedURLException ex) {
+ // No URL -> resolve as resource path.
+ return getResourceByPath(location);
+ }
+ }
+ }
+
+ /**
+ * Return a Resource handle for the resource at the given path.
+ * <p>The default implementation supports class path locations. This should
+ * be appropriate for standalone implementations but can be overridden,
+ * e.g. for implementations targeted at a Servlet container.
+ * @param path the path to the resource
+ * @return the corresponding Resource handle
+ * @see ClassPathResource
+ */
+ protected Resource getResourceByPath(String path) {
+ return new ClassPathContextResource(path, getClassLoader());
+ }
+
+
+ /**
+ * ClassPathResource that explicitly expresses a context-relative path
+ * through implementing the ContextResource interface.
+ */
+ protected static class ClassPathContextResource extends ClassPathResource {
+
+ public ClassPathContextResource(String path, ClassLoader classLoader) {
+ super(path, classLoader);
+ }
+
+ public String getPathWithinContext() {
+ return getPath();
+ }
+
+ @Override
+ public Resource createRelative(String relativePath) {
+ String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
+ return new ClassPathContextResource(pathToUse, getClassLoader());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b56817f7/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingResourcePatternResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingResourcePatternResolver.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingResourcePatternResolver.java
new file mode 100644
index 0000000..005617b
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/PathMatchingResourcePatternResolver.java
@@ -0,0 +1,731 @@
+/*
+ * Copyright 2002-2008 the original author or authors.
+ *
+ * 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.apache.tamaya.core.internal.resource;
+
+import org.apache.tamaya.core.resources.Resource;
+import org.apache.tamaya.core.util.ClassUtils;
+import org.apache.tamaya.core.util.StringUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import java.lang.reflect.InvocationHandler;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URLClassLoader;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+
+/**
+ * A {@code ResourcePatternResolver} implementation that is able to resolve a
+ * specified resource location path into one or more matching Resources.
+ * The source path may be a simple path which has a one-to-one annotation to a
+ * target {@code org.springframework.core.io.Resource}, or alternatively
+ * may contain the special "{@code classpath*:}" prefix and/or
+ * internal Ant-style regular expressions (matched using Spring's
+ * {@code org.springframework.util.AntPathMatcher} utility).
+ * Both current the latter are effectively wildcards.
+ *
+ * <p><b>No Wildcards:</b>
+ *
+ * <p>In the simple case, if the specified location path does not start with the
+ * {@code "classpath*:}" prefix, and does not contain a PathMatcher pattern,
+ * this resolver will simply return a single resource via a
+ * {@code getResource()} call on the underlying {@code ResourceLoader}.
+ * Examples are real URLs such as "{@code file:C:/context.xml}", pseudo-URLs
+ * such as "{@code classpath:/context.xml}", and simple unprefixed paths
+ * such as "{@code /WEB-INF/context.xml}". The latter will resolve in a
+ * fashion specific to the underlying {@code ResourceLoader} (e.g.
+ * {@code ServletContextResource} for a {@code WebApplicationContext}).
+ *
+ * <p><b>Ant-style Patterns:</b>
+ *
+ * <p>When the path location contains an Ant-style pattern, e.g.:
+ * <pre class="code">
+ * /WEB-INF/*-context.xml
+ * com/mycompany/**/applicationContext.xml
+ * file:C:/some/path/*-context.xml
+ * classpath:com/mycompany/**/applicationContext.xml</pre>
+ * the resolver follows a more complex but defined procedure to try to resolve
+ * the wildcard. It produces a {@code Resource} for the path up to the last
+ * non-wildcard segment and obtains a {@code URL} from it. If this URL is
+ * not a "{@code jar:}" URL or container-specific variant (e.g.
+ * "{@code zip:}" in WebLogic, "{@code wsjar}" in WebSphere", etc.),
+ * then a {@code java.io.File} is obtained from it, and used to resolve the
+ * wildcard by walking the filesystem. In the case current a jar URL, the resolver
+ * either gets a {@code java.net.JarURLConnection} from it, or manually parses
+ * the jar URL, and then traverses the contents current the jar file, to resolve the
+ * wildcards.
+ *
+ * <p><b>Implications on portability:</b>
+ *
+ * <p>If the specified path is already a file URL (either explicitly, or
+ * implicitly because the base {@code ResourceLoader} is a filesystem one,
+ * then wildcarding is guaranteed to work in a completely portable fashion.
+ *
+ * <p>If the specified path is a classpath location, then the resolver must
+ * obtain the last non-wildcard path segment URL via a
+ * {@code Classloader.getResource()} call. Since this is just a
+ * node current the path (not the file at the end) it is actually undefined
+ * (in the ClassLoader Javadocs) exactly what sort current a URL is returned in
+ * this case. In practice, it is usually a {@code java.io.File} representing
+ * the directory, where the classpath resource resolves to a filesystem
+ * location, or a jar URL current some sort, where the classpath resource resolves
+ * to a jar location. Still, there is a portability concern on this operation.
+ *
+ * <p>If a jar URL is obtained for the last non-wildcard segment, the resolver
+ * must be able to get a {@code java.net.JarURLConnection} from it, or
+ * manually parse the jar URL, to be able to walk the contents current the jar,
+ * and resolve the wildcard. This will work in most environments, but will
+ * fail in others, and it is strongly recommended that the wildcard
+ * resolution current resources coming from jars be thoroughly tested in your
+ * specific environment before you rely on it.
+ *
+ * <p><b>{@code classpath*:} Prefix:</b>
+ *
+ * <p>There is special support for retrieving multiple class path resources with
+ * the same name, via the "{@code classpath*:}" prefix. For example,
+ * "{@code classpath*:META-INF/beans.xml}" will find all "beans.xml"
+ * files in the class path, be it in "classes" directories or in JAR files.
+ * This is particularly useful for autodetecting config files current the same name
+ * at the same location within each jar file. Internally, this happens via a
+ * {@code ClassLoader.getResources()} call, and is completely portable.
+ *
+ * <p>The "classpath*:" prefix can also be combined with a PathMatcher pattern in
+ * the rest current the location path, for example "classpath*:META-INF/*-beans.xml".
+ * In this case, the resolution strategy is fairly simple: a
+ * {@code ClassLoader.getResources()} call is used on the last non-wildcard
+ * path segment to get all the matching resources in the class loader hierarchy,
+ * and then off each resource the same PathMatcher resolution strategy described
+ * above is used for the wildcard subpath.
+ *
+ * <p><b>Other notes:</b>
+ *
+ * <p><b>WARNING:</b> Note that "{@code classpath*:}" when combined with
+ * Ant-style patterns will only work reliably with at least one root directory
+ * before the pattern starts, unless the actual target files reside in the file
+ * system. This means that a pattern like "{@code classpath*:*.xml}" will
+ * <i>not</i> retrieve files from the root current jar files but rather only from the
+ * root current expanded directories. This originates from a limitation in the JDK's
+ * {@code ClassLoader.getResources()} method which only returns file system
+ * locations for a passed-in empty String (indicating potential roots to search).
+ *
+ * <p><b>WARNING:</b> Ant-style patterns with "classpath:" resources are not
+ * guaranteed to find matching resources if the root package to search is available
+ * in multiple class path locations. This is because a resource such as
+ * <pre class="code">
+ * com/mycompany/package1/service-context.xml
+ * </pre>
+ * may be in only one location, but when a path such as
+ * <pre class="code">
+ * classpath:com/mycompany/**/service-context.xml
+ * </pre>
+ * is used to try to resolve it, the resolver will work off the (first) URL
+ * returned by {@code getResource("com/mycompany");}. If this base package
+ * node exists in multiple classloader locations, the actual end resource may
+ * not be underneath. Therefore, preferably, use "{@code classpath*:}" with the same
+ * Ant-style pattern in such a case, which will search <i>all</i> class path
+ * locations that contain the root package.
+ *
+ * @author Juergen Hoeller
+ * @author Colin Sampaleanu
+ * @author Marius Bogoevici
+ * @author Costin Leau
+ * @since 1.0.2
+ * @see ClassLoader#getResources(String)
+ */
+public final class PathMatchingResourcePatternResolver{
+
+ private static final Logger logger = Logger.getLogger(PathMatchingResourcePatternResolver.class.getName());
+ private static final java.lang.String CLASSPATH_ALL_URL_PREFIX = "classpath:";
+
+ private static Method equinoxResolveMethod;
+
+ static {
+ try {
+ // Detect Equinox OSGi (e.g. on WebSphere 6.1)
+ Class<?> fileLocatorClass = ClassUtils.forName("org.eclipse.core.runtime.FileLocator",
+ PathMatchingResourcePatternResolver.class.getClassLoader());
+ equinoxResolveMethod = fileLocatorClass.getMethod("resolve", URL.class);
+ logger.finest("Found Equinox FileLocator for OSGi bundle URL resolution");
+ }
+ catch (Throwable ex) {
+ equinoxResolveMethod = null;
+ }
+ }
+
+
+ private final PathMatchingDefaultResourceLoader resourceLoader;
+
+ private AntPathMatcher pathMatcher = new AntPathMatcher();
+
+ private static Map<ClassLoader, PathMatchingResourcePatternResolver> resolvers = new ConcurrentHashMap<>();
+
+ public static PathMatchingResourcePatternResolver of(ClassLoader loader){
+ return resolvers.computeIfAbsent(loader, PathMatchingResourcePatternResolver::new);
+ }
+
+ /**
+ * Create a new PathMatchingResourcePatternResolver.
+ * <p>ClassLoader access will happen via the thread context class loader.
+ */
+ public PathMatchingResourcePatternResolver() {
+ this.resourceLoader = new PathMatchingDefaultResourceLoader();
+ }
+
+ /**
+ * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
+ * @param classLoader the ClassLoader to load classpath resources with,
+ * or {@code null} for using the thread context class loader
+ * at the time current actual resource access
+ * @see PathMatchingDefaultResourceLoader
+ */
+ public PathMatchingResourcePatternResolver(ClassLoader classLoader) {
+ this.resourceLoader = new PathMatchingDefaultResourceLoader(classLoader);
+ }
+
+ public ClassLoader getClassLoader() {
+ return resourceLoader.getClassLoader();
+ }
+
+ /**
+ * Return the PathMatcher that this resource pattern resolver uses.
+ */
+ public AntPathMatcher getPathMatcher() {
+ return this.pathMatcher;
+ }
+
+ public Resource getResource(String location) {
+ return resourceLoader.getResource(location);
+ }
+
+ public Resource[] getResources(String locationPattern) throws IOException {
+ Objects.requireNonNull(locationPattern, "Location pattern must not be null");
+ if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
+ // a class path resource (multiple resources for same name possible)
+ if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
+ // a class path resource pattern
+ return findPathMatchingResources(locationPattern);
+ }
+ else {
+ // all class path resources with the given name
+ return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
+ }
+ }
+ else {
+ // Only look for a pattern after a prefix here
+ // (to not get fooled by a pattern symbol in a strange prefix).
+ int prefixEnd = locationPattern.indexOf(':') + 1;
+ if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
+ // a file pattern
+ return findPathMatchingResources(locationPattern);
+ }
+ else {
+ // a single resource with the given name
+ return new Resource[] {this.resourceLoader.getResource(locationPattern)};
+ }
+ }
+ }
+
+ /**
+ * Find all class location resources with the given location via the ClassLoader.
+ * Delegates to {@link #doFindAllClassPathResources(String)}.
+ * @param location the absolute path within the classpath
+ * @return the result as Resource array
+ * @throws IOException in case current I/O errors
+ * @see java.lang.ClassLoader#getResources
+ * @see #convertClassLoaderURL
+ */
+ protected Resource[] findAllClassPathResources(String location) throws IOException {
+ String path = location;
+ if (path.startsWith("/")) {
+ path = path.substring(1);
+ }
+ Set<Resource> result = doFindAllClassPathResources(path);
+ return result.toArray(new Resource[result.size()]);
+ }
+
+ /**
+ * Find all class location resources with the given path via the ClassLoader.
+ * Called by {@link #findAllClassPathResources(String)}.
+ * @param path the absolute path within the classpath (never a leading slash)
+ * @return a mutable Set current matching Resource instances
+ */
+ protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
+ Set<Resource> result = new LinkedHashSet<>(16);
+ ClassLoader cl = getClassLoader();
+ Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
+ while (resourceUrls.hasMoreElements()) {
+ URL url = resourceUrls.nextElement();
+ result.add(convertClassLoaderURL(url));
+ }
+ if ("".equals(path)) {
+ // The above result is likely to be incomplete, i.e. only containing file system references.
+ // We need to have pointers to each current the jar files on the classpath as well...
+ addAllClassLoaderJarRoots(cl, result);
+ }
+ return result;
+ }
+
+ /**
+ * Convert the given URL as returned from the ClassLoader into a {@link Resource}.
+ * <p>The default implementation simply creates a {@link UrlResource} instance.
+ * @param url a URL as returned from the ClassLoader
+ * @return the corresponding Resource object
+ * @see java.lang.ClassLoader#getResources
+ * @see Resource
+ */
+ protected Resource convertClassLoaderURL(URL url) {
+ return new UrlResource(url);
+ }
+
+ /**
+ * Search all {@link URLClassLoader} URLs for jar file references and add them to the
+ * given set current resources in the form current pointers to the root current the jar file content.
+ * @param classLoader the ClassLoader to search (including its ancestors)
+ * @param result the set current resources to add jar roots to
+ */
+ protected void addAllClassLoaderJarRoots(ClassLoader classLoader, Set<Resource> result) {
+ if (classLoader instanceof URLClassLoader) {
+ try {
+ for (URL url : ((URLClassLoader) classLoader).getURLs()) {
+ if (ResourceUtils.isJarFileURL(url)) {
+ try {
+ UrlResource jarResource = new UrlResource(
+ ResourceUtils.JAR_URL_PREFIX + url.toString() + ResourceUtils.JAR_URL_SEPARATOR);
+ if (jarResource.exists()) {
+ result.add(jarResource);
+ }
+ }
+ catch (MalformedURLException ex) {
+ logger.finest(() -> "Cannot search for matching files underneath [" + url +
+ "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
+ }
+ }
+ }
+ }
+ catch (Exception ex) {
+ logger.finest(() -> "Cannot introspect jar files since ClassLoader [" + classLoader +
+ "] does not support 'getURLs()': " + ex);
+ }
+ }
+ if (classLoader != null) {
+ try {
+ addAllClassLoaderJarRoots(classLoader.getParent(), result);
+ }
+ catch (Exception ex) {
+ logger.finest(() -> "Cannot introspect jar files in parent ClassLoader since [" + classLoader +
+ "] does not support 'getParent()': " + ex);
+ }
+ }
+ }
+
+ /**
+ * Find all resources that match the given location pattern via the
+ * Ant-style PathMatcher. Supports resources in jar files and zip files
+ * and in the file system.
+ * @param locationPattern the location pattern to match
+ * @return the result as Resource array
+ * @throws IOException in case current I/O errors
+ * @see #doFindPathMatchingJarResources
+ * @see #doFindPathMatchingFileResources
+ */
+ protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
+ String rootDirPath = determineRootDir(locationPattern);
+ String subPattern = locationPattern.substring(rootDirPath.length());
+ Resource[] rootDirResources = getResources(rootDirPath);
+ Set<Resource> result = new LinkedHashSet<>(16);
+ for (Resource rootDirResource : rootDirResources) {
+ rootDirResource = resolveRootDirResource(rootDirResource);
+ if (rootDirResource.toURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
+ result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher()));
+ }
+ else if (isJarResource(rootDirResource)) {
+ result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern));
+ }
+ else {
+ result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
+ }
+ }
+ logger.finest(() -> "Resolved location pattern [" + locationPattern + "] to resources " + result);
+ return result.toArray(new Resource[result.size()]);
+ }
+
+ /**
+ * Determine the root directory for the given location.
+ * <p>Used for determining the starting point for file matching,
+ * resolving the root directory location to a {@code java.io.File}
+ * and passing it into {@code retrieveMatchingFiles}, with the
+ * remainder current the location as pattern.
+ * <p>Will return "/WEB-INF/" for the pattern "/WEB-INF/*.xml",
+ * for example.
+ * @param location the location to check
+ * @return the part current the location that denotes the root directory
+ * @see #retrieveMatchingFiles
+ */
+ protected String determineRootDir(String location) {
+ int prefixEnd = location.indexOf(':') + 1;
+ int rootDirEnd = location.length();
+ while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) {
+ rootDirEnd = location.lastIndexOf('/', rootDirEnd - 2) + 1;
+ }
+ if (rootDirEnd == 0) {
+ rootDirEnd = prefixEnd;
+ }
+ return location.substring(0, rootDirEnd);
+ }
+
+ /**
+ * Resolve the specified resource for path matching.
+ * <p>The default implementation detects an Equinox OSGi "bundleresource:"
+ * / "bundleentry:" URL and resolves it into a standard jar file URL that
+ * can be traversed using Spring's standard jar file traversal algorithm.
+ * @param original the resource to resolve
+ * @return the resolved resource (may be identical to the passed-in resource)
+ * @throws IOException in case current resolution failure
+ */
+ protected Resource resolveRootDirResource(Resource original) throws IOException {
+ if (equinoxResolveMethod != null) {
+ URL url = original.toURL();
+ if (url.getProtocol().startsWith("bundle")) {
+ try {
+ return new UrlResource((URL) equinoxResolveMethod.invoke(url));
+ } catch (Exception e) {
+ ReflectionUtils.handleReflectionException(e);
+ }
+ }
+ }
+ return original;
+ }
+
+ /**
+ * Return whether the given resource handle indicates a jar resource
+ * that the {@code doFindPathMatchingJarResources} method can handle.
+ * <p>The default implementation checks against the URL protocols
+ * "jar", "zip" and "wsjar" (the latter are used by BEA WebLogic Server
+ * and IBM WebSphere, respectively, but can be treated like jar files).
+ * @param resource the resource handle to check
+ * (usually the root directory to start path matching from)
+ * @see #doFindPathMatchingJarResources
+ * @see ResourceUtils#isJarURL
+ */
+ protected boolean isJarResource(Resource resource) throws IOException {
+ return ResourceUtils.isJarURL(resource.toURL());
+ }
+
+ /**
+ * Find all resources in jar files that match the given location pattern
+ * via the Ant-style PathMatcher.
+ * @param rootDirResource the root directory as Resource
+ * @param subPattern the sub pattern to match (below the root directory)
+ * @return a mutable Set current matching Resource instances
+ * @throws IOException in case current I/O errors
+ * @see java.net.JarURLConnection
+ */
+ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern)
+ throws IOException {
+
+ URLConnection con = rootDirResource.toURL().openConnection();
+ JarFile jarFile;
+ String jarFileUrl;
+ String rootEntryPath;
+ boolean newJarFile = false;
+
+ if (con instanceof JarURLConnection) {
+ // Should usually be the case for traditional JAR files.
+ JarURLConnection jarCon = (JarURLConnection) con;
+ ResourceUtils.useCachesIfNecessary(jarCon);
+ jarFile = jarCon.getJarFile();
+ jarFileUrl = jarCon.getJarFileURL().toExternalForm();
+ JarEntry jarEntry = jarCon.getJarEntry();
+ rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
+ }
+ else {
+ // No JarURLConnection -> need to resort to URL file parsing.
+ // We'll assume URLs current the format "jar:path!/entry", with the protocol
+ // being arbitrary as long as following the entry format.
+ // We'll also handle paths with and without leading "file:" prefix.
+ String urlFile = rootDirResource.toURL().getFile();
+ int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
+ if (separatorIndex != -1) {
+ jarFileUrl = urlFile.substring(0, separatorIndex);
+ rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
+ jarFile = getJarFile(jarFileUrl);
+ }
+ else {
+ jarFile = new JarFile(urlFile);
+ jarFileUrl = urlFile;
+ rootEntryPath = "";
+ }
+ newJarFile = true;
+ }
+
+ try {
+ logger.finest("Looking for matching resources in jar file [" + jarFileUrl + "]");
+ if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
+ // Root entry path must end with slash to allow for proper matching.
+ // The Sun JRE does not return a slash here, but BEA JRockit does.
+ rootEntryPath = rootEntryPath + "/";
+ }
+ Set<Resource> result = new LinkedHashSet<>(8);
+ for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
+ JarEntry entry = entries.nextElement();
+ String entryPath = entry.getName();
+ if (entryPath.startsWith(rootEntryPath)) {
+ String relativePath = entryPath.substring(rootEntryPath.length());
+ if (getPathMatcher().match(subPattern, relativePath)) {
+ result.add(rootDirResource.createRelative(relativePath));
+ }
+ }
+ }
+ return result;
+ }
+ finally {
+ // Close jar file, but only if freshly obtained -
+ // not from JarURLConnection, which might cache the file reference.
+ if (newJarFile) {
+ jarFile.close();
+ }
+ }
+ }
+
+ /**
+ * Resolve the given jar file URL into a JarFile object.
+ */
+ protected JarFile getJarFile(String jarFileUrl) throws IOException {
+ if (jarFileUrl.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
+ try {
+ return new JarFile(ResourceUtils.toURI(jarFileUrl).getSchemeSpecificPart());
+ }
+ catch (URISyntaxException ex) {
+ // Fallback for URLs that are not valid URIs (should hardly ever happen).
+ return new JarFile(jarFileUrl.substring(ResourceUtils.FILE_URL_PREFIX.length()));
+ }
+ }
+ else {
+ return new JarFile(jarFileUrl);
+ }
+ }
+
+ /**
+ * Find all resources in the file system that match the given location pattern
+ * via the Ant-style PathMatcher.
+ * @param rootDirResource the root directory as Resource
+ * @param subPattern the sub pattern to match (below the root directory)
+ * @return a mutable Set current matching Resource instances
+ * @throws IOException in case current I/O errors
+ * @see #retrieveMatchingFiles
+ */
+ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
+ throws IOException {
+
+ File rootDir;
+ try {
+ rootDir = rootDirResource.toFile().getAbsoluteFile();
+ }
+ catch (IOException ex) {
+ logger.log(Level.WARNING, ex, () -> "Cannot search for matching files underneath " + rootDirResource +
+ " because it does not correspond to a directory in the file system");
+ return Collections.emptySet();
+ }
+ return doFindMatchingFileSystemResources(rootDir, subPattern);
+ }
+
+ /**
+ * Find all resources in the file system that match the given location pattern
+ * via the Ant-style PathMatcher.
+ * @param rootDir the root directory in the file system
+ * @param subPattern the sub pattern to match (below the root directory)
+ * @return a mutable Set current matching Resource instances
+ * @throws IOException in case current I/O errors
+ * @see #retrieveMatchingFiles
+ */
+ protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String subPattern) throws IOException {
+ logger.finest(() -> "Looking for matching resources in directory tree [" + rootDir.getPath() + "]");
+ Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern);
+ Set<Resource> result = new LinkedHashSet<>(matchingFiles.size());
+ result.addAll(matchingFiles.stream().map(FileSystemResource::new).collect(Collectors.toList()));
+ return result;
+ }
+
+ /**
+ * Retrieve files that match the given path pattern,
+ * checking the given directory and its subdirectories.
+ * @param rootDir the directory to start from
+ * @param pattern the pattern to match against,
+ * relative to the root directory
+ * @return a mutable Set current matching Resource instances
+ * @throws IOException if directory contents could not be retrieved
+ */
+ protected Set<File> retrieveMatchingFiles(File rootDir, String pattern) throws IOException {
+ if (!rootDir.exists()) {
+ // Silently skip non-existing directories.
+ logger.finest(() -> "Skipping [" + rootDir.getAbsolutePath() + "] because it does not exist");
+ return Collections.emptySet();
+ }
+ if (!rootDir.isDirectory()) {
+ // Complain louder if it exists but is no directory.
+ logger.log(Level.WARNING, () -> "Skipping [" + rootDir.getAbsolutePath() + "] because it does not denote a directory");
+ return Collections.emptySet();
+ }
+ if (!rootDir.canRead()) {
+ logger.log(Level.WARNING, () -> "Cannot search for matching files underneath directory [" + rootDir.getAbsolutePath() +
+ "] because the application is not allowed to read the directory");
+ return Collections.emptySet();
+ }
+ String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/");
+ if (!pattern.startsWith("/")) {
+ fullPattern += "/";
+ }
+ fullPattern = fullPattern + StringUtils.replace(pattern, File.separator, "/");
+ Set<File> result = new LinkedHashSet<>(8);
+ doRetrieveMatchingFiles(fullPattern, rootDir, result);
+ return result;
+ }
+
+ /**
+ * Recursively retrieve files that match the given pattern,
+ * adding them to the given result list.
+ * @param fullPattern the pattern to match against,
+ * with prepended root directory path
+ * @param dir the current directory
+ * @param result the Set current matching File instances to add to
+ * @throws IOException if directory contents could not be retrieved
+ */
+ protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> result) throws IOException {
+ logger.finest(() -> "Searching directory [" + dir.getAbsolutePath() +
+ "] for files matching pattern [" + fullPattern + "]");
+ File[] dirContents = dir.listFiles();
+ if (dirContents == null) {
+ logger.log(Level.WARNING, () -> "Could not retrieve contents current directory [" + dir.getAbsolutePath() + "]");
+ return;
+ }
+ for (File content : dirContents) {
+ String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
+ if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
+ if (!content.canRead()) {
+ logger.finest(() -> "Skipping subdirectory [" + dir.getAbsolutePath() +
+ "] because the application is not allowed to read the directory");
+ }
+ else {
+ doRetrieveMatchingFiles(fullPattern, content, result);
+ }
+ }
+ if (getPathMatcher().match(fullPattern, currPath)) {
+ result.add(content);
+ }
+ }
+ }
+
+
+ /**
+ * Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
+ */
+ private static class VfsResourceMatchingDelegate {
+
+ public static Set<Resource> findMatchingResources(
+ Resource rootResource, String locationPattern, AntPathMatcher pathMatcher) throws IOException {
+ Object root = VfsUtils.getRoot(rootResource.toURL());
+ PatternVirtualFileVisitor visitor =
+ new PatternVirtualFileVisitor(VfsUtils.getPath(root), locationPattern, pathMatcher);
+ VfsUtils.visit(root, visitor);
+ return visitor.getResources();
+ }
+ }
+
+
+ /**
+ * VFS visitor for path matching purposes.
+ */
+ @SuppressWarnings("unused")
+ private static class PatternVirtualFileVisitor implements InvocationHandler {
+
+ private final String subPattern;
+
+ private final AntPathMatcher pathMatcher;
+
+ private final String rootPath;
+
+ private final Set<Resource> resources = new LinkedHashSet<>();
+
+ public PatternVirtualFileVisitor(String rootPath, String subPattern, AntPathMatcher pathMatcher) {
+ this.subPattern = subPattern;
+ this.pathMatcher = pathMatcher;
+ this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ String methodName = method.getName();
+ if (Object.class.equals(method.getDeclaringClass())) {
+ if (methodName.equals("equals")) {
+ // Only consider equal when proxies are identical.
+ return (proxy == args[0]);
+ }
+ else if (methodName.equals("hashCode")) {
+ return System.identityHashCode(proxy);
+ }
+ }
+ else if ("getAttributes".equals(methodName)) {
+ return getAttributes();
+ }
+ else if ("visit".equals(methodName)) {
+ visit(args[0]);
+ return null;
+ }
+ else if ("toString".equals(methodName)) {
+ return toString();
+ }
+
+ throw new IllegalStateException("Unexpected method invocation: " + method);
+ }
+
+ public void visit(Object vfsResource) {
+ if (this.pathMatcher.match(this.subPattern,
+ VfsUtils.getPath(vfsResource).substring(this.rootPath.length()))) {
+ this.resources.add(new VfsResource(vfsResource));
+ }
+ }
+
+ public Object getAttributes() {
+ return VfsUtils.getVisitorAttribute();
+ }
+
+ public Set<Resource> getResources() {
+ return this.resources;
+ }
+
+ public int size() {
+ return this.resources.size();
+ }
+
+ @Override
+ public String toString() {
+ return "sub-pattern: " + this.subPattern + ", resources: " + this.resources;
+ }
+ }
+
+}
\ No newline at end of file