Hi.
Firts of all, thanks for help. I attached the source code, and when i
run the programing this is the result.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO]
------------------------------------------------------------------------
[INFO] Building ApacheDS embedded sample 1.5.7
[INFO]
------------------------------------------------------------------------
[INFO] 
[INFO] >>> exec-maven-plugin:1.2:java (default-cli) @
apacheds-embedded-sample >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.2:java (default-cli) @
apacheds-embedded-sample <<<
[INFO] 
[INFO] --- exec-maven-plugin:1.2:java (default-cli) @
apacheds-embedded-sample ---
18/03/2011 08:40:06 com.ati.teste.HelloWorldPartition setSchemaManager
INFO: set schema manager
[08:40:15] ERROR
[org.apache.directory.shared.ldap.entry.DefaultServerAttribute] -
ERR_04450 The value {0} is incorrect, it hasnt been added
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition setSchemaManager
INFO: set schema manager
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition getSuffixDn
INFO: get suffix dn
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition isInitialized
INFO: Is initialized
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition initialize
INFO: Initializing HelloWorldPartition with suffix dc=ericsson
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition getSchemaManager
INFO: getSchemaManager
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition getSuffixDn
INFO: get suffix dn
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition initialize
INFO: Initializing done.
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition getSuffixDn
INFO: get suffix dn
18/03/2011 08:40:15 com.ati.teste.HelloWorldPartition getSuffixDn
INFO: get suffix dn
org.apache.directory.shared.ldap.exception.LdapNoSuchObjectException:
ERR_268 Cannot find a partition for dc=ericsson
        at
org.apache.directory.server.core.partition.DefaultPartitionNexus.getPartition(DefaultPartitionNexus.java:1010)
        at
org.apache.directory.server.core.partition.DefaultPartitionNexus.search(DefaultPartitionNexus.java:875)
        at
org.apache.directory.server.core.ReferralManagerImpl.init(ReferralManagerImpl.java:162)
        at
org.apache.directory.server.core.ReferralManagerImpl.<init>(ReferralManagerImpl.java:76)
        at
org.apache.directory.server.core.referral.ReferralInterceptor.init(ReferralInterceptor.java:206)
        at
org.apache.directory.server.core.interceptor.InterceptorChain.register0(InterceptorChain.java:442)
        at
org.apache.directory.server.core.interceptor.InterceptorChain.register(InterceptorChain.java:398)
        at
org.apache.directory.server.core.interceptor.InterceptorChain.init(InterceptorChain.java:258)
        at
org.apache.directory.server.core.DefaultDirectoryService.initialize(DefaultDirectoryService.java:1447)
        at
org.apache.directory.server.core.DefaultDirectoryService.startup(DefaultDirectoryService.java:907)
        at com.ati.teste.EmbeddedADS.initDirectoryService(EmbeddedADS.java:164)
        at com.ati.teste.EmbeddedADS.<init>(EmbeddedADS.java:236)
        at com.ati.teste.EmbeddedADS.main(EmbeddedADS.java:264)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:291)
        at java.lang.Thread.run(Thread.java:662)
[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]
------------------------------------------------------------------------
[INFO] Total time: 22.222s
[INFO] Finished at: Fri Mar 18 08:40:15 BRT 2011
[INFO] Final Memory: 20M/126M
[INFO]
------------------------------------------------------------------------
[WARNING] The requested profile "webclient" could not be activated
because it does not exist.

Em Qui, 2011-03-17 às 21:45 +0100, Andrea Gariboldi escreveu:
> I'm not very up-to-date but i can help you... i wrote the Oracle
> partition.
> What you need exactly? 
> 
> 
> 2011/3/17 Guilherme <[email protected]>
>         Hi.
>         I need to develope a new partition for ApacheDS could someone
>         help me.
>         Thanks.
>         
> 

package com.ati.teste;

import java.io.File;
import java.util.HashSet;
import java.util.List;

