This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
CleanDocumentStorageRule.java
73 lines (62 loc) · 2.07 KB
/
CleanDocumentStorageRule.java
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.zanata.util;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.rules.ExternalResource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Slf4j
public class CleanDocumentStorageRule extends ExternalResource {
private static String storagePath;
public static void resetFileData() {
String documentStoragePath = getDocumentStoragePath();
log.debug("document storage path: {}", documentStoragePath);
File path = new File(documentStoragePath);
if (path.exists()) {
try {
FileUtils.deleteDirectory(path);
} catch (IOException e) {
log.error("Failed to delete", path, e);
throw new RuntimeException("error");
}
}
}
public static String getDocumentStoragePath() {
if (storagePath == null) {
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
org.jboss.naming.remote.client.InitialContextFactory.class
.getName());
env.put(Context.PROVIDER_URL, "remote://localhost:4447");
InitialContext remoteContext = null;
try {
remoteContext = new InitialContext(env);
storagePath =
(String) remoteContext
.lookup("zanata/files/document-storage-directory");
}
catch (NamingException e) {
throw Throwables.propagate(e);
}
}
return storagePath;
}
@Override
protected void before() throws Throwable {
super.before();
resetFileData();
}
@Override
protected void after() {
super.after();
resetFileData();
}
}