Package org.exist.storage.txn

Examples of org.exist.storage.txn.TransactionManager


      }
    }
    public void store(java.lang.String sessionId, byte[] data, java.lang.String encoding, XmldbURI path, boolean replace) throws java.rmi.RemoteException {
        final Session session = getSession(sessionId);
        DBBroker broker = null;
        final TransactionManager transact = pool.getTransactionManager();
        final Txn txn = transact.beginTransaction();
        try {
            broker = pool.get(session.getUser());
            final XmldbURI collectionUri = path.removeLastSegment();
            final XmldbURI docUri = path.lastSegment();
            if (collectionUri==null || docUri==null) {
                transact.abort(txn);
                throw new EXistException("Illegal document path");
            }
            final Collection collection = broker.getCollection(collectionUri);
            if (collection == null) {
                transact.abort(txn);
                throw new EXistException("Collection " + collectionUri + " not found");
            }
            if(!replace) {
                final DocumentImpl old = collection.getDocument(broker, docUri);
                if(old != null) {
                    transact.abort(txn);
                    throw new RemoteException("Document exists and overwrite is not allowed");
                }
            }
            final long startTime = System.currentTimeMillis();
// TODO check XML/Binary resource
//          IndexInfo info = collection.validate(txn, broker, path, new InputSource(new ByteArrayInputStream(data)));
            final IndexInfo info = collection.validateXMLResource(txn, broker, docUri, new InputSource(new ByteArrayInputStream(data)));
            info.getDocument().getMetadata().setMimeType(MimeType.XML_TYPE.getName());
            collection.store(txn, broker, info, new InputSource(new ByteArrayInputStream(data)), false);
            transact.commit(txn);
            LOG.debug(
                    "parsing "
                    + path
                    + " took "
                    + (System.currentTimeMillis() - startTime)
                    + "ms.");
        } catch (final Exception e) {
            transact.abort(txn);
            LOG.debug(e.getMessage(), e);
            throw new RemoteException(e.getMessage(), e);
        } finally {
            transact.close(txn);
            pool.release(broker);
        }
    }
View Full Code Here


      }
    }
    public int xupdate(java.lang.String sessionId, XmldbURI collectionName, java.lang.String xupdate) throws java.rmi.RemoteException {
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        final TransactionManager transact = pool.getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        try {
            broker = pool.get(session.getUser());
            final Collection collection = broker.getCollection(collectionName);
            if (collection == null) {
                transact.abort(transaction);
                throw new RemoteException(
                        "collection " + collectionName + " not found");
            }
            final DocumentSet docs =
                    collection.allDocs(broker, new DefaultDocumentSet(), true);
            final XUpdateProcessor processor =
                    new XUpdateProcessor(broker, docs, AccessContext.SOAP);
            final Modification modifications[] =
                    processor.parse(new InputSource(new StringReader(xupdate)));
            long mods = 0;
            for (int i = 0; i < modifications.length; i++) {
                mods += modifications[i].process(transaction);
                broker.flush();
            }
            transact.commit(transaction);
            return (int) mods;
        } catch (final ParserConfigurationException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final IOException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final EXistException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final SAXException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final PermissionDeniedException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final XPathException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final LockException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } finally {
            transact.close(transaction);
            pool.release(broker);
        }
    }
View Full Code Here

      }
    }
    public int xupdateResource(java.lang.String sessionId, XmldbURI documentName, java.lang.String xupdate) throws java.rmi.RemoteException {
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        final TransactionManager transact = pool.getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        try {
            broker = pool.get(session.getUser());
// TODO check XML/Binary resource
//            DocumentImpl doc = (DocumentImpl)broker.getDocument(documentName);
            final DocumentImpl doc = broker.getXMLResource(documentName, Permission.READ);
            if (doc == null) {
                transact.abort(transaction);
                throw new RemoteException(
                        "document " + documentName + " not found");
            }
            final MutableDocumentSet docs = new DefaultDocumentSet();
            docs.add(doc);
            final XUpdateProcessor processor =
                    new XUpdateProcessor(broker, docs, AccessContext.SOAP);
            final Modification modifications[] =
                    processor.parse(new InputSource(new StringReader(xupdate)));
            long mods = 0;
            for (int i = 0; i < modifications.length; i++) {
                mods += modifications[i].process(transaction);
                broker.flush();
            }
            transact.commit(transaction);
           
            // Release lock, as reported http://markmail.org/message/pau6hoaeybg2bvch
            doc.getUpdateLock().release(Permission.READ);
           
            return (int) mods;
        } catch (final ParserConfigurationException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final IOException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final EXistException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final SAXException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final PermissionDeniedException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final XPathException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } catch (final LockException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage(), e);
        } finally {
            transact.close(transaction);
            pool.release(broker);
        }
       
    }
