View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * A utility class for loading example LDIF data.
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   * @version $Rev$, $Date$
46   */
47  public class StoreUtils
48  {
49  
50      /**
51       * Initializes and loads a store with the example data shown in
52       * <a href="http://cwiki.apache.org/confluence/display/DIRxSRVx11/Structure+and+Organization">
53       * Structure and Organization</a>
54       *
55       * TODO might want to make this load an LDIF instead in the future
56       * TODO correct size of spaces in user provided DN
57       * 
58       * @param store the store object to be initialized
59       * @param registries oid registries
60       * @throws Exception on access exceptions
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          // Entry #1
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          // Entry #2
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          // Entry #3
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         // Entry #4
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         // Entry #5
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         // Entry #6
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         // Entry #7
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         // Entry #8
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         // aliases -------------
159 
160         // Entry #9
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         // Entry #10
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         // Entry #11
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      * This is primarily a convenience method used to extract all the attributes
193      * associated with an entry.
194      *
195      * @param store the store to get the attributes from
196      * @param id the id of the entry to get index information for
197      * @return the index names and values as an Attributes object
198      * @throws Exception if there are failures accessing the underlying store
199      */
200     @SuppressWarnings("unchecked")
201     public Entry getAttributes( Store store, Long id ) throws Exception
202     {
203         Entry entry = new DefaultClientEntry();
204 
205         // Get the distinguishedName to id mapping
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         // Get all standard index attribute to value mappings
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         // Get all existence mappings for this id creating a special key
236         // that looks like so 'existence[attribute]' and the value is set to id
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         // Get all parent child mappings for this entry as the parent using the
264         // key 'child' with many entries following it.
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 }