1 16 package org.apache.cocoon.forms.binding.library; 17 18 import org.apache.avalon.framework.CascadingException; 19 import org.apache.avalon.framework.activity.Disposable; 20 import org.apache.avalon.framework.activity.Initializable; 21 import org.apache.avalon.framework.configuration.Configurable; 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.configuration.ConfigurationException; 24 import org.apache.avalon.framework.logger.AbstractLogEnabled; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.avalon.framework.service.Serviceable; 28 import org.apache.avalon.framework.thread.ThreadSafe; 29 import org.apache.cocoon.forms.CacheManager; 30 import org.apache.cocoon.forms.binding.JXPathBindingManager; 31 import org.apache.cocoon.forms.util.DomHelper; 32 import org.apache.excalibur.source.Source; 33 import org.apache.excalibur.source.SourceResolver; 34 import org.w3c.dom.Document ; 35 import org.xml.sax.InputSource ; 36 37 41 public class LibraryManagerImpl extends AbstractLogEnabled implements LibraryManager, ThreadSafe, Serviceable, 42 Configurable, Initializable, Disposable { 43 44 protected static final String PREFIX = "CocoonFormBindingLibrary:"; 45 46 private ServiceManager serviceManager; 47 private Configuration configuration; 48 private CacheManager cacheManager; 49 50 private JXPathBindingManager bindingManager; 51 52 public void configure(Configuration configuration) throws ConfigurationException { 53 this.configuration = configuration; 54 getLogger().debug("Gotten a config: top level element: "+this.configuration); 55 } 56 57 public void service(ServiceManager serviceManager) throws ServiceException { 58 this.serviceManager = serviceManager; 59 this.cacheManager = (CacheManager)serviceManager.lookup(CacheManager.ROLE); 60 } 61 62 public void setBindingManager(JXPathBindingManager bindingManager) { 63 this.bindingManager = bindingManager; 64 } 65 66 public void initialize() throws Exception { 67 68 } 70 71 public boolean libraryInCache(String librarysource) throws Exception { 72 return libraryInCache(librarysource,null); 73 } 74 75 public boolean libraryInCache(String librarysource, String relative) throws Exception { 76 SourceResolver sourceResolver = null; 77 Source source = null; 78 79 if(getLogger().isDebugEnabled()) 80 getLogger().debug("Checking if library is in cache: '"+librarysource+"' relative to '"+relative+"'"); 81 82 Library lib = null; 83 boolean result = false; 84 85 try { 86 sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE); 87 source = sourceResolver.resolveURI(librarysource, relative, null); 88 89 lib = (Library)this.cacheManager.get(source, PREFIX); 90 91 if( lib != null && lib.dependenciesHaveChanged() ) { 92 result = false; 93 this.cacheManager.set(null,source,PREFIX); } 95 else if( lib == null ) 96 result = false; 97 else 98 result = true; 99 } catch(Exception e) { 100 if(getLogger().isErrorEnabled()) 101 getLogger().error("Problem getting library '"+librarysource+"' relative to '"+relative+"'!",e); 102 throw e; 103 } finally { 104 if (source != null) 105 sourceResolver.release(source); 106 if (sourceResolver != null) 107 serviceManager.release(sourceResolver); 108 } 109 110 if(getLogger().isDebugEnabled()) { 111 if(result) 112 getLogger().debug("Library IS in cache : '"+librarysource+"' relative to '"+relative+"'"); 113 else 114 getLogger().debug("Library IS NOT in cache : '"+librarysource+"' relative to '"+relative+"'"); 115 } 116 117 return result; 118 } 119 120 public Library getLibrary(String librarysource) throws Exception { 121 return getLibrary(librarysource,null); 122 } 123 124 public Library getLibrary(String librarysource, String relative) throws Exception { 125 SourceResolver sourceResolver = null; 126 Source source = null; 127 Document libraryDocument = null; 128 129 Library lib = null; 130 131 if(getLogger().isDebugEnabled()) 132 getLogger().debug("Getting library instance: '"+librarysource+"' relative to '"+relative+"'"); 133 134 try { 135 sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE); 136 source = sourceResolver.resolveURI(librarysource, relative, null); 137 138 lib = (Library)this.cacheManager.get(source, PREFIX); 139 140 if( lib != null && lib.dependenciesHaveChanged() ) { 141 if(getLogger().isDebugEnabled()) 142 getLogger().debug("Library dependencies changed, invalidating!"); 143 144 lib = null; 145 } 146 147 if( lib == null ) { 148 if(getLogger().isDebugEnabled()) 149 getLogger().debug("Library not in cache, creating!"); 150 151 try { 152 InputSource inputSource = new InputSource (source.getInputStream()); 153 inputSource.setSystemId(source.getURI()); 154 libraryDocument = DomHelper.parse(inputSource, this.serviceManager); 155 156 lib = getNewLibrary(); 157 lib.buildLibrary(libraryDocument.getDocumentElement()); 158 159 this.cacheManager.set(lib,source,PREFIX); 160 161 } catch (Exception e) { 162 throw new CascadingException("Could not parse form definition from " + 163 source.getURI(), e); 164 } 165 } 166 } finally { 167 if (source != null) 168 sourceResolver.release(source); 169 if (sourceResolver != null) 170 serviceManager.release(sourceResolver); 171 } 172 173 return lib; 174 } 175 176 public Library getNewLibrary() { 177 Library lib = new Library(this); 178 lib.setAssistant(this.bindingManager.getBuilderAssistant()); 179 180 if(getLogger().isDebugEnabled()) 181 getLogger().debug("Created new library! "+lib); 182 183 return lib; 184 } 185 186 public void dispose() { 187 this.serviceManager.release(this.cacheManager); 188 this.cacheManager = null; 189 this.serviceManager = null; 190 } 191 192 public void debug(String msg) { 193 if(getLogger().isDebugEnabled()) { 194 getLogger().debug(msg); 195 } 196 } 197 198 } 199 | Popular Tags |