View Full Code Here

    }
    public void storeBinary(java.lang.String sessionId, byte[] data, XmldbURI path, java.lang.String mimeType, boolean replace) throws java.rmi.RemoteException {
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        Collection collection = null;
        final TransactionManager transact = pool.getTransactionManager();
        final Txn txn = transact.beginTransaction();
        try {
            broker = pool.get(session.getUser());
            final XmldbURI collectionUri = path.removeLastSegment();
            final XmldbURI docUri = path.lastSegment();
            if (collectionUri==null || docUri==null) {
                transact.abort(txn);
                throw new EXistException("Illegal document path");
            }
            collection = broker.openCollection(collectionUri, Lock.WRITE_LOCK);
            if (collection == null)
                {throw new EXistException("Collection " + collectionUri
                        + " not found");}
            if (!replace) {
                final DocumentImpl old = collection.getDocument(broker, docUri);
                if (old != null)
                    {throw new PermissionDeniedException(
                            "Old document exists and overwrite is not allowed");}
            }
            LOG.debug("Storing binary resource to collection " + collection.getURI());
           
            /*DocumentImpl doc = */
            collection.addBinaryResource(txn, broker, docUri, data, mimeType);
//            if (created != null)
//                doc.setCreated(created.getTime());
//            if (modified != null)
//                doc.setLastModified(modified.getTime());
            transact.commit(txn);
        } catch (final Exception e) {
            transact.abort(txn);
            throw new RemoteException(e.getMessage(), e);
        } finally {
            transact.close(txn);
            if(collection != null)
                {collection.release(Lock.WRITE_LOCK);}
            pool.release(broker);
        }
//        documentCache.clear();
View Full Code Here

    public void setPermissions(java.lang.String sessionId, XmldbURI resource, java.lang.String owner, java.lang.String ownerGroup, int permissions) throws java.rmi.RemoteException {
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        Collection collection = null;
        DocumentImpl doc = null;
        final TransactionManager transact = pool.getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        try {
            broker = pool.get(session.getUser());
            final org.exist.security.SecurityManager manager = pool.getSecurityManager();
            collection = broker.openCollection(resource, Lock.WRITE_LOCK);
            if (collection == null) {
                // TODO check XML/Binary resource
                doc = broker.getXMLResource(resource, Lock.WRITE_LOCK);
                if (doc == null)
                    {throw new RemoteException("document or collection "
                            + resource + " not found");}
                LOG.debug("changing permissions on document " + resource);
                final Permission perm = doc.getPermissions();
                if (perm.getOwner().equals(session.getUser())
                || manager.hasAdminPrivileges(session.getUser())) {
                    if (owner != null) {
                        perm.setOwner(owner);
                        perm.setGroup(ownerGroup);
                    }
                    perm.setMode(permissions);
// TODO check XML/Binary resource
//                    broker.storeDocument(transaction, doc);
                    broker.storeXMLResource(transaction, doc);
                    transact.commit(transaction);
                    broker.flush();
                    return;
//                    return true;
                }
                transact.abort(transaction);
                throw new PermissionDeniedException("not allowed to change permissions");
            }
            LOG.debug("changing permissions on collection " + resource);
            final Permission perm = collection.getPermissionsNoLock();
            if (perm.getOwner().equals(session.getUser())
            || manager.hasAdminPrivileges(session.getUser())) {
                perm.setMode(permissions);
                if (owner != null) {
                    perm.setOwner(owner);
                    perm.setGroup(ownerGroup);
                }
                transaction.registerLock(collection.getLock(), Lock.WRITE_LOCK);
                broker.saveCollection(transaction, collection);
                transact.commit(transaction);
                broker.flush();
                return;
            }
            transact.abort(transaction);
            throw new PermissionDeniedException("not allowed to change permissions");
        } catch (final IOException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage());
        } catch (final PermissionDeniedException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage());
        } catch (final TransactionException e) {
            throw new RemoteException(e.getMessage());
        } catch (final EXistException e) {
            throw new RemoteException(e.getMessage());
        } catch (final TriggerException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage());
    } finally {
            transact.close(transaction);
            if(doc != null)
                {doc.getUpdateLock().release(Lock.WRITE_LOCK);}
            pool.release(broker);
        }
    }