import org.apache.directory.server.constants.ServerDNConstants;
import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.DirectoryService;
import org.apache.directory.server.core.partition.Partition;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
import org.apache.directory.server.core.partition.ldif.LdifPartition;
import org.apache.directory.server.core.schema.SchemaPartition;
import org.apache.directory.server.ldap.LdapServer;
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.apache.directory.server.xdbm.Index;
import org.apache.directory.shared.ldap.entry.ServerEntry;
import org.apache.directory.shared.ldap.name.DN;
import org.apache.directory.shared.ldap.schema.SchemaManager;
import org.apache.directory.shared.ldap.schema.ldif.extractor.SchemaLdifExtractor;
import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.DefaultSchemaLdifExtractor;
import org.apache.directory.shared.ldap.schema.loader.ldif.LdifSchemaLoader;
import org.apache.directory.shared.ldap.schema.manager.impl.DefaultSchemaManager;
import org.apache.directory.shared.ldap.schema.registries.SchemaLoader;

public class EmbeddedADS {
	/** The directory service */
	private DirectoryService service;

	/** The LDAP server */
	private LdapServer server;

	/**
	 * Add a new partition to the server
	 * 
	 * @param partitionId
	 *            The partition Id
	 * @param partitionDn
	 *            The partition DN
	 * @return The newly added partition
	 * @throws Exception
	 *             If the partition can't be added
	 */
	private Partition addPartition(String partitionId, String partitionDn)
			throws Exception {
		// Create a new partition named 'foo'.
		JdbmPartition partition = new JdbmPartition();
		partition.setId(partitionId);
		partition.setPartitionDir(new File(service.getWorkingDirectory(),
				partitionId));
		partition.setSuffix(partitionDn);
		service.addPartition(partition);

		return partition;
	}

	/**
	 * Add a new set of index on the given attributes
	 * 
	 * @param partition
	 *            The partition on which we want to add index
	 * @param attrs
	 *            The list of attributes to index
	 */
	private void addIndex(Partition partition, String... attrs) {
		// Index some attributes on the apache partition
		HashSet<Index<?, ServerEntry, Long>> indexedAttributes = new HashSet<Index<?, ServerEntry, Long>>();

		for (String attribute : attrs) {
			indexedAttributes
					.add(new JdbmIndex<String, ServerEntry>(attribute));
		}

		((JdbmPartition) partition).setIndexedAttributes(indexedAttributes);
	}

	/**
	 * initialize the schema manager and add the schema partition to diectory
	 * service
	 * 
	 * @throws Exception
	 *             if the schema LDIF files are not found on the classpath
	 */
	private void initSchemaPartition() throws Exception {
		SchemaPartition schemaPartition = service.getSchemaService()
				.getSchemaPartition();

		// Init the LdifPartition
		LdifPartition ldifPartition = new LdifPartition();
		String workingDirectory = service.getWorkingDirectory().getPath();
		ldifPartition.setWorkingDirectory(workingDirectory + "/schema");

		// Extract the schema on disk (a brand new one) and load the registries
		File schemaRepository = new File(workingDirectory, "schema");
		SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(
				new File(workingDirectory));
		extractor.extractOrCopy(true);

		schemaPartition.setWrappedPartition(ldifPartition);

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

		// 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();

		schemaPartition.setSchemaManager(schemaManager);

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

		if (errors.size() != 0) {
			throw new Exception("Schema load failed : " + errors);
		}
	}

