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 java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25
26 import javax.naming.NamingException;
27 import javax.naming.directory.Attributes;
28
29
30 /***
31 * Contains constants and serialization methods used to implement functionality
32 * associated with RFC 2713 which enables the storage and representation of Java
33 * objects within an LDAP directory.
34 *
35 * @see <a href="http://www.faqs.org/rfcs/rfc2713.html">RFC 2713</a>
36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37 * @version $Rev: 226451 $
38 */
39 class JavaLdapSupport
40 {
41
42
43
44
45 /*** objectClass attribute for top */
46 public static final String TOP_ATTR = "top";
47 /*** the javaObject attribute */
48 public static final String JOBJECT_ATTR = "javaObject";
49 /*** the objectClass attribute */
50 public static final String OBJECTCLASS_ATTR = "objectClass";
51 /*** the javaContainer attribute */
52 public static final String JCONTAINER_ATTR = "javaContainer";
53 /*** the javaSerializedObject attribute */
54 public static final String JSERIALIZEDOBJ_ATTR = "javaSerializedObject";
55
56 /*** the javaClassName attribute */
57 public static final String JCLASSNAME_ATTR = "javaClassName";
58 /*** the javaClassNames attribute */
59 public static final String JCLASSNAMES_ATTR = "javaClassNames";
60 /*** the javaSerializedData attribute */
61 public static final String JSERIALDATA_ATTR = "javaSerializedData";
62
63
64
65
66
67
68
69 /***
70 * Resusitates an object from a serialized attribute in an entry that
71 * conforms to the specifications for representing Java Objects in an LDAP
72 * Directory (RFC 2713).
73 *
74 * @param attributes the entry representing a serialized object
75 * @return the deserialized object
76 * @throws NamingException if the object cannot be serialized
77 */
78 static Object deserialize( Attributes attributes ) throws NamingException
79 {
80 ObjectInputStream in = null;
81 String className = ( String ) attributes.get( JCLASSNAME_ATTR ).get();
82
83 try
84 {
85 byte [] data = ( byte [] ) attributes.get( JSERIALDATA_ATTR ).get();
86 in = new ObjectInputStream( new ByteArrayInputStream( data ) );
87 return in.readObject();
88 }
89 catch ( Exception e )
90 {
91 NamingException ne = new NamingException( "De-serialization of '"
92 + className + "' instance failed:\n" + e.getMessage() );
93 ne.setRootCause( e );
94 throw ne;
95 }
96 finally
97 {
98 try
99 {
100 in.close();
101 }
102 catch ( IOException e )
103 {
104 throw new NamingException( "object deserialization stream close() failure" );
105 }
106 }
107 }
108
109
110 /***
111 * Serializes an object into a byte array.
112 *
113 * @param obj the object to serialize
114 * @return the object's serialized byte array form
115 * @throws NamingException of the object cannot be serialized
116 */
117 static byte [] serialize( Object obj ) throws NamingException
118 {
119 ByteArrayOutputStream bytesOut = null;
120 ObjectOutputStream out = null;
121
122 try
123 {
124 bytesOut = new ByteArrayOutputStream();
125 out = new ObjectOutputStream( bytesOut );
126 out.writeObject( obj );
127 return bytesOut.toByteArray();
128 }
129 catch ( Exception e )
130 {
131 NamingException ne = new NamingException( "Serialization of '"
132 + obj + "' failed:\n" + e.getMessage() );
133 ne.setRootCause( e );
134 throw ne;
135 }
136 finally
137 {
138 try
139 {
140 out.close();
141 }
142 catch ( IOException e )
143 {
144 throw new NamingException( "object serialization stream close() failure" );
145 }
146 }
147 }
148
149
150 /***
151 * Serializes an object into an entry using the attributes specified in
152 * RFC 2713 to represent the serialized object.
153 *
154 * @param entry the set of attributes representing entry
155 * @param obj the object to serialize
156 * @throws NamingException if the object cannot be serialized
157 */
158 static void serialize( Attributes entry, Object obj ) throws NamingException
159 {
160
161
162
163
164
165
166 entry.put( OBJECTCLASS_ATTR, TOP_ATTR );
167
168 entry.put( OBJECTCLASS_ATTR, JOBJECT_ATTR );
169
170 entry.put( OBJECTCLASS_ATTR, JCONTAINER_ATTR );
171
172 entry.put( OBJECTCLASS_ATTR, JSERIALIZEDOBJ_ATTR );
173
174
175 entry.put( JCLASSNAME_ATTR, obj.getClass().getName() );
176
177 entry.put( JSERIALDATA_ATTR, serialize( obj ) );
178
179
180 Class [] classes = obj.getClass().getClasses();
181
182 for ( int ii = 0; ii < classes.length; ii++ )
183 {
184 entry.put( JCLASSNAMES_ATTR, classes[ii].getName() );
185 }
186 }
187 }