org.apache.directory.api.ldap.model.schema.LdapComparator Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.schema.LdapComparator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultSchemaManager.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Check that the given OID exists in the globalOidRegistry.
 * 
 * @param schemaObject The SchemaObject to check
 * @return <tt>true</tt> if the OID exists
 */
private boolean checkOidExist( SchemaObject schemaObject )
{
    if ( !( schemaObject instanceof LoadableSchemaObject ) )
    {
        return registries.getGlobalOidRegistry().contains( schemaObject.getOid() );
    }

    if ( schemaObject instanceof LdapComparator<?> )
    {
        return registries.getComparatorRegistry().contains( schemaObject.getOid() );
    }

    if ( schemaObject instanceof SyntaxChecker )
    {
        return registries.getSyntaxCheckerRegistry().contains( schemaObject.getOid() );
    }

    if ( schemaObject instanceof Normalizer )
    {
        return registries.getNormalizerRegistry().contains( schemaObject.getOid() );
    }

    return false;
}
 
Example #2
Source File: DefaultComparatorRegistry.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void unregisterSchemaElements( String schemaName ) throws LdapException
{
    if ( schemaName == null )
    {
        return;
    }

    // Loop on all the SchemaObjects stored and remove those associated
    // with the give schemaName
    for ( LdapComparator<?> comparator : this )
    {
        if ( schemaName.equalsIgnoreCase( comparator.getSchemaName() ) )
        {
            String oid = comparator.getOid();
            SchemaObject removed = unregister( oid );

            if ( LOG.isDebugEnabled() )
            {
                LOG.debug( I18n.msg( I18n.MSG_13702_REMOVED_FROM_REGISTRY, removed, oid ) );
            }
        }
    }
}
 
Example #3
Source File: SerializableComparator.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * @param schemaManager the schemaManager to set
 */
@SuppressWarnings("unchecked")
@Override
public void setSchemaManager( SchemaManager schemaManager )
{
    if ( wrapped == null )
    {
        try
        {
            wrapped = ( Comparator<E> )
                schemaManager.lookupComparatorRegistry( matchingRuleOid );
        }
        catch ( LdapException ne )
        {
            // Not found : get the default comparator
            wrapped = ( Comparator<E> )
                new ComparableComparator<>( matchingRuleOid );
        }
    }

    ( ( LdapComparator<E> ) wrapped ).setSchemaManager( schemaManager );
    this.schemaManager = schemaManager;
}
 
Example #4
Source File: SchemaManagerDelTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteNonExistingComparator() throws Exception
{
    SchemaManager schemaManager = loadSchema( "system" );
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();

    LdapComparator<?> lc = new BooleanComparator( "0.0" );
    assertFalse( schemaManager.delete( lc ) );

    List<Throwable> errors = schemaManager.getErrors();
    assertFalse( errors.isEmpty() );

    assertEquals( ctrSize, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );
}
 
Example #5
Source File: SchemaManagerDelTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteExistingComaparatorUsedByMatchingRule() throws Exception
{
    SchemaManager schemaManager = loadSchema( "system" );
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();

    LdapComparator<?> lc = schemaManager.lookupComparatorRegistry( "2.5.13.0" );

    // shouldn't be deleted cause there is a MR associated with it
    assertFalse( schemaManager.delete( lc ) );

    List<Throwable> errors = schemaManager.getErrors();
    assertFalse( errors.isEmpty() );
    assertTrue( errors.get( 0 ) instanceof LdapProtocolErrorException );

    assertNotNull( schemaManager.lookupComparatorRegistry( "2.5.13.0" ) );
    assertEquals( ctrSize, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );
}
 
Example #6
Source File: SchemaManagerAddTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddNewComparator() throws Exception
{
    SchemaManager schemaManager = loadSystem();
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();

    String oid = "0.0.0";
    LdapComparator<?> lc = new BooleanComparator( oid );

    assertTrue( schemaManager.add( lc ) );

    List<Throwable> errors = schemaManager.getErrors();
    assertEquals( 0, errors.size() );

    assertEquals( ctrSize + 1, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );

    LdapComparator<?> added = schemaManager.lookupComparatorRegistry( oid );

    assertNotNull( added );
    assertEquals( lc.getClass().getName(), added.getFqcn() );
}
 
