Search in sources :

Example 91 with Workspace

use of javax.jcr.Workspace in project jackrabbit by apache.

the class ShareableNodeTest method testObservation.

//------------------------------------------------------ specification tests
/**
     * Verify that observation events are sent only once (6.13.15).
     */
public void testObservation() throws Exception {
    // setup parent nodes and first child
    Node a1 = testRootNode.addNode("a1");
    Node a2 = testRootNode.addNode("a2");
    Node b1 = a1.addNode("b1");
    testRootNode.save();
    // add mixin
    b1.addMixin("mix:shareable");
    b1.save();
    // clone
    Workspace workspace = b1.getSession().getWorkspace();
    workspace.clone(workspace.getName(), b1.getPath(), a2.getPath() + "/b2", false);
    // event listener that counts events received
    class EventCounter implements SynchronousEventListener {

        private int count;

        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                events.nextEvent();
                count++;
            }
        }

        public int getEventCount() {
            return count;
        }

        public void resetCount() {
            count = 0;
        }
    }
    EventCounter el = new EventCounter();
    ObservationManager om = superuser.getWorkspace().getObservationManager();
    // add node underneath shared set: verify it generates one event only
    om.addEventListener(el, Event.NODE_ADDED, testRootNode.getPath(), true, null, null, false);
    b1.addNode("c");
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // remove node underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.NODE_REMOVED, testRootNode.getPath(), true, null, null, false);
    b1.getNode("c").remove();
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // add property underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.PROPERTY_ADDED, testRootNode.getPath(), true, null, null, false);
    b1.setProperty("c", "1");
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // modify property underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.PROPERTY_CHANGED, testRootNode.getPath(), true, null, null, false);
    b1.setProperty("c", "2");
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // remove property underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.PROPERTY_REMOVED, testRootNode.getPath(), true, null, null, false);
    b1.getProperty("c").remove();
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
}
Also used : Node(javax.jcr.Node) ObservationManager(javax.jcr.observation.ObservationManager) EventIterator(javax.jcr.observation.EventIterator) Workspace(javax.jcr.Workspace) SynchronousEventListener(org.apache.jackrabbit.core.observation.SynchronousEventListener)

Example 92 with Workspace

use of javax.jcr.Workspace in project jackrabbit by apache.

the class ShareableNodeTest method testName.

public void testName() throws RepositoryException {
    Node n1 = testRootNode.addNode(nodeName1);
    Node n2 = testRootNode.addNode(nodeName2);
    Node s = n1.addNode(nodeName3);
    s.addMixin(mixShareable);
    testRootNode.save();
    Workspace wsp = superuser.getWorkspace();
    wsp.clone(wsp.getName(), s.getPath(), n2.getPath() + "/" + nodeName4, false);
    String stmt = testPath + "//" + nodeName3;
    NodeIterator nodes = executeQuery(stmt).getNodes();
    assertEquals("wrong result size", 1, nodes.getSize());
    // spec does not say which path must be returned -> use isSame()
    assertTrue("wrong node", s.isSame(nodes.nextNode()));
    stmt = testPath + "//" + nodeName4;
    nodes = executeQuery(stmt).getNodes();
    assertEquals("wrong result size", 1, nodes.getSize());
    // spec does not say which path must be returned -> use isSame()
    assertTrue("wrong node", s.isSame(nodes.nextNode()));
    // remove a node from the shared set
    s.removeShare();
    testRootNode.save();
    s = n2.getNode(nodeName4);
    stmt = testPath + "//" + nodeName3;
    nodes = executeQuery(stmt).getNodes();
    assertEquals("wrong result size", 0, nodes.getSize());
    stmt = testPath + "//" + nodeName4;
    nodes = executeQuery(stmt).getNodes();
    assertEquals("wrong result size", 1, nodes.getSize());
    // spec does not say which path must be returned -> use isSame()
    assertTrue("wrong node", s.isSame(nodes.nextNode()));
    // remove remaining node from the shared set
    s.removeShare();
    testRootNode.save();
    stmt = testPath + "//" + nodeName3;
    nodes = executeQuery(stmt).getNodes();
    assertEquals("wrong result size", 0, nodes.getSize());
    stmt = testPath + "//" + nodeName4;
    nodes = executeQuery(stmt).getNodes();
    assertEquals("wrong result size", 0, nodes.getSize());
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) Workspace(javax.jcr.Workspace)

