Package org.exist.storage.txn

Examples of org.exist.storage.txn.TransactionManager


     *
     * @param directory
     * @param target
     */
    private void scanDirectory(File directory, XmldbURI target, boolean inRootDir) {
        final TransactionManager mgr = broker.getBrokerPool().getTransactionManager();
        final Txn txn = mgr.beginTransaction();
        Collection collection = null;
        try {
            collection = broker.getOrCreateCollection(txn, target);
            setPermissions(true, null, collection.getPermissionsNoLock());
            broker.saveCollection(txn, collection);
            mgr.commit(txn);
        } catch (final Exception e) {
            mgr.abort(txn);
        } finally {
            mgr.close(txn);
        }

        try {
            // lock the collection while we store the files
            // TODO: could be released after each operation
View Full Code Here


     * @param targetCollection
     */
    private void storeFiles(File directory, Collection targetCollection, boolean inRootDir) {
        final File[] files = directory.listFiles();
        final MimeTable mimeTab = MimeTable.getInstance();
        final TransactionManager mgr = broker.getBrokerPool().getTransactionManager();
        for (final File file : files) {
            if (inRootDir && "repo.xml".equals(file.getName()))
                {continue;}
            if (!file.isDirectory()) {
                MimeType mime = mimeTab.getContentTypeFor(file.getName());
                if (mime == null)
                    {mime = MimeType.BINARY_TYPE;}
                final XmldbURI name = XmldbURI.create(file.getName());

                final Txn txn = mgr.beginTransaction();
                try {
                    if (mime.isXMLType()) {
                        final InputSource is = new InputSource(file.toURI().toASCIIString());
                        final IndexInfo info = targetCollection.validateXMLResource(txn, broker, name, is);
                        info.getDocument().getMetadata().setMimeType(mime.getName());
                        final Permission permission = info.getDocument().getPermissions();
                        setPermissions(false, mime, permission);

                        targetCollection.store(txn, broker, info, is, false);
                    } else {
                        final long size = file.length();
                        final FileInputStream is = new FileInputStream(file);
                        final BinaryDocument doc =
                                targetCollection.addBinaryResource(txn, broker, name, is, mime.getName(), size);
                        is.close();

                        final Permission permission = doc.getPermissions();
                        setPermissions(false, mime, permission);
                        doc.getMetadata().setMimeType(mime.getName());
                        broker.storeXMLResource(txn, doc);
                    }
                    mgr.commit(txn);
                } catch (final Exception e) {
                    mgr.abort(txn);
                    e.printStackTrace();
                } finally {
                    mgr.close(txn);
                }
            }
        }
    }
View Full Code Here

                    listener.warn("Could not parse document name into a URI: " + e.getMessage());
                    return new SkippedEntryDeferredPermission();
                }
            }

          final TransactionManager txnManager = broker.getDatabase().getTransactionManager();
          final Txn txn = txnManager.beginTransaction();
          try {
            currentCollection = broker.getOrCreateCollection(txn, collUri);
           
            rh.startCollectionRestore(currentCollection, atts);
           
                broker.saveCollection(txn, currentCollection);

            txnManager.commit(txn);
          } catch (final Exception e) {
            txnManager.abort(txn);
            throw new SAXException(e);
        } finally {
                txnManager.close(txn);
            }

            currentCollection = mkcol(collUri, getDateFromXSDateTimeStringForItem(created, name));

            listener.setCurrentCollection(name);