	/**
	 * Initialize the server. It creates the partition, adds the index, and
	 * injects the context entries for the created partitions.
	 * 
	 * @param workDir
	 *            the directory to be used for storing the data
	 * @throws Exception
	 *             if there were some problems while initializing the system
	 */
	private void initDirectoryService(File workDir) throws Exception {
		// Initialize the LDAP service
		service = new DefaultDirectoryService();
		service.setWorkingDirectory(workDir);

		// first load the schema
		initSchemaPartition();

		// then the system partition
		// this is a MANDATORY partition
		Partition systemPartition = addPartition("system",
				ServerDNConstants.SYSTEM_DN);
		service.setSystemPartition(systemPartition);

		// Disable the ChangeLog system
		service.getChangeLog().setEnabled(false);
		service.setDenormalizeOpAttrsEnabled(true);

		// Now we can create as many partitions as we need
		// Create some new partitions named 'foo', 'bar' and 'apache'.
		/*
		 * Partition fooPartition = addPartition("foo", "dc=foo,dc=com");
		 * Partition barPartition = addPartition("bar", "dc=bar,dc=com");
		 * Partition apachePartition = addPartition("apache",
		 * "dc=apache,dc=org");
		 */
		Partition sglPartition = new HelloWorldPartition("ericsson",
				"dc=ericsson");
		service.addPartition(sglPartition);
		

		// Index some attributes on the apache partition
		// addIndex(sglPartition, "objectClass", "ou", "uid");

		// And start the service
		service.startup();

		// Inject the foo root entry if it does not already exist
		/*
		 * try { service.getAdminSession().lookup(fooPartition.getSuffixDn()); }
		 * catch (LdapException lnnfe) { DN dnFoo = new DN("dc=foo,dc=com");
		 * ServerEntry entryFoo = service.newEntry(dnFoo);
		 * entryFoo.add("objectClass", "top", "domain", "extensibleObject");
		 * entryFoo.add("dc", "foo"); service.getAdminSession().add(entryFoo); }
		 * 
		 * // Inject the bar root entry try {
		 * service.getAdminSession().lookup(barPartition.getSuffixDn()); } catch
		 * (LdapException lnnfe) { DN dnBar = new DN("dc=bar,dc=com");
		 * ServerEntry entryBar = service.newEntry(dnBar);
		 * entryBar.add("objectClass", "top", "domain", "extensibleObject");
		 * entryBar.add("dc", "bar"); service.getAdminSession().add(entryBar); }
		 */

		// Inject the apache root entry
		if (!service.getAdminSession().exists(sglPartition.getSuffixDn())) {
			DN dnEricsson = new DN("dc=ericsson");
			ServerEntry entryEricsson = service.newEntry(dnEricsson);
			entryEricsson.add("objectClass", "top", "dcObject", "organization");
			entryEricsson.add("dc", "ericsson");
			entryEricsson.add("o", "Sony Ericsson");
			service.getAdminSession().add(entryEricsson);
		}
		DN dnPessoas = new DN("ou=Pessoas,dc=ericsson");
		if (!service.getAdminSession().exists(dnPessoas)) {
			ServerEntry entryPessoas = service.newEntry(dnPessoas);
			entryPessoas.add("objectClass", "top", "organizationalUnit");
			entryPessoas.add("ou", "Pessoas");
			entryPessoas.add("description", "Base teste de pessoas");
			service.getAdminSession().add(entryPessoas);
		}
		DN dnAdmin = new DN("cn=admin,ou=Pessoas,dc=ericsson");
		if (!service.getAdminSession().exists(dnAdmin)) {
			ServerEntry entryAdmin = service.newEntry(dnAdmin);
			entryAdmin.add("objectClass", "top", "person");
			entryAdmin.add("cn", "admin");
			entryAdmin.add("sn", "ati");
			service.getAdminSession().add(entryAdmin);
		}
		DN dnSglopr = new DN("cn=sglopr,ou=Pessoas,dc=ericsson");
		if (!service.getAdminSession().exists(dnSglopr)) {
			ServerEntry entrySglopr = service.newEntry(dnSglopr);
			entrySglopr.add("objectClass", "top", "person");
			entrySglopr.add("cn", "sglopr");
			entrySglopr.add("sn", "ati");
			service.getAdminSession().add(entrySglopr);
		}
		DN dnSglconfig = new DN("cn=sglconfig,ou=Pessoas,dc=ericsson");
		if (!service.getAdminSession().exists(dnSglconfig)) {
			ServerEntry entrySglconfig = service.newEntry(dnSglconfig);
			entrySglconfig.add("objectClass", "top", "person");
			entrySglconfig.add("cn", "sglconfig");
			entrySglconfig.add("sn", "ati");
			// entrySglconfig.add("userPassword",
			// "e1NTSEF9bzJLSWoxTllwY3dqdk4zMWp4ZVpyZ2xnRjdCTVVqTDI=");
			service.getAdminSession().add(entrySglconfig);
		}
		// We are all done !
	}

