jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Charles Johnson ...@proteanit.net>
Subject Re: Node unfound, Node unsaved
Date Mon, 31 Mar 2008 12:18:35 GMT
Thanks Dave. Sounds like a great plan and I *was* doing something 
similar but without success, hence the rather ponderous approach. 
However, heartened by your response, I tried again. It doesn't work - 
again the <reviews> root is not found and is recreated each time. New 
code follows and log file attached. The giveaway is the line
'RC5: Node with path reviews not found so attempting to create it...'
in the log file.

CJ


import net.proteanit.mantex.util.JackUtils;

import org.apache.jackrabbit.core.TransientRepository;

import org.apache.log4j.Logger;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;


public class RC5 {
     private static Logger log = Logger.getLogger(RC5.class);
     private Session session;

     public RC5() {
         //Runtime.getRuntime().addShutdownHook(new Thread(new 
SessionCloser()));
     }

     public Session getSession() {
         return session;
     }

     private void login() {
         TransientRepository repository = null;

         try {
             repository = new TransientRepository();
             session = repository.login(new SimpleCredentials("username",
                         "password".toCharArray()));

             if ((session != null) && log.isInfoEnabled()) {
                 log.info(String.format("Got session. Workspace is %s",
                         session.getWorkspace().getName()));
             }

             if (session == null) {
                 log.error("******** Did not get session in RC5 ********");
             }
         } catch (Exception e) {
             log.error("", e);
         }
     }

     public static void main(String[] args) throws Exception {
         System.setProperty("org.apache.jackrabbit.repository.home",
             "/apache-tomcat-6.0.16/rep-mantex.co.uk");

         RC5 rc5 = new RC5();
         final String ROOT_PATH = "reviews";

         try {
             rc5.login();

             Node reviews = rc5.getOrCreateReviewsRoot(ROOT_PATH);

             //rc5.printNodes();
             rc5.dumpNodes();

             if (log.isDebugEnabled()) {
                 log.debug("Attempting to save session...");
             }

             try {
                 rc5.getSession().save();
             } catch (Exception e) {
                 log.fatal("", e);
             }
         } finally {
             if (log.isDebugEnabled()) {
                 log.debug("Attempting to log out...");
             }

             rc5.getSession().logout();
         }
     }

     private Node getOrCreateReviewsRoot(String reviewsRootPath) {
         Node reviewsRoot = null;
	Node root = null;
         try {
	    root = session.getRootNode();
             if (log.isDebugEnabled()) {
                 log.debug(String.format("Attempting to get node with 
path %s...", reviewsRootPath));
             }
             reviewsRoot = root.getNode(reviewsRootPath);
             reviewsRoot = root.addNode(reviewsRootPath);
             if (log.isDebugEnabled()) {
                 log.debug("Attempting to save newly-added node...");
             }
             session.save();
         } catch (PathNotFoundException pnfe) {
             if (log.isDebugEnabled()) {
                 log.debug(String.format("Node with path %s not found so 
attempting to create it...", reviewsRootPath));
             }
         } catch (RepositoryException e) {
             log.fatal("", e);
         }

         return reviewsRoot;
     }

     private void dumpNodes() {
         try {
             Node root = session.getRootNode();
             JackUtils.dump(root);
         } catch (RepositoryException e) {
             log.fatal("", e);
         }
     }

     private void printNodes() {
         try {
             Node root = session.getRootNode();
             NodeIterator i = root.getNodes();
             Node n = null;

             while (i.hasNext()) {
                 n = (Node) i.next();

                 if (log.isInfoEnabled()) {
                     log.info(String.format("Node:name=%s,depth=%d",
                             n.getName(), n.getDepth()));
                 }
             }
         } catch (RepositoryException e) {
             log.fatal("", e);
         }
     }


     private class SessionCloser implements Runnable {
         public void run() {
             if (log.isInfoEnabled()) {
                 log.info("Attempting to close down session...");
             }

             // Trouble at t'mill: this is runnning before it should be...
             /*
                         if ((session != null) && session.isLive()) {
                             session.logout();
                         }
             */
         }
     }
}


Dave Brosius wrote:
 > If I understand you correctly, i would think you would do
 >
 > /jcr:root/reviews[1]
 >
 > to get the first (and only) reviews node
 >
 > Although i don't think you need to search for it.
 >
 > Just get the root node
 >
 > Node root = session.getRootNode();
 >
 > and then get the reviews childnode, creating it on failure
 >
 > Node reviews;
 > try
 > {
 > reviews= crRoot.getNode("reviews");
 > }
 > catch (PathNotFoundException pnfe)
 > {
 > reviews= crRoot.addNode("reviews");
 > session.save();
 > }
 >

Mime
View raw message