Package org.exist.storage.txn

Examples of org.exist.storage.txn.TransactionManager


     
        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();
                moveResource(broker, transaction, doc, source, destination, newName);

//                resource = null;
//                collection = null;
//                initialized = false;
//                uri = ((Resource)dest).uri;

                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


      if (file == null)
        {throw new IOException();}
     
        DBBroker broker = null;
        BrokerPool db = null;
        TransactionManager tm = null;
        Txn txn = null;

        try {
            try {
                db = BrokerPool.getInstance();
                broker = db.get(null);
            } catch (final EXistException e) {
                throw new IOException(e);
            }
   
            tm = db.getTransactionManager();
            txn = tm.beginTransaction();

            FileInputSource is = new FileInputSource(file);
         
            final IndexInfo info = collection.validateXMLResource(txn, broker, uri.lastSegment(), is);
//          info.getDocument().getMetadata().setMimeType(mimeType.getName());
 
          is = new FileInputSource(file);
          collection.store(txn, broker, info, is, false);

            tm.commit(txn);
           
        } catch ( final Exception e ) {
            e.printStackTrace();
            if (txn != null) {tm.abort(txn);}
      } finally {
            tm.close(txn);
          if (db != null)
              {db.release( broker );}
      }
    }
View Full Code Here

    }
   
    public boolean delete() {
      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 txn = null;
          try {
              collection = broker.openCollection(uri.removeLastSegment(), Lock.NO_LOCK);
              if (collection == null) {
                  return false;
              }
              // keep the write lock in the transaction
              //transaction.registerLock(collection.getLock(), Lock.WRITE_LOCK);
 
              final DocumentImpl doc = collection.getDocument(broker, uri.lastSegment());
              if (doc == null) {
                return true;
              }
             
              txn = tm.beginTransaction();
              if(doc.getResourceType() == DocumentImpl.BINARY_FILE)
                  {collection.removeBinaryResource(txn, broker, doc);}
              else
                  {collection.removeXMLResource(txn, broker, uri.lastSegment());}
             
              tm.commit(txn);
              return true;
 
          } catch (final Exception e) {
            if (txn != null) {tm.abort(txn);}
              return false;
          } finally {
                tm.close(txn);
            }
        } finally {
          if (db != null)
            {db.release(broker);}
View Full Code Here

    }

    public boolean createNewFile() throws IOException {
      DBBroker broker = null;
    BrokerPool db = null;
    TransactionManager tm;

    try {
      try {
        db = BrokerPool.getInstance();
        broker = db.get(null);
      } catch (final EXistException e) {
        throw new IOException(e);
      }
     
//      if (!uri.startsWith("/db"))
//        uri = XmldbURI.DB.append(uri);
// 
      try {
        if (uri.endsWith("/"))
          {throw new IOException("It collection, but should be resource: "+uri);}
      } catch (final Exception e) {
        throw new IOException(e);
      }
     
      final XmldbURI collectionURI = uri.removeLastSegment();
      collection = broker.getCollection(collectionURI);
      if (collection == null)
        {throw new IOException("Collection not found: "+collectionURI);}
     
      final XmldbURI fileName = uri.lastSegment();
 
//      try {
//        resource = broker.getXMLResource(uri, Lock.READ_LOCK);
//      } catch (final PermissionDeniedException e1) {
//      } finally {
//        if (resource != null) {
//          resource.getUpdateLock().release(Lock.READ_LOCK);
//          collection = resource.getCollection();
//          initialized = true;
//         
//          return false;
//        }
//      }
//     
      try {
        resource = broker.getResource(uri, Lock.READ_LOCK);
      } catch (final PermissionDeniedException e1) {
      } finally {
        if (resource != null) {
          resource.getUpdateLock().release(Lock.READ_LOCK);
          collection = resource.getCollection();
          initialized = true;
         
          return false;
        }
      }

      MimeType mimeType = MimeTable.getInstance().getContentTypeFor(fileName);
 
      if (mimeType == null) {
        mimeType = MimeType.BINARY_TYPE;
      }
     
      tm = db.getTransactionManager();
      final Txn transaction = tm.beginTransaction();
 
      InputStream is = null;
      try {
        if (mimeType.isXMLType()) {
          // store as xml resource
          final String str = "<empty/>";
          final IndexInfo info = collection.validateXMLResource(transaction, broker, fileName, str);
          info.getDocument().getMetadata().setMimeType(mimeType.getName());
          info.getDocument().getPermissions().setMode(DEFAULT_RESOURCE_PERM);
          collection.store(transaction, broker, info, str, false);
 
        } else {
          // store as binary resource
          is = new ByteArrayInputStream("".getBytes(UTF_8));
         
          final BinaryDocument blob = new BinaryDocument(db, collection, fileName);
 
          blob.getPermissions().setMode(DEFAULT_RESOURCE_PERM);

          collection.addBinaryResource(transaction, broker, blob, is,
              mimeType.getName(), 0L , new Date(), new Date());
 
        }
        tm.commit(transaction);
      } catch (final Exception e) {
        tm.abort(transaction);
        throw new IOException(e);
      } finally {
                tm.close(transaction);
        closeFile(is);
 
        if (resource != null)
          {resource.getUpdateLock().release(Lock.READ_LOCK);}
      }
View Full Code Here

        broker = db.get(null);
      } catch (final EXistException e) {
        throw new IOException(e);
      }
 
      final TransactionManager tm = db.getTransactionManager();
      Txn txn = null;
     
      try {
        //collection
        if (uri.endsWith("/")) {
          collection = broker.getCollection(uri);
          if (collection == null)
            {throw new IOException("Resource not found: "+uri);}
         
        //resource
        } else {
          resource = broker.getXMLResource(uri, Lock.READ_LOCK);
          if (resource == null) {
            //may be, it's collection ... checking ...
            collection = broker.getCollection(uri);
            if (collection == null) {
              throw new IOException("Resource not found: "+uri);
            }
           
            txn = tm.beginTransaction();

            method.modify(collection);
            broker.saveCollection(txn, collection);
           
            tm.commit(txn);

          } else {
            collection = resource.getCollection();

            txn = tm.beginTransaction();
           
            method.modify(resource);
                  broker.storeMetadata(txn, resource);
           
            tm.commit(txn);
          }
        }
      } catch (final IOException e) {
        if (txn != null) {
          tm.abort(txn);
        }
        throw e;
      } catch (final Exception e) {
        if (txn != null) {
          tm.abort(txn);
        }
        throw new IOException(e);
      } finally {
                tm.close(txn);
        if (resource != null)
          {resource.getUpdateLock().release(Lock.READ_LOCK);}
      }
    } finally {
      if (db != null)
View Full Code Here

            return;
       
        if (!(file.exists() && file.canRead()))
            return;
       
        TransactionManager txManager = db.getTransactionManager();
        Txn txn = null;
        try {
            MimeType mime = getMimeTable().getContentTypeFor( file.getName() );
            if (mime != null && mime.isXMLType()) {
                txn = txManager.beginTransaction();

                IndexInfo info = col.validateXMLResource(txn, broker,
                        XmldbURI.create(file.getName()),
                        new InputSource(new FileInputStream(file))
                    );
                //info.getDocument().getMetadata().setMimeType();
                FileInputStream is = new FileInputStream(file);
                try {
                    col.store(txn, broker, info, new InputSource(is), false);
                } finally {
                    is.close();
                }

                txManager.commit(txn);
            } else {
                txn = txManager.beginTransaction();
   
                FileInputStream is = new FileInputStream(file);
                try {
                    col.addBinaryResource(txn, broker,
                            XmldbURI.create(file.getName()),
                            is,
                            MimeType.BINARY_TYPE.getName(), file.length());
                } finally {
                    is.close();
                }
   
                txManager.commit(txn);
            }
        } catch (Exception e) {
            if (txn != null) {
                txManager.abort(txn);
            }
            System.out.println("fail to load file "+file.getName());
            e.printStackTrace();
        } finally {
            txManager.close(txn);
        }
        //System.out.println(file);
    }
