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