Search in sources :

Example 16 with Item

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

the class WatchedFolder method scan.

/**
     * Scan the contents of our folder and return the corresponding
     * <code>ScanResult</code> containing the <code>InstallableResource</code>s.
     */
public ScanResult scan() throws RepositoryException {
    logger.debug("Scanning {}", path);
    Node folder = null;
    if (session.itemExists(path)) {
        final Item i = session.getItem(path);
        if (i.isNode()) {
            folder = (Node) i;
        }
    }
    // Return an InstallableResource for all child nodes for which we have a NodeConverter
    final ScanResult result = new ScanResult();
    final Set<String> resourcesSeen = new HashSet<String>();
    if (folder != null) {
        scanNode(folder, result, resourcesSeen);
    }
    // unregistered from OsgiInstaller
    for (final String url : existingResourceUrls) {
        if (!resourcesSeen.contains(url)) {
            result.toRemove.add(url);
        }
    }
    for (final String u : result.toRemove) {
        existingResourceUrls.remove(u);
        digests.remove(u);
    }
    // Update saved digests of the resources that we're returning
    for (final InstallableResource r : result.toAdd) {
        existingResourceUrls.add(r.getId());
        digests.put(r.getId(), r.getDigest());
    }
    needsScan = false;
    return result;
}
Also used : Item(javax.jcr.Item) InstallableResource(org.apache.sling.installer.api.InstallableResource) Node(javax.jcr.Node) HashSet(java.util.HashSet)

Example 17 with Item

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

the class WebloaderJob method run.

@Override
public void run() {
    log.debug("Job thread starting: {}", this);
    // TODO should use a session provided by client, but can we use it
    // safely for our async job?
    session = null;
    if (storagePath.charAt(0) == '/') {
        storagePath = storagePath.substring(1);
    }
    final String absStoragePath = "/" + storagePath;
    try {
        session = repository.loginAdministrative(null);
        if (session.itemExists(absStoragePath)) {
            final Item i = session.getItem(absStoragePath);
            if (i.isNode()) {
                storageRoot = (Node) i;
            } else {
                throw new WebloaderException("Item at " + storagePath + " is not a Node");
            }
        } else {
            // TODO deep-create hierarchy if needed
            storageRoot = session.getRootNode().addNode(storagePath);
            session.save();
        }
        int offset = 0;
        for (String type : filetypes) {
            final URL[] urls = getDocumentUrlsFromGoogle(type, offset);
            for (URL url : urls) {
                try {
                    getAndStoreDocument(url);
                    session.save();
                    numDocsLoaded++;
                    if (numDocsLoaded >= maxDocsToRetrieve) {
                        break;
                    }
                } catch (DocTooBigException dtb) {
                    log.info(dtb.getMessage());
                } catch (Exception e) {
                    log.warn("Exception while retrieving url " + url, e);
                } finally {
                    session.refresh(false);
                }
            }
            offset += 10;
            if (numDocsLoaded >= maxDocsToRetrieve) {
                break;
            }
        }
        statusInfo = "All done.";
    } catch (Exception e) {
        error = e;
        log.warn("Exception in WebloaderJob.run()", e);
        statusInfo = "Exception while running job: " + e;
    } finally {
        if (session != null) {
            session.logout();
        }
        statusDetails = "";
        running = false;
    }
    if (numDocsLoaded >= maxDocsToRetrieve) {
        log.info("Stopped after retrieving maximum number of documents ({})", maxDocsToRetrieve);
    }
    log.info("Job thread ends: {}, {} documents loaded", this, numDocsLoaded);
}
Also used : Item(javax.jcr.Item) WebloaderException(org.apache.sling.samples.webloader.WebloaderException) URL(java.net.URL) RepositoryException(javax.jcr.RepositoryException) WebloaderException(org.apache.sling.samples.webloader.WebloaderException) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 18 with Item

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

the class ConfirmedOrdersObserver method run.

