Search in sources :

Example 26 with Configuration

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);
}
Also used : Path(java.nio.file.Path) Configuration(org.exist.util.Configuration) Scheduler(org.exist.scheduler.Scheduler) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 27 with Configuration

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);
}
Also used : Path(java.nio.file.Path) Configuration(org.exist.util.Configuration) Scheduler(org.exist.scheduler.Scheduler) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 28 with Configuration

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();
}
Also used : Path(java.nio.file.Path) BrokerPool(org.exist.storage.BrokerPool) ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Subject(org.exist.security.Subject) EXistException(org.exist.EXistException) Path(java.nio.file.Path) XMLDBException(org.xmldb.api.base.XMLDBException) XQueryURLRewrite(org.exist.http.urlrewrite.XQueryURLRewrite) DatabaseManager(org.xmldb.api.DatabaseManager) ServletConfig(javax.servlet.ServletConfig) HttpServlet(javax.servlet.http.HttpServlet) AuthenticationException(org.exist.security.AuthenticationException) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Database(org.xmldb.api.base.Database) IOException(java.io.IOException) HttpAccount(org.exist.security.internal.web.HttpAccount) SecurityManager(org.exist.security.SecurityManager) Logger(org.apache.logging.log4j.Logger) Principal(java.security.Principal) XmldbPrincipal(org.exist.security.XmldbPrincipal) Paths(java.nio.file.Paths) Optional(java.util.Optional) Configuration(org.exist.util.Configuration) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) ServletException(javax.servlet.ServletException) Configuration(org.exist.util.Configuration)

Example 29 with Configuration

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();
    }
}
Also used : Path(java.nio.file.Path) DBBroker(org.exist.storage.DBBroker) Configuration(org.exist.util.Configuration) EXistException(org.exist.EXistException) IOException(java.io.IOException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 30 with Configuration

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;
    }
}
Also used : RestXqServiceException(org.exquery.restxq.RestXqServiceException) Configuration(org.exist.util.Configuration) DocumentTypedValue(org.exist.extensions.exquery.xdm.type.impl.DocumentTypedValue) XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) InputStream(java.io.InputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) Sequence(org.exquery.xquery.Sequence) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) DocumentImpl(org.exist.dom.memtree.DocumentImpl) MimeType(org.exist.util.MimeType) BinaryTypedValue(org.exist.extensions.exquery.xdm.type.impl.BinaryTypedValue) StringTypedValue(org.exist.extensions.exquery.xdm.type.impl.StringTypedValue) MimeType(org.exist.util.MimeType) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Type(org.exquery.xquery.Type) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) StringValue(org.exist.xquery.value.StringValue) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Aggregations

Configuration (org.exist.util.Configuration)30 Test (org.junit.Test)10 Path (java.nio.file.Path)9 EXistException (org.exist.EXistException)8 IOException (java.io.IOException)7 BrokerPool (org.exist.storage.BrokerPool)6 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)6 InputStream (java.io.InputStream)4 Scheduler (org.exist.scheduler.Scheduler)4 DBBroker (org.exist.storage.DBBroker)4 Paths (java.nio.file.Paths)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 Subject (org.exist.security.Subject)3 MalformedURLException (java.net.MalformedURLException)2 Map (java.util.Map)2 ServletException (javax.servlet.ServletException)2 Database (org.exist.Database)2 AuthenticationException (org.exist.security.AuthenticationException)2 SecurityManager (org.exist.security.SecurityManager)2 Ignore (org.junit.Ignore)2