Example #7
Source File: SchemaManagerAddTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddAlreadyExistingComparator() throws Exception
{
    SchemaManager schemaManager = loadSystem();
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();

    String oid = "0.0.0";
    LdapComparator<?> bc = new BooleanComparator( oid );

    assertTrue( schemaManager.add( bc ) );

    LdapComparator<?> added = schemaManager.lookupComparatorRegistry( oid );

    assertNotNull( added );
    assertEquals( bc.getClass().getName(), added.getFqcn() );

    List<Throwable> errors = schemaManager.getErrors();
    assertEquals( 0, errors.size() );
    assertEquals( ctrSize + 1, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );

    LdapComparator<?> lc = new CsnComparator( oid );

    assertFalse( schemaManager.add( lc ) );

    errors = schemaManager.getErrors();
    assertEquals( 1, errors.size() );

    assertEquals( ctrSize + 1, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );

    added = schemaManager.lookupComparatorRegistry( oid );

    assertNotNull( added );
    assertEquals( bc.getClass().getName(), added.getFqcn() );
}
 
Example #8
Source File: DefaultSchemaManager.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Add all the Schema's comparators
 * 
 * @param schema The schema in which the Comparators will be added
 * @param registries The Registries to process
 * @throws LdapException If the Comparators cannot be added
 * @throws IOException If the Comparators cannot be loaded
 */
private void addComparators( Schema schema, Registries registries ) throws LdapException, IOException
{
    if ( schema.getSchemaLoader() == null )
    {
        return;
    }
    
    for ( Entry entry : schema.getSchemaLoader().loadComparators( schema ) )
    {
        LdapComparator<?> comparator = factory.getLdapComparator( this, entry, registries, schema.getSchemaName() );

        addSchemaObject( registries, comparator, schema );
    }
}
 
Example #9
Source File: SchemaManagerDelTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
private boolean isComparatorPresent( SchemaManager schemaManager, String oid )
{
    try
    {
        LdapComparator<?> comparator = schemaManager.lookupComparatorRegistry( oid );

        return comparator != null;
    }
    catch ( LdapException ne )
    {
        return false;
    }
}
 
Example #10
Source File: SchemaManagerDelTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteExistingComparator() throws Exception
{
    SchemaManager schemaManager = loadSchema( "system" );
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();

    LdapComparator<?> lc = new BooleanComparator( "0.1.1" );
    assertTrue( schemaManager.add( lc ) );

    assertEquals( ctrSize + 1, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );

    lc = schemaManager.lookupComparatorRegistry( "0.1.1" );
    assertNotNull( lc );
    assertTrue( schemaManager.delete( lc ) );

    try
    {
        schemaManager.lookupComparatorRegistry( "0.1.1" );
        fail();
    }
    catch ( Exception e )
    {
        // expected
    }

    assertEquals( ctrSize, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );
}
 
Example #11
Source File: Registries.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Build the Comparator references
 */
private void buildComparatorReferences()
{
    for ( LdapComparator<?> comparator : comparatorRegistry )
    {
        buildReference( comparator );
    }
}
 
Example #12
Source File: SchemaManagerAddTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test that we can't add two comparators with the same class code.
 */
@Test
public void testAddComparatorWithWrongFQCN() throws Exception
{
    SchemaManager schemaManager = loadSystem();
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();

    String oid = "0.0.0";
    LdapComparator<?> lc = new BooleanComparator( oid );

    // using java.sql.ResultSet cause it is very unlikely to get loaded
    // in ADS, as the FQCN is not the one expected
    lc.setFqcn( "java.sql.ResultSet" );

    assertFalse( schemaManager.add( lc ) );

    List<Throwable> errors = schemaManager.getErrors();
    assertEquals( 1, errors.size() );

    assertEquals( ctrSize, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize, schemaManager.getGlobalOidRegistry().size() );

    try
    {
        schemaManager.lookupComparatorRegistry( oid );
        fail();
    }
    catch ( Exception e )
    {
        // Expected
        assertTrue( true );
    }
}
 
Example #13
Source File: Value.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a comparator using getMatchingRule() to resolve the matching
 * that the comparator is extracted from.
 *
 * @return a comparator associated with the attributeType or null if one cannot be found
 */
private LdapComparator<?> getLdapComparator()
{
    if ( attributeType != null )
    {
        MatchingRule mr = attributeType.getEquality();

        if ( mr != null )
        {
            return mr.getLdapComparator();
        }
    }

    return null;
}
 
