Author: fmeschbe
Date: Wed Jul 28 18:41:55 2010
New Revision: 980150
URL: http://svn.apache.org/viewvc?rev=980150&view=rev
Log:
SLING-1193 Add useful methods to the Resource interface
- Add AbstractResource abstract class with implementations of new methods
- Adapt ResourceUtil, SyntheticResource and ResourceWrapper
- Move AdapterManager field from SyntheticResource to AbstractResource
- Adapt JCR Resource support to new API and test cases
- Fix Testcase in Engine bundle to extend MockResource from AbstractResource
Added:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
(with props)
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/ResourceResolutionPlugin.java
(with props)
Modified:
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/ResourceUtil.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/SyntheticResource.java
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/SlingRequestPathInfoTest.java
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.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/JcrPropertyResource.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/helper/ResourceProviderEntryTest.java
Added:
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=980150&view=auto
==============================================================================
---
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
(added)
+++
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
Wed Jul 28 18:41:55 2010
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.resource;
+
+import java.util.Iterator;
+
+import org.apache.sling.api.adapter.AdapterManager;
+
+/**
+ * The <code>AbstractResource</code> is an abstract implementation of the
+ * {...@link Resource} interface.
+ * <p>
+ * Implementations of the {...@link Resource} interface are strongly
encouraged to
+ * either extend from this class or the {...@link ResourceWrapper} class
instead of
+ * implementing the {...@link Resource} from the ground up. This will ensure to
+ * always be able to support new methods that might be introduced in the
+ * {...@link Resource} interface in the future.
+ *
+ * @since 2.1.0
+ */
+public abstract class AbstractResource implements Resource {
+
+ /** The adapter manager used for adapting the synthetic resource. */
+ private static volatile AdapterManager ADAPTER_MANAGER;
+
+ /**
+ * Set the adapter manager to be used by a synthetic resource. A bundle
+ * implementing the adapter manager can set the manager through this
method.
+ * The set adapter manager will be used in the {...@link #adaptTo(Class)}
+ * method of a synthetic resource.
+ *
+ * @param adapterMgr The adapter manager.
+ */
+ public static void setAdapterManager(final AdapterManager adapterMgr) {
+ ADAPTER_MANAGER = adapterMgr;
+ }
+
+ /**
+ * Unset an adapter manager previously set with
+ * {...@link #setAdapterManager(AdapterManager)}. If this method is called
with
+ * an <code>AdapterManager</code> different from the currently set one it
+ * has no effect.
+ *
+ * @param adapterMgr The adapter manager
+ */
+ public static void unsetAdapterManager(final AdapterManager adapterMgr) {
+ if (ADAPTER_MANAGER == adapterMgr) {
+ ADAPTER_MANAGER = null;
+ }
+ }
+
+ /**
+ * Returns the name of this resource.
+ * <p>
+ * This method is implemented as a pure string operation by calling the
+ * {...@link ResourceUtil#getName(String)} method with the path of this
+ * resource.
+ */
+ public String getName() {
+ return ResourceUtil.getName(getPath());
+ }
+
+ /**
+ * Returns the parent resource of this resource.
+ * <p>
+ * This method is implemented by getting the parent resource path first
+ * calling the {...@link ResourceUtil#getParent(String)} method and then to
+ * retrieve that resource from the resource resolver.
+ */
+ public Resource getParent() {
+ final String parentPath = ResourceUtil.getParent(getPath());
+ if (parentPath == null) {
+ return null;
+ }
+ return getResourceResolver().getResource(parentPath);
+ }
+
+ /**
+ * Returns the indicated child of this resource.
+ * <p>
+ * This method is implemented calling the
+ * {...@link ResourceResolver#getResource(Resource, String)} method. As
such
+ * the <code>relPath</code> argument may even be an absolute path or a path
+ * containing relative path segments <code>.</code> (current resource) and
+ * <code>..</code> (parent resource).
+ * <p>
+ * Implementations should not generally overwrite this method without
+ * calling this base class implementation.
+ */
+ public Resource getChild(String relPath) {
+ return getResourceResolver().getResource(this, relPath);
+ }
+
+ /**
+ * Returns an iterator on the direct child resources.
+ * <p>
+ * This method is implemented calling the
+ * {...@link ResourceResolver#listChildren(Resource)} method.
+ * <p>
+ * Implementations should not generally overwrite this method without
+ * calling this base class implementation.
+ */
+ public Iterator<Resource> listChildren() {
+ return getResourceResolver().listChildren(this);
+ }
+
+ /**
+ * Returns <code>true</code> if this resource is of the given resource type
+ * or if any of the super resource types equals the given resource type.
+ * <p>
+ * This method is implemented by first checking the resource type then
+ * walking up the resource super type chain using the
+ * {...@link ResourceUtil#findResourceSuperType(Resource)} and
+ * {...@link ResourceUtil#getResourceSuperType(ResourceResolver, String)}
+ * methods.
+ */
+ public boolean isResourceType(String resourceType) {
+
+ if (resourceType == null) {
+ return false;
+ }
+
+ if (resourceType.equals(getResourceType())) {
+ return true;
+ }
+
+ String superType = ResourceUtil.findResourceSuperType(this);
+ while (superType != null) {
+ if (resourceType.equals(superType)) {
+ return true;
+ }
+ superType = ResourceUtil.getResourceSuperType(
+ getResourceResolver(), superType);
+ }
+
+ return false;
+ }
+
+ /**
+ * If a adapter manager has been set through
+ * {...@link #setAdapterManager(AdapterManager)} this adapter manager is
used
+ * to adapt the resource to the given type. Otherwise this method returns
+ * <code>null</code>.
+ * <p>
+ * This default base implementation is intended to be overwritten by
+ * extensions. Overwriting implementations are are encouraged to call this
+ * base class implementation if they themselves cannot adapt to the
+ * requested type.
+ */
+ public <Type> Type adaptTo(Class<Type> type) {
+ final AdapterManager adapterMgr = ADAPTER_MANAGER;
+ if (adapterMgr != null) {
+ return adapterMgr.getAdapter(this, type);
+ }
+ return null;
+ }
+
+}
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResource.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev Url
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=980150&r1=980149&r2=980150&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
Wed Jul 28 18:41:55 2010
@@ -16,6 +16,8 @@
*/
package org.apache.sling.api.resource;
+import java.util.Iterator;
+
import org.apache.sling.api.adapter.Adaptable;
/**
@@ -24,6 +26,12 @@ import org.apache.sling.api.adapter.Adap
* The <code>Resource</code> is also an {...@link Adaptable} to get adapters to
* other types. A JCR based resource might support adapting to the JCR Node on
* which the resource is based.
+ * <p>
+ * Implementor's Note: It is recommended to not implement this interface
+ * directly. Rather consider either extending from {...@link AbstractResource}
or
+ * {...@link ResourceWrapper}. This will make sure your implementation will
not be
+ * suffering from missing method problems should the Sling Resource API be
+ * extended in the future.
*/
public interface Resource extends Adaptable {
@@ -34,17 +42,57 @@ public interface Resource extends Adapta
* actually be resolved.
*
* @see #getResourceType()
- * @see ResourceResolver#resolve(javax.servlet.http.HttpServletRequest,
String)
+ * @see ResourceUtil#isNonExistingResource(Resource)
+ * @see ResourceResolver#resolve(javax.servlet.http.HttpServletRequest,
+ * String)
*/
String RESOURCE_TYPE_NON_EXISTING = "sling:nonexisting";
/**
- * This resource's path - for now that could be a JCR path. It's also
- * possible to have an URI for other data sources.
+ * Returns the absolute path of this resource in the resource tree.
*/
String getPath();
/**
+ * Returns the name of this resource. The name of a resource is the last
+ * segment of the {...@link #getPath() path}.
+ *
+ * @since 2.1.0
+ */
+ String getName();
+
+ /**
+ * Returns the parent resource or <code>null</code> if this resource
+ * represents the root of the resource tree.
+ *
+ * @since 2.1.0
+ */
+ Resource getParent();
+
+ /**
+ * Returns an iterator of the direct children of this resource.
+ * <p>
+ * This method is a convenience and returns exactly the same resources as
+ * calling <code>getResourceResolver().listChildren(resource)</code>.
+ *
+ * @since 2.1.0
+ * @see ResourceResolver#listChildren(Resource)
+ */
+ Iterator<Resource> listChildren();
+
+ /**
+ * Returns the child at the given relative path of this resource or
+ * <code>null</code> if no such child exists.
+ * <p>
+ * This method is a convenience and returns exactly the same resources as
+ * calling <code>getResourceResolver().getResource(resource,
relPath)</code>.
+ *
+ * @since 2.1.0
+ * @see ResourceResolver#getResource(Resource, String)
+ */
+ Resource getChild(String relPath);
+
+ /**
* The resource type is meant to point to rendering/processing scripts,
* editing dialogs, etc. It is usually a path in the repository, where
* scripts and other tools definitions are found, but the
@@ -53,7 +101,7 @@ public interface Resource extends Adapta
* created.
* <p>
* If the resource instance represents a resource which is not actually
- * existing, this method returns the {...@link
#RESOURCE_TYPE_NON_EXISTING}.
+ * existing, this method returns {...@link #RESOURCE_TYPE_NON_EXISTING}.
*/
String getResourceType();
@@ -64,6 +112,19 @@ public interface Resource extends Adapta
String getResourceSuperType();
/**
+ * Returns <code>true</code> if the resource type or any of the resource's
+ * super type(s) equals the given resource type.
+ *
+ * @param resourceType The resource type to check this resource against.
+ * @return <code>true</code> if the resource type or any of the resource's
+ * super type(s) equals the given resource type. <code>false</code>
+ * is also returned if <code>resourceType</code> is
+ * <code>null</code>.
+ * @since 2.1.0
+ */
+ boolean isResourceType(String resourceType);
+
+ /**
* Returns the metadata of this resource. The concrete data contained in
the
* {...@link ResourceMetadata} object returned is implementation specific
* except for the {...@link ResourceMetadata#RESOLUTION_PATH} property
which is
@@ -79,5 +140,4 @@ public interface Resource extends Adapta
* retrieved.
*/
ResourceResolver getResourceResolver();
-
}
Modified:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java?rev=980150&r1=980149&r2=980150&view=diff
==============================================================================
---
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
(original)
+++
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
Wed Jul 28 18:41:55 2010
@@ -25,10 +25,9 @@ import java.util.NoSuchElementException;
import org.apache.sling.api.wrappers.ValueMapDecorator;
-
/**
- * The <code>ResourceUtil</code> class provides helper methods dealing
- * with resources.
+ * The <code>ResourceUtil</code> class provides helper methods dealing with
+ * resources.
*/
public class ResourceUtil {
@@ -114,9 +113,9 @@ public class ResourceUtil {
* parent.
*
* @param path The path whose parent is to be returned.
- * @return <code>null</code> if <code>path</code> is the root path
(<code>/</code>)
- * or if <code>path</code> is a single name containing no slash
(<code>/</code>)
- * characters.
+ * @return <code>null</code> if <code>path</code> is the root path (
+ * <code>/</code>) or if <code>path</code> is a single name
+ * containing no slash (<code>/</code>) characters.
* @throws IllegalArgumentException If the path cannot be normalized by the
* {...@link #normalize(String)} method.
* @throws NullPointerException If <code>path</code> is <code>null</code>.
@@ -149,35 +148,34 @@ public class ResourceUtil {
/**
* Utility method returns the parent resource of the resource.
+ *
* @throws NullPointerException If <code>rsrc</code> is <code>null</code>.
* @return The parent resource or null if the rsrc is the root.
+ * @deprecated since 2.1.0, use {...@link Resource#getParent()} instead
*/
public static Resource getParent(Resource rsrc) {
- final String parentPath = getParent(rsrc.getPath());
- if ( parentPath == null ) {
- return null;
- }
- return rsrc.getResourceResolver().getResource(parentPath);
+ return rsrc.getParent();
}
/**
- * Utility method returns the parent resource of the resource.
+ * Utility method returns the name of the resource.
+ *
* @throws NullPointerException If <code>rsrc</code> is <code>null</code>.
+ * @deprecated since 2.1.0, use {...@link Resource#getName()} instead
*/
public static String getName(Resource rsrc) {
- final String name = getName(rsrc.getPath());
- return name;
+ return rsrc.getName();
}
/**
- * Utility method returns the name of the given <code>path</code>, which
- * is normalized by {...@link #normalize(String)} before resolving the
name.
+ * Utility method returns the name of the given <code>path</code>, which is
+ * normalized by {...@link #normalize(String)} before resolving the name.
*
* @param path The path whose name (the last path element) is to be
* returned.
- * @return The empty string if <code>path</code> is the root path
(<code>/</code>)
- * or if <code>path</code> is a single name containing no slash
(<code>/</code>)
- * characters.
+ * @return The empty string if <code>path</code> is the root path (
+ * <code>/</code>) or if <code>path</code> is a single name
+ * containing no slash (<code>/</code>) characters.
* @throws IllegalArgumentException If the path cannot be normalized by the
* {...@link #normalize(String)} method.
* @throws NullPointerException If <code>path</code> is <code>null</code>.
@@ -198,26 +196,33 @@ public class ResourceUtil {
}
/**
- * Returns <code>true</code> if the resource <code>res</code> is a
- * synthetic resource.
+ * Returns <code>true</code> if the resource <code>res</code> is a
synthetic
+ * resource.
* <p>
* This method checks whether the resource is an instance of the
* <code>org.apache.sling.resource.SyntheticResource</code> class.
*
* @param res The <code>Resource</code> to check whether it is a synthetic
* resource.
- * @return <code>true</code> if <code>res</code> is a synthetic
- * resource. <code>false</code> is returned if <code>res</code>
- * is <code>null</code> or not an instance of the
+ * @return <code>true</code> if <code>res</code> is a synthetic resource.
+ * <code>false</code> is returned if <code>res</code> is
+ * <code>null</code> or not an instance of the
* <code>org.apache.sling.resource.SyntheticResource</code> class.
*/
public static boolean isSyntheticResource(Resource res) {
- if(res instanceof SyntheticResource) {
- return true;
- } else if(res instanceof ResourceWrapper) {
- return ((ResourceWrapper)res).getResource() instanceof
SyntheticResource;
+ if (res instanceof SyntheticResource) {
+ return true;
}
- return false;
+
+ if (!(res instanceof ResourceWrapper)) {
+ return false;
+ }
+
+ do {
+ res = ((ResourceWrapper) res).getResource();
+ } while (res instanceof ResourceWrapper);
+
+ return res instanceof SyntheticResource;
}
/**
@@ -234,8 +239,8 @@ public class ResourceUtil {
*
* @param res The <code>Resource</code> to check whether it is a star
* resource.
- * @return <code>true</code> if <code>res</code> is to be considered a
- * star resource.
+ * @return <code>true</code> if <code>res</code> is to be considered a star
+ * resource.
* @throws NullPointerException if <code>res</code> is <code>null</code>.
*/
public static boolean isStarResource(Resource res) {
@@ -261,42 +266,42 @@ public class ResourceUtil {
}
/**
- * Returns an <code>Iterator</code> of {...@link Resource} objects loaded
- * from the children of the given <code>Resource</code>.
+ * Returns an <code>Iterator</code> of {...@link Resource} objects loaded
from
+ * the children of the given <code>Resource</code>.
* <p>
- * This is a convenience method for {...@link
ResourceResolver#listChildren(Resource)}.
+ * This is a convenience method for
+ * {...@link ResourceResolver#listChildren(Resource)}.
*
* @param parent The {...@link Resource Resource} whose children are
requested.
* @return An <code>Iterator</code> of {...@link Resource} objects.
- * @throws NullPointerException If <code>parent</code> is
- * <code>null</code>.
+ * @throws NullPointerException If <code>parent</code> is
<code>null</code>.
* @throws org.apache.sling.api.SlingException If any error occurs
acquiring
* the child resource iterator.
* @see ResourceResolver#listChildren(Resource)
+ * @deprecated since 2.1.0, use {...@link Resource#listChildren()} instead
*/
public static Iterator<Resource> listChildren(Resource parent) {
- return parent.getResourceResolver().listChildren(parent);
+ return parent.listChildren();
}
/**
* Returns an <code>ValueMap</code> object for the given
- * <code>Resource</code>.
- * This method calls {...@link Resource#adaptTo(Class)} with the
- * {...@link ValueMap} class as an argument. If the <code>adaptTo</code>
- * method returns a map, this map is returned. If the resource is not
- * adaptable to a value map, next an adaption to {...@link Map} is tried
- * and if this is successful the map is wrapped as a value map.
- * If the adaptions are not successful an empty value map is returned.
- * If <code>null</code> is provided as the resource an empty map is
- * returned as well.
+ * <code>Resource</code>. This method calls {...@link
Resource#adaptTo(Class)}
+ * with the {...@link ValueMap} class as an argument. If the
+ * <code>adaptTo</code> method returns a map, this map is returned. If the
+ * resource is not adaptable to a value map, next an adaption to {...@link
Map}
+ * is tried and if this is successful the map is wrapped as a value map. If
+ * the adaptions are not successful an empty value map is returned. If
+ * <code>null</code> is provided as the resource an empty map is returned
as
+ * well.
+ *
* @param res The <code>Resource</code> to adapt to the value map.
* @return A value map.
*/
@SuppressWarnings("unchecked")
public static ValueMap getValueMap(final Resource res) {
// adapt to ValueMap if resource is not null
- ValueMap valueMap = (res != null)?
- res.adaptTo(ValueMap.class) : null;
+ ValueMap valueMap = (res != null) ? res.adaptTo(ValueMap.class) : null;
// if no resource or no ValueMap adapter, check Map
if (valueMap == null) {
@@ -314,6 +319,7 @@ public class ResourceUtil {
return valueMap;
}
+
/**
* Helper method, which returns the given resource type as returned from
the
* {...@link org.apache.sling.api.resource.Resource#getResourceType()} as a
@@ -328,43 +334,45 @@ public class ResourceUtil {
}
/**
- * Returns the super type of the given resource type.
- * This method converts the resource type to a resource path
- * by calling {...@link #resourceTypeToPath(String)} and uses
- * the <code>resourceResolver</code> to get the corresponding
- * resource. If the resource exists, the {...@link
Resource#getResourceSuperType()}
- * metod is called.
- *
- * @param resourceResolver The <code>ResourceResolver</code> used to
- * access the resource whose path (relative or absolute) is
given
- * by the <code>resourceType</code> parameter.
+ * Returns the super type of the given resource type. This method converts
+ * the resource type to a resource path by calling
+ * {...@link #resourceTypeToPath(String)} and uses the
+ * <code>resourceResolver</code> to get the corresponding resource. If the
+ * resource exists, the {...@link Resource#getResourceSuperType()} metod is
+ * called.
+ *
+ * @param resourceResolver The <code>ResourceResolver</code> used to access
+ * the resource whose path (relative or absolute) is given by
the
+ * <code>resourceType</code> parameter.
* @param resourceType The resource type whose super type is to be
returned.
* This type is turned into a path by calling the
* {...@link #resourceTypeToPath(String)} method before trying
to
* get the resource through the <code>resourceResolver</code>.
* @return the super type of the <code>resourceType</code> or
- * <code>null</code> if the resource type does not exists
- * or returns <code>null</code> for its super type.
+ * <code>null</code> if the resource type does not exists or
returns
+ * <code>null</code> for its super type.
* @since 2.0.6
*/
- public static String getResourceSuperType(final ResourceResolver
resourceResolver,
- final String resourceType) {
+ public static String getResourceSuperType(
+ final ResourceResolver resourceResolver, final String
resourceType) {
// normalize resource type to a path string
final String rtPath = resourceTypeToPath(resourceType);
// get the resource type resource and check its super type
String resourceSuperType = null;
// if the path is absolute, use it directly
- if ( rtPath != null && rtPath.startsWith("/") ) {
+ if (rtPath != null && rtPath.startsWith("/")) {
final Resource rtResource = resourceResolver.getResource(rtPath);
- if ( rtResource != null ) {
+ if (rtResource != null) {
resourceSuperType = rtResource.getResourceSuperType();
}
} else {
// if the path is relative we use the search paths
- for(final String searchPath : resourceResolver.getSearchPath()) {
- final Resource rtResource =
resourceResolver.getResource(searchPath + rtPath);
- if ( rtResource != null && rtResource.getResourceSuperType()
!= null ) {
+ for (final String searchPath : resourceResolver.getSearchPath()) {
+ final Resource rtResource =
resourceResolver.getResource(searchPath
+ + rtPath);
+ if (rtResource != null
+ && rtResource.getResourceSuperType() != null) {
resourceSuperType = rtResource.getResourceSuperType();
break;
}
@@ -374,81 +382,71 @@ public class ResourceUtil {
}
/**
- * Returns the super type of the given resource.
- * This method checks first if the resource itself knows its super type
- * by calling {...@link Resource#getResourceSuperType()}. If that returns
+ * Returns the super type of the given resource. This method checks first
if
+ * the resource itself knows its super type by calling
+ * {...@link Resource#getResourceSuperType()}. If that returns
* <code>null</code> {...@link #getResourceSuperType(ResourceResolver,
String)}
* is invoked with the resource type of the resource.
*
* @param resource The resource to return the resource super type for.
- * @return the super type of the <code>resource</code> or
- * <code>null</code> if no super type could be computed.
+ * @return the super type of the <code>resource</code> or <code>null</code>
+ * if no super type could be computed.
* @since 2.0.6
*/
public static String findResourceSuperType(final Resource resource) {
String resourceSuperType = resource.getResourceSuperType();
- if ( resourceSuperType == null ) {
- resourceSuperType =
getResourceSuperType(resource.getResourceResolver(),
resource.getResourceType());
+ if (resourceSuperType == null) {
+ resourceSuperType = getResourceSuperType(
+ resource.getResourceResolver(), resource.getResourceType());
}
return resourceSuperType;
}
/**
- * Check if the resource is of the given type.
- * This method first checks the resource type of the resource, then
- * its super resource type and continues to go up the resource super
- * type hierarchy.
+ * Check if the resource is of the given type. This method first checks the
+ * resource type of the resource, then its super resource type and
continues
+ * to go up the resource super type hierarchy.
+ *
+ * @param resource the resource to check
+ * @param resourceType the resource type to check the resource against
+ * @retrun <code>false</code> if <code>resource</code> is
<code>null</code>.
+ * Otherwise returns the result of calling
+ * {...@link Resource#isResourceType(String)} with the given
+ * <code>resourceType</code>.
* @since 2.0.6
*/
public static boolean isA(final Resource resource, String resourceType) {
- if ( resource == null || resourceType == null ) {
- return false;
- }
- if ( resourceType.equals(resource.getResourceType()) ) {
- return true;
- }
- String superType = findResourceSuperType(resource);
- while ( superType != null ) {
- if ( resourceType.equals(superType) ) {
- return true;
- }
- superType = getResourceSuperType(resource.getResourceResolver(),
superType);
- }
- return false;
+ return resource != null && resource.isResourceType(resourceType);
}
/**
- * Return an iterator for objecs of the specified type.
- * A new iterator is returned which tries to adapt the provided resources
- * to the given type (using {...@link Resource#adaptTo(Class)}.
- * If a resource in the original iterator is not adaptable to the given
- * class, this object is skipped. This implies that the number of objects
- * returned by the new iterator might be less than the number of resource
- * objects.
+ * Return an iterator for objecs of the specified type. A new iterator is
+ * returned which tries to adapt the provided resources to the given type
+ * (using {...@link Resource#adaptTo(Class)}. If a resource in the original
+ * iterator is not adaptable to the given class, this object is skipped.
+ * This implies that the number of objects returned by the new iterator
+ * might be less than the number of resource objects.
+ *
* @param iterator A resource iterator.
* @param <T> The adapted type
* @since 2.0.6
*/
- public static <T> Iterator<T> adaptTo(final Iterator<Resource> iterator,
final Class<T> type) {
+ public static <T> Iterator<T> adaptTo(final Iterator<Resource> iterator,
+ final Class<T> type) {
return new Iterator<T>() {
- private T nextObject;
+ private T nextObject = seek();
public boolean hasNext() {
- while ( nextObject == null && iterator.hasNext() ) {
- final Resource r = iterator.next();
- nextObject = r.adaptTo(type);
- }
return nextObject != null;
}
public T next() {
- hasNext();
- if ( nextObject == null ) {
+ if (!hasNext()) {
throw new NoSuchElementException();
}
final T object = nextObject;
- nextObject = null;
+ nextObject = seek();
return object;
}
@@ -456,6 +454,14 @@ public class ResourceUtil {
throw new UnsupportedOperationException();
}
+ private T seek() {
+ T result = null;
+ while (result == null && iterator.hasNext()) {
+ final Resource r = iterator.next();
+ result = r.adaptTo(type);
+ }
+ return result;
+ }
};
}
}
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=980150&r1=980149&r2=980150&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
Wed Jul 28 18:41:55 2010
@@ -18,6 +18,8 @@
*/
package org.apache.sling.api.resource;
+import java.util.Iterator;
+
/**
* The <code>ResourceWrapper</code> is a wrapper for any <code>Resource</code>
* delegating all method calls to the wrapped resource by default. Extensions
of
@@ -38,59 +40,119 @@ public class ResourceWrapper implements
}
/**
- * Returns the <code>Resource</code> wrapped by this instance.
- * This method can be overwritten by subclasses if required. All
- * other methods use this method to get the resource object.
+ * Returns the <code>Resource</code> wrapped by this instance. This method
+ * can be overwritten by subclasses if required. All methods implemented by
+ * this class use this method to get the resource object.
*/
public Resource getResource() {
return resource;
}
/**
- * @see org.apache.sling.api.resource.Resource#getPath()
+ * Returns the value of calling <code>getPath</code> on the
+ * {...@link #getResource() wrapped resource}.
*/
public String getPath() {
return getResource().getPath();
}
/**
- * @see org.apache.sling.api.resource.Resource#getResourceMetadata()
+ * Returns the value of calling <code>getName</code> on the
+ * {...@link #getResource() wrapped resource}.
+ *
+ * @since 2.1.0
+ */
+ public String getName() {
+ return getResource().getName();
+ }
+
+ /**
+ * Returns the value of calling <code>getParent</code> on the
+ * {...@link #getResource() wrapped resource}.
+ *
+ * @since 2.1.0
+ */
+ public Resource getParent() {
+ return getResource().getParent();
+ }
+
+ /**
+ * Returns the value of calling <code>getChild</code> on the
+ * {...@link #getResource() wrapped resource}.
+ *
+ * @since 2.1.0
+ */
+ public Resource getChild(String relPath) {
+ return getResource().getChild(relPath);
+ }
+
+ /**
+ * Returns the value of calling <code>listChildren</code> on the
+ * {...@link #getResource() wrapped resource}.
+ *
+ * @since 2.1.0
+ */
+ public Iterator<Resource> listChildren() {
+ return getResource().listChildren();
+ }
+
+ /**
+ * Returns the value of calling <code>getResourceMetadata</code> on the
+ * {...@link #getResource() wrapped resource}.
*/
public ResourceMetadata getResourceMetadata() {
return getResource().getResourceMetadata();
}
/**
- * @see org.apache.sling.api.resource.Resource#getResourceResolver()
+ * Returns the value of calling <code>getResourceResolver</code> on the
+ * {...@link #getResource() wrapped resource}.
*/
public ResourceResolver getResourceResolver() {
return getResource().getResourceResolver();
}
/**
- * @see org.apache.sling.api.resource.Resource#getResourceType()
+ * Returns the value of calling <code>getResourceType</code> on the
+ * {...@link #getResource() wrapped resource}.
*/
public String getResourceType() {
return getResource().getResourceType();
}
/**
- * @see org.apache.sling.api.resource.Resource#getResourceSuperType()
+ * Returns the value of calling <code>getResourceSuperType</code> on the
+ * {...@link #getResource() wrapped resource}.
*/
public String getResourceSuperType() {
return getResource().getResourceSuperType();
}
/**
- * @see org.apache.sling.api.adapter.Adaptable#adaptTo(java.lang.Class)
+ * Returns the value of calling <code>isResourceType</code> on the
+ * {...@link #getResource() wrapped resource}.
+ *
+ * @since 2.1.0
+ */
+ public boolean isResourceType(final String resourceType) {
+ return getResource().isResourceType(resourceType);
+ }
+
+ /**
+ * Returns the value of calling <code>adaptTo</code> on the
+ * {...@link #getResource() wrapped resource}.
*/
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
return getResource().adaptTo(type);
}
/**
- * @see java.lang.Object#toString()
+ * Returns a string representation of this wrapper consisting of the class'
+ * simple name, the {...@link #getResourceType() resource type} and
+ * {...@link #getPath() path} as well as the string representation of the
+ * {...@link #getResource() wrapped resource}.
*/
+ @Override
public String toString() {
return getClass().getSimpleName() + ", type=" + getResourceType()
+ ", path=" + getPath() + ", resource=[" + getResource() + "]";
Modified:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/SyntheticResource.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/SyntheticResource.java?rev=980150&r1=980149&r2=980150&view=diff
==============================================================================
---
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/SyntheticResource.java
(original)
+++
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/SyntheticResource.java
Wed Jul 28 18:41:55 2010
@@ -25,31 +25,7 @@ import org.apache.sling.api.adapter.Adap
* <code>Resource</code> interface which may be used to provide a resource
* object which has no actual resource data.
*/
-public class SyntheticResource implements Resource {
-
- /** The adapter manager used for adapting the synthetic resource. */
- private static volatile AdapterManager ADAPTER_MANAGER;
-
- /**
- * Set the adapter manager to be used by a synthetic resource.
- * A bundle implementing the adapter manager can set the manager through
this method.
- * The set adapter manager will be used in the {...@link #adaptTo(Class)}
method
- * of a synthetic resource.
- * @param adapterMgr The adapter manager.
- */
- public static void setAdapterManager(final AdapterManager adapterMgr) {
- ADAPTER_MANAGER = adapterMgr;
- }
-
- /**
- * Unset an adapter manager previously set with {...@link
#setAdapterManager(AdapterManager)}
- * @param adapterMgr The adapter manager
- */
- public static void unsetAdapterManager(final AdapterManager adapterMgr) {
- if ( ADAPTER_MANAGER == adapterMgr ) {
- ADAPTER_MANAGER = null;
- }
- }
+public class SyntheticResource extends AbstractResource {
/** The resoure resolver to which this resource is related */
private final ResourceResolver resourceResolver;
@@ -57,9 +33,8 @@ public class SyntheticResource implement
/** The path of the synthetic resource */
private final String path;
- /** The type this synthetic resource assumes.
- * TODO as soon as we remove the {...@link #setResourceType(String)}
methode we can make this final. */
- private String resourceType;
+ /** The type this synthetic resource assumes */
+ private final String resourceType;
/** The metadata of this resource just containig the resource path */
private final ResourceMetadata resourceMetadata;
@@ -104,20 +79,6 @@ public class SyntheticResource implement
}
/**
- * Helper method for sub classes to set the resource type
- * @param resourceType The resource type
- * @deprecated The resource type should be set through the constructor.
- */
- @Deprecated
- protected void setResourceType(String resourceType) {
- if (this.resourceType != null) {
- throw new IllegalArgumentException("Resource type already set ("
- + this.resourceType + "), cannot change it");
- }
- this.resourceType = resourceType;
- }
-
- /**
* Synthetic resources by default do not have a resource super type.
*/
public String getResourceSuperType() {
@@ -140,19 +101,6 @@ public class SyntheticResource implement
return resourceResolver;
}
- /**
- * If a adapter manager has been set through {...@link
#setAdapterManager(AdapterManager)}
- * this adapter manager is used to adapt the resource to the given class.
- * Otherwise this method returns <code>null</code>.
- */
- public <Type> Type adaptTo(Class<Type> type) {
- final AdapterManager adapterMgr = ADAPTER_MANAGER;
- if ( adapterMgr != null ) {
- return adapterMgr.getAdapter(this, type);
- }
- return null;
- }
-
@Override
public String toString() {
return getClass().getSimpleName() + ", type=" + getResourceType()
Modified:
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java?rev=980150&r1=980149&r2=980150&view=diff
==============================================================================
---
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
(original)
+++
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
Wed Jul 28 18:41:55 2010
@@ -288,13 +288,14 @@ public class ResourceUtilTest {
// the resource resolver
final ResourceResolver resolver =
this.context.mock(ResourceResolver.class);
// the resource to test
- final Resource r = this.context.mock(Resource.class, "resource");
+ final Resource r = new SyntheticResource(resolver, "/a", "a:b") {
+ @Override
+ public String getResourceSuperType() {
+ return "d:e";
+ }
+ };
final Resource typeResource = this.context.mock(Resource.class,
"typeResource");
this.context.checking(new Expectations() {{
- allowing(r).getResourceType(); will(returnValue("a:b"));
- allowing(r).getResourceSuperType(); will(returnValue("d:e"));
- allowing(r).getResourceResolver(); will(returnValue(resolver));
-
allowing(typeResource).getResourceType();
will(returnValue("x:y"));
allowing(typeResource).getResourceSuperType();
Modified:
sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/SlingRequestPathInfoTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/SlingRequestPathInfoTest.java?rev=980150&r1=980149&r2=980150&view=diff
==============================================================================
---
sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/SlingRequestPathInfoTest.java
(original)
+++
sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/SlingRequestPathInfoTest.java
Wed Jul 28 18:41:55 2010
@@ -20,7 +20,7 @@ import junit.framework.TestCase;
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.request.RequestPathInfo;
-import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.AbstractResource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
@@ -283,7 +283,7 @@ public class SlingRequestPathInfoTest ex
assertEquals("html", result.getSuffix());
}
- static class MockResource implements Resource {
+ static class MockResource extends AbstractResource {
private final ResourceMetadata metadata;
Modified:
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java?rev=980150&r1=980149&r2=980150&view=diff
==============================================================================
---
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
(original)
+++
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
Wed Jul 28 18:41:55 2010
@@ -23,13 +23,13 @@ import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
-import org.apache.sling.adapter.SlingAdaptable;
+import org.apache.sling.api.resource.AbstractResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.resource.JcrResourceConstants;
-abstract class JcrItemResource extends SlingAdaptable implements Resource {
+abstract class JcrItemResource extends AbstractResource implements Resource {
private final ResourceResolver resourceResolver;
@@ -83,6 +83,6 @@ abstract class JcrItemResource extends S
* Returns an iterator over the child resources or <code>null</code> if
* there are none.
*/
- abstract Iterator<Resource> listChildren();
+ abstract Iterator<Resource> listJcrChildren();
}
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=980150&r1=980149&r2=980150&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
Wed Jul 28 18:41:55 2010
@@ -228,7 +228,7 @@ public class JcrNodeResource extends Jcr
// ---------- Descendable interface
----------------------------------------
@Override
- Iterator<Resource> listChildren() {
+ Iterator<Resource> listJcrChildren() {
try {
if (getNode().hasNodes()) {
return new JcrNodeResourceIterator(getResourceResolver(),
Modified:
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java?rev=980150&r1=980149&r2=980150&view=diff
==============================================================================
---
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
(original)
+++
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
Wed Jul 28 18:41:55 2010
@@ -205,7 +205,7 @@ public class JcrPropertyResource extends
}
@Override
- Iterator<Resource> listChildren() {
+ Iterator<Resource> listJcrChildren() {
return null;
}
}
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=980150&r1=980149&r2=980150&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
Wed Jul 28 18:41:55 2010
@@ -93,6 +93,7 @@ public class JcrResourceProvider impleme
} else if (parent instanceof ResourceWrapper) {
return listChildren(((ResourceWrapper) parent).getResource());
+
} else {
// try to get the JcrItemResource for the parent path to list
@@ -108,7 +109,7 @@ public class JcrResourceProvider impleme
// return children if there is a parent item resource, else null
return (parentItemResource != null)
- ? parentItemResource.listChildren()
+ ? parentItemResource.listJcrChildren()
: null;
}
Added:
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/ResourceResolutionPlugin.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/ResourceResolutionPlugin.java?rev=980150&view=auto
==============================================================================
---
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/ResourceResolutionPlugin.java
(added)
+++
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/ResourceResolutionPlugin.java
Wed Jul 28 18:41:55 2010
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.resolver;
+
+import org.apache.sling.api.resource.ValueMap;
+
+public interface ResourceResolutionPlugin {
+
+ static final String SERVICE = "ResourceResolutionPlugin";
+
+ /**
+ * Return a new resource resolution plugin instance configured
+ * from the given configuration. This ValueMap is created from the
+ * resource below /etc/map which instructs this plugin to be used.
+ */
+ ResourceResolutionInstance newInstance(ValueMap configuration);
+
+ }
Propchange:
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/ResourceResolutionPlugin.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/resolver/ResourceResolutionPlugin.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev Url
Modified:
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/ResourceProviderEntryTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/ResourceProviderEntryTest.java?rev=980150&r1=980149&r2=980150&view=diff
==============================================================================
---
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/ResourceProviderEntryTest.java
(original)
+++
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/ResourceProviderEntryTest.java
Wed Jul 28 18:41:55 2010
@@ -26,6 +26,7 @@ import javax.servlet.http.HttpServletReq
import junit.framework.TestCase;
+import org.apache.sling.api.resource.AbstractResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceProvider;
@@ -268,7 +269,7 @@ public class ResourceProviderEntryTest e
}
}
- private static class TestResource implements Resource {
+ private static class TestResource extends AbstractResource {
private final String path;