You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2010/11/16 12:51:38 UTC

svn commit: r1035600 [1/2] - in /directory/shared/branches/ldap-schema-index/src/main: java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java resources/META-INF/ resources/META-INF/apacheds-schema.index

Author: seelmann
Date: Tue Nov 16 11:51:37 2010
New Revision: 1035600

URL: http://svn.apache.org/viewvc?rev=1035600&view=rev
Log:
o Get schema LDIF files from classloader instead of scanning java.class.path
o All schema LDIF files are listed in an index file
o Index was generated with find src/main/resources/schema -name "*.ldif" | sed 's:src/main/resources/::'


Added:
    directory/shared/branches/ldap-schema-index/src/main/resources/META-INF/
    directory/shared/branches/ldap-schema-index/src/main/resources/META-INF/apacheds-schema.index
Modified:
    directory/shared/branches/ldap-schema-index/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java

Modified: directory/shared/branches/ldap-schema-index/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/ldap-schema-index/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java?rev=1035600&r1=1035599&r2=1035600&view=diff
==============================================================================
--- directory/shared/branches/ldap-schema-index/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java (original)
+++ directory/shared/branches/ldap-schema-index/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java Tue Nov 16 11:51:37 2010
@@ -20,8 +20,11 @@
 package org.apache.directory.shared.ldap.schema.ldif.extractor.impl;
 
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
@@ -44,45 +47,47 @@ public final class ResourceMap
     /** the system property which can be used to load schema from a user specified
      *  resource like a absolute path to a directory or jar file.
      *  This is useful to start embedded DirectoryService in a servlet container environment
-     *  
-     *  usage: -Dschema.resource.location=/tmp/schema 
+     *
+     *  usage: -Dschema.resource.location=/tmp/schema
      *                OR
      *         -Dschema.resource.location=/tmp/shared-ldap-schema-0.9.18.jar
      *  */
     private static final String SCHEMA_RESOURCE_LOCATION = "schema.resource.location";
-    
+
     /** The logger. */
     private static final Logger LOG = LoggerFactory.getLogger( ResourceMap.class );
-    
+
+
     /**
      * Private contstructor.
      */
     private ResourceMap()
     {
     }
-    
-   /**
-    * For all elements of java.class.path OR from the resource name set in the 
-    * system property 'schema.resource.location' get a Map of resources
-    * Pattern pattern = Pattern.compile(".*").  
-    * The keys represent resource names and the boolean parameter indicates
-    * whether or not the resource is in a Jar file.
-    * 
-    * @param pattern the pattern to match
-    * @return the resources with markers - true if resource is in Jar
-    */
-    public static Map<String,Boolean> getResources( Pattern pattern )
+
+
+    /**
+     * For all elements of java.class.path OR from the resource name set in the
+     * system property 'schema.resource.location' get a Map of resources
+     * Pattern pattern = Pattern.compile(".*").
+     * The keys represent resource names and the boolean parameter indicates
+     * whether or not the resource is in a Jar file.
+     *
+     * @param pattern the pattern to match
+     * @return the resources with markers - true if resource is in Jar
+     */
+    public static Map<String, Boolean> getResources( Pattern pattern )
     {
-        HashMap<String,Boolean> retval = new HashMap<String,Boolean>();
-    
+        HashMap<String, Boolean> retval = new HashMap<String, Boolean>();
+
         String schemaResourceLoc = System.getProperty( SCHEMA_RESOURCE_LOCATION, "" );
-        
-        if( schemaResourceLoc.trim().length() > 0 )
+
+        if ( schemaResourceLoc.trim().length() > 0 )
         {
             LOG.debug( "loading from the user provider schema resource {}", schemaResourceLoc );
-            
+
             File file = new File( schemaResourceLoc );
-            if( file.exists() )
+            if ( file.exists() )
             {
                 getResources( retval, schemaResourceLoc, pattern );
             }
@@ -93,26 +98,21 @@ public final class ResourceMap
         }
         else
         {
-            String classPath = System.getProperty( "java.class.path", "." );
-            String[] classPathElements = classPath.split( File.pathSeparator );
-            
-            for ( String element : classPathElements )
-            {
-                getResources( retval, element, pattern );
-            }
+            getResourcesFromClassloader( retval, pattern );
         }
-        
+
         return retval;
     }
 
 
-    private static void getResources( HashMap<String,Boolean> map, 
+    private static void getResources( HashMap<String, Boolean> map,
         String element, Pattern pattern )
     {
         File file = new File( element );
         if ( !file.exists() )
         {
             // this may happen if the class path contains an element that doesn't exist
+            LOG.debug( "element {} does not exist", element );
             return;
         }
 
@@ -127,11 +127,11 @@ public final class ResourceMap
     }
 
 
-    private static void getResourcesFromJarFile( HashMap<String,Boolean> map, 
+    private static void getResourcesFromJarFile( HashMap<String, Boolean> map,
         File file, Pattern pattern )
     {
         ZipFile zf;
-        
+
         try
         {
             zf = new ZipFile( file );
@@ -144,15 +144,15 @@ public final class ResourceMap
         {
             throw new Error( e );
         }
-        
+
         Enumeration<? extends ZipEntry> e = zf.entries();
-        
+
         while ( e.hasMoreElements() )
         {
             ZipEntry ze = e.nextElement();
             String fileName = ze.getName();
             boolean accept = pattern.matcher( fileName ).matches();
-        
+
             if ( accept )
             {
                 map.put( fileName, Boolean.TRUE );
@@ -169,11 +169,11 @@ public final class ResourceMap
     }
 
 
-    private static void getResourcesFromDirectory( 
-        HashMap<String,Boolean> map, File directory, Pattern pattern )
+    private static void getResourcesFromDirectory(
+        HashMap<String, Boolean> map, File directory, Pattern pattern )
     {
         File[] fileList = directory.listFiles();
-        
+
         for ( File file : fileList )
         {
             if ( file.isDirectory() )
@@ -186,7 +186,7 @@ public final class ResourceMap
                 {
                     String fileName = file.getCanonicalPath();
                     boolean accept = pattern.matcher( fileName ).matches();
-        
+
                     if ( accept )
                     {
                         map.put( fileName, Boolean.FALSE );
@@ -199,4 +199,31 @@ public final class ResourceMap
             }
         }
     }
+
+
+    private static void getResourcesFromClassloader( HashMap<String, Boolean> map, Pattern pattern )
+    {
+        try
+        {
+            ClassLoader cl = ResourceMap.class.getClassLoader();
+            InputStream in = cl.getResourceAsStream( "META-INF/apacheds-schema.index" );
+            BufferedReader reader = new BufferedReader( new InputStreamReader( in, "UTF-8" ) );
+            String line = reader.readLine();
+            while ( line != null )
+            {
+                boolean accept = pattern.matcher( line ).matches();
+                if ( accept )
+                {
+                    map.put( line, Boolean.TRUE );
+                }
+                line = reader.readLine();
+            }
+            reader.close();
+        }
+        catch ( IOException e )
+        {
+            throw new Error( e );
+        }
+    }
+
 }
\ No newline at end of file