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 2022/02/25 13:34:06 UTC

[directory-ldap-api] 02/02: allowDuplicateResources in JarLdifSchemaLoader

This is an automated email from the ASF dual-hosted git repository.

semancik pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git

commit 4497b3b685d15664a3c71f80369fc8f8399d5b06
Author: Radovan Semancik <ra...@evolveum.com>
AuthorDate: Fri Feb 25 14:33:04 2022 +0100

    allowDuplicateResources in JarLdifSchemaLoader
---
 .../extractor/impl/DefaultSchemaLdifExtractor.java | 21 +++++++++++
 .../ldap/schema/loader/JarLdifSchemaLoader.java    | 44 +++++++++++++++++++++-
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/DefaultSchemaLdifExtractor.java b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/DefaultSchemaLdifExtractor.java
index f626c32..29319e3 100644
--- a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/DefaultSchemaLdifExtractor.java
+++ b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/DefaultSchemaLdifExtractor.java
@@ -393,6 +393,27 @@ public class DefaultSchemaLdifExtractor implements SchemaLdifExtractor
 
 
     /**
+     * Gets resource from the class loader.
+     * In case of several files with the same name, it returns any of them.
+     * This is useful in cases when the same artefacts are loaded several times, e.g. in some testing scenarios.
+     *
+     * @param resourceName the name of the resource
+     * @param resourceDescription the description of the resource
+     * @return the URL to the resource in the class loader
+     * @throws IOException if there is an IO error
+     */
+    public static URL getAnyResource( String resourceName, String resourceDescription ) throws IOException
+    {
+        Enumeration<URL> resources = DefaultSchemaLdifExtractor.class.getClassLoader().getResources( resourceName );
+        if ( !resources.hasMoreElements() )
+        {
+            throw new UniqueResourceException( resourceName, resourceDescription );
+        }
+        URL result = resources.nextElement();
+        return result;
+    }
+
+    /**
      * Extracts the LDIF schema resource from class loader.
      *
      * @param resource the LDIF schema resource
diff --git a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java
index 0584b31..23ca0c7 100644
--- a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java
+++ b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java
@@ -70,6 +70,7 @@ public class JarLdifSchemaLoader extends AbstractSchemaLoader
     private static final Map<String, Boolean> RESOURCE_MAP = ResourceMap.getResources( Pattern
         .compile( "schema" + SEPARATOR_PATTERN + "ou=schema.*" ) );
 
+    private final boolean allowDuplicateResources;
 
     /**
      * Creates a new LDIF based SchemaLoader. The constructor checks to make
@@ -82,15 +83,56 @@ public class JarLdifSchemaLoader extends AbstractSchemaLoader
      */
     public JarLdifSchemaLoader() throws IOException, LdapException
     {
+        this.allowDuplicateResources = false;
         initializeSchemas();
     }
 
+    /**
+     * Creates a new LDIF based SchemaLoader. The constructor checks to make
+     * sure the supplied base directory exists and contains a schema.ldif file
+     * and if not complains about it.
+     *
+     * @prarm allowDuplicateResources If set to true, loading duplicate resources is allowed.
+     * E.g. loading schema definitions that are loaded several times on the classpath.
+     * In case of several files with the same name, it returns any of them.
+     * This is useful in cases when the same artefacts are loaded several times, e.g. in some testing scenarios
+     * or weird classloading situations.
+     *
+     * @throws LdapException if the base directory does not exist or does not
+     * a valid schema.ldif file
+     * @throws IOException If we can't load the schema
+     */
+    public JarLdifSchemaLoader( boolean allowDuplicateResources ) throws IOException, LdapException
+    {
+        this.allowDuplicateResources = allowDuplicateResources;
+        initializeSchemas();
+    }
+
+    /**
+     * Returns true, if loading duplicate resources is allowed.
+     * E.g. loading schema definitions that are loaded several times on the classpath.
+     * In case of several files with the same name, it returns any of them.
+     * This is useful in cases when the same artefacts are loaded several times, e.g. in some testing scenarios
+     * or weird classloading situations.
+     */
+    public boolean isAllowDuplicateResources()
+    {
+        return allowDuplicateResources;
+    }
+
 
     private URL getResource( String resource, String msg ) throws IOException
     {
         if ( RESOURCE_MAP.get( resource ) )
         {
-            return DefaultSchemaLdifExtractor.getUniqueResource( resource, msg );
+            if ( allowDuplicateResources )
+            {
+                return DefaultSchemaLdifExtractor.getAnyResource( resource, msg );
+            }
+            else
+            {
+                return DefaultSchemaLdifExtractor.getUniqueResource( resource, msg );
+            }
         }
         else
         {