From 4497b3b685d15664a3c71f80369fc8f8399d5b06 Mon Sep 17 00:00:00 2001 From: Radovan Semancik Date: Fri, 25 Feb 2022 14:33:04 +0100 Subject: [PATCH] allowDuplicateResources in JarLdifSchemaLoader --- .../impl/DefaultSchemaLdifExtractor.java | 21 +++++++++ .../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 f626c328b..29319e380 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 @@ -392,6 +392,27 @@ public static URL getUniqueResource( String resourceName, String resourceDescrip } + /** + * 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 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. * 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 0584b3156..23ca0c7a2 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 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 {