//Warning: This code has been marked up for HTML

/*******************************************************************************
 * Copyright (c) 2002-2003 Novell, Inc. All Rights Reserved.
 *
 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
 * TREATIES. USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE
 * AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS
 * THIS WORK. PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO
 * DEVELOPER A ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE
 * CODE IN ITS PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS
 * TO MARKET, DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF
 * DEVELOPER'S PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR
 * DEVELOPER'S CUSTOMERS WITH RESPECT TO THIS CODE.
 *
 * $name:           RemoveOrphanPartition.java
 * $description:    RemoveOrphanPartition.java is used to remove an orphan
 *                  naming context.
 ******************************************************************************/
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.InitialLdapContext;
import com.novell.service.ndssdk.jndi.ldap.ext.RemoveOrphanPartitionRequest;

public class RemoveOrphanPartition {
    public static void main(String[] args) {

        if (args.length != 5) {
            usage();
        }

        String hostURL      = args[0];
        String loginDN      = args[1];
        String passWord     = args[2];
        String serverDN     = args[3];
        String partitionDN  = args[4];

        try {
           // Create a Hashtable object.

            Hashtable env = new Hashtable(5, 0.75f);

            env.put(Context.INITIAL_CONTEXT_FACTORY,
                            "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, hostURL);
            env.put(Context.SECURITY_AUTHENTICATION, "simple" );
            env.put(Context.SECURITY_PRINCIPAL, loginDN );
            env.put(Context.SECURITY_CREDENTIALS, passWord );

           // Construct an LdapContext object.

            LdapContext ctx = new InitialLdapContext(env, null);


           // create extended opration request object

            RemoveOrphanPartitionRequest  reqs =
                new RemoveOrphanPartitionRequest(serverDN, partitionDN);


           // call extended operation to remove the orphan naming context.

            ctx.extendedOperation(reqs);

            System.out.println(
                "RemoveOrphanPartition operation succeeded.");
        }
        catch (NamingException e) {
            System.err.println("RemoveOrphanPartition operation failed.");
            e.getExplanation();
            e.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }

    public static void usage() {
        System.err.println("Usage  : java RemoveOrphanPartition <host URL>"
            + " <login dn> <password> <server dn> <partition dn>");
        System.err.println("Example: java RemoveOrphanPartition ldap://Acme.com"
            + ":389 cn=admin,o=Acme secret cn=myServer,o=Acme ou=sales,o=Acme");
        System.exit(1);
    }
}