Skip to content

Commit eaab6df

Browse files
committed
Fixed build break
1 parent 9c3a2a5 commit eaab6df

File tree

3 files changed

+202
-74
lines changed

3 files changed

+202
-74
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.audit4j.core.handler.file;
2+
3+
4+
public abstract class AuditFileWriter {
5+
public abstract AuditFileWriter write(String event);
6+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package org.audit4j.core.handler.file;
2+
3+
import java.io.File;
4+
import java.util.Calendar;
5+
import java.util.Date;
6+
7+
import org.audit4j.core.AuditUtil;
8+
import org.audit4j.core.CoreConstants;
9+
import org.audit4j.core.TroubleshootManager;
10+
11+
public final class FileHandlerUtil {
12+
private FileHandlerUtil() {
13+
}
14+
15+
/**
16+
* Generate output file path.
17+
*
18+
* @return the string
19+
*/
20+
@Deprecated
21+
public static String generateOutputFilePath(String path) {
22+
String tempPath = separatorsToSystem(path);
23+
tempPath = tempPath + File.separatorChar + generateAuditFileName();
24+
return tempPath;
25+
}
26+
/**
27+
* Generate output file path.
28+
*
29+
* @return the string
30+
*/
31+
public static String generateOutputFilePath(String path, String fileName) {
32+
String tempPath = separatorsToSystem(path);
33+
tempPath = tempPath + File.separatorChar + fileName;
34+
return tempPath;
35+
}
36+
37+
/**
38+
* Generate file name.
39+
*
40+
* @return the string
41+
*/
42+
public static String generatePreviousPreviousFileName() {
43+
StringBuffer name = new StringBuffer();
44+
name.append("Audit_Log-").append(AuditUtil.dateToString(new Date(), "yyyy-MM-dd"))
45+
.append(CoreConstants.AUDIT_EXTENTION);
46+
return name.toString();
47+
}
48+
49+
/**
50+
* Generate file name.
51+
*
52+
* @return the string
53+
*/
54+
public static String generateAuditFileName() {
55+
StringBuffer name = new StringBuffer();
56+
name.append("Audit_Log-").append(AuditUtil.dateToString(new Date(), "yyyy-MM-dd"))
57+
.append(CoreConstants.AUDIT_EXTENTION);
58+
return name.toString();
59+
}
60+
61+
/**
62+
* Generate file name.
63+
*
64+
* @return the string
65+
*/
66+
public static String generateAuditArchiveFileName(Date date) {
67+
StringBuffer name = new StringBuffer();
68+
name.append("Audit_Log-").append(AuditUtil.dateToString(date, "yyyy-MM-dd"))
69+
.append(".auditarchive");
70+
return name.toString();
71+
}
72+
73+
74+
/**
75+
* Generate file name.
76+
*
77+
* @return the string
78+
*/
79+
public static String generateAuditFileName(Date date) {
80+
StringBuffer name = new StringBuffer();
81+
name.append("Audit_Log-").append(AuditUtil.dateToString(date, "yyyy-MM-dd"))
82+
.append(CoreConstants.AUDIT_EXTENTION);
83+
return name.toString();
84+
}
85+
86+
/**
87+
* Generate file name.
88+
*
89+
* @return the string
90+
*/
91+
public static String generateCommonFileName(Date date) {
92+
StringBuffer name = new StringBuffer();
93+
name.append("Audit_Log-").append(AuditUtil.dateToString(date, "yyyy-MM-dd"));
94+
return name.toString();
95+
}
96+
97+
/**
98+
* Converts all separators to the Unix separator of forward slash.
99+
*
100+
* @param path
101+
* the path to be changed, null ignored
102+
* @return the updated path
103+
*/
104+
public static String separatorsToUnix(String path) {
105+
if (path == null || path.indexOf(CoreConstants.WINDOWS_SEPARATOR) == -1) {
106+
return path;
107+
}
108+
return path.replace(CoreConstants.WINDOWS_SEPARATOR, CoreConstants.UNIX_SEPARATOR);
109+
}
110+
111+
/**
112+
* Converts all separators to the Windows separator of backslash.
113+
*
114+
* @param path
115+
* the path to be changed, null ignored
116+
* @return the updated path
117+
*/
118+
public static String separatorsToWindows(String path) {
119+
if (path == null || path.indexOf(CoreConstants.UNIX_SEPARATOR) == -1) {
120+
return path;
121+
}
122+
return path.replace(CoreConstants.UNIX_SEPARATOR, CoreConstants.WINDOWS_SEPARATOR);
123+
}
124+
125+
/**
126+
* Converts all separators to the system separator.
127+
*
128+
* @param path
129+
* the path to be changed, null ignored
130+
* @return the updated path
131+
*/
132+
public static String separatorsToSystem(String path) {
133+
if (path == null) {
134+
return null;
135+
}
136+
if (TroubleshootManager.isWindows()) {
137+
return separatorsToWindows(path);
138+
} else {
139+
return separatorsToUnix(path);
140+
}
141+
}
142+
143+
/**
144+
* Checks if is file already exists.
145+
*
146+
* @param path
147+
* the path
148+
* @return true, if is file already exists
149+
*/
150+
public static boolean isFileAlreadyExists(String path) {
151+
File file = new File(path);
152+
if (file.exists()) {
153+
return true;
154+
} else {
155+
return false;
156+
}
157+
}
158+
159+
/**
160+
* Add days.
161+
*
162+
* @param date
163+
* the date
164+
* @param different
165+
* the different
166+
* @param unit
167+
* the unit
168+
* @return the date
169+
*/
170+
public static Date addDate(final Date date, final Integer different) {
171+
final Calendar cal = Calendar.getInstance();
172+
cal.setTime(date);
173+
cal.add(Calendar.DATE, different);
174+
return cal.getTime();
175+
}
176+
177+
/**
178+
* Substract days.
179+
*
180+
* @param date
181+
* the date
182+
* @param different
183+
* the different
184+
* @param unit
185+
* the unit
186+
* @return the date
187+
*/
188+
public static Date substractDate(final Date date, final Integer different) {
189+
190+
return addDate(date, -different);
191+
}
192+
}

src/main/java/org/audit4j/core/handler/file/ZeroCopyFileWriter.java

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@
2929
import java.nio.channels.FileChannel;
3030
import java.nio.channels.ReadableByteChannel;
3131
import java.nio.charset.Charset;
32-
import java.util.Date;
3332

34-
import org.audit4j.core.AuditUtil;
3533
import org.audit4j.core.CoreConstants;
36-
import org.audit4j.core.TroubleshootManager;
3734

3835
/**
3936
* The Class ZeroCopyFileWriter.
4037
*
4138
* @author <a href="mailto:[email protected]">Janith Bandara</a>
4239
*/
43-
public final class ZeroCopyFileWriter implements Serializable{
40+
public final class ZeroCopyFileWriter extends AuditFileWriter implements Serializable{
4441

4542

4643
/** The path. */
@@ -63,17 +60,18 @@ public ZeroCopyFileWriter(String path) {
6360
* org.audit4j.core.io.AuditOutputStream#write(org.audit4j.core.dto.AuditEvent
6461
* )
6562
*/
63+
@Override
6664
public ZeroCopyFileWriter write(String event) {
6765
String str2 = event + CoreConstants.NEW_LINE;
6866
RandomAccessFile randomAccessFile;
69-
String realPath = generateOutputFilePath();
67+
String realPath = FileHandlerUtil.generateOutputFilePath(path);
7068

7169
long numBytes = str2.getBytes().length;
7270

7371
InputStream inputStream = new ByteArrayInputStream(str2.getBytes(Charset.forName("UTF-8")));
7472

7573
try {
76-
if (TroubleshootManager.isFileAlreadyExists(realPath)) {
74+
if (FileHandlerUtil.isFileAlreadyExists(realPath)) {
7775
randomAccessFile = new RandomAccessFile(realPath, CoreConstants.READ_WRITE);
7876
} else {
7977
randomAccessFile = new RandomAccessFile(new File(realPath), CoreConstants.READ_WRITE);
@@ -97,74 +95,6 @@ public ZeroCopyFileWriter write(String event) {
9795
return this;
9896
}
9997

100-
/**
101-
* Generate output file path.
102-
*
103-
* @return the string
104-
*/
105-
public String generateOutputFilePath() {
106-
String tempPath = separatorsToSystem(path);
107-
tempPath = tempPath + File.separatorChar + generateFileName();
108-
return tempPath;
109-
}
110-
111-
/**
112-
* Generate file name.
113-
*
114-
* @return the string
115-
*/
116-
private String generateFileName() {
117-
StringBuffer name = new StringBuffer();
118-
name.append("Audit_Log-").append(AuditUtil.dateToString(new Date(), "yyyy-MM-dd"))
119-
.append(CoreConstants.AUDIT_EXTENTION);
120-
return name.toString();
121-
}
122-
123-
/**
124-
* Converts all separators to the Unix separator of forward slash.
125-
*
126-
* @param path
127-
* the path to be changed, null ignored
128-
* @return the updated path
129-
*/
130-
public static String separatorsToUnix(String path) {
131-
if (path == null || path.indexOf(CoreConstants.WINDOWS_SEPARATOR) == -1) {
132-
return path;
133-
}
134-
return path.replace(CoreConstants.WINDOWS_SEPARATOR, CoreConstants.UNIX_SEPARATOR);
135-
}
136-
137-
/**
138-
* Converts all separators to the Windows separator of backslash.
139-
*
140-
* @param path
141-
* the path to be changed, null ignored
142-
* @return the updated path
143-
*/
144-
public static String separatorsToWindows(String path) {
145-
if (path == null || path.indexOf(CoreConstants.UNIX_SEPARATOR) == -1) {
146-
return path;
147-
}
148-
return path.replace(CoreConstants.UNIX_SEPARATOR, CoreConstants.WINDOWS_SEPARATOR);
149-
}
150-
151-
/**
152-
* Converts all separators to the system separator.
153-
*
154-
* @param path
155-
* the path to be changed, null ignored
156-
* @return the updated path
157-
*/
158-
public static String separatorsToSystem(String path) {
159-
if (path == null) {
160-
return null;
161-
}
162-
if (TroubleshootManager.isWindows()) {
163-
return separatorsToWindows(path);
164-
} else {
165-
return separatorsToUnix(path);
166-
}
167-
}
16898

16999
/*
170100
* (non-Javadoc)

0 commit comments

Comments
 (0)