Apache Directory Project - startup and shutdown of apache 0.9.2 programmatically

This is Interesting: Free IT Magazines  
Home > Archive > Apache Directory Project > September 2005 > startup and shutdown of apache 0.9.2 programmatically





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author startup and shutdown of apache 0.9.2 programmatically
Gianmaria Clerici

2005-09-28, 5:45 pm

I am trying to start and stop apache 0.9.2 from my JUnit tests.

This is the code I have.



It starts just fine, and it seems to stop just fine.

But then I start it again, even if there are no errors, it doesn't seem
to work properly.

I get some weird LDAP error 80.



Can you suggest a better way to shut it down ?

Am I missing something ?



private void startApacheDS() throws NamingException {

Properties env;



ApplicationContext factory = new
FileSystemXmlApplicationContext("config/server.bridgestream.xml");

ServerStartupConfiguration cfg = (ServerStartupConfiguration)
factory.getBean("configuration");

env = (Properties) factory.getBean("environment");

env.setProperty(Context.PROVIDER_URL, "ou=system");

env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
ServerContextFactory.class.getName());

env.putAll(cfg.toJndiEnvironment());



new InitialDirContext(env);

}



private void stopApacheDs() throws NamingException {

Properties env;



ApplicationContext factory = new
FileSystemXmlApplicationContext("config/server.bridgestream.xml");

ShutdownConfiguration cfg = (ShutdownConfiguration)
factory.getBean("shutdownConfiguration");

env = (Properties) factory.getBean("environment");

env.setProperty(Context.PROVIDER_URL, "ou=system");

env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
ServerContextFactory.class.getName());

env.putAll(cfg.toJndiEnvironment());



new InitialDirContext(env);

}



Of course I added the bean for the shutdownConfiguration to the XML
file:



<bean id="shutdownConfiguration"
class="org.apache.ldap.server.configuration.ShutdownConfiguration">

<constructor-arg value="default"/>

</bean>


Enrique Rodriguez

2005-09-28, 5:45 pm

Gianmaria Clerici wrote:
> I am trying to start and stop apache 0.9.2 from my JUnit tests.
>
> This is the code I have.


At the risk of not directly answering your question and just confusing
you, here's what *I* do. For unit tests in the protocol-providers
(which is the same as ApacheDS in "embedded mode") I use
CoreContextFactory. With CoreContextFactory you are firing up the
backend without the LDAP or Kerberos protocols and you can configure
everything in Java, so, in your case, you'd also remove any deps on the
Spring config and XML loading. Of course, if you really need the
Directory Server and not just the directory core, then the below methods
don't apply.

The setUp line "load( ctx, "kerberos.ldif" );" is my own utility method
for loading ldifs. Similarly, "doDelete()" is my own 10 line recursive
delete dir (not commons FileUtils).

So, each setUp call results in a db and 'example' partition which is
loaded fresh with kerberos principals from my ldif. I can then test
against 'ctx' in my testXxx() methods.

HTH:

public void setUp() throws Exception
{
Hashtable env = new Hashtable( setUpPartition() );

env.put( Context.PROVIDER_URL, "dc=example, dc=com" );
env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
env.put( Context.SECURITY_AUTHENTICATION, "simple" );
env.put( Context.SECURITY_CREDENTIALS, "secret" );
env.put( Context.INITIAL_CONTEXT_FACTORY,
"org.apache.ldap.server.jndi.CoreContextFactory" );

factory = new CoreContextFactory();

ctx = (DirContext) factory.getInitialContext( env );

load( ctx, "kerberos.ldif" );
}

