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/11 01:43:51 UTC
[4/6] incubator-tamaya git commit: TAMAYA-43: Added missing Javadocs.
TAMAYA-43: Added missing Javadocs.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/202e9ac3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/202e9ac3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/202e9ac3
Branch: refs/heads/master
Commit: 202e9ac3a705676ad9fd358b58ef88a2705f12f9
Parents: 842b33e
Author: anatole <an...@apache.org>
Authored: Thu Jan 8 04:29:51 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Sun Jan 11 01:40:59 2015 +0100
----------------------------------------------------------------------
.../resource/internal/ClasspathCollector.java | 29 +++++++------
.../tamaya/resource/internal/FileCollector.java | 44 ++++++++++++++++----
2 files changed, 53 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/202e9ac3/modules/resources/src/main/java/org/apache/tamaya/resource/internal/ClasspathCollector.java
----------------------------------------------------------------------
diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/internal/ClasspathCollector.java b/modules/resources/src/main/java/org/apache/tamaya/resource/internal/ClasspathCollector.java
index 3f2093b..6124dca 100644
--- a/modules/resources/src/main/java/org/apache/tamaya/resource/internal/ClasspathCollector.java
+++ b/modules/resources/src/main/java/org/apache/tamaya/resource/internal/ClasspathCollector.java
@@ -48,37 +48,42 @@ public class ClasspathCollector {
/**
* JAR protocol.
*/
- public static final String PROTOCOL_JAR = "jar";
+ private static final String PROTOCOL_JAR = "jar";
/**
* Separator between JAR file URL and the internal jar file path.
*/
- public static final String JAR_URL_SEPARATOR = "!/";
+ private static final String JAR_URL_SEPARATOR = "!/";
/**
* ZIP protocol.
*/
- public static final String PROTOCOL_ZIP = "zip";
+ private static final String PROTOCOL_ZIP = "zip";
/**
* ZIP protocol for a JBoss jar file entry: "vfszip".
*/
- public static final String PROTOCOL_VFSZIP = "vfszip";
+ private static final String PROTOCOL_VFSZIP = "vfszip";
/**
* URL protocol for an WebSphere jar file: "wsjar".
*/
- public static final String PROTOCOL_WSJAR = "wsjar";
+ private static final String PROTOCOL_WSJAR = "wsjar";
/**
* URL protocol for an entry from an OC4J jar.
*/
- public static final String PROTOCOL_CODE_SOURCE = "code-source";
+ private static final String PROTOCOL_CODE_SOURCE = "code-source";
/**
* The logger used.
*/
private static final Logger LOG = Logger.getLogger(ClasspathCollector.class.getName());
+
+ /**
+ * Prefix used for explicitly selecting this collector.
+ */
+ public static final String CLASSPATH_PREFIX = "classpath:";
/**
* The classloader used to load the resources.
@@ -101,25 +106,24 @@ public class ClasspathCollector {
* @return the resources found.
*/
public Collection<URL> collectFiles(String expression) {
- if (expression.startsWith("classpath:")) {
- expression = expression.substring("classpath:".length());
+ if (expression.startsWith(CLASSPATH_PREFIX)) {
+ expression = expression.substring(CLASSPATH_PREFIX.length());
}
if (expression.startsWith("/")) {
expression = expression.substring(1);
}
Locator locator = Locator.of(expression);
List<URL> result = new ArrayList<>();
- String rootPath = locator.getRootPath();
try {
- Enumeration<URL> rootResources = this.classLoader.getResources(rootPath);
+ Enumeration<URL> rootResources = this.classLoader.getResources(locator.getRootPath());
while (rootResources.hasMoreElements()) {
URL resource = rootResources.nextElement();
try {
if (isJarFile(resource)) {
result.addAll(doFindPathMatchingJarResources(resource, locator.getSubPath()));
} else {
- File file = getFile(resource);
- result.addAll(FileCollector.traverseAndSelectFromChildren(file, locator.getSubPathTokens(), 0));
+ result.addAll(FileCollector.traverseAndSelectFromChildren(getFile(resource),
+ locator.getSubPathTokens(), 0));
}
} catch (Exception e) {
LOG.log(Level.SEVERE, "Error locating resources for: " + expression, e);
@@ -152,7 +156,6 @@ public class ClasspathCollector {
String rootEntryPath;
if (con instanceof JarURLConnection) {
- // Should usually be the case for traditional JAR files.
JarURLConnection jarCon = (JarURLConnection) con;
jarCon.setUseCaches(false);
jarFile = jarCon.getJarFile();
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/202e9ac3/modules/resources/src/main/java/org/apache/tamaya/resource/internal/FileCollector.java
----------------------------------------------------------------------
diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/internal/FileCollector.java b/modules/resources/src/main/java/org/apache/tamaya/resource/internal/FileCollector.java
index d506e11..0dcad83 100644
--- a/modules/resources/src/main/java/org/apache/tamaya/resource/internal/FileCollector.java
+++ b/modules/resources/src/main/java/org/apache/tamaya/resource/internal/FileCollector.java
@@ -38,14 +38,23 @@ import java.util.logging.Logger;
* </pre>
*/
public class FileCollector {
-
+ /** The prefix used to explicitly select this collector. */
public static final String FILE_PREFIX = "file:";
+ /** The logger instance. */
+ private static final Logger LOG = Logger.getLogger(FileCollector.class.getName());
+
+ /**
+ * private constructor.
+ */
private FileCollector() {
}
- private static final Logger LOG = Logger.getLogger(FileCollector.class.getName());
-
+ /**
+ * Collects the files given the expression.
+ * @param expression the expression in Ant-styled format, not null.
+ * @return the URLs found.
+ */
public static Collection<URL> collectFiles(String expression) {
expression = expression.replace("\\", "/");
Locator locator = Locator.of(expression);
@@ -62,6 +71,14 @@ public class FileCollector {
return result;
}
+ /**
+ * Internal method to traverse the file system down, hereby comparing the new path elements with the
+ * elements given by {@code subTokens}, starting at the given {@code tokenIndex}.
+ * @param dir the directory to start
+ * @param subTokens the overall subtoken to be analyzed
+ * @param tokenIndex the index where in the token list to start comparing
+ * @return the URLs matching the tokens
+ */
static Collection<URL> traverseAndSelectFromChildren(File dir, List<String> subTokens, int tokenIndex) {
if (tokenIndex >= subTokens.size() || dir.isFile()) {
return Collections.emptyList();
@@ -92,11 +109,17 @@ public class FileCollector {
return result;
}
- static Collection<URL> traverseAndSelectFromChildren(File file, String subExpression) {
+ /**
+ * Internal method to traverse the file system and comparing all child file names with the given expression.
+ * @param file the root directory
+ * @param expression the regular expression to match
+ * @return the URLs matching the expression
+ */
+ static Collection<URL> traverseAndSelectFromChildren(File file, String expression) {
List<URL> result = new ArrayList<>();
for (File childFile : file.listFiles()) {
if (childFile.isFile()) {
- if (childFile.getName().matches(subExpression)) {
+ if (childFile.getName().matches(expression)) {
try {
result.add(getURL(childFile));
} catch (Exception e) {
@@ -104,19 +127,26 @@ public class FileCollector {
}
}
} else if (childFile.isDirectory()) {
- result.addAll(traverseAndSelectFromChildren(childFile, subExpression));
+ result.addAll(traverseAndSelectFromChildren(childFile, expression));
}
}
return result;
}
+ /**
+ * Simple matcher method for a single token.
+ * @param childFile the file to match
+ * @param subTokens the subtoken list
+ * @param tokenIndex the index where to start
+ * @return true if the file matches and should be selected.
+ */
private static boolean matchesFile(File childFile, List<String> subTokens, int tokenIndex) {
if (tokenIndex < (subTokens.size() - 1)) {
// not all tokens consumed, so no match!
return false;
}
String tokenToMatch = subTokens.get(tokenIndex);
- tokenToMatch = tokenToMatch.replace("*", ".*");
+ tokenToMatch = tokenToMatch.replace("*", ".*").replace("?", ".?");
return childFile.getName().matches(tokenToMatch);
}