/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.test.ldap.framework;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import org.apache.directory.server.core.configuration.MutablePartitionConfiguration;
import org.apache.directory.server.unit.AbstractServerTest;
/**
* Tool to start and stop embedded LDAP server.
*
* @version $Id$
*/
public class LDAPRunner extends AbstractServerTest
{
/**
* Start the server.
*/
public void start() throws Exception
{
// Add partition 'sevenSeas'
MutablePartitionConfiguration pcfg = new MutablePartitionConfiguration();
pcfg.setName("sevenSeas");
pcfg.setSuffix("o=sevenseas");
// Create some indices
Set<String> indexedAttrs = new HashSet<String>();
indexedAttrs.add("objectClass");
indexedAttrs.add("o");
pcfg.setIndexedAttributes(indexedAttrs);
// Create a first entry associated to the partition
Attributes attrs = new BasicAttributes(true);
// First, the objectClass attribute
Attribute attr = new BasicAttribute("objectClass");
attr.add("top");
attr.add("organization");
attrs.put(attr);
// The the 'Organization' attribute
attr = new BasicAttribute("o");
attr.add("sevenseas");
attrs.put(attr);
// Associate this entry to the partition
pcfg.setContextEntry(attrs);
// As we can create more than one partition, we must store
// each created partition in a Set before initialization
Set<MutablePartitionConfiguration> pcfgs = new HashSet<MutablePartitionConfiguration>();
pcfgs.add(pcfg);
configuration.setContextPartitionConfigurations(pcfgs);
// Create a working directory
File workingDirectory = new File("server-work");
configuration.setWorkingDirectory(workingDirectory);
// Now, let's call the upper class which is responsible for the
// partitions creation
setUp();
System.out.println("LDAP server started on port [" + port + "]");
System.setProperty(LDAPTestSetup.SYSPROPNAME_LDAPPORT, "" + port);
// Load a demo ldif file
importLdif(this.getClass().getResourceAsStream("init.ldif"));
}
/**
* Shutdown the server.
*/
public void stop() throws Exception
{
tearDown();
System.clearProperty(LDAPTestSetup.SYSPROPNAME_LDAPPORT);
}
}