Package com.dbxml.db.core.transaction

Examples of com.dbxml.db.core.transaction.Transaction


         transformer.setParameter(name, value);
      }
   }

   private void processSource(Element sourceElem) throws QueryException {
      Transaction tx = new Transaction();
      try {
         String xpath = sourceElem.getAttribute(XPATH);
         String docName = sourceElem.getAttribute(DOCUMENT);

         boolean hasDocName = docName != null && docName.length() > 0;
         boolean hasXpath = xpath != null && xpath.length() > 0;

         if ( hasDocName && hasXpath )
            throw new CompilationException("'document' and 'xpath' attributes are mutually exclusive");
         else if ( hasDocName ) {
            Document doc = null;
            try {
               doc = domAdapter.getDocument(tx, docName);
            }
            catch ( DBException e ) {
               // Null is ok
            }

            if ( doc != null ) {
               try {
                  String path = context.getCanonicalDocumentName(new Key(docName));
                  String systemID = XSLTQueryResolver.URL_PREFIX + path;
                  src = new DOMSource(doc, systemID);
               }
               catch ( Exception e ) {
                  // This shouldn't happen
               }
            }
            else
               throw new CompilationException("Document '" + docName + "' not found");
         }
         else if ( hasXpath ) {
            ResultSet rs;
            if ( keys != null )
               rs = context.queryDocument(tx, XPathQueryResolver.STYLE_XPATH, xpath, nsMap, keys);
            else
               rs = context.queryCollection(tx, XPathQueryResolver.STYLE_XPATH, xpath, nsMap);

            Document doc = ResultSetWrapper.toDocument(rs);
            String path = context.getCanonicalName();
            String systemID = XSLTQueryResolver.URL_PREFIX+path+"/";
            src = new DOMSource(doc, systemID);
         }
         else {
            NodeList nl = sourceElem.getChildNodes();
            Element e = null;
            for ( int i = 0; i < nl.getLength(); i++ ) {
               Node n = nl.item(i);
               if ( n.getNodeType() == Node.ELEMENT_NODE ) {
                  if ( e == null )
                     e = (Element)n;
                  else
                     throw new CompilationException("Only a single Element is allowed for inline source");
               }
            }
            String path = context.getCanonicalName();
            String systemID = XSLTQueryResolver.URL_PREFIX+path+"/";
            src = new DOMSource(e, systemID);
         }
      }
      catch ( DBException e ) {
         try {
            tx.cancel();
         }
         catch ( DBException ex ) {
            ex.printStackTrace(System.err);
         }

         if ( e instanceof QueryException )
            throw (QueryException)e;
         else
            throw new CompilationException(e);
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE ) {
            try {
               tx.commit();
            }
            catch ( DBException e ) {
               e.printStackTrace(System.err);
            }
         }
View Full Code Here


            throw new CompilationException("Couldn't compile XSLT Query", e);
      }
   }

   Transformer getTransformer(String path) throws QueryException {
      Transaction tx = new Transaction();

      int idx = path.lastIndexOf('/');
      String colName = path.substring(0, idx);
      String docName = path.substring(idx + 1);

      try {
         CollectionClientImpl col = (CollectionClientImpl)client.getCollection(colName);
         if ( col == null )
            throw new CompilationException("Template Collection '" + colName + "' not found");

         RecordMetaData meta = col.getInternalCollection().getRecordMetaData(tx, new Key(docName));
         long lastModified = System.currentTimeMillis();
         if ( meta != null ) {
            Long lm = (Long)meta.getValue(RecordMetaData.MODIFIED);
            if ( lm != null )
               lastModified = lm.longValue();
         }

         TemplatesInfo info = (TemplatesInfo)cache.get(path);
         if ( info == null || info.lastModified != lastModified ) {
            Document doc = col.getDocument(docName);
            String systemID = URL_PREFIX+colName+"/"+docName;
            Source src = new DOMSource(doc, systemID);
            Templates t = tf.newTemplates(src);

            info = new TemplatesInfo(path, lastModified, t);
            cache.put(path, info);
         }

         Transformer transformer = info.tmp.newTransformer();
         transformer.setURIResolver(uriResolver);
         return transformer;
      }
      catch ( Exception e ) {
         try {
            tx.cancel();
         }
         catch ( DBException ex ) {
            ex.printStackTrace(System.err);
         }

         if ( e instanceof QueryException )
            throw (QueryException)e;
         else
            throw new CompilationException("Couldn't compile XSLT Query", e);
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE ) {
            try {
               tx.commit();
            }
            catch ( DBException e ) {
               e.printStackTrace(System.err);
            }
         }
View Full Code Here

      }
   }

   private Indexer register(Class c, Configuration cfg) throws DBException {
      String name = null;
      Transaction tx = new Transaction();

      try {
         name =  cfg.getAttribute(NAME);

         Indexer idx = (Indexer)c.newInstance();
         initialize(idx, cfg);
         name = idx.getName();

         if ( name == null || name.trim().equals("") )
            throw new CannotCreateException("No name specified");

         IndexPattern pattern = new IndexPattern(symbols, idx.getPattern(), null, true);
         String style = idx.getIndexStyle();
         IndexerInfo info = new IndexerInfo(name, style, pattern, idx);

         boolean newIndex = false;
         if ( !idx.exists() ) {
            idx.create();
            newIndex = true;
         }
         idx.open();

         if ( newIndex ) {
            info.status = STATUS_BUILDING;
            newIndexers.add(info);
            autoIndexer.addIndexManager(this);
         }
         else
            info.status = STATUS_READY;

         indexes.put(name, info);
         patternMap.put(pattern, info);

         Map tbl = (Map)bestIndexers.get(style);
         if ( tbl == null ) {
            tbl = new SoftHashMap();
            bestIndexers.put(style, tbl);
         }
         tbl.clear();
         idxList = (IndexerInfo[])indexes.values().toArray(EmptyIndexerInfo);

         collection.flushSymbolTable();

         return idx;
      }
      catch ( DBException d ) {
         tx.cancel();
         throw d;
      }
      catch ( Exception e ) {
          tx.cancel();
         throw new CannotCreateException("Cannot create Index '" + name + "'", e);
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }
View Full Code Here

      idx.setConfig(cfg);
   }

   private void populateNewIndexers() throws DBException {
      if ( newIndexers.size() > 0 ) {
         Transaction tx = new Transaction();
         try {
            IndexerInfo[] list = (IndexerInfo[])newIndexers.toArray(EmptyIndexerInfo);
            newIndexers.clear();

            ContainerSet docs = collection.getContainerSet(tx);
            while ( docs.hasMoreContainers() ) {
               Container c = docs.getNextContainer();
               if ( c != null ) {
                  try {
                     new IndexProducer(tx, c.getKey(), c.getDocument(), ACTION_CREATE, list);
                  }
                  catch ( IndexCancelException e ) {
                     break;
                  }
                  catch ( Exception e ) {
                     e.printStackTrace(System.err);
                  }
               }
            }

            for ( int i = 0; i < list.length; i++ ) {
               try {
                  if ( list[i].status != STATUS_DELETED ) {
                     list[i].indexer.flush(tx);
                     list[i].status = STATUS_READY;
                  }
               }
               catch ( Exception e ) {
               }
            }
         }
         catch ( DBException e ) {
            tx.cancel();
            throw e;
         }
         finally {
            if ( tx.getStatus() == Transaction.ACTIVE )
               tx.commit();
         }
      }
   }
