1
|
package eu.dnetlib.xml.database.exist;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileNotFoundException;
|
5
|
import java.io.FileOutputStream;
|
6
|
import java.io.FileReader;
|
7
|
import java.io.FileWriter;
|
8
|
import java.io.IOException;
|
9
|
import java.io.InputStream;
|
10
|
import java.io.StringReader;
|
11
|
import java.io.StringWriter;
|
12
|
|
13
|
import eu.dnetlib.enabling.is.store.BulkResourceImporter;
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.beans.factory.annotation.Required;
|
19
|
|
20
|
/**
|
21
|
* Persistent exist.
|
22
|
*
|
23
|
* TODO: this is copy&paste from TemporaryExistDatase. Refactor common stuff.
|
24
|
*
|
25
|
* @author marko
|
26
|
*
|
27
|
*/
|
28
|
public class PersistentExistDatabase extends ExistDatabase {
|
29
|
|
30
|
/**
|
31
|
* logger.
|
32
|
*/
|
33
|
private static final Log log = LogFactory.getLog(PersistentExistDatabase.class); // NOPMD by marko on 11/24/08 5:02 PM
|
34
|
|
35
|
/**
|
36
|
* db directory.
|
37
|
*/
|
38
|
private transient File dbDirectory;
|
39
|
|
40
|
/**
|
41
|
* this config file will be copied to a newly created temporary directory.
|
42
|
*/
|
43
|
private String configTemplate = "default-exist-conf.xml";
|
44
|
|
45
|
/**
|
46
|
* db root path.
|
47
|
*/
|
48
|
private String dbRootPath;
|
49
|
|
50
|
/**
|
51
|
* bulk importer.
|
52
|
*/
|
53
|
@Autowired
|
54
|
private BulkResourceImporter bulkImporter;
|
55
|
|
56
|
/**
|
57
|
* exist config file.
|
58
|
*/
|
59
|
private File existConfigFile;
|
60
|
|
61
|
/**
|
62
|
* true if the database permits execution of java code from xquery.
|
63
|
*/
|
64
|
private boolean XQueryJavaEnabled;
|
65
|
|
66
|
/**
|
67
|
* {@inheritDoc}
|
68
|
*
|
69
|
* @see eu.dnetlib.xml.database.exist.ExistDatabase#start()
|
70
|
*/
|
71
|
@Override
|
72
|
public void start() {
|
73
|
log.warn("STARTING PERSISTENT EXIST DATABASE");
|
74
|
dbDirectory = new File(dbRootPath);
|
75
|
|
76
|
if (dbDirectory.exists()) {
|
77
|
getBulkImporter().setEnabled(false);
|
78
|
existConfigFile = new File(dbDirectory, "conf.xml");
|
79
|
enableJava(existConfigFile, isXQueryJavaEnabled());
|
80
|
setConfigFile(existConfigFile.getAbsolutePath());
|
81
|
} else {
|
82
|
createPersistentDatabase();
|
83
|
}
|
84
|
|
85
|
super.start();
|
86
|
}
|
87
|
|
88
|
@Override
|
89
|
public void stop() {
|
90
|
log.info("shutting down xmldb");
|
91
|
super.stop();
|
92
|
log.info("xmldb closed");
|
93
|
}
|
94
|
|
95
|
protected void enableJava(final File conf, final boolean enabled) {
|
96
|
final StringWriter buffer = new StringWriter();
|
97
|
if (conf.exists()) {
|
98
|
try {
|
99
|
IOUtils.copy(new FileReader(conf), buffer);
|
100
|
final String newConf = patchConfigFileEnableJava(buffer.toString(), enabled);
|
101
|
if (!newConf.equals(buffer.toString())) {
|
102
|
FileWriter writer = new FileWriter(conf);
|
103
|
try {
|
104
|
IOUtils.copy(new StringReader(newConf), writer);
|
105
|
} finally {
|
106
|
writer.close();
|
107
|
}
|
108
|
}
|
109
|
} catch (final FileNotFoundException e) {
|
110
|
log.warn("cannot patch eXist conf file", e);
|
111
|
} catch (final IOException e) {
|
112
|
log.warn("cannot patch eXist conf file", e);
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* @param conf
|
119
|
* content of the configuration file
|
120
|
* @param enabled
|
121
|
* enabled or disabled
|
122
|
* @return new conf file
|
123
|
*/
|
124
|
protected String patchConfigFileEnableJava(final String conf, final boolean enabled) {
|
125
|
return conf.replaceAll("enable-java-binding=\"[^\"]*\"", "enable-java-binding=\"" + (enabled ? "yes" : "no") + "\"");
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* create a temporary directory and copy the default configuration file.
|
130
|
*/
|
131
|
protected void createPersistentDatabase() {
|
132
|
log.debug("creating persistent database");
|
133
|
try {
|
134
|
new File(dbDirectory, "data").mkdirs();
|
135
|
|
136
|
final InputStream defaultConf = getClass().getResourceAsStream(getConfigTemplate());
|
137
|
if (defaultConf == null)
|
138
|
throw new IOException("cannot find " + getConfigTemplate());
|
139
|
|
140
|
existConfigFile = new File(dbDirectory, "conf.xml");
|
141
|
final FileOutputStream confOutput = new FileOutputStream(existConfigFile);
|
142
|
|
143
|
try {
|
144
|
IOUtils.copy(defaultConf, confOutput);
|
145
|
} finally {
|
146
|
confOutput.close();
|
147
|
}
|
148
|
enableJava(existConfigFile, isXQueryJavaEnabled());
|
149
|
|
150
|
setConfigFile(existConfigFile.getAbsolutePath());
|
151
|
} catch (final IOException e) {
|
152
|
log.fatal("creating database dir", e);
|
153
|
throw new IllegalStateException(e);
|
154
|
}
|
155
|
log.debug("created temp database");
|
156
|
}
|
157
|
|
158
|
public File getDbDirectory() {
|
159
|
return dbDirectory;
|
160
|
}
|
161
|
|
162
|
public void setDbDirectory(final File dbDirectory) {
|
163
|
this.dbDirectory = dbDirectory;
|
164
|
}
|
165
|
|
166
|
public String getConfigTemplate() {
|
167
|
return configTemplate;
|
168
|
}
|
169
|
|
170
|
public void setConfigTemplate(final String configTemplate) {
|
171
|
this.configTemplate = configTemplate;
|
172
|
}
|
173
|
|
174
|
public BulkResourceImporter getBulkImporter() {
|
175
|
return bulkImporter;
|
176
|
}
|
177
|
|
178
|
@Required
|
179
|
public void setBulkImporter(final BulkResourceImporter bulkImporter) {
|
180
|
this.bulkImporter = bulkImporter;
|
181
|
}
|
182
|
|
183
|
public File getExistConfigFile() {
|
184
|
return existConfigFile;
|
185
|
}
|
186
|
|
187
|
public void setExistConfigFile(final File existConfigFile) {
|
188
|
this.existConfigFile = existConfigFile;
|
189
|
}
|
190
|
|
191
|
@Required
|
192
|
public String getDbRootPath() {
|
193
|
return dbRootPath;
|
194
|
}
|
195
|
|
196
|
public void setDbRootPath(final String dbRootPath) {
|
197
|
this.dbRootPath = dbRootPath;
|
198
|
}
|
199
|
|
200
|
public boolean isXQueryJavaEnabled() {
|
201
|
return XQueryJavaEnabled;
|
202
|
}
|
203
|
|
204
|
public void setXQueryJavaEnabled(final boolean xQueryJavaEnabled) {
|
205
|
XQueryJavaEnabled = xQueryJavaEnabled;
|
206
|
}
|
207
|
|
208
|
}
|