Search in sources :

Example 1 with Item

use of javax.jcr.Item in project jackrabbit-oak by apache.

the class ConcurrentTraversalTest method traverse.

protected void traverse(Session testSession, int cnt) throws RepositoryException {
    boolean logout = false;
    if (testSession == null) {
        testSession = getTestSession();
        logout = true;
    }
    try {
        int nodeCnt = 0;
        int propertyCnt = 0;
        int noAccess = 0;
        int size = allPaths.size();
        long start = System.currentTimeMillis();
        for (int i = 0; i < cnt; i++) {
            double rand = size * Math.random();
            int index = (int) Math.floor(rand);
            String path = allPaths.get(index);
            if (testSession.itemExists(path)) {
                Item item = testSession.getItem(path);
                Visitor visitor = new Visitor();
                item.accept(visitor);
                nodeCnt += visitor.nodeCnt;
                propertyCnt += visitor.propertyCnt;
            } else {
                noAccess++;
            }
        }
        long end = System.currentTimeMillis();
        if (doReport) {
            System.out.println("Session " + testSession.getUserID() + " reading " + (cnt - noAccess) + " (Nodes: " + nodeCnt + "; Properties: " + propertyCnt + ") completed in " + (end - start));
        }
    } finally {
        if (logout) {
            logout(testSession);
        }
    }
}
Also used : Item(javax.jcr.Item) TraversingItemVisitor(javax.jcr.util.TraversingItemVisitor)

Example 2 with Item

use of javax.jcr.Item in project jackrabbit-oak by apache.

the class RepositoryTest method sessionSave.

@Test
public void sessionSave() throws RepositoryException {
    Session session1 = createAdminSession();
    Session session2 = createAdminSession();
    try {
        // Add some items and ensure they are accessible through this session
        session1.getNode("/").addNode("node1");
        session1.getNode("/node1").addNode("node2");
        session1.getNode("/").addNode("node1/node3");
        Node node1 = session1.getNode("/node1");
        assertEquals("/node1", node1.getPath());
        Node node2 = session1.getNode("/node1/node2");
        assertEquals("/node1/node2", node2.getPath());
        Node node3 = session1.getNode("/node1/node3");
        assertEquals("/node1/node3", node3.getPath());
        node3.setProperty("property1", "value1");
        Item property1 = session1.getProperty("/node1/node3/property1");
        assertFalse(property1.isNode());
        assertEquals("value1", ((Property) property1).getString());
        // Make sure these items are not accessible through another session
        assertFalse(session2.itemExists("/node1"));
        assertFalse(session2.itemExists("/node1/node2"));
        assertFalse(session2.itemExists("/node1/node3"));
        assertFalse(session2.itemExists("/node1/node3/property1"));
        session1.save();
        // Make sure they are accessible through another session
        assertTrue(session2.itemExists("/node1"));
        assertTrue(session2.itemExists("/node1/node2"));
        assertTrue(session2.itemExists("/node1/node3"));
        assertTrue(session2.itemExists("/node1/node3/property1"));
        session2.refresh(false);
        // Make sure they are accessible through another session after refresh
        assertTrue(session2.itemExists("/node1"));
        assertTrue(session2.itemExists("/node1/node2"));
        assertTrue(session2.itemExists("/node1/node3"));
        assertTrue(session2.itemExists("/node1/node3/property1"));
    } finally {
        session1.logout();
        session2.logout();
    }
}
Also used : Item(javax.jcr.Item) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Session(javax.jcr.Session) Test(org.junit.Test)

Example 3 with Item

use of javax.jcr.Item in project sling by apache.

the class JcrItemResourceFactory method createResource.

/**
     * Creates a <code>Resource</code> instance for the item found at the
     * given path. If no item exists at that path or the item does not have
     * read-access for the session of this resolver, <code>null</code> is
     * returned.
     *
     * @param resourcePath The absolute path
     * @return The <code>Resource</code> for the item at the given path.
     * @throws RepositoryException If an error occurrs accessing checking the
     *             item in the repository.
     */
