1 17 package org.apache.ldap.server.jndi; 18 19 20 import javax.naming.NamingException ; 21 import javax.naming.directory.Attributes ; 22 import java.io.*; 23 24 25 34 public class JavaLdapSupport 35 { 36 40 41 public static final String TOP_ATTR = "top"; 42 43 public static final String JOBJECT_ATTR = "javaObject"; 44 45 public static final String OBJECTCLASS_ATTR = "objectClass"; 46 47 public static final String JCONTAINER_ATTR = "javaContainer"; 48 49 public static final String JSERIALIZEDOBJ_ATTR = "javaSerializedObject"; 50 51 52 public static final String JCLASSNAME_ATTR = "javaClassName"; 53 54 public static final String JCLASSNAMES_ATTR = "javaClassNames"; 55 56 public static final String JSERIALDATA_ATTR = "javaSerializedData"; 57 58 59 63 64 73 static Object deserialize( Attributes attributes ) throws NamingException 74 { 75 ObjectInputStream in = null; 76 String className = ( String ) attributes.get( JCLASSNAME_ATTR ).get(); 77 78 try 79 { 80 byte [] data = ( byte [] ) attributes.get( JSERIALDATA_ATTR ).get(); 81 in = new ObjectInputStream( new ByteArrayInputStream( data ) ); 82 return in.readObject(); 83 } 84 catch ( Exception e ) 85 { 86 NamingException ne = new NamingException ( "De-serialization of '" 87 + className + "' instance failed:\n" + e.getMessage() ); 88 ne.setRootCause( e ); 89 throw ne; 90 } 91 finally 92 { 93 try 94 { 95 in.close(); 96 } 97 catch ( IOException e ) 98 { 99 throw new NamingException ( "object deserialization stream close() failure" ); 100 } 101 } 102 } 103 104 105 112 static byte [] serialize( Object obj ) throws NamingException 113 { 114 ByteArrayOutputStream bytesOut = null; 115 ObjectOutputStream out = null; 116 117 try 118 { 119 bytesOut = new ByteArrayOutputStream(); 120 out = new ObjectOutputStream( bytesOut ); 121 out.writeObject( obj ); 122 return bytesOut.toByteArray(); 123 } 124 catch ( Exception e ) 125 { 126 NamingException ne = new NamingException ( "Serialization of '" 127 + obj + "' failed:\n" + e.getMessage() ); 128 ne.setRootCause( e ); 129 throw ne; 130 } 131 finally 132 { 133 try 134 { 135 out.close(); 136 } 137 catch ( IOException e ) 138 { 139 throw new NamingException ( "object serialization stream close() failure" ); 140 } 141 } 142 } 143 144 145 153 static void serialize( Attributes entry, Object obj ) throws NamingException 154 { 155 161 entry.put( OBJECTCLASS_ATTR, TOP_ATTR ); 162 163 entry.put( OBJECTCLASS_ATTR, JOBJECT_ATTR ); 164 165 entry.put( OBJECTCLASS_ATTR, JCONTAINER_ATTR ); 166 167 entry.put( OBJECTCLASS_ATTR, JSERIALIZEDOBJ_ATTR ); 168 169 entry.put( JCLASSNAME_ATTR, obj.getClass().getName() ); 171 172 entry.put( JSERIALDATA_ATTR, serialize( obj ) ); 173 174 Class [] classes = obj.getClass().getClasses(); 176 177 for ( int ii = 0; ii < classes.length; ii++ ) 178 { 179 entry.put( JCLASSNAMES_ATTR, classes[ii].getName() ); 180 } 181 } 182 } 183 | Popular Tags |