KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > jndi > JavaLdapSupport


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.ldap.server.jndi;
18
19
20 import javax.naming.NamingException JavaDoc;
21 import javax.naming.directory.Attributes JavaDoc;
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: 169198 $
33  */

34 public class JavaLdapSupport
35 {
36     // ------------------------------------------------------------------------
37
// Attribute Id Constants Used By The Java LDAP BootstrapSchema
38
// ------------------------------------------------------------------------
39

40     /** objectClass attribute for top */
41     public static final String JavaDoc TOP_ATTR = "top";
42     /** the javaObject attribute */
43     public static final String JavaDoc JOBJECT_ATTR = "javaObject";
44     /** the objectClass attribute */
45     public static final String JavaDoc OBJECTCLASS_ATTR = "objectClass";
46     /** the javaContainer attribute */
47     public static final String JavaDoc JCONTAINER_ATTR = "javaContainer";
48     /** the javaSerializedObject attribute */
49     public static final String JavaDoc JSERIALIZEDOBJ_ATTR = "javaSerializedObject";
50
51     /** the javaClassName attribute */
52     public static final String JavaDoc JCLASSNAME_ATTR = "javaClassName";
53     /** the javaClassNames attribute */
54     public static final String JavaDoc JCLASSNAMES_ATTR = "javaClassNames";
55     /** the javaSerializedData attribute */
56     public static final String JavaDoc JSERIALDATA_ATTR = "javaSerializedData";
57
58
59     // ------------------------------------------------------------------------
60
// Package Friendly & Private Utility Methods
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 JavaDoc deserialize( Attributes JavaDoc attributes ) throws NamingException JavaDoc
74     {
75         ObjectInputStream in = null;
76         String JavaDoc className = ( String JavaDoc ) 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 JavaDoc e )
85         {
86             NamingException JavaDoc ne = new NamingException JavaDoc( "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 JavaDoc( "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 JavaDoc obj ) throws NamingException JavaDoc
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 JavaDoc e )
125         {
126             NamingException JavaDoc ne = new NamingException JavaDoc( "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 JavaDoc( "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 JavaDoc entry, Object JavaDoc obj ) throws NamingException JavaDoc
154     {
155         /* Let's add the object classes first:
156          * objectClass: top
157          * objectClass: javaObject
158          * objectClass: javaContainer
159          * objectClass: javaSerializedObject
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         // Add the javaClassName and javaSerializedData attributes
170
entry.put( JCLASSNAME_ATTR, obj.getClass().getName() );
171
172         entry.put( JSERIALDATA_ATTR, serialize( obj ) );
173
174         // Add all the class names this object can be cast to:
175
Class JavaDoc [] 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