private Hashtable setUpPartition()
{
MutableStartupConfiguration config = new
MutableStartupConfiguration();

MutableContextPartitionConfiguration partConfig = new
MutableContextPartitionConfiguration();
partConfig.setName( "example" );

HashSet indices = new HashSet();
indices.add( "dc" );
indices.add( "ou" );
indices.add( "objectClass" );
indices.add( "krb5PrincipalName" );
indices.add( "uid" );
partConfig.setIndexedAttributes( indices );

partConfig.setSuffix( "dc=example, dc=com" );

LockableAttributesImpl attrs = new LockableAttributesImpl();
LockableAttributeImpl attr = new LockableAttributeImpl(
"objectClass" );
attr.add( "top" );
attr.add( "domain" );
attrs.put( attr );
attrs.put( "dc", "example" );
partConfig.setContextEntry( attrs );

Set schemas = new HashSet();
schemas.add( new CoreSchema() );
schemas.add( new CosineSchema() );
schemas.add( new ApacheSchema() );
schemas.add( new InetorgpersonSchema() );
schemas.add( new Krb5kdcSchema() );
schemas.add( new SystemSchema() );
schemas.add( new ApachednsSchema() );
config.setBootstrapSchemas( schemas );
config.setContextPartitionConfigurations(
Collections.singleton( partConfig ) );

return config.toJndiEnvironment();
}

public void tearDown() throws Exception
{
Hashtable env = new Hashtable();

env.put( Context.PROVIDER_URL, "ou=system" );
env.put( Context.INITIAL_CONTEXT_FACTORY,
CoreContextFactory.class.getName() );
env.putAll( new ShutdownConfiguration().toJndiEnvironment() );
env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
env.put( Context.SECURITY_CREDENTIALS, "secret" );
env.put( Context.SECURITY_AUTHENTICATION, "simple" );

new InitialContext( env );

context = null;
ldifPath = null;
loadClass = null;
configuration = new MutableStartupConfiguration();

doDelete( configuration.getWorkingDirectory() );
}

Nick Faiz

2005-09-28, 8:45 pm

Can you send a copy of the LDAP error 80?

I start and stop ApacheDS for quite a few unit tests without seeing the
problem you mention.

My startup code is identical to yours, more or less, but for the
shutdown I don't bother accessing a shutdown configuration object from
the Spring context:

public static void shutdownApacheEve() throws NamingException
{
Hashtable env = new ShutdownConfiguration().toJndiEnvironment();
env.put(Context.INITIAL_CONTEXT_FACTORY,
CoreContextFactory.class.getName());
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=system");

new InitialDirContext(env);
}

In both my shutdown and startup util methods I specify the
CoreContextFactory as the JDNI context factory, not the
ServerContextFactory .

On a side note, I've been working haphazardly on a memory partition for
exactly this sort of testing. It takes way too long to write to the file
system when you want to start and stop ApacheDS in hundreds of unit
tests. The memory partition should be out available pretty soon (fingers
crossed).

Cheers,
Nick

Gianmaria Clerici wrote:

> I am trying to start and stop apache 0.9.2 from my JUnit tests.
>
> This is the code I have.
>
> It starts just fine, and it seems to stop just fine.
>
> But then I start it again, even if there are no errors, it doesn’t
> seem to work properly.
>
> I get some weird LDAP error 80.
>
> Can you suggest a better way to shut it down ?
>
> Am I missing something ?
>
> private void startApacheDS() throws NamingException {
>
> Properties env;
>
> ApplicationContext factory = new
> FileSystemXmlApplicationContext("config/server.bridgestream.xml");
>
> ServerStartupConfiguration cfg = (ServerStartupConfiguration)
> factory.getBean("configuration");
>
> env = (Properties) factory.getBean("environment");
>
> env.setProperty(Context.PROVIDER_URL, "ou=system");
>
> env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
> ServerContextFactory.class.getName());
>
> env.putAll(cfg.toJndiEnvironment());
>
> new InitialDirContext(env);
>
> }
>
> private void stopApacheDs() throws NamingException {
>
> Properties env;
>
> ApplicationContext factory = new
> FileSystemXmlApplicationContext("config/server.bridgestream.xml");
>
> ShutdownConfiguration cfg = (ShutdownConfiguration)
> factory.getBean("shutdownConfiguration");
>
> env = (Properties) factory.getBean("environment");
>
> env.setProperty(Context.PROVIDER_URL, "ou=system");
>
> env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
> ServerContextFactory.class.getName());
>
> env.putAll(cfg.toJndiEnvironment());
>
> new InitialDirContext(env);
>
> }
>
> Of course I added the bean for the shutdownConfiguration to the XML file:
>
> <bean id="shutdownConfiguration"
> class="org.apache.ldap.server.configuration.ShutdownConfiguration">
>
> <constructor-arg value="default"/>
>
> </bean>
>



