KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > acting > ResourceExistsAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: ResourceExistsAction.java 42891 2004-04-24 20:59:15Z gregor $ */
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.framework.thread.ThreadSafe;
28 import org.apache.cocoon.acting.ServiceableAction;
29 import org.apache.cocoon.environment.Redirector;
30 import org.apache.cocoon.environment.SourceResolver;
31 import org.apache.excalibur.source.Source;
32
33
34 /**
35  * This action simply checks to see if a given resource exists. It checks
36  * whether the specified in the src attribute source exists or not.
37  * The action returns empty <code>Map</code> if it exists, null otherwise.
38  * <p>Instead of src attribute, source can be specified using
39  * parameter named 'url' (this is old syntax).
40  * <p>In order to differentiate between files and directories, the type can be specified
41  * using the parameter 'type' (&lt;map:parameter name="type" value="file"/&gt; or
42  * &lt;map:parameter name="type" value="directory"/&gt;). The parameter 'type' is optional.
43  * <p>
44  * <strong>Note:</strong> {@link org.apache.cocoon.selection.ResourceExistsSelector}
45  * should be preferred to this component, as the semantics of a Selector better
46  * match the supplied functionality.
47  */

48 public class ResourceExistsAction extends ServiceableAction implements ThreadSafe {
49     /**
50      *
51      */

52     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc source,
53         Parameters parameters) throws Exception JavaDoc {
54         String JavaDoc urlstring = parameters.getParameter("url", source);
55         String JavaDoc typestring = parameters.getParameter("type", "resource");
56         Source src = null;
57
58         try {
59             src = resolver.resolveURI(urlstring);
60
61             File JavaDoc resource = new File JavaDoc(new URL JavaDoc(src.getURI()).getFile());
62
63             if (typestring.equals("resource") && src.exists()) {
64                 getLogger().debug(".act(): Resource (file or directory) exists: " + src.getURI());
65
66                 return EMPTY_MAP;
67             } else if (typestring.equals("file") && resource.isFile()) {
68                 getLogger().debug(".act(): File exists: " + resource);
69
70                 return EMPTY_MAP;
71             } else if (typestring.equals("directory") && resource.isDirectory()) {
72                 getLogger().debug(".act(): Directory exists: " + resource);
73
74                 return EMPTY_MAP;
75             } else {
76                 getLogger().debug(".act(): Resource " + resource + " as type \"" + typestring +
77                     "\" does not exist");
78             }
79         } catch (Exception JavaDoc e) {
80             getLogger().warn(".act(): Exception", e);
81         } finally {
82             resolver.release(src);
83         }
84
85         return null;
86     }
87 }
88
Popular Tags