Search in sources :

Example 91 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class CopyResourceRecoveryTest method storeAborted.

private void storeAborted(final String testCollectionName, final String subCollection) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        Collection testCollection;
        DocumentImpl doc;
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection root = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test"));
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            testCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName));
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            final Collection subTestCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName).append(subCollection));
            assertNotNull(subTestCollection);
            broker.saveCollection(transaction, subTestCollection);
            final String sample;
            try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
                assertNotNull(is);
                sample = InputStreamUtil.readString(is, UTF_8);
            }
            broker.storeDocument(transaction, XmldbURI.create("test2.xml"), new StringInputSource(sample), MimeType.XML_TYPE, subTestCollection);
            doc = subTestCollection.getDocument(broker, XmldbURI.create("test2.xml"));
            transact.commit(transaction);
        }
        final Txn transaction = transact.beginTransaction();
        broker.copyResource(transaction, doc, testCollection, XmldbURI.create("new_test2.xml"));
        broker.saveCollection(transaction, testCollection);
        // DO NOT COMMIT TRANSACTION
        pool.getJournalManager().get().flush(true, false);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 92 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class AbstractUpdateTest method update.

@Test
public final void update() throws EXistException, DatabaseConfigurationException, LockException, SAXException, PermissionDeniedException, IOException, ParserConfigurationException, XPathException {
    BrokerPool.FORCE_CORRUPTION = true;
    BrokerPool pool = startDb();
    try {
        final TransactionManager transact = pool.getTransactionManager();
        try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
            final DocumentImpl doc = init(broker, transact);
            final MutableDocumentSet docs = new DefaultDocumentSet();
            docs.add(doc);
            doUpdate(broker, transact, docs);
            pool.getJournalManager().get().flush(true, false);
        }
        BrokerPool.FORCE_CORRUPTION = false;
        existEmbeddedServer.restart(false);
        pool = existEmbeddedServer.getBrokerPool();
        read(pool);
    } finally {
        existEmbeddedServer.stopDb(true);
    }
}
Also used : MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) TransactionManager(org.exist.storage.txn.TransactionManager) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Test(org.junit.Test)

Example 93 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class CollectionTest method read.