	/**
	 * Creates a new instance of EmbeddedADS. It initializes the directory
	 * service.
	 * 
	 * @throws Exception
	 *             If something went wrong
	 */
	public EmbeddedADS(File workDir) throws Exception {
		initDirectoryService(workDir);
	}

	/**
	 * starts the LdapServer
	 * 
	 * @throws Exception
	 */
	public void startServer() throws Exception {
		server = new LdapServer();
		int serverPort = 10389;
		server.setTransports(new TcpTransport(serverPort));
		server.setDirectoryService(service);
		server.start();
	}

	/**
	 * Main class.
	 * 
	 * @param args
	 *            Not used.
	 */
	public static void main(String[] args) {
		try {
			File workDir = new File("/media/Dados/apacheds_1.5.7/example.com");
			workDir.mkdirs();

			// Create the server
			EmbeddedADS ads = new EmbeddedADS(workDir);

			// Read an entry
			/*
			 * Entry result = ads.service.getAdminSession().lookup( new
			 * DN("cn=sglconfig,ou=Pessoas,dc=ericsson"));
			 */

			// And print it if available
			// System.out.println("Found entry : " + result);

			// optionally we can start a server too
			ads.startServer();
		} catch (Exception e) {
			// Ok, we have something wrong going on ...
			e.printStackTrace();
		}
	}
}
package com.ati.teste;

import java.util.logging.Logger;

import org.apache.directory.server.core.entry.ClonedServerEntry;
import org.apache.directory.server.core.filtering.BaseEntryFilteringCursor;
import org.apache.directory.server.core.filtering.EntryFilteringCursor;
import org.apache.directory.server.core.interceptor.context.AddOperationContext;
import org.apache.directory.server.core.interceptor.context.BindOperationContext;
import org.apache.directory.server.core.interceptor.context.DeleteOperationContext;
import org.apache.directory.server.core.interceptor.context.EntryOperationContext;
import org.apache.directory.server.core.interceptor.context.ListOperationContext;
import org.apache.directory.server.core.interceptor.context.LookupOperationContext;
import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
import org.apache.directory.server.core.interceptor.context.MoveAndRenameOperationContext;
import org.apache.directory.server.core.interceptor.context.MoveOperationContext;
import org.apache.directory.server.core.interceptor.context.RenameOperationContext;
import org.apache.directory.server.core.interceptor.context.SearchOperationContext;
import org.apache.directory.server.core.interceptor.context.UnbindOperationContext;
import org.apache.directory.server.core.partition.Partition;
import org.apache.directory.shared.ldap.cursor.EmptyCursor;
import org.apache.directory.shared.ldap.cursor.SingletonCursor;
import org.apache.directory.shared.ldap.entry.DefaultServerEntry;
import org.apache.directory.shared.ldap.entry.ServerEntry;
import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.name.DN;
import org.apache.directory.shared.ldap.schema.SchemaManager;

public class HelloWorldPartition implements Partition {

	private static final Logger LOG = Logger
			.getLogger(HelloWorldPartition.class.getName());
	private String id;
	private String suffix;
	private ServerEntry entryEricsson;
	private SchemaManager schemaManager;
	private DN dn;

	public HelloWorldPartition(String id, String suffix) {
		this.id = id;
		this.suffix = suffix;
	}

	public void add(AddOperationContext arg0) throws Exception {
		LOG.info("add");
		// Não faz nada
	}

