001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.cocoon.components.modules.input;
019:
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.Serviceable;
030: import org.apache.cocoon.components.modules.input.AbstractInputModule;
031: import org.apache.excalibur.source.Source;
032: import org.apache.excalibur.source.SourceNotFoundException;
033: import org.apache.excalibur.source.SourceResolver;
034:
035: /**
036: * Checks if a certain resource exists and returns either the string "true" or "false".
037: * @version $Id: ResourceExistsModule.java 473861 2006-11-12 03:51:14Z gregor $
038: */
039: public class ResourceExistsModule extends AbstractInputModule implements
040: Serviceable, Disposable {
041:
042: /**
043: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
044: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
045: */
046: public Object getAttribute(String name, Configuration modeConf,
047: Map objectModel) throws ConfigurationException {
048:
049: String resourceURI = name;
050:
051: Source source = null;
052: boolean exists = false;
053: try {
054: source = this .resolver.resolveURI(resourceURI);
055: exists = source.exists();
056: } catch (SourceNotFoundException e) {
057: exists = false;
058: } catch (Exception e) {
059: getLogger().warn(
060: "Exception resolving resource [" + resourceURI
061: + "]", e);
062: exists = false;
063: } finally {
064: if (source != null) {
065: this .resolver.release(source);
066: }
067: }
068:
069: return Boolean.toString(exists);
070: }
071:
072: /**
073: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
074: * java.util.Map)
075: */
076: public Iterator getAttributeNames(Configuration modeConf,
077: Map objectModel) throws ConfigurationException {
078: return Collections.EMPTY_SET.iterator();
079: }
080:
081: private ServiceManager manager;
082: private SourceResolver resolver;
083:
084: /**
085: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
086: */
087: public void service(ServiceManager _manager)
088: throws ServiceException {
089: this .manager = _manager;
090: this .resolver = (SourceResolver) _manager
091: .lookup(SourceResolver.ROLE);
092: }
093:
094: /**
095: * @see org.apache.avalon.framework.activity.Disposable#dispose()
096: */
097: public void dispose() {
098: super .dispose();
099: this .manager.release(this .resolver);
100: this .resolver = null;
101: this .manager = null;
102: }
103:
104: /**
105: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
106: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
107: */
108: public Object[] getAttributeValues(String name,
109: Configuration modeConf, Map objectModel)
110: throws ConfigurationException {
111: Object result = this .getAttribute(name, modeConf, objectModel);
112: return (result == null ? null : new Object[] { result });
113: }
114:
115: }
|