View Full Code Here

    public void configure(DBBroker broker, org.exist.collections.Collection parent, Map<String, List<?>> parameters) throws TriggerException {
        super.configure(broker, parent, parameters);
        XmldbURI docPath = XmldbURI.create("messages.xml");
        System.out.println("TestTrigger prepares");
        TransactionManager transactMgr = broker.getBrokerPool().getTransactionManager();
        Txn transaction = transactMgr.beginTransaction();
        try {
            this.doc = parent.getDocument(broker, docPath);
            if (this.doc == null) {
               
               
       
                LOG.debug("creating new file for collection contents");

                // IMPORTANT: temporarily disable triggers on the collection.
                // We would end up in infinite recursion if we don't do that
                parent.setTriggersEnabled(false);
                IndexInfo info = parent.validateXMLResource(transaction, broker, docPath, TEMPLATE);
                //TODO : unlock the collection here ?
                parent.store(transaction, broker, info, TEMPLATE, false);
                this.doc = info.getDocument();

                transactMgr.commit(transaction);
            }
        } catch (Exception e) {
            transactMgr.abort(transaction);
            throw new TriggerException(e.getMessage(), e);
        } finally {
            parent.setTriggersEnabled(true);
        }
    }
View Full Code Here

      }
    }
    public boolean createCollection(java.lang.String sessionId, XmldbURI path) 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());
            LOG.debug("creating collection " + path);
            final org.exist.collections.Collection coll =
                    broker.getOrCreateCollection(txn, path);
            if (coll == null) {
                LOG.debug("failed to create collection");
                return false;
            }
            broker.saveCollection(txn, coll);
            transact.commit(txn);
            broker.flush();
            broker.sync(Sync.MINOR_SYNC);
            return true;
        } catch (final Exception e) {
            transact.abort(txn);
            LOG.debug(e.getMessage(), e);
            throw new RemoteException(e.getMessage());
        } finally {
            transact.close(txn);
            pool.release(broker);
        }
    }
View Full Code Here

      }
    }
    public boolean removeCollection(java.lang.String sessionId, XmldbURI path) 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 Collection collection = broker.getCollection(path);
            if(collection == null) {
                transact.abort(txn);
                return false;
            }
            final boolean removed = broker.removeCollection(txn, collection);
            transact.commit(txn);
            return removed;
        } 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 boolean removeDocument(java.lang.String sessionId, XmldbURI path) 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");
            }
            final DocumentImpl doc = collection.getDocument(broker, docUri);
            if(doc == null)
                {throw new EXistException("Document " + docUri + " not found");}
           
            if(doc.getResourceType() == DocumentImpl.BINARY_FILE)
                {collection.removeBinaryResource(txn, broker, doc);}
            else
                {collection.removeXMLResource(txn, broker, docUri);}
           
            transact.commit(txn);
            return true;
        } 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

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.