KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > components > modules > input > ResourceExistsModule


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 package org.apache.lenya.cms.cocoon.components.modules.input;
18
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.cocoon.components.modules.input.AbstractInputModule;
30 import org.apache.excalibur.source.Source;
31 import org.apache.excalibur.source.SourceNotFoundException;
32 import org.apache.excalibur.source.SourceResolver;
33
34 /**
35  * Checks if a certain resource exists and returns either the string "true" or "false".
36  * @version $Id: ResourceExistsModule.java 42860 2004-04-15 14:11:02Z andreas $
37  */

38 public class ResourceExistsModule extends AbstractInputModule implements Serviceable, Disposable {
39
40     /* (non-Javadoc)
41      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
42      */

43     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
44         throws ConfigurationException {
45
46         String JavaDoc resourceURI = name;
47         
48         Source source = null;
49         boolean exists = false;
50         try {
51             source = resolver.resolveURI(resourceURI);
52             exists = source.exists();
53         } catch (SourceNotFoundException e) {
54             exists = false;
55         } catch (Exception JavaDoc e) {
56             getLogger().warn("Exception resolving resource [" + resourceURI + "]", e);
57             exists = false;
58         } finally {
59             if (source != null) {
60                 resolver.release(source);
61             }
62         }
63
64         return Boolean.toString(exists);
65     }
66
67     /* (non-Javadoc)
68      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
69      */

70     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
71         throws ConfigurationException {
72         return Collections.EMPTY_SET.iterator();
73     }
74
75     private ServiceManager manager;
76     private SourceResolver resolver;
77
78     /* (non-Javadoc)
79      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
80      */

81     public void service(ServiceManager manager) throws ServiceException {
82         this.manager = manager;
83         this.resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
84     }
85
86     /* (non-Javadoc)
87      * @see org.apache.avalon.framework.activity.Disposable#dispose()
88      */

89     public void dispose() {
90         super.dispose();
91         this.manager.release(this.resolver);
92         this.resolver = null;
93         this.manager = null;
94     }
95
96     /* (non-Javadoc)
97      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
98      */

99     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
100         throws ConfigurationException {
101         Object JavaDoc result = this.getAttribute(name, modeConf, objectModel);
102         return (result == null ? null : new Object JavaDoc[] {result});
103     }
104
105 }
106
Popular Tags