Example #14
Source File: InMemoryDirectoryServiceFactory.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void init(String name) throws Exception {
    if ((directoryService != null) && directoryService.isStarted()) {
        return;
    }

    directoryService.setInstanceId(name);

    // instance layout
    InstanceLayout instanceLayout = new InstanceLayout(System.getProperty("java.io.tmpdir") + "/server-work-" + name);
    if (instanceLayout.getInstanceDirectory().exists()) {
        try {
            FileUtils.deleteDirectory(instanceLayout.getInstanceDirectory());
        } catch (IOException e) {
            LOG.warn("couldn't delete the instance directory before initializing the DirectoryService", e);
        }
    }
    directoryService.setInstanceLayout(instanceLayout);

    // EhCache in disabled-like-mode
    Configuration ehCacheConfig = new Configuration();
    CacheConfiguration defaultCache = new CacheConfiguration("ApacheDSTestCache", 1).eternal(false).timeToIdleSeconds(30)
            .timeToLiveSeconds(30).overflowToDisk(false);
    ehCacheConfig.addDefaultCache(defaultCache);
    cacheManager = new CacheManager(ehCacheConfig);
    CacheService cacheService = new CacheService(cacheManager);
    directoryService.setCacheService(cacheService);

    // Init the schema
    // SchemaLoader loader = new SingleLdifSchemaLoader();
    SchemaLoader loader = new JarLdifSchemaLoader();
    SchemaManager schemaManager = new DefaultSchemaManager(loader);
    schemaManager.loadAllEnabled();
    ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
    for (LdapComparator<?> comparator : comparatorRegistry) {
        if (comparator instanceof NormalizingComparator) {
            ((NormalizingComparator) comparator).setOnServer();
        }
    }
    directoryService.setSchemaManager(schemaManager);
    InMemorySchemaPartition inMemorySchemaPartition = new InMemorySchemaPartition(schemaManager);

    SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
    schemaPartition.setWrappedPartition(inMemorySchemaPartition);
    directoryService.setSchemaPartition(schemaPartition);
    List<Throwable> errors = schemaManager.getErrors();
    if (errors.size() != 0) {
        throw new Exception(I18n.err(I18n.ERR_317, Exceptions.printErrors(errors)));
    }

    // Init system partition
    Partition systemPartition = partitionFactory.createPartition(directoryService.getSchemaManager(), "system",
            ServerDNConstants.SYSTEM_DN, 500, new File(directoryService.getInstanceLayout().getPartitionsDirectory(),
                    "system"));
    systemPartition.setSchemaManager(directoryService.getSchemaManager());
    partitionFactory.addIndex(systemPartition, SchemaConstants.OBJECT_CLASS_AT, 100);
    directoryService.setSystemPartition(systemPartition);

    directoryService.startup();
}
 
Example #15
Source File: SchemaManagerDelTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Check that a Comparator which has been used by a deleted MatchingRule
 * can be removed
 */
@Test
public void testDeleteExistingComparatorUsedByRemovedMatchingRule() throws Exception
{
    SchemaManager schemaManager = loadSchema( "system" );
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int mrrSize = schemaManager.getMatchingRuleRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();

    String OID = "2.5.13.33";

    // Check that the MR and C are present
    assertTrue( isMatchingRulePresent( schemaManager, OID ) );
    assertTrue( isComparatorPresent( schemaManager, OID ) );

    // Now try to remove the C
    LdapComparator<?> lc = schemaManager.lookupComparatorRegistry( OID );

    // shouldn't be deleted cause there is a MR associated with it
    assertFalse( schemaManager.delete( lc ) );

    List<Throwable> errors = schemaManager.getErrors();
    assertFalse( errors.isEmpty() );
    assertTrue( errors.get( 0 ) instanceof LdapProtocolErrorException );

    // Now delete the using MR : it should be OK
    MatchingRule mr = new MatchingRule( OID );
    assertTrue( schemaManager.delete( mr ) );

    assertEquals( mrrSize - 1, schemaManager.getMatchingRuleRegistry().size() );
    assertEquals( goidSize - 1, schemaManager.getGlobalOidRegistry().size() );

    assertFalse( isMatchingRulePresent( schemaManager, OID ) );

    // and try to delete the Comparator again
    assertTrue( schemaManager.delete( lc ) );

    assertFalse( isComparatorPresent( schemaManager, OID ) );
    assertEquals( ctrSize - 1, schemaManager.getComparatorRegistry().size() );
    assertEquals( goidSize - 1, schemaManager.getGlobalOidRegistry().size() );
}
 
