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  package org.apache.directory.server.core.changelog;
20  
21  
22  import java.util.List;
23  
24  import org.apache.directory.server.core.DirectoryService;
25  import org.apache.directory.server.core.authn.LdapPrincipal;
26  import org.apache.directory.server.core.partition.Partition;
27  import org.apache.directory.shared.ldap.ldif.LdifEntry;
28  
29  
30  /**
31   * The default ChangeLog service implementation.
32   *
33   * @org.apache.xbean.XBean
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev$, $Date$
37   */
38  public class DefaultChangeLog implements ChangeLog
39  {
40      private boolean enabled;
41      private Tag latest;
42      private ChangeLogStore store = new MemoryChangeLogStore();
43  
44      private boolean exposeChangeLog;
45  
46      // default values for ChangeLogStorePartition containers
47      private String partitionSuffix = "ou=changelog";
48      private String revContainerName = "ou=revisions";
49      private String tagContainerName = "ou=tags";
50  
51  
52      public ChangeLogStore getChangeLogStore()
53      {
54          return store;
55      }
56  
57  
58      public void setChangeLogStore( ChangeLogStore store )
59      {
60          this.store = store;
61      }
62  
63  
64      public long getCurrentRevision() throws Exception
65      {
66          return store.getCurrentRevision();
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      public ChangeLogEvent log( LdapPrincipal principal, LdifEntry forward, LdifEntry reverse ) throws Exception
74      {
75          if ( !enabled )
76          {
77              throw new IllegalStateException( "The ChangeLog has not been enabled." );
78          }
79  
80          return store.log( principal, forward, reverse );
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      public ChangeLogEvent log( LdapPrincipal principal, LdifEntry forward, List<LdifEntry> reverses ) throws Exception
88      {
89          if ( !enabled )
90          {
91              throw new IllegalStateException( "The ChangeLog has not been enabled." );
92          }
93  
94          return store.log( principal, forward, reverses );
95      }
96  
97  
98      public boolean isLogSearchSupported()
99      {
100         return store instanceof SearchableChangeLogStore;
101     }
102 
103 
104     public boolean isTagSearchSupported()
105     {
106         return store instanceof TaggableSearchableChangeLogStore;
107     }
108 
109 
110     public boolean isTagStorageSupported()
111     {
112         return store instanceof TaggableChangeLogStore;
113     }
114 
115 
116     public ChangeLogSearchEngine getChangeLogSearchEngine()
117     {
118         if ( isLogSearchSupported() )
119         {
120             return ( ( SearchableChangeLogStore ) store ).getChangeLogSearchEngine();
121         }
122 
123         throw new UnsupportedOperationException(
124             "The underlying changelog store does not support searching through it's logs" );
125     }
126 
127 
128     public TagSearchEngine getTagSearchEngine()
129     {
130         if ( isTagSearchSupported() )
131         {
132             return ( ( TaggableSearchableChangeLogStore ) store ).getTagSearchEngine();
133         }
134 
135         throw new UnsupportedOperationException(
136             "The underlying changelog store does not support searching through it's tags" );
137     }
138 
139 
140     public Tag tag( long revision, String description ) throws Exception
141     {
142         if ( revision < 0 )
143         {
144             throw new IllegalArgumentException( "revision must be greater than or equal to 0" );
145         }
146 
147         if ( revision > store.getCurrentRevision() )
148         {
149             throw new IllegalArgumentException( "revision must be less than or equal to the current revision" );
150         }
151 
152         if ( store instanceof TaggableChangeLogStore )
153         {
154             return latest = ( ( TaggableChangeLogStore ) store ).tag( revision );
155         }
156 
157         return latest = new Tag( revision, description );
158     }
159 
160 
161     public Tag tag( long revision ) throws Exception
162     {
163         return tag( revision, null );
164     }
165 
166 
167     public Tag tag( String description ) throws Exception
168     {
169         return tag( store.getCurrentRevision(), description );
170     }
171 
172 
173     public Tag tag() throws Exception
174     {
175         return tag( store.getCurrentRevision(), null );
176     }
177 
178 
179     public void setEnabled( boolean enabled )
180     {
181         this.enabled = enabled;
182     }
183 
184 
185     public boolean isEnabled()
186     {
187         return enabled;
188     }
189 
190 
191     public Tag getLatest() throws Exception
192     {
193         if ( latest != null )
194         {
195             return latest;
196         }
197 
198         if ( store instanceof TaggableChangeLogStore )
199         {
200             return latest = ( ( TaggableChangeLogStore ) store ).getLatest();
201         }
202 
203         return null;
204     }
205 
206 
207     public void init( DirectoryService service ) throws Exception
208     {
209         if ( enabled )
210         {
211             store.init( service );
212 
213             if ( exposeChangeLog && isTagSearchSupported() )
214             {
215                 Partition partition = ( ( TaggableSearchableChangeLogStore ) store ).getPartition( partitionSuffix, revContainerName, tagContainerName );
216                 partition.init( service );
217 
218                 service.addPartition( partition );
219             }
220         }
221     }
222 
223 
224     public void sync() throws Exception
225     {
226         if ( enabled )
227         {
228             store.sync();
229         }
230     }
231 
232 
233     public void destroy() throws Exception
234     {
235         if ( enabled )
236         {
237             store.destroy();
238         }
239     }
240 
241 
242     /**
243      * @see ChangeLog#isExposeChangeLog()
244      */
245     public boolean isExposeChangeLog()
246     {
247         return exposeChangeLog;
248     }
249 
250 
251     /**
252      * @see ChangeLog#setExposeChangeLog(boolean)
253      */
254     public void setExposeChangeLog( boolean exposeChangeLog )
255     {
256         this.exposeChangeLog = exposeChangeLog;
257     }
258 
259 
260     /**
261      * @see ChangeLog#setPartitionSuffix(String)
262      */
263     public void setPartitionSuffix( String suffix )
264     {
265         this.partitionSuffix = suffix;
266     }
267 
268 
269     /**
270      * @see ChangeLog#setRevisionsContainerName(String)
271      */
272     public void setRevisionsContainerName( String revContainerName )
273     {
274         this.revContainerName = revContainerName;
275     }
276 
277 
278     /**
279      * @see ChangeLog#setTagsContainerName(String)
280      */
281     public void setTagsContainerName( String tagContainerName )
282     {
283         this.tagContainerName = tagContainerName;
284     }
285 
286     
287     /**
288      * @see Object#toString()
289      */
290     public String toString()
291     {
292         StringBuilder sb = new StringBuilder();
293         
294         sb.append( "ChangeLog tag[" ).append( latest ).append( "]\n" );
295         sb.append( "    store : \n" ).append( store );
296         
297         return sb.toString();
298     }
299 }