olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [22/38] git commit: [OLINGO-239] Changed folder path handling
Date Sun, 18 May 2014 05:06:50 GMT
[OLINGO-239] Changed folder path handling


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/e1b6ead9
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/e1b6ead9
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/e1b6ead9

Branch: refs/heads/Olingo-129_PocJpaDataStore
Commit: e1b6ead92c2bb71055e99cc9f762e4bccbd96974
Parents: df76930
Author: Michael Bolz <mibo@apache.org>
Authored: Fri Apr 11 06:00:04 2014 +0200
Committer: Michael Bolz <mibo@apache.org>
Committed: Fri Apr 11 08:27:13 2014 +0200

----------------------------------------------------------------------
 .../processor/core/util/ClassHelper.java        | 56 ++++++++++++--------
 .../processor/core/util/ClassHelperTest.java    | 20 +++++++
 2 files changed, 53 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/e1b6ead9/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelper.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelper.java
b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelper.java
index 22ba73c..d81d9cf 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelper.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelper.java
@@ -23,6 +23,8 @@ import java.io.FileFilter;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -58,25 +60,22 @@ public class ClassHelper {
     }
   };
 
-  public static final List<Class<?>> loadClasses(final String packageToScan,
final ClassValidator cv) {
+  public static List<Class<?>> loadClasses(final String packageToScan, final
ClassValidator cv) {
     return loadClasses(packageToScan, CLASSFILE_FILTER, cv);
   }
 
-  public static final List<Class<?>> loadClasses(final String packageToScan,
final FilenameFilter ff,
+  public static List<Class<?>> loadClasses(final String packageToScan, final
FilenameFilter ff,
       final ClassValidator cv) {
     final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-    String folderToScan = packageToScan.replace(PACKAGE_SEPARATOR, RESOURCE_SEPARATOR);
-    URL url = classLoader.getResource(folderToScan);
-    if (url == null) {
-      throw new IllegalArgumentException("No folder to scan found for package '" + packageToScan
+ "'.");
-    }
+
+    URI uri = getResourceUri(packageToScan, classLoader);
 
     final Collection<String> fqnForClasses;
-    File folder = new File(url.getFile());
+    File folder = new File(uri.getSchemeSpecificPart());
     if (folder.isDirectory()) {
       fqnForClasses = getClassFqnFromDir(ff, folder, packageToScan);
-    } else if (isJarFile(url)) {
-      fqnForClasses = getClassFqnFromJar(url.getFile().substring(5), packageToScan);
+    } else if (isJarFile(uri)) {
+      fqnForClasses = getClassFqnFromJar(uri, packageToScan);
     } else {
       fqnForClasses = null;
     }
@@ -94,7 +93,7 @@ public class ClassHelper {
         }
       } catch (ClassNotFoundException ex) {
         throw new IllegalArgumentException("Exception during class loading of class '" +
fqn +
-            " from resource '" + url.getFile() + "'" +
+            " from resource '" + uri + "'" +
             "' with message '" + ex.getMessage() + "'.");
       }
     }
@@ -102,18 +101,29 @@ public class ClassHelper {
     return annotatedClasses;
   }
 
-  private static boolean isJarFile(final URL url) {
-    String filename = url.getFile();
-    int index = filename.indexOf(JAR_RESOURCE_SEPARATOR);
-    if (index > JAR_FILE_ENDING.length()) {
-      String fileEnding = filename.substring(index - JAR_FILE_ENDING.length(), index);
-      return JAR_FILE_ENDING.equalsIgnoreCase(fileEnding);
+  private static URI getResourceUri(String packageToScan, ClassLoader classLoader) {
+    String folderToScan = packageToScan.replace(PACKAGE_SEPARATOR, RESOURCE_SEPARATOR);
+    URL url = classLoader.getResource(folderToScan);
+    if (url == null) {
+      throw new IllegalArgumentException("No folder to scan found for package '" + packageToScan
+ "'.");
+    }
+    try {
+      URI uri = url.toURI();
+      if (uri == null) {
+        throw new IllegalArgumentException("No folder to scan found for package '" + packageToScan
+ "'.");
+      }
+      return uri;
+    } catch (URISyntaxException e) {
+      throw new IllegalArgumentException("Invalid folder path for path URL '" + url +
+              "' from thread context class loader.");
     }
-    return false;
   }
 
-  private static Collection<String> getClassFqnFromDir(final FilenameFilter ff, final
File folder,
-      final String packageToScan) {
+  private static boolean isJarFile(URI uri) {
+    return JAR_FILE_ENDING.equals(uri.getScheme());
+  }
+
+  private static Collection<String> getClassFqnFromDir(final FilenameFilter ff, File
folder, String packageToScan) {
     List<String> classFiles = new ArrayList<String>();
     String[] classFilesForFolder = folder.list(ff);
     for (String name : classFilesForFolder) {
@@ -129,10 +139,9 @@ public class ClassHelper {
     return classFiles;
   }
 
-  private static Collection<String> getClassFqnFromJar(final String filepath, final
String packageToScan) {
-    JarFile jarFile = null;
-
+  private static Collection<String> getClassFqnFromJar(URI uri, String packageToScan)
{
     final String jarFilePath;
+    String filepath = uri.getSchemeSpecificPart().substring(5);
     String[] split = filepath.split(JAR_RESOURCE_SEPARATOR);
     if (split.length == 2) {
       jarFilePath = split[0];
@@ -140,6 +149,7 @@ public class ClassHelper {
       throw new IllegalArgumentException("Illegal jar file path '" + filepath + "'.");
     }
 
+    JarFile jarFile = null;
     try {
       jarFile = new JarFile(jarFilePath);
       List<String> classFileNames = new ArrayList<String>();

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/e1b6ead9/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelperTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelperTest.java
b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelperTest.java
index 393b651..42dd259 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelperTest.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/ClassHelperTest.java
@@ -15,6 +15,8 @@
  */
 package org.apache.olingo.odata2.annotation.processor.core.util;
 
+import java.io.File;
+import java.net.URL;
 import java.util.List;
 
 import junit.framework.Assert;
@@ -59,6 +61,24 @@ public class ClassHelperTest {
     Assert.assertEquals(SimpleEntity.class.getName(), loadedClasses.get(0).getName());
   }
 
+  @Test(expected = ClassFormatError.class)
+  public void loadFromSpaceDir() throws Exception {
+    URL currentPath = Thread.currentThread().getContextClassLoader().getResource(".");
+    File folder = new File(currentPath.getFile(), "space space/package");
+    folder.mkdirs();
+    File classFile = new File(folder, "Invalid.class");
+    classFile.createNewFile();
+    String packageToScan = "space space.package";
+
+    //
+    List<Class<?>> loadedClasses = ClassHelper.loadClasses(packageToScan, annotatedTestEntityInnerClasses);
+
+    //
+    Assert.assertEquals(1, loadedClasses.size());
+    Assert.assertEquals(SimpleEntity.class.getName(), loadedClasses.get(0).getName());
+  }
+
+
   @Test
   public void loadSingleEntityFromJar() throws ODataException {
     String packageToScan = AnnotatedEntity.class.getPackage().getName();


Mime
View raw message