use of org.exist.util.Configuration in project exist by eXist-db.
the class JournalTest method switchFiles.
@Test
public void switchFiles() throws EXistException, IOException, ReadOnlyException, InterruptedException {
final BrokerPool mockBrokerPool = mock(BrokerPool.class);
final Configuration mockConfiguration = mock(Configuration.class);
final Scheduler mockScheduler = createNiceMock(Scheduler.class);
expect(mockBrokerPool.getConfiguration()).andReturn(mockConfiguration);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_SYNC_ON_COMMIT, Journal.DEFAULT_SYNC_ON_COMMIT)).andReturn(Journal.DEFAULT_SYNC_ON_COMMIT);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_JOURNAL_DIR)).andReturn(null);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_SIZE_MIN, Journal.DEFAULT_MIN_SIZE)).andReturn(Journal.DEFAULT_MIN_SIZE);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_SIZE_LIMIT, Journal.DEFAULT_MAX_SIZE)).andReturn(Journal.DEFAULT_MAX_SIZE);
expect(mockBrokerPool.getScheduler()).andReturn(mockScheduler);
replay(mockBrokerPool, mockConfiguration);
final Path tempJournalDir = TEMPORARY_FOLDER.newFolder().toPath();
Files.createDirectories(tempJournalDir);
assertTrue(Files.exists(tempJournalDir));
final Journal journal = new Journal(mockBrokerPool, tempJournalDir);
journal.initialize();
// journal is not yet operating!
assertEquals(-1, journal.getCurrentJournalFileNumber());
// start the journal by calling switch files
journal.switchFiles();
short currentJournalFileNumber = journal.getCurrentJournalFileNumber();
assertEquals(0, currentJournalFileNumber);
final Path journalFile0 = journal.getFile(currentJournalFileNumber);
assertNotNull(journalFile0);
assertTrue(Files.exists(journalFile0));
// advance to a new journal by calling switch files
journal.switchFiles();
currentJournalFileNumber = journal.getCurrentJournalFileNumber();
assertEquals(1, currentJournalFileNumber);
final Path journalFile1 = journal.getFile(currentJournalFileNumber);
assertNotNull(journalFile1);
assertNotEquals(journalFile0.toAbsolutePath().toString(), journalFile1.toAbsolutePath().toString());
assertTrue(Files.exists(journalFile1));
verify(mockBrokerPool, mockConfiguration);
}
use of org.exist.util.Configuration in project exist by eXist-db.
the class JournalTest method switchFilesWrapsCurrentJournalFileNumberAround.
@Test
public void switchFilesWrapsCurrentJournalFileNumberAround() throws EXistException, IOException, ReadOnlyException {
final BrokerPool mockBrokerPool = mock(BrokerPool.class);
final Configuration mockConfiguration = mock(Configuration.class);
final Scheduler mockScheduler = createNiceMock(Scheduler.class);
expect(mockBrokerPool.getConfiguration()).andReturn(mockConfiguration);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_SYNC_ON_COMMIT, Journal.DEFAULT_SYNC_ON_COMMIT)).andReturn(Journal.DEFAULT_SYNC_ON_COMMIT);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_JOURNAL_DIR)).andReturn(null);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_SIZE_MIN, Journal.DEFAULT_MIN_SIZE)).andReturn(Journal.DEFAULT_MIN_SIZE);
expect(mockConfiguration.getProperty(Journal.PROPERTY_RECOVERY_SIZE_LIMIT, Journal.DEFAULT_MAX_SIZE)).andReturn(Journal.DEFAULT_MAX_SIZE);
expect(mockBrokerPool.getScheduler()).andReturn(mockScheduler);
replay(mockBrokerPool, mockConfiguration);
final Path tempJournalDir = TEMPORARY_FOLDER.newFolder().toPath();
Files.createDirectories(tempJournalDir);
assertTrue(Files.exists(tempJournalDir));
final Journal journal = new Journal(mockBrokerPool, tempJournalDir);
journal.initialize();
// journal is not yet operating!
assertEquals(-1, journal.getCurrentJournalFileNumber());
// set the current journal file number to one less than the maximum
journal.setCurrentJournalFileNumber((short) (Short.MAX_VALUE - 1));
short currentJournalFileNumber = journal.getCurrentJournalFileNumber();
// journal is not yet operating!
assertEquals(Short.MAX_VALUE - 1, currentJournalFileNumber);
// start the journal by calling switch files
journal.switchFiles();
currentJournalFileNumber = journal.getCurrentJournalFileNumber();
// we should now be at the maximum
assertEquals(Short.MAX_VALUE, currentJournalFileNumber);
final Path journalFileMax = journal.getFile(currentJournalFileNumber);
assertNotNull(journalFileMax);
assertTrue(Files.exists(journalFileMax));
// advance to a new journal by calling switch files, this should cause the journal file number to wrap around
journal.switchFiles();
currentJournalFileNumber = journal.getCurrentJournalFileNumber();
// should be back at the start of the range for journal file numbers
assertEquals(0, currentJournalFileNumber);
final Path journalFile0 = journal.getFile(currentJournalFileNumber);
assertNotNull(journalFile0);
assertNotEquals(journalFileMax.toAbsolutePath().toString(), journalFile0.toAbsolutePath().toString());
assertTrue(Files.exists(journalFile0));
verify(mockBrokerPool, mockConfiguration);
}
use of org.exist.util.Configuration in project exist by eXist-db.
the class AbstractExistHttpServlet method getOrCreateBrokerPool.
private BrokerPool getOrCreateBrokerPool(final ServletConfig config) throws EXistException, DatabaseConfigurationException, ServletException {
// Configure BrokerPool
if (BrokerPool.isConfigured()) {
getLog().info("Database already started. Skipping configuration ...");
} else {
final String confFile = Optional.ofNullable(config.getInitParameter("configuration")).orElse("conf.xml");
final Optional<Path> dbHome = Optional.ofNullable(config.getInitParameter("basedir")).map(baseDir -> Optional.ofNullable(config.getServletContext().getRealPath(baseDir)).map(rp -> Optional.of(Paths.get(rp))).orElse(Optional.ofNullable(config.getServletContext().getRealPath("/")).map(dir -> Paths.get(dir).resolve("WEB-INF").toAbsolutePath()))).orElse(Optional.ofNullable(config.getServletContext().getRealPath("/")).map(Paths::get));
getLog().info("EXistServlet: exist.home={}", dbHome.map(Path::toString).orElse("null"));
final Path cf = dbHome.map(h -> h.resolve(confFile)).orElse(Paths.get(confFile));
getLog().info("Reading configuration from {}", cf.toAbsolutePath().toString());
if (!Files.isReadable(cf)) {
throw new ServletException("Configuration file " + confFile + " not found or not readable");
}
final Configuration configuration = new Configuration(confFile, dbHome);
final String start = config.getInitParameter("start");
if (start != null && "true".equals(start)) {
doDatabaseStartup(configuration);
}
}
return BrokerPool.getInstance();
}
use of org.exist.util.Configuration in project exist by eXist-db.
the class RestXqServiceRegistryPersistence method getRegistryFile.
private Optional<Path> getRegistryFile(final boolean temp) {
try (final DBBroker broker = getBrokerPool().getBroker()) {
final Configuration configuration = broker.getConfiguration();
final Path dataDir = (Path) configuration.getProperty(BrokerPool.PROPERTY_DATA_DIR);
final Path registryFile;
if (temp) {
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
registryFile = temporaryFileManager.getTemporaryFile();
} else {
registryFile = dataDir.resolve(REGISTRY_FILENAME);
}
return Optional.of(registryFile);
} catch (final EXistException | IOException e) {
log.error(e.getMessage(), e);
return Optional.empty();
}
}
use of org.exist.util.Configuration in project exist by eXist-db.
the class RestXqServiceImpl method extractRequestBody.
@Override
protected Sequence extractRequestBody(final HttpRequest request) throws RestXqServiceException {
// TODO don't use close shield input stream and move parsing of form parameters from HttpServletRequestAdapter into RequestBodyParser
InputStream is;
FilterInputStreamCache cache = null;
try {
// first, get the content of the request
is = new CloseShieldInputStream(request.getInputStream());
if (is.available() <= 0) {
return null;
}
// if marking is not supported, we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt)
if (!is.markSupported()) {
cache = FilterInputStreamCacheFactory.getCacheInstance(() -> {
final Configuration configuration = getBrokerPool().getConfiguration();
return (String) configuration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
}, is);
is = new CachingFilterInputStream(cache);
}
is.mark(Integer.MAX_VALUE);
} catch (final IOException ioe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
}
Sequence result = null;
try {
// was there any POST content?
if (is != null && is.available() > 0) {
String contentType = request.getContentType();
// 1) determine if exists mime database considers this binary data
if (contentType != null) {
// strip off any charset encoding info
if (contentType.contains(";")) {
contentType = contentType.substring(0, contentType.indexOf(";"));
}
MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
if (mimeType != null && !mimeType.isXMLType()) {
// binary data
try {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), is);
if (binaryValue != null) {
result = new SequenceImpl<>(new BinaryTypedValue(binaryValue));
}
} catch (final XPathException xpe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, xpe);
}
}
}
if (result == null) {
// 2) not binary, try and parse as an XML document
final DocumentImpl doc = parseAsXml(is);
if (doc != null) {
result = new SequenceImpl<>(new DocumentTypedValue(doc));
}
}
if (result == null) {
String encoding = request.getCharacterEncoding();
// 3) not a valid XML document, return a string representation of the document
if (encoding == null) {
encoding = "UTF-8";
}
try {
// reset the stream, as we need to reuse for string parsing
is.reset();
final StringValue str = parseAsString(is, encoding);
if (str != null) {
result = new SequenceImpl<>(new StringTypedValue(str));
}
} catch (final IOException ioe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
}
}
}
} catch (IOException e) {
throw new RestXqServiceException(e.getMessage());
} finally {
if (cache != null) {
try {
cache.invalidate();
} catch (final IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
}
if (is != null) {
/*
* Do NOT close the stream if its a binary value,
* because we will need it later for serialization
*/
boolean isBinaryType = false;
if (result != null) {
try {
final Type type = result.head().getType();
isBinaryType = (type == Type.BASE64_BINARY || type == Type.HEX_BINARY);
} catch (final IndexOutOfBoundsException ioe) {
LOG.warn("Called head on an empty HTTP Request body sequence", ioe);
}
}
if (!isBinaryType) {
try {
is.close();
} catch (final IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
}
}
}
if (result != null) {
return result;
} else {
return Sequence.EMPTY_SEQUENCE;
}
}
Aggregations