View Full Code Here

            listener.setCurrentResource(name);
            if(currentCollection instanceof Observable) {
                listener.observe((Observable)currentCollection);
            }

      final TransactionManager txnManager = broker.getDatabase().getTransactionManager();
      final Txn txn = txnManager.beginTransaction();
 
      DocumentImpl resource = null;
      try {
        if ("XMLResource".equals(type)) {
          // store as xml resource
         
          final IndexInfo info = currentCollection.validateXMLResource(txn, broker, docUri, is);
         
          resource = info.getDocument();
          final DocumentMetadata meta = resource.getMetadata();
          meta.setMimeType(mimetype);
          meta.setCreated(date_created.getTime());
          meta.setLastModified(date_modified.getTime());
         
                  if((publicid != null) || (systemid != null)) {
                    final DocumentType docType = new DocumentTypeImpl(namedoctype, publicid, systemid);
                    meta.setDocType(docType);
                  }

          rh.startDocumentRestore(resource, atts);

          currentCollection.store(txn, broker, info, is, false);
 
        } else {
          // store as binary resource
          resource = currentCollection.validateBinaryResource(txn, broker, docUri, is.getByteStream(), mimetype, is.getByteStreamLength() , date_created, date_modified);
         
          rh.startDocumentRestore(resource, atts);

          resource = currentCollection.addBinaryResource(txn, broker, (BinaryDocument)resource, is.getByteStream(), mimetype, is.getByteStreamLength() , date_created, date_modified);
        }

        txnManager.commit(txn);

                final DeferredPermission deferredPermission;
                if(name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
                    //prevents restore of a backup from changing system collection resource ownership
                    deferredPermission = new ResourceDeferredPermission(listener, resource, SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(perms, 8));
                } else {
                    deferredPermission = new ResourceDeferredPermission(listener, resource, owner, group, Integer.parseInt(perms, 8));
                }
               
                rh.endDocumentRestore(resource);

                listener.restored(name);
               
                return deferredPermission;
      } catch (final Exception e) {
        txnManager.abort(txn);
        throw new IOException(e);
      } finally {
                txnManager.close(txn);
//        if (resource != null)
//          resource.getUpdateLock().release(Lock.READ_LOCK);
      }

        } catch(final Exception e) {
View Full Code Here

          try {
            final Collection col = broker.getCollection(currentCollection.getURI().append(name));
            if(col != null) {
              //delete
              final TransactionManager txnManager = broker.getDatabase().getTransactionManager();
              final Txn txn = txnManager.beginTransaction();
              try {
                    broker.removeCollection(txn, col);
                txnManager.commit(txn);
              } catch (final Exception e) {
                txnManager.abort(txn);
               
                    listener.warn("Failed to remove deleted collection: " + name + ": " + e.getMessage());
          } finally {
                        txnManager.close(txn);
                    }
            }
          } catch (final Exception e) {
                listener.warn("Failed to remove deleted collection: " + name + ": " + e.getMessage());
      }

        } else if("resource".equals(type)) {

          try {
            final XmldbURI uri = XmldbURI.create(name);
            final DocumentImpl doc = currentCollection.getDocument(broker, uri);
           
            if (doc != null) {
              final TransactionManager txnManager = broker.getDatabase().getTransactionManager();
              final Txn txn = txnManager.beginTransaction();
                try {
                 
                  if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
                      currentCollection.removeBinaryResource(txn, broker, uri);
                  } else {
                    currentCollection.removeXMLResource(txn, broker, uri);
                  }
                  txnManager.commit(txn);
 
                } catch(final Exception e) {
                  txnManager.abort(txn);
                 
                    listener.warn("Failed to remove deleted resource: " + name + ": " + e.getMessage());
                } finally {
                        txnManager.close(txn);
                    }
                }
          } catch (final Exception e) {
                listener.warn("Failed to remove deleted resource: " + name + ": " + e.getMessage());
      }
View Full Code Here

        return date_created;
    }
   
    private Collection mkcol(XmldbURI collPath, Date created) throws SAXException {
       
      final TransactionManager txnManager = broker.getDatabase().getTransactionManager();
      final Txn txn = txnManager.beginTransaction();
      try {
        final Collection col = broker.getOrCreateCollection(txn, collPath);
       
        txnManager.commit(txn);
       
        return col;
      } catch (final Exception e) {
        txnManager.abort(txn);
        throw new SAXException(e);
    } finally {
            txnManager.close(txn);
        }
    }
