You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2003/12/12 16:17:12 UTC
cvs commit: cocoon-2.2/src/java/org/apache/cocoon/selection ResourceExistsSelector.java
vgritsenko 2003/12/12 07:17:12
Modified: src/java/org/apache/cocoon/acting ResourceExistsAction.java
src/java/org/apache/cocoon/selection
ResourceExistsSelector.java
Log:
update resource exists selector to match resource exists action functionality
Revision Changes Path
1.6 +15 -10 cocoon-2.2/src/java/org/apache/cocoon/acting/ResourceExistsAction.java
Index: ResourceExistsAction.java
===================================================================
RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/acting/ResourceExistsAction.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ResourceExistsAction.java 27 Oct 2003 07:28:26 -0000 1.5
+++ ResourceExistsAction.java 12 Dec 2003 15:17:12 -0000 1.6
@@ -56,6 +56,7 @@
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceNotFoundException;
/**
* This action simply checks to see if a given resource exists. It checks
@@ -75,21 +76,25 @@
* @avalon.service type="Action"
* @x-avalon.lifestyle type="singleton"
* @x-avalon.info name="resource-exists"
- *
*/
public class ResourceExistsAction extends AbstractAction {
- public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception {
- String urlstring = parameters.getParameter("url", source);
- Source src = null;
+ public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String src, Parameters parameters) throws Exception {
+ String resourceURI = parameters.getParameter("url", src);
+ Source source = null;
try {
- src = resolver.resolveURI(urlstring);
- if (src.exists())
- return EMPTY_MAP;
+ source = resolver.resolveURI(resourceURI);
+ if (source.exists()) {
+ return EMPTY_MAP;
+ }
+ } catch (SourceNotFoundException e) {
+ // Do not log
} catch (Exception e) {
- getLogger().warn("Exception", e);
+ getLogger().warn("Exception resolving resource " + resourceURI, e);
} finally {
- resolver.release(src);
+ if (source != null) {
+ resolver.release(source);
+ }
}
return null;
}
1.3 +63 -54 cocoon-2.2/src/java/org/apache/cocoon/selection/ResourceExistsSelector.java
Index: ResourceExistsSelector.java
===================================================================
RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/selection/ResourceExistsSelector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ResourceExistsSelector.java 20 Mar 2003 12:32:18 -0000 1.2
+++ ResourceExistsSelector.java 12 Dec 2003 15:17:12 -0000 1.3
@@ -50,91 +50,100 @@
*/
package org.apache.cocoon.selection;
-import java.net.MalformedURLException;
-import java.net.URL;
import java.util.Map;
+import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.thread.ThreadSafe;
-import org.apache.cocoon.environment.Context;
-import org.apache.cocoon.environment.ObjectModelHelper;
-
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.SourceResolver;
/**
- * Selects the first of a set of Resources (usually files) that exists in the
- * context.
- * <p>
- * The 'test' expression is interpreted as a context-rooted ('/' = context)
- * path, resolved by the servlet container, <em>not</em> a Source.
+ * Selects the first of a set of Resources (usually files) that exists.
+ *
* <p>
- * A parameter,
+ * A parameter 'prefix',
* <pre>
- * <map:parameter src="prefix" value="<code>/</code>"/>
+ * <map:parameter name="prefix" value="<code>some/path</code>"/<
* </pre>
* may be supplied to the selector instance. This prefix is prepended to all
- * test expressions before evaluation. The default prefix is '<code>/</code>',
- * meaning that all expressions are context root-relative, unless explicitly
- * overridden.
+ * test expressions before evaluation. The default prefix is '' (empty string),
+ * meaning that all expressions are relative to the current sitemap, unless
+ * explicitly overridden.
+ *
+ * <p><b>NOTE:</b>
+ * Provided resource URI is resolved as Source, relative to the current
+ * sitemap, which differs from behavior of selector in previous versions.
+ * To resolve resource paths relative to the context root, provide prefix
+ * parameter:
+ * <pre>
+ * <map:parameter src="prefix" value="context://"/<
+ * </pre>
+ *
* <p>
* For example, we could define a ResourceExistsSelector with:
* <pre>
* <map:selector name="resource-exists"
* logger="sitemap.selector.resource-exists"
- * src="org.apache.cocoon.selection.ResourceExistsSelector" />
+ * src="org.apache.cocoon.selection.ResourceExistsSelector" /<
* </pre>
* And use it to build a PDF from XSL:FO or a higher-level XML format with:
*
* <pre>
- * <map:match pattern="**.pdf">
- * <map:select type="resource-exists">
- * <map:when test="context/xdocs/{1}.fo">
- * <map:generate src="content/xdocs/{1}.fo" />
- * </map:when>
- * <map:otherwise>
- * <map:generate src="content/xdocs/{1}.xml" />
- * <map:transform src="stylesheets/document2fo.xsl" />
- * </map:otherwise>
- * </map:select>
- * <map:serialize type="fo2pdf" />
+ * <map:match pattern="**.pdf"<
+ * <map:select type="resource-exists"<
+ * <map:when test="context/xdocs/{1}.fo"<
+ * <map:generate src="content/xdocs/{1}.fo" /<
+ * </map:when<
+ * <map:otherwise<
+ * <map:generate src="content/xdocs/{1}.xml" /<
+ * <map:transform src="stylesheets/document2fo.xsl" /<
+ * </map:otherwise<
+ * </map:select<
+ * <map:serialize type="fo2pdf" /<
* </pre>
*
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
+ * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
* @version CVS $Id$
*/
public class ResourceExistsSelector extends AbstractLogEnabled
- implements ThreadSafe, Selector {
+ implements ThreadSafe, Serviceable, Disposable, Selector {
+ private ServiceManager manager;
+ private SourceResolver resolver;
+
+ public void service(ServiceManager manager) throws ServiceException {
+ this.manager = manager;
+ this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
+ }
+
+ public void dispose() {
+ this.manager.release(this.resolver);
+ this.resolver = null;
+ this.manager = null;
+ }
+
public boolean select(String expression, Map objectModel, Parameters parameters) {
-
- Context context = ObjectModelHelper.getContext(objectModel);
- URL url = null;
- expression = parameters.getParameter("prefix", "/") + expression;
+ String resourceURI = parameters.getParameter("prefix", "") + expression;
+ Source source = null;
try {
- url = context.getResource(expression);
- if (url == null) {
- return false;
- } else { return true; }
- } catch (MalformedURLException e) {
- getLogger().warn("Selector expression '"+expression+"' is not a valid URL");
+ source = resolver.resolveURI(resourceURI);
+ return source.exists();
+ } catch (SourceNotFoundException e) {
return false;
- }
-
- /* While the servlet Javadocs state that getResource should return null
- * if a resource doesn't exist, early versions of Tomcat didn't respect
- * this. If this turns to be an issue with other containers, remove
- * the 'else return true' above and uncomment this code. (JT)
- InputStream is = null;
- try {
- is = url.openStream();
- return true;
- } catch (IOException e) {
+ } catch (Exception e) {
+ getLogger().warn("Exception resolving resource " + resourceURI, e);
return false;
} finally {
- try {
- if (is != null) is.close();
- } catch (IOException e) {}
+ if (source != null) {
+ resolver.release(source);
+ }
}
- */
}
}