You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2012/07/13 11:17:02 UTC
svn commit: r1361122 - in /sling/trunk/bundles:
api/src/main/java/org/apache/sling/api/resource/
jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/
jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/
jcr/resour...
Author: cziegeler
Date: Fri Jul 13 09:17:01 2012
New Revision: 1361122
URL: http://svn.apache.org/viewvc?rev=1361122&view=rev
Log:
SLING-2530 : Implement CRUD based on resources (WiP)
Modified:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ModifyingResourceProvider.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/Resource.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/MockResourceResolver.java
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceResolverContext.java
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java Fri Jul 13 09:17:01 2012
@@ -125,21 +125,22 @@ public abstract class AbstractResource
/**
* @see org.apache.sling.api.resource.Resource#remove()
*/
- public void remove() {
+ public void remove() throws PersistenceException {
this.getResourceResolver().delete(this);
}
/**
* @see org.apache.sling.api.resource.Resource#update(org.apache.sling.api.resource.ValueMap)
*/
- public void update(final ValueMap properties) {
+ public void update(final ValueMap properties) throws PersistenceException {
this.getResourceResolver().update(this, properties);
}
/**
* @see org.apache.sling.api.resource.Resource#addChild(java.lang.String, org.apache.sling.api.resource.ValueMap)
*/
- public Resource addChild(final String name, final ValueMap properties) {
+ public Resource addChild(final String name, final ValueMap properties)
+ throws PersistenceException {
return this.getResourceResolver().addChild(this, name, properties);
}
}
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ModifyingResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ModifyingResourceProvider.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ModifyingResourceProvider.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ModifyingResourceProvider.java Fri Jul 13 09:17:01 2012
@@ -37,13 +37,20 @@ package org.apache.sling.api.resource;
*/
public interface ModifyingResourceProvider {
- Resource create(ResourceResolver resolver, String path, ValueMap properties);
+ Resource create(ResourceResolver resolver, String path, ValueMap properties)
+ throws PersistenceException;
- boolean delete(ResourceResolver resolver, String path);
+ void delete(ResourceResolver resolver, String path)
+ throws PersistenceException;
- void update(ResourceResolver resolver, String path, ValueMap properties);
+ void update(ResourceResolver resolver, String path, ValueMap properties)
+ throws PersistenceException;
- void revert();
+ void revert()
+ throws PersistenceException;
- void commit();
+ void commit()
+ throws PersistenceException;
+
+ boolean hasChanges();
}
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/Resource.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/Resource.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/Resource.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/Resource.java Fri Jul 13 09:17:01 2012
@@ -148,19 +148,20 @@ public interface Resource extends Adapta
/**
* @since 2.2.0
- * @throws UnsupportedOperationException
+ * @throws UnsupportedOperationException, PersistenceException
*/
- void remove();
+ void remove() throws PersistenceException;
/**
* @since 2.2.0
- * @throws UnsupportedOperationException
+ * @throws UnsupportedOperationException, PersistenceException
*/
- void update(final ValueMap properties);
+ void update(final ValueMap properties) throws PersistenceException;
/**
* @since 2.2.0
- * @throws UnsupportedOperationException
+ * @throws UnsupportedOperationException, PersistenceException
*/
- Resource addChild(final String name, final ValueMap properties);
+ Resource addChild(final String name, final ValueMap properties)
+ throws PersistenceException;
}
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceResolver.java Fri Jul 13 09:17:01 2012
@@ -501,13 +501,51 @@ public interface ResourceResolver extend
*/
Object getAttribute(String name);
- boolean delete(Resource resource);
+ /**
+ * Delete the resource
+ * @param resource The resource to delete
+ *
+ * @throws PersistenceException, NullPointerException
+ */
+ void delete(Resource resource)
+ throws PersistenceException;
- Resource addChild(Resource parent, String name, ValueMap properties);
+ /**
+ * Add a child resource to the given parent resource
+ * @param parent The parent resource
+ * @param name The name of the child resource
+ * @param properties Optional properties for the resource
+ * @return The new resource
+ *
+ * @throws PersistenceException, NullPointerException
+ */
+ Resource addChild(Resource parent, String name, ValueMap properties)
+ throws PersistenceException;
- void update(Resource resource, ValueMap properties);
+ /**
+ * Update the resource with the new properties set.
+ * @param resource The resource
+ * @param properties The properties
+ *
+ * @throws PersistenceException, NullPointerException
+ */
+ void update(Resource resource, ValueMap properties)
+ throws PersistenceException;
- void revert();
+ /**
+ * Revert all pending changes.
+ */
+ void revert() throws PersistenceException;
- void commit();
+ /**
+ * Persist all pending changes.
+ *
+ * @throws PersistenceException
+ */
+ void commit() throws PersistenceException;
+
+ /**
+ * Are there any pending changes?
+ */
+ boolean hasChanges();
}
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java Fri Jul 13 09:17:01 2012
@@ -168,21 +168,22 @@ public class ResourceWrapper implements
/**
* @see org.apache.sling.api.resource.Resource#remove()
*/
- public void remove() {
+ public void remove() throws PersistenceException {
getResource().remove();
}
/**
* @see org.apache.sling.api.resource.Resource#update(org.apache.sling.api.resource.ValueMap)
*/
- public void update(final ValueMap properties) {
+ public void update(final ValueMap properties) throws PersistenceException {
getResource().update(properties);
}
/**
* @see org.apache.sling.api.resource.Resource#addChild(java.lang.String, org.apache.sling.api.resource.ValueMap)
*/
- public Resource addChild(final String name, final ValueMap properties) {
+ public Resource addChild(final String name, final ValueMap properties)
+ throws PersistenceException {
return getResource().addChild(name, properties);
}
}
Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java Fri Jul 13 09:17:01 2012
@@ -294,4 +294,10 @@ public class JcrNodeResource extends Jcr
}
}
+ /**
+ * @see org.apache.sling.api.resource.AbstractResource#isModifiable()
+ */
+ public boolean isModifiable() {
+ return true;
+ }
}
Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java Fri Jul 13 09:17:01 2012
@@ -42,6 +42,7 @@ import org.apache.sling.api.adapter.Slin
import org.apache.sling.api.resource.AttributableResourceProvider;
import org.apache.sling.api.resource.DynamicResourceProvider;
import org.apache.sling.api.resource.ModifyingResourceProvider;
+import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.QueriableResourceProvider;
import org.apache.sling.api.resource.QuerySyntaxException;
import org.apache.sling.api.resource.Resource;
@@ -380,7 +381,8 @@ public class JcrResourceProvider
/**
* @see org.apache.sling.api.resource.ModifyingResourceProvider#create(ResourceResolver, java.lang.String, org.apache.sling.api.resource.ValueMap)
*/
- public Resource create(final ResourceResolver resolver, final String path, final ValueMap properties) {
+ public Resource create(final ResourceResolver resolver, final String path, final ValueMap properties)
+ throws PersistenceException {
// check for node type
final String nodeType = (properties != null ? properties.get("jcr:primaryType", String.class) : null);
try {
@@ -399,24 +401,23 @@ public class JcrResourceProvider
return new JcrNodeResource(resolver, node, this.dynamicClassLoader);
} catch (final RepositoryException e) {
- log.error("Unable to create node at " + path, e);
+ throw new PersistenceException("Unable to create node at " + path, e);
}
- return null;
}
/**
* @see org.apache.sling.api.resource.ModifyingResourceProvider#delete(ResourceResolver, java.lang.String)
*/
- public boolean delete(final ResourceResolver resolver, final String path) {
+ public void delete(final ResourceResolver resolver, final String path)
+ throws PersistenceException {
try {
if ( session.itemExists(path) ) {
session.getItem(path).remove();
- return true;
}
+ throw new PersistenceException("Unable to delete item at " + path);
} catch (final RepositoryException e) {
- log.error("Unable to delete item at " + path, e);
+ throw new PersistenceException("Unable to delete item at " + path, e);
}
- return false;
}
private void update(final Node node, final ValueMap properties) throws RepositoryException {
@@ -430,36 +431,49 @@ public class JcrResourceProvider
/**
* @see org.apache.sling.api.resource.ModifyingResourceProvider#update(org.apache.sling.api.resource.ResourceResolver, java.lang.String, org.apache.sling.api.resource.ValueMap)
*/
- public void update(final ResourceResolver resolver, final String path, final ValueMap properties) {
+ public void update(final ResourceResolver resolver, final String path, final ValueMap properties)
+ throws PersistenceException {
try {
final Node node = this.session.getNode(path);
this.update(node, properties);
} catch (final RepositoryException e) {
- log.error("Unable to update node at " + path, e);
+ throw new PersistenceException("Unable to update node at " + path, e);
}
}
/**
* @see org.apache.sling.api.resource.ModifyingResourceProvider#revert()
*/
- public void revert() {
+ public void revert() throws PersistenceException {
try {
this.session.refresh(false);
} catch (final RepositoryException e) {
- log.error("Unable to refresh session.", e);
+ throw new PersistenceException("Unable to refresh session.", e);
}
}
/**
* @see org.apache.sling.api.resource.ModifyingResourceProvider#commit()
*/
- public void commit() {
+ public void commit() throws PersistenceException {
try {
this.session.save();
} catch (final RepositoryException e) {
- log.error("Unable to commit changes to session.", e);
+ throw new PersistenceException("Unable to commit changes to session.", e);
}
}
+
+ /**
+ * @see org.apache.sling.api.resource.ModifyingResourceProvider#hasChanges()
+ */
+ public boolean hasChanges() {
+ try {
+ return this.session.hasPendingChanges();
+ } catch (final RepositoryException ignore) {
+ log.warn("Unable to check session for pending changes.", ignore);
+ }
+ return false;
+ }
}
Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java (original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java Fri Jul 13 09:17:01 2012
@@ -203,9 +203,8 @@ public class JcrResourceListenerTest ext
return null;
}
- public boolean delete(Resource resource) {
+ public void delete(Resource resource) {
// TODO Auto-generated method stub
- return false;
}
public Resource addChild(Resource parent, String name, ValueMap properties) {
@@ -227,6 +226,11 @@ public class JcrResourceListenerTest ext
// TODO Auto-generated method stub
}
+
+ public boolean hasChanges() {
+ // TODO Auto-generated method stub
+ return false;
+ }
};
final ResourceResolverFactory factory = new ResourceResolverFactory() {
Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/MockResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/MockResourceResolver.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/MockResourceResolver.java (original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/MockResourceResolver.java Fri Jul 13 09:17:01 2012
@@ -127,9 +127,8 @@ public class MockResourceResolver implem
return Collections.<String> emptyList().iterator();
}
- public boolean delete(Resource resource) {
+ public void delete(Resource resource) {
// TODO Auto-generated method stub
- return false;
}
public Resource addChild(Resource parent, String name, ValueMap properties) {
@@ -151,4 +150,9 @@ public class MockResourceResolver implem
// TODO Auto-generated method stub
}
+
+ public boolean hasChanges() {
+ // TODO Auto-generated method stub
+ return false;
+ }
}
Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java Fri Jul 13 09:17:01 2012
@@ -39,6 +39,7 @@ import org.apache.sling.api.adapter.Slin
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifyingResourceProvider;
import org.apache.sling.api.resource.NonExistingResource;
+import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceNotFoundException;
import org.apache.sling.api.resource.ResourceResolver;
@@ -999,18 +1000,25 @@ public class ResourceResolverImpl extend
/**
* @see org.apache.sling.api.resource.ResourceResolver#delete(org.apache.sling.api.resource.Resource)
*/
- public boolean delete(final Resource resource) {
+ public void delete(final Resource resource)
+ throws PersistenceException {
+ // if resource is null, we get an NPE as stated in the API
final String path = resource.getPath();
final ModifyingResourceProvider mrp = this.factory.getRootProviderEntry().getModifyingProvider(this.context,
this,
path);
- return mrp.delete(this, path);
+ mrp.delete(this, path);
}
/**
* @see org.apache.sling.api.resource.ResourceResolver#addChild(org.apache.sling.api.resource.Resource, java.lang.String, org.apache.sling.api.resource.ValueMap)
*/
- public Resource addChild(final Resource parent, final String name, final ValueMap properties) {
+ public Resource addChild(final Resource parent, final String name, final ValueMap properties)
+ throws PersistenceException {
+ // if parent or name is null, we get an NPE as stated in the API
+ if ( name == null ) {
+ throw new NullPointerException("name");
+ }
final String path = parent.getPath() + '/' + name;
final ModifyingResourceProvider mrp = this.factory.getRootProviderEntry().getModifyingProvider(this.context,
this,
@@ -1021,7 +1029,9 @@ public class ResourceResolverImpl extend
/**
* @see org.apache.sling.api.resource.ResourceResolver#update(org.apache.sling.api.resource.Resource, org.apache.sling.api.resource.ValueMap)
*/
- public void update(final Resource resource, final ValueMap properties) {
+ public void update(final Resource resource, final ValueMap properties)
+ throws PersistenceException {
+ // if resource is null, we get an NPE as stated in the API
final String path = resource.getPath();
final ModifyingResourceProvider mrp = this.factory.getRootProviderEntry().getModifyingProvider(this.context,
this,
@@ -1032,14 +1042,21 @@ public class ResourceResolverImpl extend
/**
* @see org.apache.sling.api.resource.ResourceResolver#revert()
*/
- public void revert() {
+ public void revert() throws PersistenceException {
this.context.revert();
}
/**
* @see org.apache.sling.api.resource.ResourceResolver#commit()
*/
- public void commit() {
+ public void commit() throws PersistenceException {
this.context.commit();
}
+
+ /**
+ * @see org.apache.sling.api.resource.ResourceResolver#hasChanges()
+ */
+ public boolean hasChanges() {
+ return this.context.hasChanges();
+ }
}
Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceResolverContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceResolverContext.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceResolverContext.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceResolverContext.java Fri Jul 13 09:17:01 2012
@@ -24,6 +24,7 @@ import java.util.Set;
import org.apache.sling.api.resource.DynamicResourceProvider;
import org.apache.sling.api.resource.ModifyingResourceProvider;
+import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceProvider;
/**
@@ -124,7 +125,7 @@ public class ResourceResolverContext {
/**
* Revert all transient changes.
*/
- public void revert() {
+ public void revert() throws PersistenceException {
for(final ModifyingResourceProvider provider : this.modifyingProviders) {
provider.revert();
}
@@ -133,9 +134,21 @@ public class ResourceResolverContext {
/**
* Commit all transient changes
*/
- public void commit() {
+ public void commit() throws PersistenceException {
for(final ModifyingResourceProvider provider : this.modifyingProviders) {
provider.commit();
}
}
+
+ /**
+ * Do we have changes?
+ */
+ public boolean hasChanges() {
+ for(final ModifyingResourceProvider provider : this.modifyingProviders) {
+ if ( provider.hasChanges() ) {
+ return true;
+ }
+ }
+ return false;
+ }
}
Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java?rev=1361122&r1=1361121&r2=1361122&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java (original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java Fri Jul 13 09:17:01 2012
@@ -300,9 +300,8 @@ public class ResourceProviderEntryTest {
return Collections.<String> emptyList().iterator();
}
- public boolean delete(Resource resource) {
+ public void delete(Resource resource) {
// TODO Auto-generated method stub
- return false;
}
public Resource addChild(Resource parent, String name, ValueMap properties) {
@@ -323,6 +322,11 @@ public class ResourceProviderEntryTest {
// TODO Auto-generated method stub
}
+
+ public boolean hasChanges() {
+ // TODO Auto-generated method stub
+ return false;
+ }
}
private static class TestResource extends AbstractResource {