Example #16
Source File: InMemoryDirectoryServiceFactory.java    From bouncr with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void init(String name) throws Exception {
    if ((directoryService != null) && directoryService.isStarted()) {
        return;
    }

    directoryService.setInstanceId(name);

    // instance layout
    InstanceLayout instanceLayout = new InstanceLayout(System.getProperty("java.io.tmpdir") + "/server-work-" + name);
    if (instanceLayout.getInstanceDirectory().exists()) {
        try {
            FileUtils.deleteDirectory(instanceLayout.getInstanceDirectory());
        } catch (IOException e) {
            LOG.warn("couldn't delete the instance directory before initializing the DirectoryService", e);
        }
    }
    directoryService.setInstanceLayout(instanceLayout);

    // EhCache in disabled-like-mode
    Configuration ehCacheConfig = new Configuration();
    CacheConfiguration defaultCache = new CacheConfiguration("default", 1).eternal(false).timeToIdleSeconds(30)
            .timeToLiveSeconds(30).overflowToDisk(false);
    ehCacheConfig.addDefaultCache(defaultCache);
    CacheService cacheService = new CacheService(new CacheManager(ehCacheConfig));
    directoryService.setCacheService(cacheService);

    // Init the schema
    // SchemaLoader loader = new SingleLdifSchemaLoader();
    SchemaLoader loader = new JarLdifSchemaLoader();
    SchemaManager schemaManager = new DefaultSchemaManager(loader);
    schemaManager.loadAllEnabled();
    ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
    for (LdapComparator<?> comparator : comparatorRegistry) {
        if (comparator instanceof NormalizingComparator) {
            ((NormalizingComparator) comparator).setOnServer();
        }
    }
    directoryService.setSchemaManager(schemaManager);
    InMemorySchemaPartition inMemorySchemaPartition = new InMemorySchemaPartition(schemaManager);

    SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
    schemaPartition.setWrappedPartition(inMemorySchemaPartition);
    directoryService.setSchemaPartition(schemaPartition);
    List<Throwable> errors = schemaManager.getErrors();
    if (errors.size() != 0) {
        throw new Exception(I18n.err(I18n.ERR_317, Exceptions.printErrors(errors)));
    }

    // Init system partition
    Partition systemPartition = partitionFactory.createPartition(directoryService.getSchemaManager(),
            directoryService.getDnFactory(), "system", ServerDNConstants.SYSTEM_DN, 500,
            new File(directoryService.getInstanceLayout().getPartitionsDirectory(), "system"));
    systemPartition.setSchemaManager(directoryService.getSchemaManager());
    partitionFactory.addIndex(systemPartition, SchemaConstants.OBJECT_CLASS_AT, 100);
    directoryService.setSystemPartition(systemPartition);

    directoryService.startup();
}
 
Example #17
Source File: InMemoryDirectoryServiceFactory.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void init(String name) throws Exception {
   if ((directoryService == null) || directoryService.isStarted()) {
      return;
   }

   directoryService.setInstanceId(name);

   // instance layout
   InstanceLayout instanceLayout = new InstanceLayout(System.getProperty("java.io.tmpdir") + "/server-work-" + name);
   if (instanceLayout.getInstanceDirectory().exists()) {
      try {
         FileUtils.deleteDirectory(instanceLayout.getInstanceDirectory());
      } catch (IOException e) {
         LOG.warn("couldn't delete the instance directory before initializing the DirectoryService", e);
      }
   }
   directoryService.setInstanceLayout(instanceLayout);

   // EhCache in disabled-like-mode
   Configuration ehCacheConfig = new Configuration();
   CacheConfiguration defaultCache = new CacheConfiguration("default", 1).eternal(false).timeToIdleSeconds(30).timeToLiveSeconds(30).overflowToDisk(false);
   ehCacheConfig.addDefaultCache(defaultCache);
   CacheService cacheService = new CacheService(new CacheManager(ehCacheConfig));
   directoryService.setCacheService(cacheService);

   // Init the schema
   // SchemaLoader loader = new SingleLdifSchemaLoader();
   SchemaLoader loader = new JarLdifSchemaLoader();
   SchemaManager schemaManager = new DefaultSchemaManager(loader);
   schemaManager.loadAllEnabled();
   ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
   for (LdapComparator<?> comparator : comparatorRegistry) {
      if (comparator instanceof NormalizingComparator) {
         ((NormalizingComparator) comparator).setOnServer();
      }
   }
   directoryService.setSchemaManager(schemaManager);
   InMemorySchemaPartition inMemorySchemaPartition = new InMemorySchemaPartition(schemaManager);

   SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
   schemaPartition.setWrappedPartition(inMemorySchemaPartition);
   directoryService.setSchemaPartition(schemaPartition);
   List<Throwable> errors = schemaManager.getErrors();
   if (errors.size() != 0) {
      throw new Exception(I18n.err(I18n.ERR_317, Exceptions.printErrors(errors)));
   }

   // Init system partition
   Partition systemPartition = partitionFactory.createPartition(directoryService.getSchemaManager(), directoryService.getDnFactory(), "system", ServerDNConstants.SYSTEM_DN, 500, new File(directoryService.getInstanceLayout().getPartitionsDirectory(), "system"));
   systemPartition.setSchemaManager(directoryService.getSchemaManager());
   partitionFactory.addIndex(systemPartition, SchemaConstants.OBJECT_CLASS_AT, 100);
   directoryService.setSystemPartition(systemPartition);

   directoryService.startup();
}
 
