Hey Ersin and Emmanuel,

Thanks for all the code and helpful comments!

In this case I just need the entry
"cn=test, cn=test1, cn=test2, ou=system"
to exist, before attempting to store a DataGraph,
and I discovered that ApacheDS
lets me do

ctx.createSubcontext("cn=whatever");

without specifying any other attributes, which
works fine for this particular scenario, since the
DAS will be creating the entry, per using the cn attribute
"Convention".

I'm assuming this works since cn is part of the
top ObjectClass, which I'm guessing is added automatically.

So if we don't have a utility for doing

ctx.createSubcontext("cn=whatever2, cn=whatever1, ou=system");

I'll just create one.

Do we have a home for utilities like this or should
I just keep it as a DAS specific utility?

Thanks,
- Ole




Emmanuel Lecharny wrote:
Ole Ersoy a écrit :

Anyone know whether we have / there exists a utility
library for creating subcontext(s).

Sort of like commons FileUtil...but for JNDI.

Here's what I want to do:
Suppose the suffix I'm binding to is ou=system.
That's the only entry so far.

I wish to create the following entry:

String testEntry = "cn=test, cn=test1, cn=test2, ou=system"

You simply have to create entry "cn=test2, ou=system", then entry "cn=test1, cn=test2, ou=system", then entry "cn=test, cn=test1, cn=test2, ou=system", and changing the context each time.


It's like with directory :
mkdir /system/test2
mkdir /system/test2/test1
mkdir /system/test2/test1/test

you don't have such a command like
mkdirall /system/test2/test1/test

Here, each entry must exist before you add sub elements to it (there is not exactly a difference between entires and context, a wontext is just a position in a tree. It's not the same as directory and files, even if a directory is seen as a file on linux - you can vi a directory -)

So in your case, here what you can do :
Hashtable<String,Object> adminEnv = new Hashtable<String,Object>( envFinal );
       env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
       env.put( Context.SECURITY_CREDENTIALS, "secret" );
       env.put( Context.SECURITY_AUTHENTICATION, "simple" );
       env.put( Context.PROVIDER_URL, "ou=schema" );
       systemRoot = new InitialLdapContext( env, null );

       // creating cn=test2,ou=system
       Attributes attributes = new AttributesImpl( true );
       Attribute attribute = new AttributeImpl( "objectClass" );
       attribute.add( "top" );
       attribute.add( "person" );
       attributes.put( attribute );
       attributes.put( "cn", "test2" );
       attributes.put( "sn", "test2" );

DirContext ctx = systemRoot.createSubcontext( "cn=test2", attributes" );

       // creating cn=test1,cn=test2,ou=system
       ctx = ( DirContext ) systemRoot.lookup( "cn=test2" );

       Attributes attributes = new AttributesImpl( true );
       Attribute attribute = new AttributeImpl( "objectClass" );
       attribute.add( "top" );
       attribute.add( "person" );
       attributes.put( attribute );
       attributes.put( "cn", "test1" );
       attributes.put( "sn", "test1" );

       ctx.createSubcontext( "cn=test1", attributes );

       // creating cn=test,cn=test1,cn=test2,ou=system
       ctx = ( DirContext ) systemRoot.lookup( "cn=test1,cn=test2" );

       Attributes attributes = new AttributesImpl( true );
       Attribute attribute = new AttributeImpl( "objectClass" );
       attribute.add( "top" );
       attribute.add( "person" );
       attributes.put( attribute );
       attributes.put( "cn", "test" );
       attributes.put( "sn", "test" );

       ctx.createSubcontext( "cn=test", attributes );

It should work (not tested).

Emmanuel


Reply via email to