1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 /***
26 * Contains constants and serialization methods used to implement functionality
27 * associated with RFC 2713 which enables the storage and representation of Java
28 * objects within an LDAP directory.
29 *
30 * @see <a href="http://www.faqs.org/rfcs/rfc2713.html">RFC 2713</a>
31 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32 * @version $Rev: 159259 $
33 */
34 public class JavaLdapSupport
35 {
36
37
38
39
40 /*** objectClass attribute for top */
41 public static final String TOP_ATTR = "top";
42 /*** the javaObject attribute */
43 public static final String JOBJECT_ATTR = "javaObject";
44 /*** the objectClass attribute */
45 public static final String OBJECTCLASS_ATTR = "objectClass";
46 /*** the javaContainer attribute */
47 public static final String JCONTAINER_ATTR = "javaContainer";
48 /*** the javaSerializedObject attribute */
49 public static final String JSERIALIZEDOBJ_ATTR = "javaSerializedObject";
50
51 /*** the javaClassName attribute */
52 public static final String JCLASSNAME_ATTR = "javaClassName";
53 /*** the javaClassNames attribute */
54 public static final String JCLASSNAMES_ATTR = "javaClassNames";
55 /*** the javaSerializedData attribute */
56 public static final String JSERIALDATA_ATTR = "javaSerializedData";
57
58
59
60
61
62
63
64 /***
65 * Resusitates an object from a serialized attribute in an entry that
66 * conforms to the specifications for representing Java Objects in an LDAP
67 * Directory (RFC 2713).
68 *
69 * @param attributes the entry representing a serialized object
70 * @return the deserialized object
71 * @throws NamingException if the object cannot be serialized
72 */
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 /***
106 * Serializes an object into a byte array.
107 *
108 * @param obj the object to serialize
109 * @return the object's serialized byte array form
110 * @throws NamingException of the object cannot be serialized
111 */
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 /***
146 * Serializes an object into an entry using the attributes specified in
147 * RFC 2713 to represent the serialized object.
148 *
149 * @param entry the set of attributes representing entry
150 * @param obj the object to serialize
151 * @throws NamingException if the object cannot be serialized
152 */
153 static void serialize( Attributes entry, Object obj ) throws NamingException
154 {
155
156
157
158
159
160
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
170 entry.put( JCLASSNAME_ATTR, obj.getClass().getName() );
171
172 entry.put( JSERIALDATA_ATTR, serialize( obj ) );
173
174
175 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 }