Example 93 with Workspace

use of javax.jcr.Workspace in project jackrabbit by apache.

the class AbstractItemResource method copy.

/**
     * Copies the underlying repository item to the indicated destination. If
     * the locator of the specified destination resource indicates a different
     * workspace, {@link Workspace#copy(String, String, String)} is used to perform
     * the copy operation, {@link Workspace#copy(String, String)} otherwise.
     * <p>
     * Note, that this implementation does not support shallow copy.
     *
     * @param destination
     * @param shallow
     * @throws DavException
     * @see DavResource#copy(DavResource, boolean)
     * @see Workspace#copy(String, String)
     * @see Workspace#copy(String, String, String)
     */
@Override
public void copy(DavResource destination, boolean shallow) throws DavException {
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    // TODO: support shallow and deep copy is required by RFC 2518
    if (shallow) {
        throw new DavException(DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy.");
    }
    try {
        String itemPath = getLocator().getRepositoryPath();
        String destItemPath = destination.getLocator().getRepositoryPath();
        Workspace workspace = getRepositorySession().getWorkspace();
        if (getLocator().isSameWorkspace(destination.getLocator())) {
            workspace.copy(itemPath, destItemPath);
        } else {
            log.error("Copy between workspaces is not yet implemented (src: '" + getHref() + "', dest: '" + destination.getHref() + "')");
            throw new DavException(DavServletResponse.SC_NOT_IMPLEMENTED);
        }
    } catch (PathNotFoundException e) {
        // according to RFC 2518, should not occur
        throw new DavException(DavServletResponse.SC_NOT_FOUND, e.getMessage());
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) Workspace(javax.jcr.Workspace)

Example 94 with Workspace

use of javax.jcr.Workspace in project jackrabbit by apache.

the class JcrRemotingServlet method clone.

private static String clone(Session session, String[] cloneArgs, DavResourceLocator reqLocator) throws RepositoryException {
    Workspace wsp = session.getWorkspace();
    String destPath = null;
    for (String cloneArg : cloneArgs) {
        String[] args = cloneArg.split(",");
        if (args.length == 4) {
            wsp.clone(args[0], args[1], args[2], Boolean.valueOf(args[3]));
            destPath = args[2];
        } else {
            throw new RepositoryException(":clone parameter must have a value consisting of the 4 args needed for a Workspace.clone() call.");
        }
    }
    return buildLocationHref(session, destPath, reqLocator);
}
Also used : RepositoryException(javax.jcr.RepositoryException) Workspace(javax.jcr.Workspace)

Example 95 with Workspace

use of javax.jcr.Workspace in project jackrabbit by apache.

the class ItemManagerImpl method dispose.

//--------------------------------------------------------< ItemManager >---
/**
     * @see ItemManager#dispose()
     */
public void dispose() {
    // stop listening
    Workspace wsp = session.getWorkspace();
    if (wsp instanceof WorkspaceImpl) {
        ((WorkspaceImpl) wsp).getItemStateFactory().removeCreationListener(this);
    }
    // ... and clear the cache.
    itemCache.clear();
}
Also used : Workspace(javax.jcr.Workspace)

Aggregations

Workspace (javax.jcr.Workspace)105 Node (javax.jcr.Node)51 Session (javax.jcr.Session)25 AccessDeniedException (javax.jcr.AccessDeniedException)18 Test (org.junit.Test)18 RepositoryException (javax.jcr.RepositoryException)16 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)16 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)10 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)8 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)7 File (java.io.File)6 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 ObservationManager (javax.jcr.observation.ObservationManager)6 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)6 FileOutputStream (java.io.FileOutputStream)5 NodeIterator (javax.jcr.NodeIterator)4 Version (javax.jcr.version.Version)4