Skip to content
Permalink
Browse files
allowDuplicateResources in JarLdifSchemaLoader
  • Loading branch information
semancik committed Feb 25, 2022
1 parent 76f4895 commit 4497b3b685d15664a3c71f80369fc8f8399d5b06
Showing 2 changed files with 64 additions and 1 deletion.
@@ -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<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.
*
@@ -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
{

0 comments on commit 4497b3b

Please sign in to comment.