	public void delete(DeleteOperationContext arg0) throws Exception {
		LOG.info("delete");
		// Não faz nada
	}

	public void modify(ModifyOperationContext arg0) throws Exception {
		LOG.info("modify");
		// Não faz nada
	}

	public void move(MoveOperationContext arg0) throws Exception {
		LOG.info("move");
		// Não faz nada
	}

	public void moveAndRename(MoveAndRenameOperationContext arg0)
			throws Exception {
		LOG.info("move and rename");
		// Não faz nada
	}

	public void rename(RenameOperationContext arg0) throws Exception {
		LOG.info("rename");
		// Não faz nada
	}

	public void bind(BindOperationContext arg0) throws Exception {
		LOG.info("bind");
		// Não faz nada
	}

	public void sync() throws Exception {
		LOG.info("sync");
		// Não faz nada
	}

	public void unbind(UnbindOperationContext arg0) throws Exception {
		LOG.info("ubind");
		// Não faz nada
	}

	public void destroy() throws Exception {
		LOG.info("destroy");
	}

	public String getId() {
		LOG.info("get id");
		return id;
	}

	public String getSuffix() {
		LOG.info("get suffix");
		return suffix;
	}

	public DN getSuffixDn() {
		LOG.info("get suffix dn");
		if (dn == null) {
			try {
				dn = new DN(suffix);
			} catch (LdapInvalidDnException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return dn;
	}

	public void initialize() throws Exception {
		String infoMsg = String.format("Initializing %s with suffix %s", this
				.getClass().getSimpleName(), this.suffix);
		LOG.info(infoMsg);
		entryEricsson = new DefaultServerEntry(getSchemaManager(), getSuffixDn());
		entryEricsson.add("objectClass", "top", "dcObject", "organization");
		entryEricsson.add("dc", "ericsson");
		entryEricsson.add("o", "Sony Ericsson");
		LOG.info("Initializing done.");
	}

	public boolean isInitialized() {
		LOG.info("Is initialized");
		return this.entryEricsson != null;
	}

	public void setId(String arg0) {
		LOG.info("set id");
		id = arg0;
	}

	/**
	 * Verifica o se existe uma entrada no LDAP.
	 */
	public boolean hasEntry(EntryOperationContext entry) throws Exception {
		LOG.info("hasEntry");
		return entry.getDn().equals(dn);
	}

	public SchemaManager getSchemaManager() {
		LOG.info("getSchemaManager");
		return schemaManager;
	}

	public EntryFilteringCursor list(ListOperationContext arg0)
			throws Exception {
		LOG.info("list");
		return null;
	}

	public ClonedServerEntry lookup(LookupOperationContext lookupContext)
			throws Exception {
		LOG.info("lookup");
		return null;
	}

	public EntryFilteringCursor search(SearchOperationContext ctx)
			throws Exception {
		LOG.info("search!!!!!");
		LOG.info("search((dn=" + ctx.getDn() + ", filter=" + ctx.getFilter()
				+ ", scope=" + ctx.getScope() + ")");
		if (ctx.getDn().equals(this.dn)) {
			switch (ctx.getScope()) {
			case OBJECT:
				// return a result with the only entry we have
				return new BaseEntryFilteringCursor(
						new SingletonCursor<ServerEntry>(this.entryEricsson),
						ctx);
			}
		}

		// return an empty result
		return new BaseEntryFilteringCursor(new EmptyCursor<ServerEntry>(), ctx);
	}

	public void setSchemaManager(SchemaManager arg0) {
		LOG.info("set schema manager");
		this.schemaManager = arg0;
	}

	public void setSuffix(String suffix) throws LdapInvalidDnException {
		LOG.info("set suffix");
		if (!suffix.startsWith("ou=")) {
			throw new IllegalArgumentException("suffix has to start with ou");
		}
		this.suffix = suffix;
	}

}

Reply via email to