jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Charles Johnson ...@proteanit.net>
Subject Node unfound, Node unsaved
Date Sun, 30 Mar 2008 19:17:42 GMT
I'm trying to get a repository set up for web use, initially offline.

There seem to be problems with the most fundamental functionality. I'm 
trying to create a single Node, /reviews, which I'll later use as the 
root for /reviews/review[0..n]

I try a 'find or create' approach, but it never gets found and it never 
gets saved. There must be (I hope) something simple that's wrong. Below 
is the code and attached is its log file, representing three runs of the 
programme.

Charles

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.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 rootPath = "/reviews[0]";

        try {
            rc5.login();

            Node reviews = rc5.getReviewsRoot(rootPath);

            if ((reviews != null) && log.isDebugEnabled()) {
                log.debug("/reviews node found - no need to create it");
            }

            if (reviews == null) {
                if (log.isDebugEnabled()) {
                    log.debug("/reviews node not found - creating it");
                }

                rc5.createReviewsRoot();
            }

            //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 getReviewsRoot(String rootPath) {
        Node reviewsRoot = null;

        try {
            Workspace workSpace = session.getWorkspace();
            QueryManager queryManager = workSpace.getQueryManager();

            Query query = queryManager.createQuery(rootPath, Query.XPATH);

            QueryResult queryResult = query.execute();

            NodeIterator queryResultNodeIterator = queryResult.getNodes();

            if (queryResultNodeIterator.hasNext()) {
                reviewsRoot = (Node) queryResultNodeIterator.next();
            }
        } 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 Node createReviewsRoot() {
        Node reviews = null;

        try {
            final String rootPath = "reviews";
            Node root = session.getRootNode();

            if (log.isDebugEnabled()) {
                log.debug(String.format(
                        "Attempting to create node with path %s", 
rootPath));
            }

            reviews = root.addNode(rootPath);

            if (log.isDebugEnabled()) {
                log.debug(String.format("Attempting to save node %s", 
rootPath));
            }

            session.save();
        } catch (RepositoryException e) {
            log.fatal("", e);
        }

        return reviews;
    }

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

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



Mime
View raw message