1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.jndi;
21
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27
28 import javax.naming.NamingException;
29
30 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
31 import org.apache.directory.api.ldap.model.entry.Attribute;
32 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
33 import org.apache.directory.api.ldap.model.entry.Entry;
34 import org.apache.directory.api.ldap.model.exception.LdapException;
35 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
36 import org.apache.directory.api.ldap.model.schema.AttributeType;
37 import org.apache.directory.api.ldap.model.schema.SchemaManager;
38 import org.apache.directory.server.i18n.I18n;
39
40
41
42
43
44
45
46
47
48
49 final class JavaLdapSupport
50 {
51 private JavaLdapSupport()
52 {
53 }
54
55
56
57
58
59
60 public static final String JOBJECT_ATTR = "javaObject";
61
62 public static final String JCONTAINER_ATTR = "javaContainer";
63
64 public static final String JSERIALIZEDOBJ_ATTR = "javaSerializedObject";
65
66
67 public static final String JCLASSNAME_ATTR = "javaClassName";
68
69 public static final String JCLASSNAMES_ATTR = "javaClassNames";
70
71 public static final String JSERIALDATA_ATTR = "javaSerializedData";
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 static Object deserialize( Entry serverEntry ) throws NamingException
88 {
89 String className = null;
90
91 try
92 {
93 className = serverEntry.get( JCLASSNAME_ATTR ).getString();
94 }
95 catch ( LdapInvalidAttributeValueException liave )
96 {
97 NamingException ne = new NamingException( I18n.err( I18n.ERR_479, className, liave.getLocalizedMessage() ) );
98 ne.setRootCause( liave );
99 throw ne;
100 }
101
102 try ( ObjectInputStream in = new ObjectInputStream(
103 new ByteArrayInputStream( ( byte[] ) serverEntry.get( JSERIALDATA_ATTR ).getBytes() ) ) )
104 {
105 return in.readObject();
106 }
107 catch ( Exception e )
108 {
109 NamingException ne = new NamingException( I18n.err( I18n.ERR_479, className, e.getLocalizedMessage() ) );
110 ne.setRootCause( e );
111 throw ne;
112 }
113 }
114
115
116
117
118
119
120
121
122
123 static byte[] serialize( Object obj ) throws LdapException
124 {
125 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
126
127 try ( ObjectOutputStream out = new ObjectOutputStream( bytesOut ) )
128 {
129 out.writeObject( obj );
130
131 return bytesOut.toByteArray();
132 }
133 catch ( Exception e )
134 {
135 throw new LdapException( I18n.err( I18n.ERR_481, obj, e.getLocalizedMessage() ) );
136 }
137 }
138
139
140
141
142
143
144
145
146
147
148 static void serialize( Entry entry, Object obj, SchemaManager schemaManager ) throws LdapException
149 {
150
151
152
153
154
155
156 entry.put( SchemaConstants.OBJECT_CLASS_AT,
157 SchemaConstants.TOP_OC,
158 JOBJECT_ATTR,
159 JCONTAINER_ATTR,
160 JSERIALIZEDOBJ_ATTR );
161
162
163 entry.put( JCLASSNAME_ATTR, obj.getClass().getName() );
164 entry.put( JSERIALDATA_ATTR, serialize( obj ) );
165
166
167 Class<?>[] classes = obj.getClass().getClasses();
168 AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( JCLASSNAMES_ATTR );
169 Attribute javaClassNames = new DefaultAttribute( attributeType, JCLASSNAMES_ATTR );
170
171 for ( int ii = 0; ii < classes.length; ii++ )
172 {
173 javaClassNames.add( classes[ii].getName() );
174 }
175
176 entry.put( javaClassNames );
177 }
178 }