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.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   * This class implements a read-only configuration partition.
50   *
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   */
53  public class ReadOnlyConfigurationPartition extends AbstractLdifPartition
54  {
55      /** The input stream */
56      private InputStream inputStream;
57  
58  
59      /**
60       * Creates a new instance of ReadOnlyConfigurationPartition.
61       *
62       * @param inputStream
63       *      the input stream
64       * @param schemaManager
65       *      the schema manager
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              // Nothing to do
80          }
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      protected void doInit() throws LdapException
89      {
90          if ( !initialized )
91          {
92              // Initializing the wrapped partition
93              super.doInit();
94  
95              // Load LDIF entries
96              loadLdifEntries();
97          }
98      }
99  
100 
101     /**
102      * Loads the LDIF entries from the input stream.
103      * 
104      * @throws Exception
105      */
106     private void loadLdifEntries() throws LdapException
107     {
108         if ( inputStream != null )
109         {
110             // Initializing the reader and the entry iterator
111             try ( LdifReader reader = new LdifReader( inputStream ) )
112             {
113                 Iterator<LdifEntry> itr = reader.iterator();
114     
115                 // Exiting if there's no entry
116                 if ( !itr.hasNext() )
117                 {
118                     return;
119                 }
120     
121                 // Getting the context entry
122                 LdifEntry ldifEntry = itr.next();
123                 Entry contextEntry = new DefaultEntry( schemaManager, ldifEntry.getEntry() );
124     
125                 // Checking the context entry
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                 // Iterating on all entries
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      * Adds the CSN and UUID attributes to the entry if they are not present.
156      */
157     private void addMandatoryOpAt( Entry entry ) throws LdapException
158     {
159         // entryCSN
160         if ( entry.get( SchemaConstants.ENTRY_CSN_AT ) == null )
161         {
162             entry.add( SchemaConstants.ENTRY_CSN_AT, defaultCSNFactory.newInstance().toString() );
163         }
164 
165         // entryUUID
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     // Operations
176     //---------------------------------------------------------------------------------------------
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public void add( AddOperationContext arg0 ) throws LdapException
182     {
183         // Does nothing (Read-Only)
184     }
185 
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public Entry delete( PartitionTxn partitionTxn, String id ) throws LdapException
192     {
193         // Does nothing (Read-Only)
194         return null;
195     }
196 
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public Entry delete( DeleteOperationContext deleteContext ) throws LdapException
203     {
204         // Does nothing (Read-Only)
205         return null;
206     }
207 
208 
209     /**
210      * {@inheritDoc}
211      */
212     @Override
213     public void modify( ModifyOperationContext arg0 ) throws LdapException
214     {
215         // Does nothing (Read-Only)
216     }
217 
218 
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     public void move( MoveOperationContext arg0 ) throws LdapException
224     {
225         // Does nothing (Read-Only)
226     }
227 
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
233     public void moveAndRename( MoveAndRenameOperationContext arg0 ) throws LdapException
234     {
235         // Does nothing (Read-Only)
236     }
237 
238 
239     /**
240      * {@inheritDoc}
241      */
242     @Override
243     public void rename( RenameOperationContext arg0 ) throws LdapException
244     {
245         // Does nothing (Read-Only)
246     }
247 }