//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/f91a68eb-ad37-4526-92b1-b1938f37b871/LDAPSchemaDSMLSerialization.java //Warning: This code has been marked up for HTML
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.UnsupportedEncodingException;



import com.novell.ldap.LDAPAttribute;

import com.novell.ldap.LDAPConnection;

import com.novell.ldap.LDAPEntry;

import com.novell.ldap.LDAPException;

import com.novell.ldap.LDAPSchema;



/*******************************************************************************

 * $Novell: LDAPSchemaDSMLSerialization.java,v 1.1 2004/08/25 10:06:10 $

 * Copyright (c) 2000, 2001 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:         LDAPSchemaDSMLSerialization.java

 * $description:  LDAPSchemaDSMLSerialization.java demonstrates how to

 *               use default DSML Serialization provided by Java LDAP APIs

 *                The program has two parts:

 *               Part - 1 # Demonstrates how to serializes LDAPSchema

 *                       Object read from LDAP Directory Server into a 

 *                       file provided by user during execution

 *               Part - 2 # Demonstrates how to deserializes the stored

 *                     LDAPSchema Object serialized in Part - 1

 ******************************************************************************/

public class LDAPSchemaDSMLSerialization {



   /**

    * 

    */

   public LDAPSchemaDSMLSerialization() {

      super();

     // TODO Auto-generated constructor stub


   }



   public static void main(String[] args) {

     //this is the check whether the option of being serialized/de-serialized


     // is being entered


      if (args.length < 1) {

         usage();

         System.exit(1);

      }



      String option = args[0];

      if(args[0].equalsIgnoreCase("-s"))

      {          

         if(args.length != 5)

         {

            usage();

            System.exit(1);

         }

         serialize(args);

      }

      else if(args[0].equalsIgnoreCase("-d"))

      {

         if(args.length != 2)

         {

            usage();

            System.exit(1);

         }

         deserialize(args);

      }

      else

      {

         usage();

         System.exit(1);

      }

      

   }



   public static void usage() {

      

      String tab="   ";

      System.err.println("To serialize:");

      

      System.err.println(tab + "Usage:   java LDAPSchemaDSMLSerialization" +

         " <-options> <host name> <login dn>\n              <password> <file name>");

                  

                  

      System.err.println(tab + "Example: java LDAPSchemaDSMLSerialization" +

         " -s Acme.com \"cn=admin,o=Acme\" \n              secret" +
         " \"D:\\Temp\\schema.xml\"");

         

      System.err.println("\nTo de-serialize:");

      

      System.err.println(tab + "Usage:   java LDAPSchemaDSMLSerialization" +

      " <-options> <file name>");

            

      System.err.println(tab + "Example: java LDAPSchemaDSMLSerialization" +

      " -d \"D:\\Temp\\schema.xml\""); 

      

      System.err.println("\nwhere options include:\n" 

                        +   tab + "-s   to serialize the Object\n"

                        +   tab + "-d   to de-serialize the Object\n");

        

   }

   private static void serialize(String[] args){

   int ldapPort = LDAPConnection.DEFAULT_PORT;

   int searchScope = LDAPConnection.SCOPE_ONE;

   int ldapVersion  = LDAPConnection.LDAP_V3;

   String ldapHost = args[1];

   String loginDN = args[2];

   String password = args[3];

//   String searchBase = args[4];


//   String attr = args[5];

//   String attrs[] = {attr}; 


//   boolean attributeOnly = false; 

   String fileName = args[4];

   LDAPConnection lc = new LDAPConnection();

   

   LDAPSchema schema = null;

//   LDAPAttribute ldapattr = null;


   try {

     // connect to the server


      lc.connect( ldapHost, ldapPort );

     // bind to the server


      lc.bind( ldapVersion, loginDN, password.getBytes("UTF8") );

      

      schema = lc.fetchSchema( lc.getSchemaDN() );

      

      if(schema == null)

      {

         System.out.println("No schema entry found in the LDAP Server. " +

            "Contact System Administrator for this problem.");

         System.exit(0);

      }

                     

     // disconnect with the server


      lc.disconnect();

   }

   catch( LDAPException e ) {

      System.out.println( "Error: " + e.toString() );

   }

   catch( UnsupportedEncodingException e ) {

      System.out.println( "Error: " + e.toString() );

   } 

   

//   call serialization method


   try{

      ObjectOutputStream out =

         new ObjectOutputStream(

            new FileOutputStream(fileName));

     

      out.writeObject(schema);

      out.close();

      System.out.println("Object written =" + schema.toString());

   }

   catch(IOException e){

      System.out.println( "Error: " + e.toString() );

   }

  }

   

   private static void deserialize(String[] args){

   String fileName = args[1];

//   call deserialization method


   try{

      ObjectInputStream in =

         new ObjectInputStream(

            new FileInputStream(fileName));

     

      LDAPSchema schema = (LDAPSchema)in.readObject();

      System.out.println("Object read =" + schema.toString());

      in.close();   



   }

   catch(IOException io){

      System.out.println( "Error: " + io.toString() );

   }

   catch(ClassNotFoundException ce){

         System.out.println( "Error: " + ce.toString() );

      }       

      

   }

      

}