/** Meant to be called often (every second maybe) by the Sling scheduler */
public void run() {
    if (changedPropertyPaths.isEmpty()) {
        return;
    }
    final List<String> paths = new ArrayList<String>();
    final List<String> toRetry = new ArrayList<String>();
    synchronized (changedPropertyPaths) {
        paths.addAll(changedPropertyPaths);
        changedPropertyPaths.clear();
    }
    try {
        while (!paths.isEmpty()) {
            final String path = paths.remove(0);
            if (session.itemExists(path)) {
                final Item it = session.getItem(path);
                if (!it.isNode()) {
                    final Property p = (Property) it;
                    final Node n = p.getParent();
                    if (!n.hasProperty(SlingbucksConstants.LAST_MODIFIED_PROPERTY_NAME)) {
                        log.debug("Node {} doesn't have property {}, ignored", n.getPath(), SlingbucksConstants.LAST_MODIFIED_PROPERTY_NAME);
                    } else {
                        Calendar lastMod = n.getProperty(SlingbucksConstants.LAST_MODIFIED_PROPERTY_NAME).getDate();
                        if (System.currentTimeMillis() - lastMod.getTime().getTime() < WAIT_AFTER_LAST_CHANGE_MSEC) {
                            log.debug("Node {} modified more recently than {} msec, ignored", n.getPath(), WAIT_AFTER_LAST_CHANGE_MSEC);
                            toRetry.add(path);
                        } else {
                            final String targetPath = SlingbucksConstants.CONFIRMED_ORDERS_PATH + "/" + n.getName();
                            session.getWorkspace().move(n.getPath(), targetPath);
                            log.info("Confirmed order node {} moved to {}", n.getPath(), targetPath);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception in run()", e);
    } finally {
        // Re-add any paths that we didn't process
        synchronized (changedPropertyPaths) {
            changedPropertyPaths.addAll(paths);
            changedPropertyPaths.addAll(toRetry);
        }
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Property(javax.jcr.Property) RepositoryException(javax.jcr.RepositoryException)

Example 19 with Item

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

the class LinkProcessor method process.

public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
    Session session = request.getResourceResolver().adaptTo(Session.class);
    RequestParameter linkParam = request.getRequestParameter(":link");
    if (linkParam != null) {
        String linkPath = linkParam.getString();
        // check if a new node have been created
        if (changes.size() > 0 && changes.get(0).getType() == ModificationType.CREATE) {
            // hack to get the resource path
            // is it possible to add the response to the method header ?
            String resourcePath = changes.get(0).getSource();
            Node source = (Node) session.getItem(resourcePath);
            // create a symetric link
            if (session.itemExists(linkPath)) {
                Item targetItem = session.getItem(linkPath);
                if (targetItem.isNode()) {
                    linkHelper.createSymetricLink(source, (Node) targetItem, "link");
                }
            }
        }
    }
}
Also used : Item(javax.jcr.Item) RequestParameter(org.apache.sling.api.request.RequestParameter) Node(javax.jcr.Node) Session(javax.jcr.Session)

Example 20 with Item

use of javax.jcr.Item in project magnolia-vanity-url by aperto.

the class FolderUrlNameColumnFormatter method generateCell.

public Object generateCell(Table source, Object itemId, Object columnId) {
    String cell = "";
    Item jcrItem = getJcrItem(source, itemId);
    if ((jcrItem != null) && (jcrItem.isNode())) {
        Node node = (Node) jcrItem;
        try {
            if (isNodeType(node, NT_VANITY)) {
                cell = getString(node, PN_VANITY_URL, "");
            } else {
                cell = node.getName();
            }
        } catch (RepositoryException e) {
            LOGGER.warn("Unable to get the displayed value for the name column.", e);
        }
    }
    return cell;
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) PropertyUtil.getString(info.magnolia.jcr.util.PropertyUtil.getString)

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