Example #18
Source File: ApacheDSStartStopListener.java    From syncope with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the schema manager and add the schema partition to directory service.
 *
 * @throws Exception if the schema LDIF files are not found on the classpath
 */
private void initSchemaPartition() throws Exception {
    File workingDirectory = service.getInstanceLayout().getPartitionsDirectory();

    // Extract the schema on disk (a brand new one) and load the registries
    File schemaRepository = new File(workingDirectory, "schema");
    SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(workingDirectory);
    try {
        extractor.extractOrCopy();
    } catch (IOException ioe) {
        // The schema has already been extracted, bypass
    }

    SchemaLoader loader = new LdifSchemaLoader(schemaRepository);
    SchemaManager schemaManager = new DefaultSchemaManager(loader);

    // We have to load the schema now, otherwise we won't be able
    // to initialize the Partitions, as we won't be able to parse 
    // and normalize their suffix Dn
    schemaManager.loadAllEnabled();

    // Tell all the normalizer comparators that they should not normalize anything
    ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
    for (LdapComparator<?> comparator : comparatorRegistry) {
        if (comparator instanceof NormalizingComparator) {
            ((NormalizingComparator) comparator).setOnServer();
        }
    }

    service.setSchemaManager(schemaManager);

    // Init the LdifPartition
    LdifPartition ldifPartition = new LdifPartition(schemaManager, service.getDnFactory());
    ldifPartition.setPartitionPath(new File(workingDirectory, "schema").toURI());
    SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
    schemaPartition.setWrappedPartition(ldifPartition);
    service.setSchemaPartition(schemaPartition);

    List<Throwable> errors = schemaManager.getErrors();
    if (!errors.isEmpty()) {
        throw new IllegalStateException(I18n.err(I18n.ERR_317, Exceptions.printErrors(errors)));
    }
}
 
Example #19
Source File: InMemoryDirectoryServiceFactory.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void init(String name) throws Exception {
    if ((directoryService != null) && directoryService.isStarted()) {
        return;
    }

    directoryService.setInstanceId(name);

    // instance layout
    InstanceLayout instanceLayout = new InstanceLayout(System.getProperty("java.io.tmpdir") + "/server-work-" + name);
    if (instanceLayout.getInstanceDirectory().exists()) {
        try {
            FileUtils.deleteDirectory(instanceLayout.getInstanceDirectory());
        } catch (IOException e) {
            LOG.warn("couldn't delete the instance directory before initializing the DirectoryService", e);
        }
    }
    directoryService.setInstanceLayout(instanceLayout);

    // EhCache in disabled-like-mode
    Configuration ehCacheConfig = new Configuration();
    CacheConfiguration defaultCache = new CacheConfiguration("ApacheDSTestCache", 1).eternal(false).timeToIdleSeconds(30)
            .timeToLiveSeconds(30).overflowToDisk(false);
    ehCacheConfig.addDefaultCache(defaultCache);
    cacheManager = new CacheManager(ehCacheConfig);
    CacheService cacheService = new CacheService(cacheManager);
    directoryService.setCacheService(cacheService);

    // Init the schema
    // SchemaLoader loader = new SingleLdifSchemaLoader();
    SchemaLoader loader = new JarLdifSchemaLoader();
    SchemaManager schemaManager = new DefaultSchemaManager(loader);
    schemaManager.loadAllEnabled();
    ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
    for (LdapComparator<?> comparator : comparatorRegistry) {
        if (comparator instanceof NormalizingComparator) {
            ((NormalizingComparator) comparator).setOnServer();
        }
    }
    directoryService.setSchemaManager(schemaManager);
    InMemorySchemaPartition inMemorySchemaPartition = new InMemorySchemaPartition(schemaManager);

    SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
    schemaPartition.setWrappedPartition(inMemorySchemaPartition);
    directoryService.setSchemaPartition(schemaPartition);
    List<Throwable> errors = schemaManager.getErrors();
    if (errors.size() != 0) {
        throw new Exception(I18n.err(I18n.ERR_317, Exceptions.printErrors(errors)));
    }

    // Init system partition
    Partition systemPartition = partitionFactory.createPartition(directoryService.getSchemaManager(), "system",
            ServerDNConstants.SYSTEM_DN, 500, new File(directoryService.getInstanceLayout().getPartitionsDirectory(),
                    "system"));
    systemPartition.setSchemaManager(directoryService.getSchemaManager());
    partitionFactory.addIndex(systemPartition, SchemaConstants.OBJECT_CLASS_AT, 100);
    directoryService.setSystemPartition(systemPartition);

    directoryService.startup();
}
 