public void read(final BrokerPool pool) throws EXistException, IOException, PermissionDeniedException, BTreeException, DatabaseConfigurationException, LockException {
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        BTree btree = ((NativeBroker) broker).getStorage(NativeBroker.COLLECTIONS_DBX_ID);
        Writer writer = new StringWriter();
        btree.dump(writer);
        Collection test = broker.getCollection(TEST_COLLECTION_URI.append("test2"));
        assertNotNull(test);
        for (Iterator<DocumentImpl> i = test.iterator(broker); i.hasNext(); ) {
            DocumentImpl next = i.next();
        }
    }
}
Also used : StringWriter(java.io.StringWriter) BTree(org.exist.storage.btree.BTree) Collection(org.exist.collections.Collection) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 94 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class Scan method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Source source = null;
    String name;
    if (getArgumentCount() == 2) {
        byte[] data;
        try {
            data = binaryValueToByteArray((BinaryValue) args[0].itemAt(0));
        } catch (IOException ioe) {
            throw new XPathException(ioe.getMessage(), ioe);
        }
        name = args[1].getStringValue();
        source = new BinarySource(data, true);
    } else {
        String uri = args[0].getStringValue();
        if (uri.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
            try {
                XmldbURI resourceURI = XmldbURI.xmldbUriFor(uri);
                try (final Collection collection = context.getBroker().openCollection(resourceURI.removeLastSegment(), LockMode.READ_LOCK)) {
                    if (collection == null) {
                        LOG.warn("collection not found: {}", resourceURI.getCollectionPath());
                        return Sequence.EMPTY_SEQUENCE;
                    }
                    try (final LockedDocument lockedDoc = collection.getDocumentWithLock(context.getBroker(), resourceURI.lastSegment(), LockMode.READ_LOCK)) {
                        // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                        collection.close();
                        final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
                        if (doc == null) {
                            return Sequence.EMPTY_SEQUENCE;
                        }
                        if (doc.getResourceType() != DocumentImpl.BINARY_FILE || !doc.getMimeType().equals("application/xquery")) {
                            throw new XPathException(this, "XQuery resource: " + uri + " is not an XQuery or " + "declares a wrong mime-type");
                        }
                        source = new DBSource(context.getBroker(), (BinaryDocument) doc, false);
                        name = doc.getFileURI().toString();
                    }
                } catch (LockException e) {
                    throw new XPathException(this, "internal lock error: " + e.getMessage());
                } catch (PermissionDeniedException pde) {
                    throw new XPathException(this, pde.getMessage(), pde);
                }
            } catch (URISyntaxException e) {
                throw new XPathException(this, "invalid module uri: " + uri + ": " + e.getMessage(), e);
            }
        } else {
            // first check if the URI points to a registered module
            String location = context.getModuleLocation(uri);
            if (location != null)
                uri = location;
            try {
                source = SourceFactory.getSource(context.getBroker(), context.getModuleLoadPath(), uri, false);
                if (source == null) {
                    throw new XPathException(this, "failed to read module " + uri);
                }
                name = extractName(uri);
            } catch (IOException e) {
                throw new XPathException(this, "failed to read module " + uri, e);
            } catch (PermissionDeniedException e) {
                throw new XPathException(this, "permission denied to read module " + uri, e);
            }
        }
    }
    try {
        XQDocHelper helper = new XQDocHelper();
        String xml = helper.scan(source, name);
        NodeValue root = ModuleUtils.stringToXML(context, xml);
        if (root == null)
            return Sequence.EMPTY_SEQUENCE;
        return normalize((NodeValue) ((Document) root).getDocumentElement());
    } catch (XQDocException | SAXException e) {
        throw new XPathException(this, "error while scanning module: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new XPathException(this, "IO error while scanning module: " + e.getMessage(), e);
    }
}
Also used : XQDocException(org.xqdoc.conversion.XQDocException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) LockedDocument(org.exist.dom.persistent.LockedDocument) Document(org.w3c.dom.Document) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) BinaryDocument(org.exist.dom.persistent.BinaryDocument) LockException(org.exist.util.LockException) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) XQDocHelper(org.exist.xqdoc.XQDocHelper)

Example 95 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class RootNode method registerUpdateListener.

protected void registerUpdateListener() {
    if (listener == null) {
        listener = new UpdateListener() {

            @Override
            public void documentUpdated(DocumentImpl document, int event) {
                // clear all
                cachedDocs = null;
                cached = null;
            }

            @Override
            public void unsubscribe() {
                RootNode.this.listener = null;
            }

            @Override
            public void nodeMoved(NodeId oldNodeId, NodeHandle newNode) {
            // not relevant
            }

            @Override
            public void debug() {
                LOG.debug("UpdateListener: Line: {}", RootNode.this.toString());
            }
        };
        context.registerUpdateListener(listener);
    }
}
Also used : NodeHandle(org.exist.dom.persistent.NodeHandle) NodeId(org.exist.numbering.NodeId) UpdateListener(org.exist.storage.UpdateListener) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

DocumentImpl (org.exist.dom.persistent.DocumentImpl)128 PermissionDeniedException (org.exist.security.PermissionDeniedException)51 Collection (org.exist.collections.Collection)40 LockedDocument (org.exist.dom.persistent.LockedDocument)39 Txn (org.exist.storage.txn.Txn)35 XmldbURI (org.exist.xmldb.XmldbURI)34 EXistException (org.exist.EXistException)27 LockException (org.exist.util.LockException)26 DBBroker (org.exist.storage.DBBroker)25 IOException (java.io.IOException)19 NodeProxy (org.exist.dom.persistent.NodeProxy)18 URISyntaxException (java.net.URISyntaxException)17 BrokerPool (org.exist.storage.BrokerPool)17 TransactionManager (org.exist.storage.txn.TransactionManager)17 Test (org.junit.Test)17 XPathException (org.exist.xquery.XPathException)16 NodeId (org.exist.numbering.NodeId)14 SAXException (org.xml.sax.SAXException)13 StoredNode (org.exist.dom.persistent.StoredNode)12 BinaryDocument (org.exist.dom.persistent.BinaryDocument)11