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.xdbm.tools;
21
22
23 import java.util.Set;
24
25 import org.apache.directory.server.core.cursor.Cursor;
26 import org.apache.directory.server.core.entry.DefaultServerEntry;
27 import org.apache.directory.server.core.entry.ServerEntry;
28 import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
29 import org.apache.directory.server.schema.registries.Registries;
30 import org.apache.directory.server.xdbm.ForwardIndexEntry;
31 import org.apache.directory.server.xdbm.Index;
32 import org.apache.directory.server.xdbm.IndexEntry;
33 import org.apache.directory.server.xdbm.Store;
34 import org.apache.directory.shared.ldap.entry.Entry;
35 import org.apache.directory.shared.ldap.entry.EntryAttribute;
36 import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
37 import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
38 import org.apache.directory.shared.ldap.name.LdapDN;
39
40
41
42
43
44
45
46
47 public class StoreUtils
48 {
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public static void loadExampleData( Store<ServerEntry> store, Registries registries ) throws Exception
63 {
64 store.setSuffixDn( "o=Good Times Co." );
65
66 LdapDN suffixDn = new LdapDN( "o=Good Times Co." );
67 suffixDn.normalize( registries.getAttributeTypeRegistry().getNormalizerMapping() );
68
69 AttributeTypeRegistry attributeRegistry = registries.getAttributeTypeRegistry();
70
71 store.init( registries );
72
73
74 DefaultServerEntry entry = new DefaultServerEntry( registries, suffixDn );
75 entry.add( "objectClass", "organization" );
76 entry.add( "o", "Good Times Co." );
77 entry.add( "postalCode", "1" );
78 entry.add( "postOfficeBox", "1" );
79 store.add( suffixDn, entry );
80
81
82
83 LdapDN dn = new LdapDN( "ou=Sales,o=Good Times Co." );
84 dn.normalize( attributeRegistry.getNormalizerMapping() );
85 entry = new DefaultServerEntry( registries, dn );
86 entry.add( "objectClass", "top", "organizationalUnit" );
87 entry.add( "ou", "Sales" );
88 entry.add( "postalCode", "1" );
89 entry.add( "postOfficeBox", "1" );
90 store.add( dn, entry );
91
92
93 dn = new LdapDN( "ou=Board of Directors,o=Good Times Co." );
94 dn.normalize( attributeRegistry.getNormalizerMapping() );
95 entry = new DefaultServerEntry( registries, dn );
96 entry.add( "objectClass", "top", "organizationalUnit" );
97 entry.add( "ou", "Board of Directors" );
98 entry.add( "postalCode", "1" );
99 entry.add( "postOfficeBox", "1" );
100 store.add( dn, entry );
101
102
103 dn = new LdapDN( "ou=Engineering,o=Good Times Co." );
104 dn.normalize( attributeRegistry.getNormalizerMapping() );
105 entry = new DefaultServerEntry( registries, dn );
106 entry.add( "objectClass", "top", "organizationalUnit" );
107 entry.add( "ou", "Engineering" );
108 entry.add( "postalCode", "2" );
109 entry.add( "postOfficeBox", "2" );
110 store.add( dn, entry );
111
112
113 dn = new LdapDN( "cn=JOhnny WAlkeR,ou=Sales,o=Good Times Co." );
114 dn.normalize( attributeRegistry.getNormalizerMapping() );
115 entry = new DefaultServerEntry( registries, dn );
116 entry.add( "objectClass", "top", "person", "organizationalPerson" );
117 entry.add( "ou", "Sales" );
118 entry.add( "cn", "JOhnny WAlkeR");
119 entry.add( "sn", "WAlkeR");
120 entry.add( "postalCode", "3" );
121 entry.add( "postOfficeBox", "3" );
122 store.add( dn, entry );
123
124
125 dn = new LdapDN( "cn=JIM BEAN,ou=Sales,o=Good Times Co." );
126 dn.normalize( attributeRegistry.getNormalizerMapping() );
127 entry = new DefaultServerEntry( registries, dn );
128 entry.add( "objectClass", "top", "person", "organizationalPerson" );
129 entry.add( "ou", "Sales" );
130 entry.add( "cn", "JIM BEAN");
131 entry.add( "surName", "BEAN");
132 entry.add( "postalCode", "4" );
133 entry.add( "postOfficeBox", "4" );
134 store.add( dn, entry );
135
136
137 dn = new LdapDN( "ou=Apache,ou=Board of Directors,o=Good Times Co." );
138 dn.normalize( attributeRegistry.getNormalizerMapping() );
139 entry = new DefaultServerEntry( registries, dn );
140 entry.add( "objectClass", "top", "organizationalUnit" );
141 entry.add( "ou", "Apache" );
142 entry.add( "postalCode", "5" );
143 entry.add( "postOfficeBox", "5" );
144 store.add( dn, entry );
145
146
147 dn = new LdapDN( "cn=Jack Daniels,ou=Engineering,o=Good Times Co." );
148 dn.normalize( attributeRegistry.getNormalizerMapping() );
149 entry = new DefaultServerEntry( registries, dn );
150 entry.add( "objectClass", "top", "person", "organizationalPerson" );
151 entry.add( "ou", "Engineering" );
152 entry.add( "cn", "Jack Daniels");
153 entry.add( "SN", "Daniels");
154 entry.add( "postalCode", "6" );
155 entry.add( "postOfficeBox", "6" );
156 store.add( dn, entry );
157
158
159
160
161 dn = new LdapDN( "commonName=Jim Bean,ou=Apache,ou=Board of Directors,o=Good Times Co." );
162 dn.normalize( attributeRegistry.getNormalizerMapping() );
163 entry = new DefaultServerEntry( registries, dn );
164 entry.add( "objectClass", "top", "alias", "extensibleObject" );
165 entry.add( "ou", "Apache" );
166 entry.add( "commonName", "Jim Bean");
167 entry.add( "aliasedObjectName", "cn=Jim Bean,ou=Sales,o=Good Times Co." );
168 store.add( dn, entry );
169
170
171 dn = new LdapDN( "commonName=Jim Bean,ou=Board of Directors,o=Good Times Co." );
172 dn.normalize( attributeRegistry.getNormalizerMapping() );
173 entry = new DefaultServerEntry( registries, dn );
174 entry.add( "objectClass", "top", "alias", "extensibleObject" );
175 entry.add( "commonName", "Jim Bean");
176 entry.add( "aliasedObjectName", "cn=Jim Bean,ou=Sales,o=Good Times Co." );
177 store.add( dn, entry );
178
179
180 dn = new LdapDN( "2.5.4.3=Johnny Walker,ou=Engineering,o=Good Times Co." );
181 dn.normalize( attributeRegistry.getNormalizerMapping() );
182 entry = new DefaultServerEntry( registries, dn );
183 entry.add( "objectClass", "top", "alias", "extensibleObject" );
184 entry.add( "ou", "Engineering" );
185 entry.add( "2.5.4.3", "Johnny Walker");
186 entry.add( "aliasedObjectName", "cn=Johnny Walker,ou=Sales,o=Good Times Co." );
187 store.add( dn, entry );
188 }
189
190
191
192
193
194
195
196
197
198
199
200 @SuppressWarnings("unchecked")
201 public Entry getAttributes( Store store, Long id ) throws Exception
202 {
203 Entry entry = new DefaultClientEntry();
204
205
206 entry.put( "_nDn", store.getEntryDn( id ) );
207 entry.put( "_upDn", store.getEntryUpdn( id ) );
208 entry.put( "_parent", Long.toString( store.getParentId( id ) ) );
209
210
211 for ( Index index : ( Set<Index> )store.getUserIndices() )
212 {
213 Cursor<ForwardIndexEntry> list = index.reverseCursor();
214 ForwardIndexEntry recordForward = new ForwardIndexEntry();
215 recordForward.setId( id );
216 list.before( recordForward );
217
218 while ( list.next() )
219 {
220 IndexEntry rec = list.get();
221 String val = rec.getValue().toString();
222 String attrId = index.getAttribute().getName();
223 EntryAttribute attr = entry.get( attrId );
224
225 if ( attr == null )
226 {
227 attr = new DefaultClientAttribute( attrId );
228 }
229
230 attr.add( val );
231 entry.put( attr );
232 }
233 }
234
235
236
237 Cursor<IndexEntry> list = store.getPresenceIndex().reverseCursor();
238 ForwardIndexEntry recordForward = new ForwardIndexEntry();
239 recordForward.setId( id );
240 list.before( recordForward );
241 StringBuffer val = new StringBuffer();
242
243 while ( list.next() )
244 {
245 IndexEntry rec = list.get();
246 val.append( "_existance[" );
247 val.append( rec.getValue().toString() );
248 val.append( "]" );
249
250 String valStr = val.toString();
251 EntryAttribute attr = entry.get( valStr );
252
253 if ( attr == null )
254 {
255 attr = new DefaultClientAttribute( valStr );
256 }
257
258 attr.add( rec.getId().toString() );
259 entry.put( attr );
260 val.setLength( 0 );
261 }
262
263
264
265 Cursor<IndexEntry> children = store.getOneLevelIndex().forwardCursor();
266 ForwardIndexEntry longRecordForward = new ForwardIndexEntry();
267 recordForward.setId( id );
268 children.before( longRecordForward );
269
270 EntryAttribute childAttr = new DefaultClientAttribute( "_child" );
271 entry.put( childAttr );
272
273 while ( children.next() )
274 {
275 IndexEntry rec = children.get();
276 childAttr.add( rec.getId().toString() );
277 }
278
279 return entry;
280 }
281
282 }