Search in sources :

Example 16 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class Sync method startSync.

private Sequence startSync(final String target, final String collectionPath, final Map<String, Sequence> options) throws XPathException {
    final Date startDate = options.get(AFTER_OPT).hasOne() ? ((DateTimeValue) options.get(AFTER_OPT)).getDate() : null;
    final boolean prune = ((BooleanValue) options.get(PRUNE_OPT)).getValue();
    final List<String> excludes = new ArrayList<>(Collections.emptyList());
    for (final SequenceIterator si = options.get(EXCLUDES_OPT).iterate(); si.hasNext(); ) {
        excludes.add(si.nextItem().getStringValue());
    }
    final Path p = FileModuleHelper.getFile(target);
    context.pushDocumentContext();
    final MemTreeBuilder output = context.getDocumentBuilder();
    final Path targetDir;
    try {
        if (p.isAbsolute()) {
            targetDir = p;
        } else {
            final Optional<Path> home = context.getBroker().getConfiguration().getExistHome();
            targetDir = FileUtils.resolve(home, target);
        }
        output.startDocument();
        output.startElement(FILE_SYNC_ELEMENT, null);
        output.addAttribute(FILE_COLLECTION_ATTRIBUTE, collectionPath);
        output.addAttribute(FILE_DIR_ATTRIBUTE, targetDir.toAbsolutePath().toString());
        final String rootTargetAbsPath = targetDir.toAbsolutePath().toString();
        final String separator = rootTargetAbsPath.endsWith(File.separator) ? "" : File.separator;
        syncCollection(XmldbURI.create(collectionPath), rootTargetAbsPath + separator, targetDir, startDate, prune, excludes, output);
        output.endElement();
        output.endDocument();
    } catch (final PermissionDeniedException | LockException e) {
        throw new XPathException(this, e);
    } finally {
        context.popDocumentContext();
    }
    return output.getDocument();
}
Also used : Path(java.nio.file.Path) XPathException(org.exist.xquery.XPathException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 17 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class GetThumbnailsFunction method eval.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
	 *      org.exist.xquery.value.Sequence)
	 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    ValueSequence result = new ValueSequence();
    // boolean isDatabasePath = false;
    boolean isSaveToDataBase = false;
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    AnyURIValue picturePath = (AnyURIValue) args[0].itemAt(0);
    if (picturePath.getStringValue().startsWith("xmldb:exist://")) {
        picturePath = new AnyURIValue(picturePath.getStringValue().substring(14));
    }
    AnyURIValue thumbPath = null;
    if (args[1].isEmpty()) {
        thumbPath = new AnyURIValue(picturePath.toXmldbURI().append(THUMBPATH));
        isSaveToDataBase = true;
    } else {
        thumbPath = (AnyURIValue) args[1].itemAt(0);
        if (thumbPath.getStringValue().startsWith("file:")) {
            isSaveToDataBase = false;
            thumbPath = new AnyURIValue(thumbPath.getStringValue().substring(5));
        } else {
            isSaveToDataBase = true;
            try {
                XmldbURI thumbsURI = XmldbURI.xmldbUriFor(thumbPath.getStringValue());
                if (!thumbsURI.isAbsolute())
                    thumbsURI = picturePath.toXmldbURI().append(thumbPath.toString());
                thumbPath = new AnyURIValue(thumbsURI.toString());
            } catch (URISyntaxException e) {
                throw new XPathException(this, e.getMessage());
            }
        }
    }
    // result.add(new StringValue(picturePath.getStringValue()));
    // result.add(new StringValue(thumbPath.getStringValue() + " isDB?= "
    // + isSaveToDataBase));
    int maxThumbHeight = MAXTHUMBHEIGHT;
    int maxThumbWidth = MAXTHUMBWIDTH;
    if (!args[2].isEmpty()) {
        maxThumbHeight = ((IntegerValue) args[2].itemAt(0)).getInt();
        if (args[2].hasMany())
            maxThumbWidth = ((IntegerValue) args[2].itemAt(1)).getInt();
    }
    String prefix = THUMBPREFIX;
    if (!args[3].isEmpty()) {
        prefix = args[3].itemAt(0).getStringValue();
    }
    // result.add(new StringValue("maxThumbHeight = " + maxThumbHeight
    // + ", maxThumbWidth = " + maxThumbWidth));
    BrokerPool pool = null;
    try {
        pool = BrokerPool.getInstance();
    } catch (Exception e) {
        result.add(new StringValue(e.getMessage()));
        return result;
    }
    final DBBroker dbbroker = context.getBroker();
    // Start transaction
    final TransactionManager transact = pool.getTransactionManager();
    try (final Txn transaction = transact.beginTransaction()) {
        Collection thumbCollection = null;
        Path thumbDir = null;
        if (isSaveToDataBase) {
            try {
                thumbCollection = dbbroker.getOrCreateCollection(transaction, thumbPath.toXmldbURI());
                dbbroker.saveCollection(transaction, thumbCollection);
            } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
            }
        } else {
            thumbDir = Paths.get(thumbPath.toString());
            if (!Files.isDirectory(thumbDir))
                try {
                    Files.createDirectories(thumbDir);
                } catch (IOException e) {
                    throw new XPathException(this, e.getMessage());
                }
        }
        Collection allPictures = null;
        Collection existingThumbsCol = null;
        List<Path> existingThumbsArray = null;
        try {
            allPictures = dbbroker.getCollection(picturePath.toXmldbURI());
            if (allPictures == null) {
                return Sequence.EMPTY_SEQUENCE;
            }
            if (isSaveToDataBase) {
                existingThumbsCol = dbbroker.getCollection(thumbPath.toXmldbURI());
            } else {
                existingThumbsArray = FileUtils.list(thumbDir, path -> {
                    final String fileName = FileUtils.fileName(path);
                    return fileName.endsWith(".jpeg") || fileName.endsWith(".jpg");
                });
            }
        } catch (PermissionDeniedException | IOException e) {
            throw new XPathException(this, e.getMessage(), e);
        }
        DocumentImpl docImage = null;
        BinaryDocument binImage = null;
        @SuppressWarnings("unused") BufferedImage bImage = null;
        @SuppressWarnings("unused") byte[] imgData = null;
        Image image = null;
        UnsynchronizedByteArrayOutputStream os = null;
        try {
            Iterator<DocumentImpl> i = allPictures.iterator(dbbroker);
            while (i.hasNext()) {
                docImage = i.next();
                // is not already existing??
                if (!((fileExist(context.getBroker(), existingThumbsCol, docImage, prefix)) || (fileExist(existingThumbsArray, docImage, prefix)))) {
                    if (docImage.getResourceType() == DocumentImpl.BINARY_FILE)
                        // TODO maybe extends for gifs too.
                        if (docImage.getMimeType().startsWith("image/jpeg")) {
                            binImage = (BinaryDocument) docImage;
                            try (final InputStream is = dbbroker.getBinaryResource(transaction, binImage)) {
                                image = ImageIO.read(is);
                            } catch (IOException ioe) {
                                throw new XPathException(this, ioe.getMessage());
                            }
                            try {
                                bImage = ImageModule.createThumb(image, maxThumbHeight, maxThumbWidth);
                            } catch (Exception e) {
                                throw new XPathException(this, e.getMessage());
                            }
                            if (isSaveToDataBase) {
                                os = new UnsynchronizedByteArrayOutputStream();
                                try {
                                    ImageIO.write(bImage, "jpg", os);
                                } catch (Exception e) {
                                    throw new XPathException(this, e.getMessage());
                                }
                                try (final StringInputSource sis = new StringInputSource(os.toByteArray())) {
                                    thumbCollection.storeDocument(transaction, dbbroker, XmldbURI.create(prefix + docImage.getFileURI()), sis, new MimeType("image/jpeg", MimeType.BINARY));
                                } catch (final Exception e) {
                                    throw new XPathException(this, e.getMessage());
                                }
                            // result.add(new
                            // StringValue(""+docImage.getFileURI()+"|"+thumbCollection.getURI()+THUMBPREFIX
                            // + docImage.getFileURI()));
                            } else {
                                try {
                                    ImageIO.write(bImage, "jpg", Paths.get(thumbPath.toString() + "/" + prefix + docImage.getFileURI()).toFile());
                                } catch (Exception e) {
                                    throw new XPathException(this, e.getMessage());
                                }
                            // result.add(new StringValue(
                            // thumbPath.toString() + "/"
                            // + THUMBPREFIX
                            // + docImage.getFileURI()));
                            }
                        }
                } else {
                    // result.add(new StringValue(""+docImage.getURI()+"|"
                    // + ((existingThumbsCol != null) ? ""
                    // + existingThumbsCol.getURI() : thumbDir
                    // .toString()) + "/" + prefix
                    // + docImage.getFileURI()));
                    result.add(new StringValue(docImage.getFileURI().toString()));
                }
            }
        } catch (final PermissionDeniedException | LockException e) {
            throw new XPathException(this, e.getMessage(), e);
        }
        try {
            transact.commit(transaction);
        } catch (Exception e) {
            throw new XPathException(this, e.getMessage());
        }
    }
    final Optional<JournalManager> journalManager = pool.getJournalManager();
    journalManager.ifPresent(j -> j.flush(true, false));
    dbbroker.closeDocument();
    return result;
}
Also used : Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) QName(org.exist.dom.QName) URISyntaxException(java.net.URISyntaxException) ValueSequence(org.exist.xquery.value.ValueSequence) StringInputSource(org.exist.util.StringInputSource) FunctionSignature(org.exist.xquery.FunctionSignature) PermissionDeniedException(org.exist.security.PermissionDeniedException) Cardinality(org.exist.xquery.Cardinality) MimeType(org.exist.util.MimeType) FileUtils(org.exist.util.FileUtils) BasicFunction(org.exist.xquery.BasicFunction) StringValue(org.exist.xquery.value.StringValue) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) LockException(org.exist.util.LockException) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) ImageIO(javax.imageio.ImageIO) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Path(java.nio.file.Path) XQueryContext(org.exist.xquery.XQueryContext) Iterator(java.util.Iterator) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) JournalManager(org.exist.storage.journal.JournalManager) AnyURIValue(org.exist.xquery.value.AnyURIValue) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) TransactionManager(org.exist.storage.txn.TransactionManager) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Paths(java.nio.file.Paths) DBBroker(org.exist.storage.DBBroker) IntegerValue(org.exist.xquery.value.IntegerValue) Optional(java.util.Optional) Sequence(org.exist.xquery.value.Sequence) LogManager(org.apache.logging.log4j.LogManager) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) XPathException(org.exist.xquery.XPathException) URISyntaxException(java.net.URISyntaxException) Txn(org.exist.storage.txn.Txn) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BufferedImage(java.awt.image.BufferedImage) MimeType(org.exist.util.MimeType) StringInputSource(org.exist.util.StringInputSource) LockException(org.exist.util.LockException) ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue) XmldbURI(org.exist.xmldb.XmldbURI) Path(java.nio.file.Path) InputStream(java.io.InputStream) AnyURIValue(org.exist.xquery.value.AnyURIValue) IntegerValue(org.exist.xquery.value.IntegerValue) JournalManager(org.exist.storage.journal.JournalManager) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) IOException(java.io.IOException) XPathException(org.exist.xquery.XPathException) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool)

