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;
}
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);
}
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);
}
}
}
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");
}
}
}
}
}
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;
}
Aggregations