Search in sources :

Example 56 with Property

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

the class NodeImpl method getProperty.

/**
     * @see Node#getProperty(String)
     */
public Property getProperty(String relPath) throws PathNotFoundException, RepositoryException {
    checkStatus();
    PropertyEntry entry = resolveRelativePropertyPath(relPath);
    if (entry == null) {
        throw new PathNotFoundException(relPath);
    }
    try {
        return (Property) getItemManager().getItem(entry);
    } catch (AccessDeniedException e) {
        throw new PathNotFoundException(relPath);
    } catch (ItemNotFoundException e) {
        throw new PathNotFoundException(relPath);
    }
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) PropertyEntry(org.apache.jackrabbit.jcr2spi.hierarchy.PropertyEntry) PathNotFoundException(javax.jcr.PathNotFoundException) Property(javax.jcr.Property) AddProperty(org.apache.jackrabbit.jcr2spi.operation.AddProperty) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 57 with Property

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

the class NodeImpl method getWeakReferences.

/**
     * {@inheritDoc}
     */
public PropertyIterator getWeakReferences(String name) throws RepositoryException {
    if (name == null) {
        return getWeakReferences();
    }
    // check state of this instance
    sanityCheck();
    // shortcut if node isn't referenceable
    if (!isNodeType(NameConstants.MIX_REFERENCEABLE)) {
        return PropertyIteratorAdapter.EMPTY;
    }
    try {
        StringBuilder stmt = new StringBuilder();
        stmt.append("//*[@").append(ISO9075.encode(name));
        stmt.append(" = '").append(data.getId()).append("']");
        Query q = getSession().getWorkspace().getQueryManager().createQuery(stmt.toString(), Query.XPATH);
        QueryResult result = q.execute();
        ArrayList<Property> l = new ArrayList<Property>();
        for (NodeIterator nit = result.getNodes(); nit.hasNext(); ) {
            Node n = nit.nextNode();
            l.add(n.getProperty(name));
        }
        if (l.isEmpty()) {
            return PropertyIteratorAdapter.EMPTY;
        } else {
            return new PropertyIteratorAdapter(l);
        }
    } catch (RepositoryException e) {
        String msg = "Unable to retrieve WEAKREFERENCE properties that refer to " + id;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) PropertyIteratorAdapter(org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 58 with Property

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

the class LockTest method testSequence.

/**
     * Tests the utility {@link org.apache.jackrabbit.util.Locked} by
     * implementing a persistent counter.
     */
public void testSequence() throws RepositoryException {
    final Node counter = testRootNode.addNode(nodeName1);
    counter.setProperty("value", 0);
    counter.addMixin(mixLockable);
    superuser.save();
    final List<Thread> worker = new ArrayList<Thread>();
    for (int i = 0; i < NUM_THREADS; i++) {
        worker.add(new Thread() {

            private final int threadNumber = worker.size();

            public void run() {
                final Session s;
                try {
                    s = getHelper().getSuperuserSession();
                } catch (RepositoryException e) {
                    fail(e.getMessage());
                    return;
                }
                try {
                    for (int i = 0; i < NUM_VALUE_GETS; i++) {
                        Node n = (Node) s.getItem(counter.getPath());
                        long currentValue = ((Long) new Locked() {

                            protected Object run(Node n) throws RepositoryException {
                                Property seqProp = n.getProperty("value");
                                long value = seqProp.getLong();
                                seqProp.setValue(++value);
                                s.save();
                                return new Long(value);
                            }
                        }.with(n, false)).longValue();
                        log.println("Thread" + threadNumber + ": got sequence number: " + currentValue);
                        // do a random wait
                        Thread.sleep(new Random().nextInt(100));
                    }
                } catch (RepositoryException e) {
                    log.println("exception while running code with lock:" + e.getMessage());
                } catch (InterruptedException e) {
                    log.println(Thread.currentThread() + " interrupted while waiting for lock");
                } finally {
                    s.logout();
                }
            }
        });
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        it.next().start();
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        try {
            it.next().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Locked(org.apache.jackrabbit.util.Locked) Random(java.util.Random) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 59 with Property

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

the class LockedWrapperTest method buildNewSequenceUpdateCallable.

public Callable<Long> buildNewSequenceUpdateCallable(final String counterPath, final boolean withSessionRefresh) {
    return new Callable<Long>() {

        public Long call() throws RepositoryException, InterruptedException {
            final Session s = getHelper().getSuperuserSession();
            try {
                if (withSessionRefresh) {
                    s.refresh(false);
                }
                Node n = s.getNode(counterPath);
                long value = new LockedWrapper<Long>() {

                    @Override
                    protected Long run(Node node) throws RepositoryException {
                        Property seqProp = node.getProperty("value");
                        long value = seqProp.getLong();
                        seqProp.setValue(++value);
                        s.save();
                        return value;
                    }
                }.with(n, false);
                // check that the sequence is ok
                assertEquals(counter.getAndIncrement(), value);
                // do a random wait
                Thread.sleep(random.nextInt(100));
                return value;
            } finally {
                s.logout();
            }
        }
    };
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property) Callable(java.util.concurrent.Callable) Session(javax.jcr.Session)

Example 60 with Property

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

the class LockTest method testSequenceWithTimeout.

/**
     * Tests the utility {@link org.apache.jackrabbit.util.Locked} by
     * implementing a persistent counter with a timeout when the next value of
     * the counter is retrieved. The number of values that can be retrieved by
     * this test depends on system performance and the configured persistence
     * manager.
     */
public void testSequenceWithTimeout() throws RepositoryException {
    final Node counter = testRootNode.addNode(nodeName1);
    counter.setProperty("value", 0);
    counter.addMixin(mixLockable);
    superuser.save();
    final List<Thread> worker = new ArrayList<Thread>();
    for (int i = 0; i < NUM_THREADS; i++) {
        worker.add(new Thread() {

            private final int threadNumber = worker.size();

            public void run() {
                final Session s;
                try {
                    s = getHelper().getSuperuserSession();
                } catch (RepositoryException e) {
                    fail(e.getMessage());
                    return;
                }
                try {
                    for (int i = 0; i < NUM_VALUE_GETS; i++) {
                        Node n = (Node) s.getItem(counter.getPath());
                        Object ret = new Locked() {

                            protected Object run(Node n) throws RepositoryException {
                                Property seqProp = n.getProperty("value");
                                long value = seqProp.getLong();
                                seqProp.setValue(++value);
                                s.save();
                                return new Long(value);
                            }
                        }.with(n, false, // expect a value after
                        10 * 1000);
                        // ten seconds
                        if (ret == Locked.TIMED_OUT) {
                            log.println("Thread" + threadNumber + ": could not get a sequence number within 10 seconds");
                        } else {
                            long currentValue = ((Long) ret).longValue();
                            log.println("Thread" + threadNumber + ": got sequence number: " + currentValue);
                        }
                        // do a random wait
                        Thread.sleep(new Random().nextInt(100));
                    }
                } catch (RepositoryException e) {
                    log.println("exception while running code with lock:" + e.getMessage());
                } catch (InterruptedException e) {
                    log.println(Thread.currentThread() + " interrupted while waiting for lock");
                } finally {
                    s.logout();
                }
            }
        });
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        it.next().start();
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        try {
            it.next().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Locked(org.apache.jackrabbit.util.Locked) Random(java.util.Random) Property(javax.jcr.Property) Session(javax.jcr.Session)

Aggregations

Property (javax.jcr.Property)445 Node (javax.jcr.Node)252 Value (javax.jcr.Value)99 Test (org.junit.Test)87 Session (javax.jcr.Session)78 PropertyIterator (javax.jcr.PropertyIterator)68 RepositoryException (javax.jcr.RepositoryException)64 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)47 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)37 ValueFormatException (javax.jcr.ValueFormatException)34 QValue (org.apache.jackrabbit.spi.QValue)29 ArrayList (java.util.ArrayList)24 NodeIterator (javax.jcr.NodeIterator)24 QValueValue (org.apache.jackrabbit.spi.commons.value.QValueValue)23 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)20 Item (javax.jcr.Item)19 PathNotFoundException (javax.jcr.PathNotFoundException)17 InvalidItemStateException (javax.jcr.InvalidItemStateException)16 Version (javax.jcr.version.Version)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15