Example 18 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class AbstractRealm method initialiseRealmStorage.

private void initialiseRealmStorage(final DBBroker broker, final Txn transaction) throws EXistException {
    final XmldbURI realmCollectionURL = SecurityManager.SECURITY_COLLECTION_URI.append(getId());
    try {
        collectionRealm = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL);
        collectionAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts"));
        collectionGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups"));
        collectionRemovedAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts").append("removed"));
        collectionRemovedGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups").append("removed"));
    } catch (final PermissionDeniedException | IOException | TriggerException | LockException e) {
        throw new EXistException(e);
    }
}
Also used : LockException(org.exist.util.LockException) IOException(java.io.IOException) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 19 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class NativeStructuralIndexWorker method findElementsByTagName.

public NodeSet findElementsByTagName(byte type, DocumentSet docs, QName qname, NodeSelector selector, Expression parent) {
    final NewArrayNodeSet result = new NewArrayNodeSet();
    final FindElementsCallback callback = new FindElementsCallback(type, qname, result, docs, selector, parent);
    // for each document id range, scan the index to find matches
    for (final Range range : getDocIdRanges(docs)) {
        final byte[] fromKey = computeKey(type, qname, range.start);
        final byte[] toKey = computeKey(type, qname, range.end + 1);
        final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
        try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
            index.btree.query(query, callback);
        } catch (final LockException e) {
            NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
        } catch (final TerminatedException e) {
            NativeStructuralIndex.LOG.warn("Query was terminated while searching structural index: {}", e.getMessage(), e);
        } catch (final Exception e) {
            NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
        }
    }
    return result;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) IndexQuery(org.exist.storage.btree.IndexQuery) LockException(org.exist.util.LockException) Value(org.exist.storage.btree.Value) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Example 20 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class NativeStructuralIndexWorker method removeDocument.