View Full Code Here

   }

   private void prepareNextNode() throws QueryException {
      node = null;
      while ( nextKey() != null ) {
         Transaction tx = new Transaction();
         try {
            Document d = domAdapter.getDocument(tx, currentKey);

            if ( d == null )
               continue;

            Node n = d.getDocumentElement();

            XPathContext xpc = new XPathContext();
            PrefixResolver pfx;
            if ( pr == null ) {
               pfx = new PrefixResolverDefault(d.getDocumentElement());
               xp = new XPath(xpath, null, pfx, XPath.SELECT, null);
            }
            else {
               pfx = pr;
               if ( xp == null )
                  xp = new XPath(xpath, null, pfx, XPath.SELECT, null);
            }

            ni = xp.execute(xpc, n, pfx).nodeset();

            node = ni.nextNode();
            if ( node != null )
               break;
         }
         catch ( Exception e ) {
            try {
               tx.cancel();
            }
            catch ( DBException ex ) {
               ex.printStackTrace(System.err);
            }
         }
         finally {
            if ( tx.getStatus() == Transaction.ACTIVE ) {
               try {
                  tx.commit();
               }
               catch ( DBException e ) {
                  e.printStackTrace(System.err);
               }
            }
View Full Code Here

      result.put(CONTENT_TYPE, new Integer(type));
      return result;
   }

   public byte[] get() throws HTTPException, DBException {
      Transaction tx = new Transaction();

      try {
         Key key = new Key(docName);

         // Let's see if we can interact with Labrador's HTTP Server for REST calls
         BrokerContext context = Broker.getInstance().getBrokerContext();
         Handler handler = context.getHandler();
         Endpoint endpoint = context.getRequestEndpoint();

         boolean restHandler = handler instanceof RESTHandler;
         boolean httpEndpoint = endpoint instanceof HTTPServerBase;

         if ( restHandler && httpEndpoint ) {
            Request request = context.getRequest();
            Response response = context.getResponse();

            RecordMetaData meta = col.getRecordMetaData(tx, key);
            if ( meta != null ) {
               Long lm = (Long)meta.getValue(RecordMetaData.MODIFIED);
               if ( lm != null ) {
                  String lastModified = GDF.format(new Date(lm.longValue()));

                  response.setHeader(HTTP.HEADER_CACHE_CONTROL, HTTP.VALUE_CACHE);
                  response.setHeader(HTTP.HEADER_LAST_MODIFIED, lastModified);

                  String since = request.getHeader(HTTP.HEADER_IF_MODIFIED_SINCE);
                  if ( since != null && since.length() > 0 && since.equals(lastModified) )
                     throw new HTTPException(HTTP.CODE_NOT_MODIFIED, HTTP.STATUS_NOT_MODIFIED);
               }
            }
         }

         if ( type == Container.TYPE_DOCUMENT ) {
            if ( restHandler )
               context.setProperty(RESTHandler.CONTENT_TYPE, Headers.TYPE_TEXT_XML);

            String doc = DTSMHelper.tableToText(col.getDocument(tx, docName));
            return UTF8.toUTF8(doc);
         }
         else
            return col.getRecord(tx, docName).getValue().getData();
      }
      catch ( DTSMException e ) {
         tx.cancel();
         //e.printStackTrace(System.err);
         throw new DBException(FaultCodes.COL_CANNOT_RETRIEVE, "Can't retrieve '" + docName + "'", e);
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }
View Full Code Here

   }

   public static final String REST_CONTENT_TYPE_getDocument = Headers.TYPE_TEXT_XML;

   public String getDocument() throws DBException {
      Transaction tx = new Transaction();
      try {
         return DTSMHelper.tableToText(col.getDocument(tx, docName));
      }
      catch ( DTSMException e ) {
         tx.cancel();
         throw new DBException(FaultCodes.COL_CANNOT_RETRIEVE, "Can't retrieve '" + docName + "'", e);
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }
View Full Code Here

            tx.commit();
      }
   }

   public byte[] getValue() throws DBException {
      Transaction tx = new Transaction();
      try {
         return col.getRecord(tx, docName).getValue().getData();
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }
View Full Code Here

         catch ( Exception e ) {
            e.printStackTrace(System.err);
            throw new dbXMLException("Couldn't initialize System Collection", e);
         }

         Transaction tx = new Transaction();
         try {
            // Bootstrap from the database itself...  This is accomplished
            // by intercepting the setConfig call and using a Configuration
            // retrieved from the database instead of the standard config
            Collection col = sysCol.getCollection(SystemCollection.CONFIGS);
            DOMAdapter domAdapter = new DOMAdapter(col);
            Document colDoc = domAdapter.getDocument(tx, COLKEY);

            if ( colDoc == null ) {
               colDoc = DOMHelper.newDocument();
               Element root = colDoc.createElement(DATABASE);
               root.setAttribute(NAME, name);
               colDoc.appendChild(root);

               domAdapter.setDocument(tx, COLKEY, colDoc);
            }

            super.setConfig(new Configuration(colDoc));
         }
         catch ( Throwable e ) {
            tx.cancel();
            e.printStackTrace(System.err);
            throw new dbXMLException("Couldn't bootstrap Database", e);
         }
         finally {
            if ( tx.getStatus() == Transaction.ACTIVE )
               tx.commit();
         }

         try {
            Configuration securityCfg = config.getChild(SECURITY);
            if ( securityCfg != null ) {
View Full Code Here

   }

   public static final String[] PARAMS_setDocument = {"document"};

   public void setDocument(String document) throws DBException {
      Transaction tx = new Transaction();
      try {
         DocumentTable dt = DTSMHelper.textToTable(document, col.getSymbols());
         col.setDocument(tx, docName, dt);
      }
      catch ( DTSMException e ) {
         tx.cancel();
         throw new DBException(FaultCodes.COL_CANNOT_STORE, "Can't parse Document '" + docName + "'", e);
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }
View Full Code Here

TOP

Related Classes of com.dbxml.db.core.transaction.Transaction

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.