Example #20
Source File: InMemoryDirectoryServiceFactory.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void init(String name) throws Exception {
    if ((directoryService != null) && directoryService.isStarted()) {
        return;
    }

    directoryService.setInstanceId(name);

    // instance layout
    InstanceLayout instanceLayout = new InstanceLayout(System.getProperty("java.io.tmpdir") + "/server-work-" + name);
    if (instanceLayout.getInstanceDirectory().exists()) {
        try {
            FileUtils.deleteDirectory(instanceLayout.getInstanceDirectory());
        } catch (IOException e) {
            LOG.warn("couldn't delete the instance directory before initializing the DirectoryService", e);
        }
    }
    directoryService.setInstanceLayout(instanceLayout);

    // EhCache in disabled-like-mode
    Configuration ehCacheConfig = new Configuration();
    CacheConfiguration defaultCache = new CacheConfiguration("ApacheDSTestCache", 1).eternal(false).timeToIdleSeconds(30)
            .timeToLiveSeconds(30).overflowToDisk(false);
    ehCacheConfig.addDefaultCache(defaultCache);
    cacheManager = new CacheManager(ehCacheConfig);
    CacheService cacheService = new CacheService(cacheManager);
    directoryService.setCacheService(cacheService);

    // Init the schema
    // SchemaLoader loader = new SingleLdifSchemaLoader();
    SchemaLoader loader = new JarLdifSchemaLoader();
    SchemaManager schemaManager = new DefaultSchemaManager(loader);
    schemaManager.loadAllEnabled();
    ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
    for (LdapComparator<?> comparator : comparatorRegistry) {
        if (comparator instanceof NormalizingComparator) {
            ((NormalizingComparator) comparator).setOnServer();
        }
    }
    directoryService.setSchemaManager(schemaManager);
    InMemorySchemaPartition inMemorySchemaPartition = new InMemorySchemaPartition(schemaManager);

    SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
    schemaPartition.setWrappedPartition(inMemorySchemaPartition);
    directoryService.setSchemaPartition(schemaPartition);
    List<Throwable> errors = schemaManager.getErrors();
    if (errors.size() != 0) {
        throw new Exception(I18n.err(I18n.ERR_317, Exceptions.printErrors(errors)));
    }

    // Init system partition
    Partition systemPartition = partitionFactory.createPartition(directoryService.getSchemaManager(), "system",
            ServerDNConstants.SYSTEM_DN, 500, new File(directoryService.getInstanceLayout().getPartitionsDirectory(),
                    "system"));
    systemPartition.setSchemaManager(directoryService.getSchemaManager());
    partitionFactory.addIndex(systemPartition, SchemaConstants.OBJECT_CLASS_AT, 100);
    directoryService.setSystemPartition(systemPartition);

    directoryService.startup();
}
 
