1 16 package org.apache.cocoon.selection; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.activity.Disposable; 21 import org.apache.avalon.framework.logger.AbstractLogEnabled; 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.avalon.framework.service.ServiceException; 24 import org.apache.avalon.framework.service.ServiceManager; 25 import org.apache.avalon.framework.service.Serviceable; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 import org.apache.excalibur.source.Source; 28 import org.apache.excalibur.source.SourceNotFoundException; 29 import org.apache.excalibur.source.SourceResolver; 30 31 80 public class ResourceExistsSelector extends AbstractLogEnabled 81 implements ThreadSafe, Serviceable, Disposable, Selector { 82 83 private ServiceManager manager; 84 private SourceResolver resolver; 85 86 public void service(ServiceManager manager) throws ServiceException { 87 this.manager = manager; 88 this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); 89 } 90 91 public void dispose() { 92 this.manager.release(this.resolver); 93 this.resolver = null; 94 this.manager = null; 95 } 96 97 public boolean select(String expression, Map objectModel, Parameters parameters) { 98 String resourceURI = parameters.getParameter("prefix", "") + expression; 99 Source source = null; 100 try { 101 source = resolver.resolveURI(resourceURI); 102 return source.exists(); 103 } catch (SourceNotFoundException e) { 104 return false; 105 } catch (Exception e) { 106 getLogger().warn("Exception resolving resource " + resourceURI, e); 107 return false; 108 } finally { 109 if (source != null) { 110 resolver.release(source); 111 } 112 } 113 } 114 } 115 | Popular Tags |