protected void removeDocument(DocumentImpl docToRemove) {
    if (index.btree == null) {
        return;
    }
    final List<QName> qnames = getQNamesForDoc(docToRemove);
    for (final QName qname : qnames) {
        final byte[] fromKey = computeKey(qname.getNameType(), qname, docToRemove.getDocId());
        final byte[] toKey = computeKey(qname.getNameType(), qname, docToRemove.getDocId() + 1);
        final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
        try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
            index.btree.remove(query, null);
        } catch (final LockException e) {
            NativeStructuralIndex.LOG.warn("Failed to lock structural index: {}", e.getMessage(), e);
        } catch (final Exception e) {
            NativeStructuralIndex.LOG.warn("Exception caught while removing structural index for document {}: {}", docToRemove.getURI(), e.getMessage(), e);
        }
    }
    removeQNamesForDoc(docToRemove);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) LockException(org.exist.util.LockException) QName(org.exist.dom.QName) Value(org.exist.storage.btree.Value) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Aggregations

LockException (org.exist.util.LockException)72 PermissionDeniedException (org.exist.security.PermissionDeniedException)44 EXistException (org.exist.EXistException)29 DocumentImpl (org.exist.dom.persistent.DocumentImpl)26 XmldbURI (org.exist.xmldb.XmldbURI)26 ReentrantLock (java.util.concurrent.locks.ReentrantLock)19 Collection (org.exist.collections.Collection)16 IOException (java.io.IOException)12 Tuple3 (com.evolvedbinary.j8fu.tuple.Tuple3)11 LockedDocument (org.exist.dom.persistent.LockedDocument)11 Value (org.exist.storage.btree.Value)11 Txn (org.exist.storage.txn.Txn)11 TriggerException (org.exist.collections.triggers.TriggerException)10 NodeProxy (org.exist.dom.persistent.NodeProxy)10 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)10 QName (org.exist.dom.QName)9 LockMode (org.exist.storage.lock.Lock.LockMode)9 XPathException (org.exist.xquery.XPathException)9 SAXException (org.xml.sax.SAXException)9 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)8