This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
CleanDocumentStorageRule.java
96 lines (85 loc) · 3.29 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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.net.URL;
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.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
env.put(Context.INITIAL_CONTEXT_FACTORY,
org.jboss.naming.remote.client.InitialContextFactory.class
.getName());
long portOffset = Integer.parseInt(
PropertiesHolder.getProperty("cargo.port.offset", "0"));
long rmiPort = 4447 + portOffset;
env.put(Context.PROVIDER_URL, "remote://localhost:" + rmiPort);
InitialContext remoteContext = null;
try {
remoteContext = new InitialContext(env);
storagePath =
(String) remoteContext
.lookup("zanata/files/document-storage-directory");
}
catch (NamingException e) {
try {
// wildfly uses 'http-remoting:' not 'remote:'
rmiPort = 8080+portOffset;
env.put(Context.PROVIDER_URL, "http-remoting://localhost:" + rmiPort);
remoteContext = new InitialContext(env);
storagePath =
(String) remoteContext
.lookup("zanata/files/document-storage-directory");
} catch (NamingException e1) {
// fall back option:
URL testClassRoot =
Thread.currentThread().getContextClassLoader()
.getResource("setup.properties");
File targetDir =
new File(testClassRoot.getPath()).getParentFile();
storagePath =
new File(targetDir, "zanata-documents")
.getAbsolutePath();
}
}
}
return storagePath;
}
@Override
protected void before() throws Throwable {
super.before();
resetFileData();
}
@Override
protected void after() {
super.after();
resetFileData();
}
}