1 package com.github.triceo.splitlog.api;
2
3 import java.io.File;
4 import java.util.ServiceLoader;
5 import java.util.concurrent.TimeUnit;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public abstract class LogWatchBuilder {
32
33 public static final long DEFAULT_DELAY_BETWEEN_READS_IN_MILLISECONDS = 1000;
34 public static final long DEFAULT_DELAY_BETWEEN_SWEEPS_IN_MILLISECONDS = 60 * 1000;
35 public static final int DEFAULT_READ_BUFFER_SIZE_IN_BYTES = 4096;
36
37
38
39
40
41
42
43 public static LogWatchBuilder getDefault() {
44 final ServiceLoader<LogWatchBuilder> ldr = ServiceLoader.load(LogWatchBuilder.class);
45 if (!ldr.iterator().hasNext()) {
46 throw new IllegalStateException("No LogWatchBuilder implementation registered.");
47 }
48 return ldr.iterator().next();
49 }
50
51 private static long getDelay(final int length, final TimeUnit unit) {
52 if (length < 1) {
53 throw new IllegalArgumentException("The length of time must be at least 1.");
54 }
55 switch (unit) {
56 case NANOSECONDS:
57 if (length < (1000 * 1000)) {
58 throw new IllegalArgumentException("The length of time must amount to at least 1 ms.");
59 }
60 break;
61 case MICROSECONDS:
62 if (length < 1000) {
63 throw new IllegalArgumentException("The length of time must amount to at least 1 ms.");
64 }
65 break;
66 default:
67
68 }
69 return unit.toMillis(length);
70 }
71
72 private boolean autoStarting = true;
73 private int bufferSize = LogWatchBuilder.DEFAULT_READ_BUFFER_SIZE_IN_BYTES;
74 private boolean closingBetweenReads;
75 private long delayBetweenReads = LogWatchBuilder.DEFAULT_DELAY_BETWEEN_READS_IN_MILLISECONDS;
76 private long delayBetweenSweeps = LogWatchBuilder.DEFAULT_DELAY_BETWEEN_SWEEPS_IN_MILLISECONDS;
77 private File fileToWatch;
78 private SimpleMessageCondition gateCondition;
79 private int limitCapacityTo = Integer.MAX_VALUE;
80 private boolean readingFromBeginning = true;
81 private SimpleMessageCondition storageCondition;
82
83
84
85
86
87
88
89
90
91
92
93 public abstract LogWatch build();
94
95
96
97
98
99
100
101
102
103 public abstract Follower buildFollowing();
104
105
106
107
108
109
110
111
112
113
114
115
116 public abstract Follower buildFollowingWith(final TailSplitter splitter);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 public abstract LogWatch buildWith(final TailSplitter splitter);
135
136
137
138
139
140
141
142 public LogWatchBuilder closingAfterReading() {
143 this.closingBetweenReads = true;
144 return this;
145 }
146
147
148
149
150
151
152 public LogWatchBuilder doNotStart() {
153 this.autoStarting = false;
154 return this;
155 }
156
157
158
159
160
161
162 public int getCapacityLimit() {
163 return this.limitCapacityTo;
164 }
165
166
167
168
169
170
171 public long getDelayBetweenReads() {
172 return this.delayBetweenReads;
173 }
174
175
176
177
178
179
180 public long getDelayBetweenSweeps() {
181 return this.delayBetweenSweeps;
182 }
183
184
185
186
187
188
189 public File getFileToWatch() {
190 return this.fileToWatch;
191 }
192
193
194
195
196
197
198 public SimpleMessageCondition getGateCondition() {
199 return this.gateCondition;
200 }
201
202
203
204
205
206
207 public int getReadingBufferSize() {
208 return this.bufferSize;
209 }
210
211
212
213
214
215
216
217 public SimpleMessageCondition getStorageCondition() {
218 return this.storageCondition;
219 }
220
221
222
223
224
225
226
227
228 public LogWatchBuilder ignoringPreexistingContent() {
229 this.readingFromBeginning = false;
230 return this;
231 }
232
233
234
235
236 public boolean isClosingBetweenReads() {
237 return this.closingBetweenReads;
238 }
239
240
241
242
243
244 public boolean isReadingFromBeginning() {
245 return this.readingFromBeginning;
246 }
247
248
249
250
251
252
253
254
255 public LogWatchBuilder limitCapacityTo(final int size) {
256 if (size <= 0) {
257 throw new IllegalArgumentException("Size of the memory store must be larger than zero.");
258 }
259 this.limitCapacityTo = size;
260 return this;
261 }
262
263 @Override
264 public String toString() {
265 final StringBuilder builder = new StringBuilder();
266 builder.append("LogWatchBuilder [bufferSize=").append(this.bufferSize).append(", closingBetweenReads=")
267 .append(this.closingBetweenReads).append(", delayBetweenReads=").append(this.delayBetweenReads)
268 .append(", delayBetweenSweeps=").append(this.delayBetweenSweeps).append(", ");
269 if (this.fileToWatch != null) {
270 builder.append("fileToWatch=").append(this.fileToWatch).append(", ");
271 }
272 if (this.gateCondition != null) {
273 builder.append("gateCondition=").append(this.gateCondition).append(", ");
274 }
275 builder.append("limitCapacityTo=").append(this.limitCapacityTo).append(", readingFromBeginning=")
276 .append(this.readingFromBeginning).append(", ");
277 if (this.storageCondition != null) {
278 builder.append("storageCondition=").append(this.storageCondition);
279 }
280 builder.append("]");
281 return builder.toString();
282 }
283
284
285
286
287
288
289
290 public LogWatchBuilder watchedFile(final File f) {
291 if (f == null) {
292 throw new IllegalArgumentException("File can not be null.");
293 }
294 this.fileToWatch = f;
295 return this;
296 }
297
298
299
300
301
302
303
304
305 @Deprecated
306 public LogWatchBuilder watchingFile(final File f) {
307 return this.watchedFile(f);
308 }
309
310
311
312 public boolean willBeStarted() {
313 return this.autoStarting;
314 }
315
316
317
318
319
320
321
322
323
324
325 public LogWatchBuilder withDelayBetweenReads(final int length, final TimeUnit unit) {
326 this.delayBetweenReads = LogWatchBuilder.getDelay(length, unit);
327 return this;
328 }
329
330
331
332
333
334
335
336
337
338
339
340 public LogWatchBuilder withDelayBetweenSweeps(final int length, final TimeUnit unit) {
341 this.delayBetweenSweeps = LogWatchBuilder.getDelay(length, unit);
342 return this;
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356 public LogWatchBuilder withGateCondition(final SimpleMessageCondition condition) {
357 if (condition == null) {
358 throw new IllegalArgumentException("Gate condition must not be null.");
359 }
360 this.gateCondition = condition;
361 return this;
362 }
363
364
365
366
367
368
369
370
371
372 public LogWatchBuilder withReadingBufferSize(final int bufferSize) {
373 if (bufferSize < 1) {
374 throw new IllegalArgumentException("Buffer size must be at least 1.");
375 }
376 this.bufferSize = bufferSize;
377 return this;
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 public LogWatchBuilder withStorageCondition(final SimpleMessageCondition condition) {
394 if (condition == null) {
395 throw new IllegalArgumentException("Storage acceptance condition must not be null.");
396 }
397 this.storageCondition = condition;
398 return this;
399 }
400
401 }