Example #21
Source File: InMemoryDirectoryServiceFactory.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void init(String name) throws Exception {
    if ((directoryService != null) && directoryService.isStarted()) {
        return;
    }

    directoryService.setInstanceId(name);

    // instance layout
    InstanceLayout instanceLayout = new InstanceLayout(System.getProperty("java.io.tmpdir") + "/server-work-" + name);
    if (instanceLayout.getInstanceDirectory().exists()) {
        try {
            FileUtils.deleteDirectory(instanceLayout.getInstanceDirectory());
        } catch (IOException e) {
            LOG.warn("couldn't delete the instance directory before initializing the DirectoryService", e);
        }
    }
    directoryService.setInstanceLayout(instanceLayout);

    // EhCache in disabled-like-mode
    Configuration ehCacheConfig = new Configuration();
    CacheConfiguration defaultCache = new CacheConfiguration("ApacheDSTestCache", 1).eternal(false).timeToIdleSeconds(30)
            .timeToLiveSeconds(30).overflowToDisk(false);
    ehCacheConfig.addDefaultCache(defaultCache);
    cacheManager = new CacheManager(ehCacheConfig);
    CacheService cacheService = new CacheService(cacheManager);
    directoryService.setCacheService(cacheService);

    // Init the schema
    // SchemaLoader loader = new SingleLdifSchemaLoader();
    SchemaLoader loader = new JarLdifSchemaLoader();
    SchemaManager schemaManager = new DefaultSchemaManager(loader);
    schemaManager.loadAllEnabled();
    ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
    for (LdapComparator<?> comparator : comparatorRegistry) {
        if (comparator instanceof NormalizingComparator) {
            ((NormalizingComparator) comparator).setOnServer();
        }
    }
    directoryService.setSchemaManager(schemaManager);
    InMemorySchemaPartition inMemorySchemaPartition = new InMemorySchemaPartition(schemaManager);

    SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
    schemaPartition.setWrappedPartition(inMemorySchemaPartition);
    directoryService.setSchemaPartition(schemaPartition);
    List<Throwable> errors = schemaManager.getErrors();
    if (errors.size() != 0) {
        throw new Exception(I18n.err(I18n.ERR_317, Exceptions.printErrors(errors)));
    }

    // Init system partition
    Partition systemPartition = partitionFactory.createPartition(directoryService.getSchemaManager(), "system",
            ServerDNConstants.SYSTEM_DN, 500, new File(directoryService.getInstanceLayout().getPartitionsDirectory(),
                    "system"));
    systemPartition.setSchemaManager(directoryService.getSchemaManager());
    partitionFactory.addIndex(systemPartition, SchemaConstants.OBJECT_CLASS_AT, 100);
    directoryService.setSystemPartition(systemPartition);

    directoryService.startup();
}
 
Example #22
Source File: TestEntryUtils.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
static AttributeType getIA5StringAttributeType()
{
    AttributeType attributeType = new AttributeType( "1.1" );
    attributeType.addName( "1.1" );
    LdapSyntax syntax = new LdapSyntax( "1.1.1", "", true );

    syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2" )
    {
        /** The mandatory serialVersionUID field */
        public static final long serialVersionUID = 1L;


        public boolean isValidSyntax( Object value )
        {
            String trimmedValue = Strings.deepTrim( ( String )value );

            return ( trimmedValue == null ) || ( trimmedValue.length() < 7 );
        }
    } );

    MatchingRule matchingRule = new MatchingRule( "1.1.2" );
    matchingRule.setSyntax( syntax );

    matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() )
    {
        /** The mandatory serialVersionUID field */
        public static final long serialVersionUID = 1L;


        public int compare( String o1, String o2 )
        {
            return ( ( o1 == null ) ? ( o2 == null ? 0 : -1 ) : ( o2 == null ? 1 : o1.compareTo( o2 ) ) );
        }
    } );

    matchingRule.setNormalizer( new DeepTrimToLowerNormalizer( matchingRule.getOid() ) );

    attributeType.setEquality( matchingRule );
    attributeType.setSyntax( syntax );

    return attributeType;
}
 
Example #23
Source File: DefaultSchemaManager.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public LdapComparator<?> lookupComparatorRegistry( String oid ) throws LdapException
{
    return registries.getComparatorRegistry().lookup( oid );
}
 
Example #24
Source File: SchemaEntityFactory.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public LdapComparator<?> getLdapComparator( SchemaManager schemaManager,
    LdapComparatorDescription comparatorDescription, Registries targetRegistries, String schemaName )
    throws LdapException
{
    checkDescription( comparatorDescription, SchemaConstants.COMPARATOR );

    // The Comparator OID
    String oid = getOid( comparatorDescription, SchemaConstants.COMPARATOR );

    // Get the schema
    Schema schema = getSchema( schemaName, targetRegistries );

    if ( schema == null )
    {
        // The schema is not loaded. We can't create the requested Comparator
        String msg = I18n.err( I18n.ERR_16022_CANNOT_ADD_CMP, comparatorDescription.getName(), schemaName );
        
        if ( LOG.isWarnEnabled() )
        {
            LOG.warn( msg );
        }
        
        throw new LdapUnwillingToPerformException( ResultCodeEnum.UNWILLING_TO_PERFORM, msg );
    }

    // The FQCN
    String fqcn = getFqcn( comparatorDescription, SchemaConstants.COMPARATOR );

    // get the byteCode
    Attribute byteCode = getByteCode( comparatorDescription, SchemaConstants.COMPARATOR );

    // Class load the comparator
    LdapComparator<?> comparator = classLoadComparator( schemaManager, oid, fqcn, byteCode );

    // Update the common fields
    setSchemaObjectProperties( comparator, comparatorDescription, schema );

    return comparator;
}
 
