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.config;
21
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Iterator;
26 import java.util.UUID;
27
28 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
29 import org.apache.directory.api.ldap.model.entry.DefaultEntry;
30 import org.apache.directory.api.ldap.model.entry.Entry;
31 import org.apache.directory.api.ldap.model.exception.LdapException;
32 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
33 import org.apache.directory.api.ldap.model.exception.LdapOtherException;
34 import org.apache.directory.api.ldap.model.ldif.LdifEntry;
35 import org.apache.directory.api.ldap.model.ldif.LdifReader;
36 import org.apache.directory.api.ldap.model.name.Dn;
37 import org.apache.directory.api.ldap.model.schema.SchemaManager;
38 import org.apache.directory.server.core.api.interceptor.context.AddOperationContext;
39 import org.apache.directory.server.core.api.interceptor.context.DeleteOperationContext;
40 import org.apache.directory.server.core.api.interceptor.context.ModifyOperationContext;
41 import org.apache.directory.server.core.api.interceptor.context.MoveAndRenameOperationContext;
42 import org.apache.directory.server.core.api.interceptor.context.MoveOperationContext;
43 import org.apache.directory.server.core.api.interceptor.context.RenameOperationContext;
44 import org.apache.directory.server.core.api.partition.PartitionTxn;
45 import org.apache.directory.server.core.partition.ldif.AbstractLdifPartition;
46
47
48
49
50
51
52
53 public class ReadOnlyConfigurationPartition extends AbstractLdifPartition
54 {
55
56 private InputStream inputStream;
57
58
59
60
61
62
63
64
65
66
67 public ReadOnlyConfigurationPartition( InputStream inputStream, SchemaManager schemaManager )
68 {
69 super( schemaManager );
70 this.inputStream = inputStream;
71 id = "config";
72
73 try
74 {
75 suffixDn = new Dn( schemaManager, "ou=config" );
76 }
77 catch ( LdapInvalidDnException lide )
78 {
79
80 }
81 }
82
83
84
85
86
87 @Override
88 protected void doInit() throws LdapException
89 {
90 if ( !initialized )
91 {
92
93 super.doInit();
94
95
96 loadLdifEntries();
97 }
98 }
99
100
101
102
103
104
105
106 private void loadLdifEntries() throws LdapException
107 {
108 if ( inputStream != null )
109 {
110
111 try ( LdifReader reader = new LdifReader( inputStream ) )
112 {
113 Iterator<LdifEntry> itr = reader.iterator();
114
115
116 if ( !itr.hasNext() )
117 {
118 return;
119 }
120
121
122 LdifEntry ldifEntry = itr.next();
123 Entry contextEntry = new DefaultEntry( schemaManager, ldifEntry.getEntry() );
124
125
126 if ( suffixDn.equals( contextEntry.getDn() ) )
127 {
128 addMandatoryOpAt( contextEntry );
129
130 super.add( new AddOperationContext( null, contextEntry ) );
131 }
132 else
133 {
134 throw new LdapException( "The given LDIF file doesn't contain the context entry" );
135 }
136
137
138 while ( itr.hasNext() )
139 {
140 Entry entry = new DefaultEntry( schemaManager, itr.next().getEntry() );
141 addMandatoryOpAt( entry );
142
143 super.add( new AddOperationContext( null, entry ) );
144 }
145 }
146 catch ( IOException ioe )
147 {
148 throw new LdapOtherException( ioe.getMessage(), ioe );
149 }
150 }
151 }
152
153
154
155
156
157 private void addMandatoryOpAt( Entry entry ) throws LdapException
158 {
159
160 if ( entry.get( SchemaConstants.ENTRY_CSN_AT ) == null )
161 {
162 entry.add( SchemaConstants.ENTRY_CSN_AT, defaultCSNFactory.newInstance().toString() );
163 }
164
165
166 if ( entry.get( SchemaConstants.ENTRY_UUID_AT ) == null )
167 {
168 String uuid = UUID.randomUUID().toString();
169 entry.add( SchemaConstants.ENTRY_UUID_AT, uuid );
170 }
171 }
172
173
174
175
176
177
178
179
180 @Override
181 public void add( AddOperationContext arg0 ) throws LdapException
182 {
183
184 }
185
186
187
188
189
190 @Override
191 public Entry delete( PartitionTxn partitionTxn, String id ) throws LdapException
192 {
193
194 return null;
195 }
196
197
198
199
200
201 @Override
202 public Entry delete( DeleteOperationContext deleteContext ) throws LdapException
203 {
204
205 return null;
206 }
207
208
209
210
211
212 @Override
213 public void modify( ModifyOperationContext arg0 ) throws LdapException
214 {
215
216 }
217
218
219
220
221
222 @Override
223 public void move( MoveOperationContext arg0 ) throws LdapException
224 {
225
226 }
227
228
229
230
231
232 @Override
233 public void moveAndRename( MoveAndRenameOperationContext arg0 ) throws LdapException
234 {
235
236 }
237
238
239
240
241
242 @Override
243 public void rename( RenameOperationContext arg0 ) throws LdapException
244 {
245
246 }
247 }