Search in sources :

Example 1 with FullXmldbURI

use of org.exist.xmldb.FullXmldbURI in project exist by eXist-db.

the class Configurator method save.

public static DocumentImpl save(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI uri) throws IOException, ConfigurationException {
    final StringWriter writer = new StringWriter();
    final SAXSerializer serializer = new SAXSerializer(writer, null);
    try {
        serializer.startDocument();
        serialize(instance, serializer);
        serializer.endDocument();
    } catch (final SAXException saxe) {
        throw new ConfigurationException(saxe.getMessage(), saxe);
    }
    final String data = writer.toString();
    if (data == null || data.length() == 0) {
        return null;
    }
    FullXmldbURI fullURI = null;
    final BrokerPool pool = broker.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    LOG.info("Storing configuration {}/{}", collection.getURI(), uri);
    final SecurityManager securityManager = pool.getSecurityManager();
    try {
        final Subject systemSubject = securityManager.getSystemSubject();
        broker.pushSubject(systemSubject);
        Txn txn = broker.getCurrentTransaction();
        final boolean txnInProgress = txn != null;
        if (!txnInProgress) {
            txn = transact.beginTransaction();
        }
        try {
            txn.acquireCollectionLock(() -> pool.getLockManager().acquireCollectionWriteLock(collection.getURI()));
            fullURI = getFullURI(pool, collection.getURI().append(uri));
            saving.add(fullURI);
            final Permission systemResourcePermission = PermissionFactory.getDefaultResourcePermission(pool.getSecurityManager());
            systemResourcePermission.setOwner(systemSubject);
            systemResourcePermission.setGroup(systemSubject.getDefaultGroup());
            systemResourcePermission.setMode(Permission.DEFAULT_SYSTEM_RESOURCE_PERM);
            broker.storeDocument(txn, uri, new StringInputSource(data), MimeType.XML_TYPE, null, null, systemResourcePermission, null, null, collection);
            broker.saveCollection(txn, collection);
            if (!txnInProgress) {
                transact.commit(txn);
            }
        } catch (final EXistException | PermissionDeniedException | SAXException | LockException e) {
            if (!txnInProgress) {
                transact.abort(txn);
            }
            throw e;
        } finally {
            if (!txnInProgress) {
                txn.close();
            }
        }
        saving.remove(fullURI);
        broker.flush();
        broker.sync(Sync.MAJOR);
        return collection.getDocument(broker, uri.lastSegment());
    } catch (final EXistException | PermissionDeniedException | SAXException | LockException e) {
        LOG.error(e);
        if (fullURI != null) {
            saving.remove(fullURI);
        }
        throw new IOException(e);
    } finally {
        broker.popSubject();
    }
}
Also used : SecurityManager(org.exist.security.SecurityManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) SAXException(org.xml.sax.SAXException) StringInputSource(org.exist.util.StringInputSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) FullXmldbURI(org.exist.xmldb.FullXmldbURI) SAXSerializer(org.exist.util.serializer.SAXSerializer) BrokerPool(org.exist.storage.BrokerPool)

Example 2 with FullXmldbURI

use of org.exist.xmldb.FullXmldbURI in project exist by eXist-db.

the class Configurator method parse.

public static Configuration parse(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI fileURL) throws ConfigurationException {
    final FullXmldbURI key = getFullURI(broker.getBrokerPool(), collection.getURI().append(fileURL));
    Configuration conf = hotConfigs.get(key);
    if (conf != null) {
        return conf;
    }
    // XXX: locking required
    DocumentImpl document;
    try {
        document = collection.getDocument(broker, fileURL);
    } catch (final PermissionDeniedException pde) {
        throw new ConfigurationException(pde.getMessage(), pde);
    }
    if (document == null) {
        if (broker.isReadOnly()) {
            // create in memory document & configuration
            try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
                final Writer writer = new OutputStreamWriter(os)) {
                final SAXSerializer serializer = new SAXSerializer(writer, null);
                serializer.startDocument();
                serialize(instance, serializer);
                serializer.endDocument();
                if (os.size() == 0) {
                    return null;
                }
                return parse(os.toInputStream());
            } catch (final IOException | SAXException e) {
                throw new ConfigurationException(e.getMessage(), e);
            }
        }
        try {
            document = save(instance, broker, collection, fileURL);
        } catch (final IOException e) {
            LOG.error(e.getMessage(), e);
            // TODO : throw exception ? -pb
            return null;
        }
    }
    if (document == null) {
        // possibly on corrupted database, find better solution (recovery flag?)
        return null;
    }
    final Element confElement = document.getDocumentElement();
    if (confElement == null) {
        // possibly on corrupted database, find better solution (recovery flag?)
        return null;
    }
    conf = new ConfigurationImpl(confElement);
    hotConfigs.put(key, conf);
    return conf;
}
Also used : Element(org.w3c.dom.Element) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FullXmldbURI(org.exist.xmldb.FullXmldbURI) SAXSerializer(org.exist.util.serializer.SAXSerializer)

Example 3 with FullXmldbURI

use of org.exist.xmldb.FullXmldbURI in project exist by eXist-db.

the class Configurator method clear.

public static synchronized void clear(final Database db) {
    for (final Entry<FullXmldbURI, Configuration> entry : hotConfigs.entrySet()) {
        final FullXmldbURI uri = entry.getKey();
        if (uri.getInstanceName().equals(db.getId())) {
            final Configuration conf = entry.getValue();
            if (conf instanceof ConfigurationImpl) {
                ((ConfigurationImpl) conf).configuredObjectReference = null;
            }
        }
        hotConfigs.remove(uri);
    }
}
Also used : FullXmldbURI(org.exist.xmldb.FullXmldbURI)

Example 4 with FullXmldbURI

use of org.exist.xmldb.FullXmldbURI in project exist by eXist-db.

the class Configurator method parse.

public static Configuration parse(final BrokerPool pool, final DocumentImpl document) {
    if (document == null) {
        return null;
    }
    Configuration conf;
    final FullXmldbURI key = getFullURI(pool, document.getURI());
    conf = hotConfigs.get(key);
    if (conf != null) {
        return conf;
    }
    final Element confElement = document.getDocumentElement();
    if (confElement == null) {
        // possibly on corrupted database, find better solution (recovery flag?)
        return null;
    }
    conf = new ConfigurationImpl(confElement);
    hotConfigs.put(key, conf);
    return conf;
}
Also used : Element(org.w3c.dom.Element) FullXmldbURI(org.exist.xmldb.FullXmldbURI)

Aggregations

FullXmldbURI (org.exist.xmldb.FullXmldbURI)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXSerializer (org.exist.util.serializer.SAXSerializer)2 Element (org.w3c.dom.Element)2 SAXException (org.xml.sax.SAXException)2 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)1 EXistException (org.exist.EXistException)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 SecurityManager (org.exist.security.SecurityManager)1 BrokerPool (org.exist.storage.BrokerPool)1 TransactionManager (org.exist.storage.txn.TransactionManager)1 Txn (org.exist.storage.txn.Txn)1 LockException (org.exist.util.LockException)1 StringInputSource (org.exist.util.StringInputSource)1