Exemplo n.º 1
0
  @AfterClass
  public static void tearDown() {

    TransactionManager transact = null;
    Txn transaction = null;
    try {
      transact = pool.getTransactionManager();
      transaction = transact.beginTransaction();
      root =
          broker.getOrCreateCollection(
              transaction, XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test"));
      //          broker.removeCollection(transaction, root);

      transact.commit(transaction);
    } catch (Exception e) {
      if (transaction != null) {
        transact.abort(transaction);
      }
    } finally {
      if (pool != null) pool.release(broker);
    }
    BrokerPool.stopAll(false);
    pool = null;
    root = null;
  }
  private <R> R modifyCollection(
      DBBroker broker,
      XmldbURI collectionURI,
      DatabaseItemModifier<org.exist.collections.Collection, R> modifier)
      throws XMLDBException, LockException, PermissionDeniedException, IOException, EXistException,
          TriggerException, SyntaxException {
    final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
    final Txn transaction = transact.beginTransaction();

    org.exist.collections.Collection coll = null;

    try {
      coll = broker.openCollection(collectionURI, Lock.WRITE_LOCK);
      if (coll == null) {
        throw new XMLDBException(
            ErrorCodes.INVALID_COLLECTION, "Collection " + collectionURI.toString() + " not found");
      }

      final R result = modifier.modify(coll);

      broker.saveCollection(transaction, coll);
      transact.commit(transaction);
      broker.flush();

      return result;

    } catch (final EXistException ee) {
      transact.abort(transaction);
      throw ee;
    } catch (final XMLDBException xmldbe) {
      transact.abort(transaction);
      throw xmldbe;
    } catch (final LockException le) {
      transact.abort(transaction);
      throw le;
    } catch (final PermissionDeniedException pde) {
      transact.abort(transaction);
      throw pde;
    } catch (final IOException ioe) {
      transact.abort(transaction);
      throw ioe;
    } catch (final TriggerException te) {
      transact.abort(transaction);
      throw te;
    } catch (final SyntaxException se) {
      transact.abort(transaction);
      throw se;
    } finally {
      transact.close(transaction);
      if (coll != null) {
        coll.release(Lock.WRITE_LOCK);
      }
    }
  }
Exemplo n.º 3
0
  @BeforeClass
  public static void setUp() throws Exception {
    TransactionManager transact = null;
    Txn transaction = null;
    try {
      pool = startDB();
      broker = pool.get(pool.getSecurityManager().getSystemSubject());
      transact = pool.getTransactionManager();
      transaction = transact.beginTransaction();

      root =
          broker.getOrCreateCollection(
              transaction, XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test"));
      broker.saveCollection(transaction, root);

      String existHome = System.getProperty("exist.home");
      File existDir = existHome == null ? new File(".") : new File(existHome);
      String directory = "samples/shakespeare";
      File dir = new File(existDir, directory);

      // store some documents.
      for (File f : dir.listFiles(new XMLFilenameFilter())) {
        IndexInfo info =
            root.validateXMLResource(
                transaction,
                broker,
                XmldbURI.create(f.getName()),
                new InputSource(f.toURI().toASCIIString()));
        root.store(transaction, broker, info, new InputSource(f.toURI().toASCIIString()), false);
      }

      IndexInfo info =
          root.validateXMLResource(transaction, broker, XmldbURI.create("nested.xml"), NESTED_XML);
      root.store(transaction, broker, info, NESTED_XML, false);
      transact.commit(transaction);

      // for the tests
      docs = root.allDocs(broker, new DefaultDocumentSet(), true);
      seqSpeech = executeQuery(broker, "//SPEECH", 2628, null);

    } catch (Exception e) {
      if (pool != null) {
        pool.release(broker);
        BrokerPool.stopAll(false);
        pool = null;
        root = null;
      }
      throw e;
    }
  }
  private <R> R modifyResource(
      DBBroker broker, Resource resource, DatabaseItemModifier<DocumentImpl, R> modifier)
      throws XMLDBException, LockException, PermissionDeniedException, EXistException,
          SyntaxException {
    final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
    final Txn transaction = transact.beginTransaction();

    DocumentImpl document = null;
    try {
      document = ((AbstractEXistResource) resource).openDocument(broker, Lock.WRITE_LOCK);
      final SecurityManager sm = broker.getBrokerPool().getSecurityManager();
      if (!document.getPermissions().validate(user, Permission.WRITE)
          && !sm.hasAdminPrivileges(user)) {
        throw new XMLDBException(
            ErrorCodes.PERMISSION_DENIED,
            "you are not the owner of this resource; owner = "
                + document.getPermissions().getOwner());
      }

      final R result = modifier.modify(document);

      broker.storeXMLResource(transaction, document);
      transact.commit(transaction);

      return result;

    } catch (final EXistException ee) {
      transact.abort(transaction);
      throw ee;
    } catch (final XMLDBException xmldbe) {
      transact.abort(transaction);
      throw xmldbe;
    } catch (final LockException le) {
      transact.abort(transaction);
      throw le;
    } catch (final PermissionDeniedException pde) {
      transact.abort(transaction);
      throw pde;
    } catch (final SyntaxException se) {
      transact.abort(transaction);
      throw se;
    } finally {
      transact.close(transaction);
      if (document != null) {
        ((AbstractEXistResource) resource).closeDocument(document, Lock.WRITE_LOCK);
      }
    }
  }
Exemplo n.º 5
0
  public void testUpdate() {
    BrokerPool.FORCE_CORRUPTION = true;
    BrokerPool pool = null;
    DBBroker broker = null;
    try {
      pool = startDB();
      assertNotNull(pool);
      broker = pool.get(SecurityManager.SYSTEM_USER);
      assertNotNull(broker);
      TransactionManager mgr = pool.getTransactionManager();
      assertNotNull(mgr);

      IndexInfo info = init(broker, mgr);
      assertNotNull(info);
      DocumentSet docs = new DocumentSet();
      docs.add(info.getDocument());
      XUpdateProcessor proc = new XUpdateProcessor(broker, docs, AccessContext.TEST);
      assertNotNull(proc);

      Txn transaction = mgr.beginTransaction();
      assertNotNull(transaction);
      System.out.println("Transaction started ...");

      String xupdate;
      Modification modifications[];

      // append some new element to records
      for (int i = 1; i <= 200; i++) {
        xupdate =
            "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">"
                + "   <xu:append select=\"/products\">"
                + "       <product>"
                + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>"
                + "           <description>Product "
                + i
                + "</description>"
                + "           <price>"
                + (i * 2.5)
                + "</price>"
                + "           <stock>"
                + (i * 10)
                + "</stock>"
                + "       </product>"
                + "   </xu:append>"
                + "</xu:modifications>";
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
      }

      DOMFile domDb = ((NativeBroker) broker).getDOMFile();
      assertNotNull(domDb);
      System.out.println(domDb.debugPages(info.getDocument(), false));

      mgr.commit(transaction);
      System.out.println("Transaction commited ...");

      // the following transaction will not be committed and thus undone during recovery
      transaction = mgr.beginTransaction();
      assertNotNull(transaction);
      System.out.println("Transaction started ...");

      // rename elements
      xupdate =
          "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">"
              + "   <xu:rename select=\"/products/product/description\">descript</xu:rename>"
              + "</xu:modifications>";
      proc.setBroker(broker);
      proc.setDocumentSet(docs);
      modifications = proc.parse(new InputSource(new StringReader(xupdate)));
      assertNotNull(modifications);
      modifications[0].process(transaction);
      proc.reset();

      //          Don't commit...
      pool.getTransactionManager().getJournal().flushToLog(true);
      System.out.println("Transaction interrupted ...");
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      pool.release(broker);
    }
  }