Project

General

Profile

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.Required;
18

    
19
/**
20
 * Persistent exist.
21
 *
22
 * TODO: this is copy&paste from TemporaryExistDatase. Refactor common stuff.
23
 *
24
 * @author marko
25
 *
26
 */
27
public class PersistentExistDatabase extends ExistDatabase {
28

    
29
	/**
30
	 * logger.
31
	 */
32
	private static final Log log = LogFactory.getLog(PersistentExistDatabase.class); // NOPMD by marko on 11/24/08 5:02 PM
33

    
34
	/**
35
	 * db directory.
36
	 */
37
	private transient File dbDirectory;
38

    
39
	/**
40
	 * this config file will be copied to a newly created temporary directory.
41
	 */
42
	private String configTemplate = "default-exist-conf.xml";
43

    
44
	/**
45
	 * db root path.
46
	 */
47
	private String dbRootPath;
48

    
49
	/**
50
	 * bulk importer.
51
	 */
52
	private BulkResourceImporter bulkImporter;
53

    
54
	/**
55
	 * exist config file.
56
	 */
57
	private File existConfigFile;
58

    
59
	/**
60
	 * true if the database permits execution of java code from xquery.
61
	 */
62
	private boolean XQueryJavaEnabled;
63

    
64
	/**
65
	 * {@inheritDoc}
66
	 *
67
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#start()
68
	 */
69
	@Override
70
	public void start() {
71
		log.warn("STARTING PERSISTENT EXIST DATABASE");
72
		dbDirectory = new File(dbRootPath);
73

    
74
		if (dbDirectory.exists()) {
75
			getBulkImporter().setEnabled(false);
76
			existConfigFile = new File(dbDirectory, "conf.xml");
77
			enableJava(existConfigFile, isXQueryJavaEnabled());
78
			setConfigFile(existConfigFile.getAbsolutePath());
79
		} else {
80
			createPersistentDatabase();
81
		}
82

    
83
		super.start();
84
	}
85

    
86
	@Override
87
	public void stop() {
88
		log.info("shutting down xmldb");
89
		super.stop();
90
		log.info("xmldb closed");
91
	}
92

    
93
	protected void enableJava(final File conf, final boolean enabled) {
94
		final StringWriter buffer = new StringWriter();
95
		if (conf.exists()) {
96
			try {
97
				IOUtils.copy(new FileReader(conf), buffer);
98
				final String newConf = patchConfigFileEnableJava(buffer.toString(), enabled);
99
				if (!newConf.equals(buffer.toString())) {
100
					FileWriter writer = new FileWriter(conf);
101
					try {
102
						IOUtils.copy(new StringReader(newConf), writer);
103
					} finally {
104
						writer.close();
105
					}
106
				}
107
			} catch (final FileNotFoundException e) {
108
				log.warn("cannot patch eXist conf file", e);
109
			} catch (final IOException e) {
110
				log.warn("cannot patch eXist conf file", e);
111
			}
112
		}
113
	}
114

    
115
	/**
116
	 * @param conf
117
	 *            content of the configuration file
118
	 * @param enabled
119
	 *            enabled or disabled
120
	 * @return new conf file
121
	 */
122
	protected String patchConfigFileEnableJava(final String conf, final boolean enabled) {
123
		return conf.replaceAll("enable-java-binding=\"[^\"]*\"", "enable-java-binding=\"" + (enabled ? "yes" : "no") + "\"");
124
	}
125

    
126
	/**
127
	 * create a temporary directory and copy the default configuration file.
128
	 */
129
	protected void createPersistentDatabase() {
130
		log.debug("creating persistent database");
131
		try {
132
			new File(dbDirectory, "data").mkdirs();
133

    
134
			final InputStream defaultConf = getClass().getResourceAsStream(getConfigTemplate());
135
			if (defaultConf == null)
136
				throw new IOException("cannot find " + getConfigTemplate());
137

    
138
			existConfigFile = new File(dbDirectory, "conf.xml");
139
			final FileOutputStream confOutput = new FileOutputStream(existConfigFile);
140

    
141
			try {
142
				IOUtils.copy(defaultConf, confOutput);
143
			} finally {
144
				confOutput.close();
145
			}
146
			enableJava(existConfigFile, isXQueryJavaEnabled());
147

    
148
			setConfigFile(existConfigFile.getAbsolutePath());
149
		} catch (final IOException e) {
150
			log.fatal("creating database dir", e);
151
			throw new IllegalStateException(e);
152
		}
153
		log.debug("created temp database");
154
	}
155

    
156
	public File getDbDirectory() {
157
		return dbDirectory;
158
	}
159

    
160
	public void setDbDirectory(final File dbDirectory) {
161
		this.dbDirectory = dbDirectory;
162
	}
163

    
164
	public String getConfigTemplate() {
165
		return configTemplate;
166
	}
167

    
168
	public void setConfigTemplate(final String configTemplate) {
169
		this.configTemplate = configTemplate;
170
	}
171

    
172
	public BulkResourceImporter getBulkImporter() {
173
		return bulkImporter;
174
	}
175

    
176
	@Required
177
	public void setBulkImporter(final BulkResourceImporter bulkImporter) {
178
		this.bulkImporter = bulkImporter;
179
	}
180

    
181
	public File getExistConfigFile() {
182
		return existConfigFile;
183
	}
184

    
185
	public void setExistConfigFile(final File existConfigFile) {
186
		this.existConfigFile = existConfigFile;
187
	}
188

    
189
	@Required
190
	public String getDbRootPath() {
191
		return dbRootPath;
192
	}
193

    
194
	public void setDbRootPath(final String dbRootPath) {
195
		this.dbRootPath = dbRootPath;
196
	}
197

    
198
	public boolean isXQueryJavaEnabled() {
199
		return XQueryJavaEnabled;
200
	}
201

    
202
	public void setXQueryJavaEnabled(final boolean xQueryJavaEnabled) {
203
		XQueryJavaEnabled = xQueryJavaEnabled;
204
	}
205

    
206
}
(7-7/8)