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.core.journal;
21  
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.OutputStreamWriter;
27  import java.io.PrintWriter;
28  import java.io.Writer;
29  
30  import org.apache.directory.api.ldap.model.exception.LdapException;
31  import org.apache.directory.api.ldap.model.ldif.LdifEntry;
32  import org.apache.directory.api.ldap.model.ldif.LdifUtils;
33  import org.apache.directory.server.core.api.DirectoryService;
34  import org.apache.directory.server.core.api.LdapPrincipal;
35  import org.apache.directory.server.core.api.journal.JournalStore;
36  
37  
38  /**
39   * @todo : Missing Javadoc
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  */
42  public class DefaultJournalStore implements JournalStore
43  {
44      /** The directory where the journal is stored */
45      private File workingDirectory;
46  
47      /** The journal file name */
48      private String fileName;
49  
50      /** The file containing the journal */
51      private File journal;
52  
53      /** The stream used to write data into the journal */
54      private Writer writer;
55  
56  
57      /**
58       * {@inheritDoc}
59       */
60      public void destroy() throws Exception
61      {
62          if ( writer != null )
63          {
64              writer.close();
65          }
66      }
67  
68  
69      /**
70       * Initialize the interceptor
71       */
72      public void init( DirectoryService service ) throws Exception
73      {
74          if ( workingDirectory == null )
75          {
76              workingDirectory = service.getInstanceLayout().getLogDirectory();
77          }
78  
79          /** Load or create the journal file */
80          if ( fileName == null )
81          {
82              fileName = "journal.ldif";
83          }
84  
85          journal = new File( workingDirectory, fileName );
86  
87          // The new requests are added at the end of the existing journal
88          writer = new PrintWriter(
89              new OutputStreamWriter(
90                  new FileOutputStream( journal, true ) ) );
91      }
92  
93  
94      /**
95       * Stores an event into the journal.
96       * 
97       * @param principal The principal who is logging the change
98       * @param revision The operation revision
99       * @param forward The change to log
100      */
101     public boolean log( LdapPrincipal principal, long revision, LdifEntry forward )
102     {
103         synchronized ( writer )
104         {
105             try
106             {
107                 // Write the LdapPrincipal
108                 writer.write( "# principal: " );
109                 writer.write( principal.getName() );
110                 writer.write( '\n' );
111 
112                 // Write the timestamp
113                 writer.write( "# timestamp: " );
114                 writer.write( Long.toString( System.currentTimeMillis() ) );
115                 writer.write( '\n' );
116 
117                 // Write the revision
118                 writer.write( "# revision: " );
119                 writer.write( Long.toString( revision ) );
120                 writer.write( "\n" );
121 
122                 // Write the entry
123                 writer.write( LdifUtils.convertToLdif( forward, 80 ) );
124                 writer.flush();
125             }
126             catch ( LdapException ne )
127             {
128                 return false;
129             }
130             catch ( IOException ioe )
131             {
132                 return false;
133             }
134         }
135 
136         return true;
137     }
138 
139 
140     /**
141      * Records a ack for a change
142      *
143      * @param revision The change revision which is acked
144      * @return <code>true</code> if the ack has been written
145      * @throws Exception if there are problems logging the ack
146      */
147     public boolean ack( long revision )
148     {
149         synchronized ( writer )
150         {
151             try
152             {
153                 // Write the revision
154                 writer.write( "# ack-revision: " );
155                 writer.write( Long.toString( revision ) );
156                 writer.write( "\n\n" );
157 
158                 writer.flush();
159             }
160             catch ( IOException ioe )
161             {
162                 return false;
163             }
164         }
165 
166         return true;
167     }
168 
169 
170     /**
171      * Records a nack for a change
172      *
173      * @param revision The change revision which is nacked
174      * @return <code>true</code> if the nack has been written
175      * @throws Exception if there are problems logging the nack
176      */
177     public boolean nack( long revision )
178     {
179         synchronized ( writer )
180         {
181             try
182             {
183                 // Write the revision
184                 writer.write( "# nack-revision: " );
185                 writer.write( Long.toString( revision ) );
186                 writer.write( "\n\n" );
187 
188                 writer.flush();
189             }
190             catch ( IOException ioe )
191             {
192                 return false;
193             }
194         }
195 
196         return true;
197     }
198 
199 
200     public void sync() throws Exception
201     {
202         // TODO Auto-generated method stub
203 
204     }
205 
206 
207     public long getCurrentRevision()
208     {
209         // TODO Auto-generated method stub
210         return 0;
211     }
212 
213 
214     /**
215      * @return the fileName
216      */
217     public String getFileName()
218     {
219         return fileName;
220     }
221 
222 
223     /**
224      * @param fileName the fileName to set
225      */
226     public void setFileName( String fileName )
227     {
228         this.fileName = fileName;
229     }
230 
231 
232     /**
233      * {@inheritDoc}
234      */
235     public void setWorkingDirectory( String workingDirectoryName )
236     {
237         this.workingDirectory = new File( workingDirectoryName );
238     }
239 }