Example #25
Source File: Ava.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * @see Comparable#compareTo(Object)
 */
@Override
public int compareTo( Ava that )
{
    if ( that == null )
    {
        return 1;
    }

    int comp;

    if ( schemaManager == null )
    {
        // Compare the ATs
        comp = normType.compareTo( that.normType );

        if ( comp != 0 )
        {
            return comp;
        }

        // and compare the values
        if ( value == null )
        {
            if ( that.value == null )
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
        else
        {
            if ( that.value == null )
            {
                return 1;
            }
            else
            {
                comp = value.compareTo( ( Value ) that.value );

                return comp;
            }
        }
    }
    else
    {
        if ( that.schemaManager == null )
        {
            // Problem : we will apply the current Ava SchemaManager to the given Ava
            try
            {
                that.apply( schemaManager );
            }
            catch ( LdapInvalidDnException lide )
            {
                return 1;
            }
        }

        // First compare the AT OID
        comp = attributeType.getOid().compareTo( that.attributeType.getOid() );

        if ( comp != 0 )
        {
            return comp;
        }

        // Now, compare the two values using the ordering matchingRule comparator, if any
        MatchingRule orderingMR = attributeType.getOrdering();

        if ( orderingMR != null )
        {
            LdapComparator<Object> comparator = ( LdapComparator<Object> ) orderingMR.getLdapComparator();

            if ( comparator != null )
            {
                comp = value.compareTo( that.value );

                return comp;
            }
            else
            {
                comp = compareValues( that );

                return comp;
            }
        }
        else
        {
            comp = compareValues( that );

            return comp;
        }
    }
}
 
Example #26
Source File: DefaultComparatorRegistry.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new default ComparatorRegistry instance.
 */
public DefaultComparatorRegistry()
{
    super( SchemaObjectType.COMPARATOR, new OidRegistry<LdapComparator<?>>() );
}
 
Example #27
Source File: DefaultComparatorRegistry.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * @see Object#toString()
 */
@Override
public String toString()
{
    StringBuilder sb = new StringBuilder();

    sb.append( schemaObjectType ).append( ": " );
    boolean isFirst = true;

    for ( Map.Entry<String, LdapComparator<?>> entry : byName.entrySet() )
    {
        if ( isFirst )
        {
            isFirst = false;
        }
        else
        {
            sb.append( ", " );
        }

        LdapComparator<?> comparator = entry.getValue();

        String fqcn = comparator.getFqcn();
        int lastDotPos = fqcn.lastIndexOf( '.' );

        sb.append( '<' ).append( comparator.getOid() ).append( ", " );

        if ( lastDotPos > 0 )
        {
            sb.append( fqcn.substring( lastDotPos + 1 ) );
        }
        else
        {
            sb.append( fqcn );
        }

        sb.append( '>' );
    }

    return sb.toString();
}
 
Example #28
Source File: ImmutableComparatorRegistry.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void register( LdapComparator<?> comparator ) throws LdapException
{
    throw new LdapUnwillingToPerformException( ResultCodeEnum.NO_SUCH_OPERATION, I18n.err( I18n.ERR_13702_CANNOT_MODIFY_CMP_REGISTRY_COPY ) );
}
 
Example #29
Source File: ImmutableComparatorRegistry.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public LdapComparator<?> unregister( String numericOid ) throws LdapException
{
    throw new LdapUnwillingToPerformException( ResultCodeEnum.NO_SUCH_OPERATION, I18n.err( I18n.ERR_13702_CANNOT_MODIFY_CMP_REGISTRY_COPY ) );
}
 
Example #30
Source File: ImmutableComparatorRegistry.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Iterator<LdapComparator<?>> iterator()
{
    return immutableComparatorRegistry.iterator();
}