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 org.apache.commons.io.IOUtils;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.springframework.beans.factory.annotation.Required;
17

    
18
import eu.dnetlib.enabling.tools.BulkResourceImporter;
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
	private BulkResourceImporter bulkImporter;
54

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

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

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

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

    
84
		super.start();
85
	}
86

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
207
}
(7-7/8)