View Full Code Here

    addPlugin("org.exist.monitoring.MonitoringManager");
  }

  @Override
  public void start(DBBroker broker) throws EXistException {
        final TransactionManager transaction = db.getTransactionManager();
        Txn txn = null;

        try {
          collection = broker.getCollection(COLLETION_URI);
      if (collection == null) {
        txn = transaction.beginTransaction();
        collection = broker.getOrCreateCollection(txn, COLLETION_URI);
        if (collection == null) {return;}
          //if db corrupted it can lead to unrunnable issue
          //throw new ConfigurationException("Collection '/db/system/plugins' can't be created.");
       
        collection.setPermissions(Permission.DEFAULT_SYSTEM_SECURITY_COLLECTION_PERM);
        broker.saveCollection(txn, collection);

        transaction.commit(txn);
      }
        } catch (final Exception e) {
      transaction.abort(txn);
      e.printStackTrace();
      LOG.debug("loading configuration failed: " + e.getMessage());
    } finally {
            transaction.close(txn);
        }

        final Configuration _config_ = Configurator.parse(this, broker, collection, CONFIG_FILE_URI);
    configuration = Configurator.configure(this, _config_);
   
View Full Code Here

    }
   
    public boolean mkdir() {
      DBBroker broker = null;
    BrokerPool db = null;
    TransactionManager tm;

    try {
      try {
        db = BrokerPool.getInstance();
        broker = db.get(null);
      } catch (final EXistException e) {
        return false;
      }
 
          final Collection collection = broker.getCollection(uri.toCollectionPathURI());
          if (collection != null) {return true;}
 
          final Collection parent_collection = broker.getCollection(uri.toCollectionPathURI().removeLastSegment());
          if (parent_collection == null) {return false;}
 
          tm = db.getTransactionManager();
      final Txn transaction = tm.beginTransaction();
     
      try {
        final Collection child = broker.getOrCreateCollection(transaction, uri.toCollectionPathURI());
        broker.saveCollection(transaction, child);
        tm.commit(transaction);
      } catch (final Exception e) {
          tm.abort(transaction);
        return false;
      } finally {
                tm.close(transaction);
            }
        } catch (final Exception e) {
      return false;
     
    } finally {
View Full Code Here

    }

    public boolean mkdirs() {
      DBBroker broker = null;
    BrokerPool db = null;
    TransactionManager tm;

    try {
      try {
        db = BrokerPool.getInstance();
        broker = db.get(null);
      } catch (final EXistException e) {
        return false;
      }
 
          final Collection collection = broker.getCollection(uri.toCollectionPathURI());
          if (collection != null) {return true;}
 
      tm = db.getTransactionManager();
      final Txn transaction = tm.beginTransaction();
     
      try {
        final Collection child = broker.getOrCreateCollection(transaction, uri.toCollectionPathURI());
        broker.saveCollection(transaction, child);
        tm.commit(transaction);
      } catch (final Exception e) {
          tm.abort(transaction);
        return false;
      } finally {
                tm.close(transaction);
            }

    } catch (final Exception e) {
      return false;
   
View Full Code Here

    public boolean _renameTo(File dest) {
      final XmldbURI destinationPath = ((Resource)dest).uri;

      DBBroker broker = null;
    BrokerPool db = null;
    TransactionManager tm;

    try {
      try {
        db = BrokerPool.getInstance();
        broker = db.get(null);
      } catch (final EXistException e) {
        return false;
      }
 
      tm = db.getTransactionManager();
      Txn transaction = null;
 
          org.exist.collections.Collection destination = null;
          org.exist.collections.Collection source = null;
          XmldbURI newName;
      try {
           source = broker.openCollection(uri.removeLastSegment(), Lock.WRITE_LOCK);
          if(source == null) {
            return false;
              }
          final DocumentImpl doc = source.getDocument(broker, uri.lastSegment());
              if(doc == null) {
                  return false;
              }
              destination = broker.openCollection(destinationPath.removeLastSegment(), Lock.WRITE_LOCK);
              if(destination == null) {
                  return false;
              }
             
              newName = destinationPath.lastSegment();
 
              transaction = tm.beginTransaction();
              broker.moveResource(transaction, doc, destination, newName);
              tm.commit(transaction);
              return true;
             
          } catch ( final Exception e ) {
            e.printStackTrace();
            if (transaction != null) {tm.abort(transaction);}
            return false;
          } finally {
                tm.close(transaction);
            if(source != null) {source.release(Lock.WRITE_LOCK);}
            if(destination != null) {destination.release(Lock.WRITE_LOCK);}
          }
        } finally {
          if (db != null)
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.