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