1 17 package org.apache.lenya.cms.cocoon.components.modules.input; 18 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.Map ; 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 38 public class ResourceExistsModule extends AbstractInputModule implements Serviceable, Disposable { 39 40 43 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 44 throws ConfigurationException { 45 46 String 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 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 70 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) 71 throws ConfigurationException { 72 return Collections.EMPTY_SET.iterator(); 73 } 74 75 private ServiceManager manager; 76 private SourceResolver resolver; 77 78 81 public void service(ServiceManager manager) throws ServiceException { 82 this.manager = manager; 83 this.resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 84 } 85 86 89 public void dispose() { 90 super.dispose(); 91 this.manager.release(this.resolver); 92 this.resolver = null; 93 this.manager = null; 94 } 95 96 99 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) 100 throws ConfigurationException { 101 Object result = this.getAttribute(name, modeConf, objectModel); 102 return (result == null ? null : new Object [] {result}); 103 } 104 105 } 106 | Popular Tags |