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.journal;
20  
21  
22  import java.io.IOException;
23  
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.ldif.LdifEntry;
26  import org.apache.directory.server.core.api.DirectoryService;
27  import org.apache.directory.server.core.api.LdapPrincipal;
28  import org.apache.directory.server.core.api.journal.Journal;
29  import org.apache.directory.server.core.api.journal.JournalStore;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * The default journal implementation. It stores the operation and the
36   * associated status (acked or nacked) in a file which will be used to
37   * restore the server if it crashes.
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class DefaultJournal implements Journal
42  {
43      /** The class logger */
44      private static final Logger LOG = LoggerFactory.getLogger( DefaultJournal.class );
45  
46      /** Tells if the service is activated or not */
47      private boolean enabled;
48  
49      /** An instance of the Journal store */
50      private JournalStore store;
51  
52      /** 
53       * A parameter indicating the number of operations stored in a journal
54       * before it is rotated. If set to 0, no rotation is done
55       */
56      private int rotation;
57  
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public void destroy() throws LdapException
64      {
65          LOG.debug( "Stopping the journal" );
66  
67          // We have to release the file, otherwise Windows won't be able
68          // to stop the server
69          if ( store != null )
70          {
71              try
72              {
73                  store.destroy();
74              }
75              catch ( IOException ioe )
76              {
77                  throw new LdapException( ioe.getMessage(), ioe );
78              }
79          }
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public JournalStore getJournalStore()
88      {
89          return store;
90      }
91  
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public void init( DirectoryService directoryService ) throws LdapException
98      {
99          LOG.debug( "Starting the journal" );
100 
101         if ( store == null )
102         {
103             store = new DefaultJournalStore();
104         }
105 
106         try
107         {
108             store.init( directoryService );
109         }
110         catch ( IOException ioe )
111         {
112             throw new LdapException( ioe.getMessage(), ioe );
113         }
114 
115         LOG.debug( "The Journal service has been initialized" );
116     }
117 
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public boolean isEnabled()
124     {
125         return enabled;
126     }
127 
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public void log( LdapPrincipal principal, long revision, LdifEntry entry ) throws LdapException
134     {
135         store.log( principal, revision, entry );
136     }
137 
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public void ack( long revision )
144     {
145         store.ack( revision );
146     }
147 
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public void nack( long revision )
154     {
155         store.nack( revision );
156     }
157 
158 
159     /**
160      * @return the rotation
161      */
162     @Override
163     public int getRotation()
164     {
165         return rotation;
166     }
167 
168 
169     /**
170      * @param rotation the rotation to set
171      */
172     @Override
173     public void setRotation( int rotation )
174     {
175         this.rotation = rotation;
176     }
177 
178 
179     @Override
180     public void setEnabled( boolean enabled )
181     {
182         this.enabled = enabled;
183     }
184 
185 
186     @Override
187     public void setJournalStore( JournalStore store )
188     {
189         this.store = store;
190     }
191 }