directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefan Seelmann <seelm...@apache.org>
Subject Schema Index and Maven plugin was: Re: svn commit: r1038592 - in /directory/shared/trunk/ldap-schema/src/main: java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java resources/META-INF/ resources/META-INF/apacheds-schema.in
Date Wed, 24 Nov 2010 15:10:57 GMT
Hi guys,

I committed the change below. It allows to use the schema loaders in
embedded environments (especially in Apache Directory Studio, but
should also work for web applications).

The idea is to have a fixed-named schema index file
"META-INF/apacheds-schema.index" that contains all resource paths to
the schema LDIF files. The ResourceMap class then can read that file
form its class loader to get all resources. Thanks to Owen Jacobson
for the idea.

Currently the index file is in the repository. I'm going to write a
Maven plugin to create that file automatically during the build. The
Maven plugin should bind to the generate-resource phase. The
alternative would be to use the exec plugin that executes something
like
  find src/main/resources/schema -name "*.ldif" | sed
's:src/main/resources/::' >
target/classes/META-INF/apacheds-schema.index
but that wouldn't work on Windows.

If anyone has a better idea how to avoid a new Maven plugin, please tell.

Kind Regards,
Stefan


On Wed, Nov 24, 2010 at 2:08 PM,  <seelmann@apache.org> wrote:
> Author: seelmann
> Date: Wed Nov 24 13:08:26 2010
> New Revision: 1038592
>
> URL: http://svn.apache.org/viewvc?rev=1038592&view=rev
> Log:
> Fix that schema could be loaded from class loader, required for embedded environments
(OSGi). Also added an index file, which should be generated automatically.
>
> Added:
>    directory/shared/trunk/ldap-schema/src/main/resources/META-INF/
>      - copied from r1037065, directory/shared/branches/ldap-schema-index/src/main/resources/META-INF/
>    directory/shared/trunk/ldap-schema/src/main/resources/META-INF/apacheds-schema.index
>      - copied unchanged from r1037065, directory/shared/branches/ldap-schema-index/src/main/resources/META-INF/apacheds-schema.index
> Modified:
>    directory/shared/trunk/ldap-schema/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java
>
> Modified: directory/shared/trunk/ldap-schema/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java
> URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap-schema/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java?rev=1038592&r1=1038591&r2=1038592&view=diff
> ==============================================================================
> --- directory/shared/trunk/ldap-schema/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java
(original)
> +++ directory/shared/trunk/ldap-schema/src/main/java/org/apache/directory/shared/ldap/schema/ldif/extractor/impl/ResourceMap.java
Wed Nov 24 13:08:26 2010
> @@ -20,8 +20,12 @@
>  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.net.URL;
>  import java.util.Enumeration;
>  import java.util.HashMap;
>  import java.util.Map;
> @@ -44,45 +48,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 +99,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 +128,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 +145,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 +170,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 +187,7 @@ public final class ResourceMap
>                 {
>                     String fileName = file.getCanonicalPath();
>                     boolean accept = pattern.matcher( fileName ).matches();
> -
> +
>                     if ( accept )
>                     {
>                         map.put( fileName, Boolean.FALSE );
> @@ -199,4 +200,35 @@ public final class ResourceMap
>             }
>         }
>     }
> +
> +
> +    private static void getResourcesFromClassloader( HashMap<String, Boolean>
map, Pattern pattern )
> +    {
> +        try
> +        {
> +            ClassLoader cl = ResourceMap.class.getClassLoader();
> +            Enumeration<URL> indexes = cl.getResources( "META-INF/apacheds-schema.index"
);
> +            while(indexes.hasMoreElements()) {
> +                URL index = indexes.nextElement();
> +                InputStream in = index.openStream();
> +                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
>
>
>

Mime
View raw message