View Full Code Here

      }
    }
    private void moveOrCopyResource(String sessionId, XmldbURI docPath, XmldbURI destinationPath,
            XmldbURI newName, boolean move)
            throws RemoteException {
        final TransactionManager transact = pool.getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        Collection collection = null;
        Collection destination = null;
        DocumentImpl doc = null;
        try {
            broker = pool.get(session.getUser());
            final XmldbURI collectionUri = docPath.removeLastSegment();
            final XmldbURI docUri = docPath.lastSegment();
            if (collectionUri==null || docUri==null) {
                transact.abort(transaction);
                throw new EXistException("Illegal document path");
            }
            collection = broker.openCollection(collectionUri, move ? Lock.WRITE_LOCK : Lock.READ_LOCK);
            if (collection == null) {
                transact.abort(transaction);
                throw new RemoteException("Collection " + collectionUri
                        + " not found");
            }
            doc = collection.getDocumentWithLock(broker, docUri, Lock.WRITE_LOCK);
            if(doc == null) {
                transact.abort(transaction);
                throw new RemoteException("Document " + docUri + " not found");
            }
           
            // get destination collection
            destination = broker.openCollection(destinationPath, Lock.WRITE_LOCK);
            if(destination == null) {
                transact.abort(transaction);
                throw new RemoteException("Destination collection " + destinationPath + " not found");
            }
            if(move)
// TODO check XML/Binary resource
//                broker.moveResource(transaction, doc, destination, newName);
                {broker.moveResource(transaction, doc, destination, newName);}
            else
// TODO check XML/Binary resource
//                broker.copyResource(transaction, doc, destination, newName);
                {broker.copyResource(transaction, doc, destination, newName);}
            transact.commit(transaction);
//            documentCache.clear();
            return;
        } catch (final LockException e) {
            transact.abort(transaction);
            throw new RemoteException("Could not acquire lock on document " + docPath);
        } catch (final PermissionDeniedException e) {
            transact.abort(transaction);
            throw new RemoteException("Could not move/copy document " + docPath);
        } catch (final IOException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage());
        } catch (final TransactionException e) {
            throw new RemoteException("Error commiting transaction " + e.getMessage());
        } catch (final EXistException e) {
            throw new RemoteException(e.getMessage());
        } catch (final TriggerException e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage());
    } finally {
            transact.close(transaction);
            if(destination != null)
                {destination.release(Lock.WRITE_LOCK);}
            if(doc != null)
                {doc.getUpdateLock().release(Lock.WRITE_LOCK);}
            if(collection != null)
View Full Code Here

      }
    }
    private boolean moveOrCopyCollection(String sessionId, XmldbURI collectionPath, XmldbURI destinationPath,
        XmldbURI newName, boolean move)
            throws EXistException, PermissionDeniedException, RemoteException {
        final TransactionManager transact = pool.getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        Collection collection = null;
        Collection destination = null;
        try {
            final Subject user = session.getUser();
            broker = pool.get(user);
            // get source document
            collection = broker.openCollection(collectionPath, move ? Lock.WRITE_LOCK : Lock.READ_LOCK);
            if (collection == null) {
                transact.abort(transaction);
                throw new EXistException("Collection " + collectionPath
                        + " not found");
            }
            // get destination collection
            destination = broker.openCollection(destinationPath, Lock.WRITE_LOCK);
            if(destination == null) {
                transact.abort(transaction);
                throw new EXistException("Destination collection " + destinationPath + " not found");
            }
            if(move)
                {broker.moveCollection(transaction, collection, destination, newName);}
            else
                {broker.copyCollection(transaction, collection, destination, newName);}
            transact.commit(transaction);
//            documentCache.clear();
            return true;
        } catch (final IOException e) {
          transact.abort(transaction);
            throw new RemoteException(e.getMessage());           
        } catch (final LockException e) {
          transact.abort(transaction);
            throw new PermissionDeniedException(e.getMessage());
        } catch (final TriggerException e) {
          transact.abort(transaction);
            throw new RemoteException(e.getMessage());           
    } finally {
            transact.close(transaction);
            if(collection != null)
                {collection.release(move ? Lock.WRITE_LOCK : Lock.READ_LOCK);}
            if(destination != null)
                {destination.release(Lock.WRITE_LOCK);}
            pool.release(broker);
View Full Code Here

    public void lockResource(java.lang.String sessionId, XmldbURI path, java.lang.String userName) throws java.rmi.RemoteException {
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        final Subject user = session.getUser();
        DocumentImpl doc = null;
        final TransactionManager transact = pool.getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        try {
            broker = pool.get(user);
// TODO check XML/Binary resource
//            doc = (DocumentImpl) broker.openDocument(path, Lock.WRITE_LOCK);
            doc = broker.getXMLResource(path, Lock.WRITE_LOCK);
            if (doc == null) {
                throw new EXistException("Resource "
                        + path + " not found");
            }
            if (!doc.getPermissions().validate(user, Permission.WRITE))
                {throw new PermissionDeniedException("User is not allowed to lock resource " + path);}
           
            final org.exist.security.SecurityManager manager = pool.getSecurityManager();
            if (!(userName.equals(user.getName()) || manager.hasAdminPrivileges(user)))
                {throw new PermissionDeniedException("User " + user.getName() + " is not allowed " +
                        "to lock the resource for user " + userName);}
            final Account lockOwner = doc.getUserLock();
            if(lockOwner != null && (!lockOwner.equals(user)) && (!manager.hasAdminPrivileges(user)))
                {throw new PermissionDeniedException("Resource is already locked by user " +
                        lockOwner.getName());}
            final Account lo = manager.getAccount(userName);
            doc.setUserLock(lo);
// TODO check XML/Binary resource
//            broker.storeDocument(transaction, doc);
            broker.storeXMLResource(transaction, doc);
            transact.commit(transaction);
            return;
        } catch (final Exception e) {
            transact.abort(transaction);
            throw new RemoteException(e.getMessage());
        } finally {
            transact.close(transaction);
            if(doc != null)
                {doc.getUpdateLock().release(Lock.WRITE_LOCK);}
            pool.release(broker);
        }
    }
View Full Code Here

    public void unlockResource(java.lang.String sessionId, XmldbURI path) throws java.rmi.RemoteException {
        DBBroker broker = null;
        final Session session = getSession(sessionId);
        final Subject user = session.getUser();
        DocumentImpl doc = null;
        final TransactionManager transact = pool.getTransactionManager();
        Txn transaction = null;
        try {
            broker = pool.get(user);
            // TODO check XML/Binary resource
            doc = broker.getXMLResource(path, Lock.WRITE_LOCK);
            if (doc == null)
                {throw new EXistException("Resource "
                        + path + " not found");}
            if (!doc.getPermissions().validate(user, Permission.WRITE))
                {throw new PermissionDeniedException("User is not allowed to lock resource " + path);}
            final org.exist.security.SecurityManager manager = pool.getSecurityManager();
            final Account lockOwner = doc.getUserLock();
            if(lockOwner != null && (!lockOwner.equals(user)) && (!manager.hasAdminPrivileges(user)))
                {throw new PermissionDeniedException("Resource is already locked by user " +
                        lockOwner.getName());}
            transaction = transact.beginTransaction();
            doc.setUserLock(null);
// TODO check XML/Binary resource
//            broker.storeDocument(transaction, doc);
            broker.storeXMLResource(transaction, doc);
            transact.commit(transaction);
            return;
        } catch (final Exception ex){
            transact.abort(transaction);
            throw new RemoteException(ex.getMessage());
        } finally {
            transact.close(transaction);
            if(doc != null)
                {doc.getUpdateLock().release(Lock.WRITE_LOCK);}
            pool.release(broker);
        }
    }
View Full Code Here

        }
        reindexCollection(collection, NodeProcessor.MODE_STORE);
    }

    public void reindexCollection(final Collection collection, final int mode) throws PermissionDeniedException {
        final TransactionManager transact = pool.getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        long start = System.currentTimeMillis();

        try {
            LOG.info(String.format("Start indexing collection %s", collection.getURI().toString()));
            pool.getProcessMonitor().startJob(ProcessMonitor.ACTION_REINDEX_COLLECTION, collection.getURI());
            reindexCollection(transaction, collection, mode);
            transact.commit(transaction);

        } catch(final Exception e) {
            transact.abort(transaction);
            LOG.warn("An error occurred during reindex: " + e.getMessage(), e);

        } finally {
            transact.close(transaction);
            pool.getProcessMonitor().endJob();
            LOG.info(String.format("Finished indexing collection %s in %s ms.",
                collection.getURI().toString(), System.currentTimeMillis() - start));
        }
    }
View Full Code Here

TOP

Related Classes of org.exist.storage.txn.TransactionManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.