public JcrItemResource<?> createResource(final ResourceResolver resourceResolver, final String resourcePath, final Resource parent, final Map<String, String> parameters) throws RepositoryException {
    final String jcrPath = resourcePath;
    if (jcrPath == null) {
        log.debug("createResource: {} maps to an empty JCR path", resourcePath);
        return null;
    }
    final String version;
    if (parameters != null && parameters.containsKey("v")) {
        version = parameters.get("v");
    } else {
        version = null;
    }
    Node parentNode = null;
    String parentResourcePath = null;
    if (parent != null) {
        parentNode = parent.adaptTo(Node.class);
        parentResourcePath = parent.getPath();
    }
    Item item;
    if (parentNode != null && resourcePath.startsWith(parentResourcePath)) {
        String subPath = resourcePath.substring(parentResourcePath.length());
        if (!subPath.isEmpty() && subPath.charAt(0) == '/') {
            subPath = subPath.substring(1);
        }
        item = getSubitem(parentNode, subPath);
    } else {
        item = getItemOrNull(jcrPath);
    }
    if (item != null && version != null) {
        item = getHistoricItem(item, version);
    }
    if (item == null) {
        log.debug("createResource: No JCR Item exists at path '{}'", jcrPath);
        return null;
    } else {
        final JcrItemResource<?> resource;
        if (item.isNode()) {
            log.debug("createResource: Found JCR Node Resource at path '{}'", resourcePath);
            resource = new JcrNodeResource(resourceResolver, resourcePath, version, (Node) item, helper);
        } else {
            log.debug("createResource: Found JCR Property Resource at path '{}'", resourcePath);
            resource = new JcrPropertyResource(resourceResolver, resourcePath, version, (Property) item);
        }
        resource.getResourceMetadata().setParameterMap(parameters);
        return resource;
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) Property(javax.jcr.Property)

Example 4 with Item

use of javax.jcr.Item in project sling by apache.

the class JcrItemResourceFactoryTest method compareGetItemOrNull.

private void compareGetItemOrNull(String path, String expectedPath) throws RepositoryException {
    HelperData helper = new HelperData(new AtomicReference<DynamicClassLoaderManager>());
    Item item1 = new JcrItemResourceFactory(session, helper).getItemOrNull(path);
    Item item2 = new JcrItemResourceFactory(nonJackrabbitSession, helper).getItemOrNull(path);
    if (expectedPath == null) {
        assertNull(item1);
        assertNull(item2);
    } else {
        assertNotNull(item1);
        assertEquals(expectedPath, item1.getPath());
        assertNotNull(item2);
        assertEquals(expectedPath, item2.getPath());
    }
}
Also used : Item(javax.jcr.Item) HelperData(org.apache.sling.jcr.resource.internal.HelperData) DynamicClassLoaderManager(org.apache.sling.commons.classloader.DynamicClassLoaderManager)

Example 5 with Item

use of javax.jcr.Item in project sling by apache.

the class ScriptableItemMap method get.

@Override
public Object get(String name, Scriptable start) {
    // special provision for the "length" property to simulate an array
    if ("length".equals(name)) {
        return ScriptRuntime.toNumber(this.items.keySet().size() + "");
    }
    Item item = items.get(name);
    Object result = Undefined.instance;
    if (item != null) {
        result = ScriptRuntime.toObject(this, item);
    }
    return result;
}
Also used : Item(javax.jcr.Item) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Aggregations

Item (javax.jcr.Item)138 Node (javax.jcr.Node)61 RepositoryException (javax.jcr.RepositoryException)34 Session (javax.jcr.Session)26 Property (javax.jcr.Property)24 PathNotFoundException (javax.jcr.PathNotFoundException)20 ArrayList (java.util.ArrayList)7 JcrCallback (org.springframework.extensions.jcr.JcrCallback)7 PrintWriter (java.io.PrintWriter)6 ItemNotFoundException (javax.jcr.ItemNotFoundException)6 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 PropertyIterator (javax.jcr.PropertyIterator)5 ValueFormatException (javax.jcr.ValueFormatException)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)5 MetadataAccessException (com.thinkbiganalytics.metadata.api.MetadataAccessException)4 MetadataExecutionException (com.thinkbiganalytics.metadata.api.MetadataExecutionException)4 JcrPath (com.thinkbiganalytics.metadata.modeshape.support.JcrPath)4