Gianmaria Clerici

2005-09-29, 5:45 pm


I found the problem.
My JUnit code was originally caching the DirContext, opps.
I forgot about that, as it was hidden in some base classes.

So after the first shutdown, and the next restart, I was still using the
same DirContext from the previous run.

And that was causing problems, for good reasons.

Thanks for the help.

-----Original Message-----
From: Nick Faiz [mailto:nick-YrDk4eUcT3KaMJb+Lgu22Q@public.gmane.org]=20
Sent: Wednesday, September 28, 2005 5:11 PM
To: Apache Directory Developers List
Subject: Re: startup and shutdown of apache 0.9.2 programmatically

Can you send a copy of the LDAP error 80?

I start and stop ApacheDS for quite a few unit tests without seeing the=20
problem you mention.

My startup code is identical to yours, more or less, but for the=20
shutdown I don't bother accessing a shutdown configuration object from=20
the Spring context:

public static void shutdownApacheEve() throws NamingException
{
Hashtable env =3D new ShutdownConfiguration().toJndiEnvironment();
env.put(Context.INITIAL_CONTEXT_FACTORY,=20
CoreContextFactory.class.getName());
env.put(Context.SECURITY_PRINCIPAL, "uid=3Dadmin,ou=3Dsystem");
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, "ou=3Dsystem");

new InitialDirContext(env);
}

In both my shutdown and startup util methods I specify the=20
CoreContextFactory as the JDNI context factory, not the=20
ServerContextFactory .

On a side note, I've been working haphazardly on a memory partition for=20
exactly this sort of testing. It takes way too long to write to the file

system when you want to start and stop ApacheDS in hundreds of unit=20
tests. The memory partition should be out available pretty soon (fingers

crossed).

Cheers,
Nick

Gianmaria Clerici wrote:

> I am trying to start and stop apache 0.9.2 from my JUnit tests.
>
> This is the code I have.
>
> It starts just fine, and it seems to stop just fine.
>
> But then I start it again, even if there are no errors, it doesn't=20
> seem to work properly.
>
> I get some weird LDAP error 80.
>
> Can you suggest a better way to shut it down ?
>
> Am I missing something ?
>
> private void startApacheDS() throws NamingException {
>
> Properties env;
>
> ApplicationContext factory =3D new=20
> FileSystemXmlApplicationContext("config/server.bridgestream.xml");
>
> ServerStartupConfiguration cfg =3D (ServerStartupConfiguration)=20
> factory.getBean("configuration");
>
> env =3D (Properties) factory.getBean("environment");
>
> env.setProperty(Context.PROVIDER_URL, "ou=3Dsystem");
>
> env.setProperty(Context.INITIAL_CONTEXT_FACTORY,=20
> ServerContextFactory.class.getName());
>
> env.putAll(cfg.toJndiEnvironment());
>
> new InitialDirContext(env);
>
> }
>
> private void stopApacheDs() throws NamingException {
>
> Properties env;
>
> ApplicationContext factory =3D new=20
> FileSystemXmlApplicationContext("config/server.bridgestream.xml");
>
> ShutdownConfiguration cfg =3D (ShutdownConfiguration)=20
> factory.getBean("shutdownConfiguration");
>
> env =3D (Properties) factory.getBean("environment");
>
> env.setProperty(Context.PROVIDER_URL, "ou=3Dsystem");
>
> env.setProperty(Context.INITIAL_CONTEXT_FACTORY,=20
> ServerContextFactory.class.getName());
>
> env.putAll(cfg.toJndiEnvironment());
>
> new InitialDirContext(env);
>
> }
>
> Of course I added the bean for the shutdownConfiguration to the XML

file:
>
> <bean id=3D"shutdownConfiguration"=20
> class=3D"org.apache.ldap.server.configuration.ShutdownConfiguration">
>
> <constructor-arg value=3D"default"/>
>
> </bean>
>



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com