-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWiggleIndexerPlugin.java
More file actions
127 lines (98 loc) · 3.95 KB
/
Copy pathWiggleIndexerPlugin.java
File metadata and controls
127 lines (98 loc) · 3.95 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
//import org.neo4j.kernel.impl.util.FileUtils;
import tasklisteners.AfterAnalyze;
import com.sun.source.util.JavacTask;
public class WiggleIndexerPlugin implements com.sun.source.util.Plugin {
private static final String PLUGIN_NAME = "WiggleIndexerPlugin";
private GraphDatabaseBuilder graphDbBuilder;
private String wiggleDbPath;
private String wiggleClearDb;
@Override
public void init(JavacTask task, String[] args) {
createDb();
Map<String, String> env = System.getenv();
Map<String, String> cuProps = new HashMap<>();
for (Map.Entry<String, String> e : env.entrySet()) {
if (e.getKey().startsWith("WIGGLE_CUPROP_")) {
cuProps.put(underToCamel(e.getKey().substring("WIGGLE_CUPROP_".length())), e.getValue());
}
}
String projectName = getProjectName();
System.out.println("Running " + PLUGIN_NAME);
System.out.println("WIGGLE_PROJECT_NAME:" + projectName);
System.out.println("WIGGLE_DB_PATH:" + wiggleDbPath);
System.out.println("WIGGLE_CLEAR_DB:" + wiggleClearDb);
cuProps.put("projectName", projectName);
task.setTaskListener(new AfterAnalyze(task, graphDbBuilder, cuProps));
System.out.println("finished");
}
// Turn SOME_CAMEL_IDENT into someCamelIdent. A trailing underscore is left intact
private static String underToCamel(String s) {
String r = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '_' && i + 1 < s.length()) {
r += Character.toUpperCase(s.charAt(i + 1)); // Upper case following char
i += 1; // Skip next char now we've dealt with it
} else {
r += Character.toLowerCase(s.charAt(i));
}
}
return r;
}
private String getProjectName() {
String projectName = System.getenv("WIGGLE_PROJECT_NAME");
if (projectName == null)
projectName = "NO_NAME";
return projectName;
}
private String getDBPath() {
String dbPath = System.getenv("WIGGLE_DB_PATH");
if (dbPath == null)
dbPath = "./neo4j/data/wiggle.db";
return dbPath;
}
private File getDBFile() {
return new File("./neo4j/data/graph4code.db");
}
public void createDb() {
this.wiggleDbPath = getDBPath();
this.wiggleClearDb = System.getenv("WIGGLE_CLEAR_DB");
if (wiggleClearDb != null && wiggleClearDb.equals("y")) {
clearDb(wiggleDbPath);
}
graphDbBuilder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(getDBFile()).
setConfig(GraphDatabaseSettings.node_keys_indexable, "nodeType").
setConfig(GraphDatabaseSettings.relationship_keys_indexable, "typeKind").
setConfig(GraphDatabaseSettings.node_auto_indexing, "true").
setConfig(GraphDatabaseSettings.relationship_auto_indexing, "true");
//registerShutdownHook( graphDb );
}
private void clearDb(String dbPath) {
// try {
// FileUtils.deleteRecursively(new File(dbPath));
System.out.println("delete recursively");
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
}
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
}
@Override
public String getName() {
return PLUGIN_NAME;
}
}