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.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
40
41
42 public class DefaultJournalStore implements JournalStore
43 {
44
45 private File workingDirectory;
46
47
48 private String fileName;
49
50
51 private File journal;
52
53
54 private Writer writer;
55
56
57
58
59
60 public void destroy() throws Exception
61 {
62 if ( writer != null )
63 {
64 writer.close();
65 }
66 }
67
68
69
70
71
72 public void init( DirectoryService service ) throws Exception
73 {
74 if ( workingDirectory == null )
75 {
76 workingDirectory = service.getInstanceLayout().getLogDirectory();
77 }
78
79
80 if ( fileName == null )
81 {
82 fileName = "journal.ldif";
83 }
84
85 journal = new File( workingDirectory, fileName );
86
87
88 writer = new PrintWriter(
89 new OutputStreamWriter(
90 new FileOutputStream( journal, true ) ) );
91 }
92
93
94
95
96
97
98
99
100
101 public boolean log( LdapPrincipal principal, long revision, LdifEntry forward )
102 {
103 synchronized ( writer )
104 {
105 try
106 {
107
108 writer.write( "# principal: " );
109 writer.write( principal.getName() );
110 writer.write( '\n' );
111
112
113 writer.write( "# timestamp: " );
114 writer.write( Long.toString( System.currentTimeMillis() ) );
115 writer.write( '\n' );
116
117
118 writer.write( "# revision: " );
119 writer.write( Long.toString( revision ) );
120 writer.write( "\n" );
121
122
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
142
143
144
145
146
147 public boolean ack( long revision )
148 {
149 synchronized ( writer )
150 {
151 try
152 {
153
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
172
173
174
175
176
177 public boolean nack( long revision )
178 {
179 synchronized ( writer )
180 {
181 try
182 {
183
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
203
204 }
205
206
207 public long getCurrentRevision()
208 {
209
210 return 0;
211 }
212
213
214
215
216
217 public String getFileName()
218 {
219 return fileName;
220 }
221
222
223
224
225
226 public void setFileName( String fileName )
227 {
228 this.fileName = fileName;
229 }
230
231
232
233
234
235 public void setWorkingDirectory( String workingDirectoryName )
236 {
237 this.workingDirectory = new File( workingDirectoryName );
238 }
239 }