Skip to content

Commit 604126f

Browse files
committed
added archive job
1 parent ef6abc2 commit 604126f

File tree

5 files changed

+236
-7
lines changed

5 files changed

+236
-7
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.audit4j.core.handler.file;
2+
3+
public abstract class ArchiveJob {
4+
protected Integer archiveDateDiff;
5+
6+
protected String path;
7+
8+
abstract void archive();
9+
10+
public void setArchiveDateDiff(Integer archiveDateDiff) {
11+
this.archiveDateDiff = archiveDateDiff;
12+
}
13+
14+
public void setPath(String path) {
15+
this.path = path;
16+
}
17+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.audit4j.core.handler.file;
2+
3+
import it.sauronsoftware.cron4j.Scheduler;
4+
5+
public class ArchiveManager {
6+
7+
private String datePattern;
8+
9+
private String path;
10+
11+
private String cronPattern;
12+
13+
private ArchiveJob job;
14+
15+
public void init() {
16+
Scheduler s = new Scheduler();
17+
System.out.println("Start Archive Manager");
18+
s.schedule(cronPattern, new Runnable() {
19+
@Override
20+
public void run() {
21+
System.out.println("Start Archive Manager");
22+
ArchiveJob archiveJob = job;
23+
archiveJob.setArchiveDateDiff(extractArchiveDateCount(datePattern));
24+
archiveJob.setPath(path);
25+
archiveJob.archive();
26+
}
27+
});
28+
}
29+
30+
public Integer extractArchiveDateCount(String datePattern) {
31+
int dateCount = 0;
32+
String[] splits = datePattern.split("d|M|y");
33+
if (splits.length>1) {
34+
dateCount = dateCount + Integer.valueOf(splits[0]);
35+
}
36+
37+
System.out.println(dateCount);
38+
39+
if (splits.length>2) {
40+
dateCount = dateCount + (Integer.valueOf(splits[1]) * 30);
41+
}
42+
System.out.println(dateCount);
43+
44+
if (splits.length>3) {
45+
dateCount = dateCount + (Integer.valueOf(splits[2]) * 365);
46+
}
47+
System.out.println(dateCount);
48+
return dateCount;
49+
}
50+
51+
public void setArchiveDate(String datePattern) {
52+
this.datePattern = datePattern;
53+
}
54+
55+
public void setPath(String path) {
56+
this.path = path;
57+
}
58+
59+
public void setCronPattern(String cronPattern) {
60+
this.cronPattern = cronPattern;
61+
}
62+
63+
public void setJob(ArchiveJob job) {
64+
this.job = job;
65+
}
66+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.audit4j.core.handler.file;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.util.Date;
8+
import java.util.zip.ZipEntry;
9+
import java.util.zip.ZipOutputStream;
10+
11+
import org.audit4j.core.Log;
12+
13+
public class FileArchiveJob extends ArchiveJob {
14+
15+
byte[] buffer = new byte[1024];
16+
17+
@Override
18+
public void archive() {
19+
Log.info("Starting archiving...");
20+
String fileName = FileHandlerUtil.generateAuditFileName(FileHandlerUtil.substractDate(new Date(),
21+
archiveDateDiff));
22+
String archiveFileName = FileHandlerUtil.generateAuditArchiveFileName(FileHandlerUtil.substractDate(new Date(),
23+
archiveDateDiff));
24+
25+
String filePath = FileHandlerUtil.generateOutputFilePath(path, fileName);
26+
String archivePath = FileHandlerUtil.generateOutputFilePath(path, archiveFileName);
27+
28+
if (FileHandlerUtil.isFileAlreadyExists(filePath)) {
29+
30+
FileOutputStream fos = null;
31+
try {
32+
fos = new FileOutputStream(archivePath);
33+
} catch (FileNotFoundException e) {
34+
// TODO Auto-generated catch block
35+
e.printStackTrace();
36+
}
37+
ZipOutputStream zos = new ZipOutputStream(fos);
38+
ZipEntry ze = new ZipEntry(fileName);
39+
try {
40+
zos.putNextEntry(ze);
41+
42+
FileInputStream in = new FileInputStream(filePath);
43+
44+
int len;
45+
while ((len = in.read(buffer)) > 0) {
46+
zos.write(buffer, 0, len);
47+
}
48+
49+
in.close();
50+
zos.closeEntry();
51+
zos.close();
52+
System.out.println("Done");
53+
} catch (IOException e) {
54+
// TODO Auto-generated catch block
55+
e.printStackTrace();
56+
}
57+
}
58+
}
59+
60+
}

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

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@
1414
*/
1515
public class FileAuditHandler extends Handler {
1616

17-
/**
18-
* asdas
19-
*/
17+
/** asdas. */
2018
private static final long serialVersionUID = 1L;
2119

2220
/** The writer. */
2321
ZeroCopyFileWriter writer;
2422

23+
private String archive;
24+
25+
/** The date pattern. */
26+
private String datePattern;
27+
28+
/** The path. */
29+
private String path;
30+
31+
/** The cron pattern. */
32+
private String cronPattern;
33+
34+
/** The job. */
35+
private ArchiveJob job;
36+
2537
/*
2638
* (non-Javadoc)
2739
*
@@ -30,10 +42,19 @@ public class FileAuditHandler extends Handler {
3042
@Override
3143
public void init() throws InitializationException {
3244
writer = new ZeroCopyFileWriter(getProperty("log.file.location"));
33-
if (!hasDiskAccess(getProperty("log.file.location"))) {
34-
throw new InitializationException(
35-
"Can not write a file. Disk is not accessible. Please set read,write permission for "
36-
+ getProperty("log.file.location"));
45+
// if (!hasDiskAccess(getProperty("log.file.location"))) {
46+
// throw new InitializationException(
47+
// "Can not write a file. Disk is not accessible. Please set read,write permission for "
48+
// + getProperty("log.file.location"));
49+
// }
50+
51+
if (null == archive || "true".equals(archive)) {
52+
ArchiveManager manager = new ArchiveManager();
53+
manager.setArchiveDate(datePattern);
54+
manager.setCronPattern(cronPattern);
55+
manager.setPath(getProperty("log.file.location"));
56+
manager.setJob(job);
57+
manager.init();
3758
}
3859
}
3960

@@ -44,6 +65,8 @@ public void init() throws InitializationException {
4465
*/
4566
@Override
4667
public void handle() {
68+
System.out.println("inside handler");
69+
System.out.println("writer" + writer);
4770
writer.write(getQuery());
4871
}
4972

@@ -62,5 +85,37 @@ static boolean hasDiskAccess(final String path) {
6285
return false;
6386
}
6487
}
88+
89+
90+
91+
public void setArchive(String archive) {
92+
this.archive = archive;
93+
}
6594

95+
/**
96+
* Sets the date pattern.
97+
*
98+
* @param datePattern the new date pattern
99+
*/
100+
public void setDatePattern(String datePattern) {
101+
this.datePattern = datePattern;
102+
}
103+
104+
/**
105+
* Sets the path.
106+
*
107+
* @param path the new path
108+
*/
109+
public void setPath(String path) {
110+
this.path = path;
111+
}
112+
113+
/**
114+
* Sets the cron pattern.
115+
*
116+
* @param cronPattern the new cron pattern
117+
*/
118+
public void setCronPattern(String cronPattern) {
119+
this.cronPattern = cronPattern;
120+
}
66121
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.audit4j.core.handler.file;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ArchiveManagerTest {
7+
8+
@Test
9+
public void testExtractArchiveDateCount_d(){
10+
ArchiveManager manager = new ArchiveManager();
11+
int numOfdates = manager.extractArchiveDateCount("1d");
12+
Assert.assertEquals(1, numOfdates);
13+
14+
}
15+
16+
@Test
17+
public void testExtractArchiveDateCount_d_M(){
18+
ArchiveManager manager = new ArchiveManager();
19+
int numOfdates = manager.extractArchiveDateCount("1d1M");
20+
Assert.assertEquals(31, numOfdates);
21+
22+
}
23+
24+
@Test
25+
public void testExtractArchiveDateCount_d_M_y(){
26+
ArchiveManager manager = new ArchiveManager();
27+
int numOfdates = manager.extractArchiveDateCount("1d1M1y");
28+
Assert.assertEquals(396, numOfdates);
29+
30+
}
31+
}

0 commit comments

Comments
 (0)