Author: fmeschbe
Date: Mon Dec 3 05:23:51 2007
New Revision: 600523
URL: http://svn.apache.org/viewvc?rev=600523&view=rev
Log:
SLING-109 Support Resource adapter
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java?rev=600523&r1=600522&r2=600523&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/SyntheticResource.java
Mon Dec 3 05:23:51 2007
@@ -42,12 +42,6 @@
/** The metadat of this resource just containig the resource path */
private ResourceMetadata resourceMetadata;
- /** Optional raw data (JCR Item mostly) attached to this resource */
- private Object rawData;
-
- /** Optional object attached to this resource */
- private Object object;
-
/**
* Creates a synthetic content with the given path and component Id.
*
@@ -74,23 +68,8 @@
return resourceMetadata;
}
- public void setRawData(Object rawData) {
- this.rawData = rawData;
- }
-
- public Object getRawData() {
- return rawData;
- }
-
- public void setObject(Object object) {
- this.object = object;
- }
-
- public Object getObject() {
- return object;
- }
-
- public InputStream getInputStream() {
+ public <Type> Type adaptTo(Class<Type> type) {
return null;
}
+
}
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java?rev=600523&r1=600522&r2=600523&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
Mon Dec 3 05:23:51 2007
@@ -41,19 +41,14 @@
import javax.jcr.Session;
import org.apache.jackrabbit.net.URLFactory;
-import org.apache.sling.api.resource.NodeProvider;
-import org.apache.sling.api.resource.ObjectProvider;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
-import org.apache.sling.api.resource.StreamProvider;
-import org.apache.sling.api.resource.URLProvider;
import org.apache.sling.jcr.resource.internal.JcrResourceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** A Resource that wraps a JCR Node */
-public class JcrNodeResource implements Resource, NodeProvider, StreamProvider,
- ObjectProvider, URLProvider, Descendable {
+public class JcrNodeResource implements Resource, Descendable {
/** default log */
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -115,49 +110,60 @@
return metadata;
}
- JcrResourceManager getResourceManager() {
- return resourceManager;
+ @SuppressWarnings("unchecked")
+ public <Type> Type adaptTo(Class<Type> type) {
+ if (type == Node.class) {
+ return (Type) node; // unchecked cast
+ } else if (type == InputStream.class) {
+ return (Type) getInputStream(); // unchecked cast
+ } else if (type == URL.class) {
+ return (Type) getURL(); // unchecked cast
+ } else if (type.isInstance(getObject())) {
+ return (Type) getObject(); // unchecked cast
+ }
+
+ // fall back to nothing
+ return null;
}
public String toString() {
return "JcrNodeResource, type=" + resourceType + ", path=" + path;
}
- //---------- NodeProvider interface ---------------------------------------
+ JcrResourceManager getResourceManager() {
+ return resourceManager;
+ }
- public Node getNode() {
+ Node getNode() {
return node;
}
-
- //---------- StreamProvider interface -------------------------------------
+
+ // ---------- internal
-----------------------------------------------------
/**
* Returns a stream to the <em>jcr:content/jcr:data</em> property if the
* [EMAIL PROTECTED] #getRawData() raw data} is an <em>nt:file</em> node.
Otherwise
* returns <code>null</code>.
*/
- public InputStream getInputStream() throws IOException {
+ private InputStream getInputStream() {
// implement this for nt:file only
- if (getNode() == null) {
- return null;
- }
-
- try {
- if (node.isNodeType(NT_FILE) && node.hasProperty(FILE_DATA_PROP)) {
- return node.getProperty(FILE_DATA_PROP).getStream();
+ if (node != null) {
+ try {
+ if (node.isNodeType(NT_FILE)
+ && node.hasProperty(FILE_DATA_PROP)) {
+ return node.getProperty(FILE_DATA_PROP).getStream();
+ }
+ } catch (RepositoryException re) {
+ log.error("getInputStream: Cannot get InputStream for " + this,
+ re);
}
- } catch (RepositoryException re) {
- throw (IOException) new IOException("Cannot get InputStream for "
- + getURI()).initCause(re);
}
// fallback to non-streamable resource
return null;
}
- //---------- ObjectProvider interface
---------------------------------------
-
- public Object getObject() {
+ private Object getObject() {
if (object == UNDEFINED) {
// lazy loaded object
object = resourceManager.getObject(getURI(), objectType);
@@ -166,18 +172,17 @@
return object;
}
- //---------- URLProvider interface ----------------------------------------
-
- public URL getURL() throws MalformedURLException {
+ private URL getURL() {
try {
return URLFactory.createURL(node.getSession(), node.getPath());
- } catch (RepositoryException re) {
- throw (MalformedURLException) new MalformedURLException(
- "Cannot create URL for " + this).initCause(re);
+ } catch (Exception ex) {
+ log.error("getURL: Cannot create URL for " + this, ex);
}
+
+ return null;
}
- //---------- Descendable interface ----------------------------------------
+ // ---------- Descendable interface
----------------------------------------
public Iterator<Resource> listChildren() {
return new JcrNodeResourceIterator(this);
@@ -186,16 +191,16 @@
public Resource getDescendent(String relPath) {
try {
if (node.hasNode(relPath)) {
- return new JcrNodeResource(resourceManager,
node.getNode(relPath));
+ return new JcrNodeResource(resourceManager,
+ node.getNode(relPath));
}
- log.error("getResource: There is no node at {} below {}",
- path, getURI());
+ log.error("getResource: There is no node at {} below {}", path,
+ getURI());
return null;
} catch (RepositoryException re) {
- log.error(
- "getResource: Problem accessing relative resource at "
- + path, re);
+ log.error("getResource: Problem accessing relative resource at "
+ + path, re);
return null;
}
}
Modified:
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java?rev=600523&r1=600522&r2=600523&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java
Mon Dec 3 05:23:51 2007
@@ -37,7 +37,6 @@
import junit.framework.TestCase;
import org.apache.sling.api.SlingConstants;
-import org.apache.sling.api.resource.NodeProvider;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceManager;
@@ -110,9 +109,8 @@
assertEquals(rootNode.getPrimaryNodeType().getName(),
res.getResourceType());
- assertTrue(res instanceof NodeProvider);
- assertNotNull(((NodeProvider) res).getNode());
- assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
+ assertNotNull(res.adaptTo(Node.class));
+ assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
// missing resource
String path = root + "/missing";
@@ -128,9 +126,8 @@
assertEquals(rootNode.getPrimaryNodeType().getName(),
res.getResourceType());
- assertTrue(res instanceof NodeProvider);
- assertNotNull(((NodeProvider) res).getNode());
- assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
+ assertNotNull(res.adaptTo(Node.class));
+ assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
// missing resource below root should resolve root
String path = root + "/missing";
@@ -140,9 +137,8 @@
assertEquals(rootNode.getPrimaryNodeType().getName(),
res.getResourceType());
- assertTrue(res instanceof NodeProvider);
- assertNotNull(((NodeProvider) res).getNode());
- assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
+ assertNotNull(res.adaptTo(Node.class));
+ assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
// root with selectors/ext should resolve root
path = root + ".print.a4.html";
@@ -152,9 +148,8 @@
assertEquals(rootNode.getPrimaryNodeType().getName(),
res.getResourceType());
- assertTrue(res instanceof NodeProvider);
- assertNotNull(((NodeProvider) res).getNode());
- assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
+ assertNotNull(res.adaptTo(Node.class));
+ assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
// missing resource should return NON_EXISTING Resource
path = root + System.currentTimeMillis();