1 16 package org.apache.cocoon.forms; 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.component.Component; 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.context.Context; 26 import org.apache.avalon.framework.context.ContextException; 27 import org.apache.avalon.framework.context.Contextualizable; 28 import org.apache.avalon.framework.logger.AbstractLogEnabled; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.avalon.framework.service.ServiceManager; 31 import org.apache.avalon.framework.service.ServiceSelector; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.avalon.framework.thread.ThreadSafe; 34 import org.apache.cocoon.forms.formmodel.Form; 35 import org.apache.cocoon.forms.formmodel.FormDefinition; 36 import org.apache.cocoon.forms.formmodel.FormDefinitionBuilder; 37 import org.apache.cocoon.forms.formmodel.WidgetDefinitionBuilder; 38 import org.apache.cocoon.forms.formmodel.library.LibraryManager; 39 import org.apache.cocoon.forms.formmodel.library.LibraryManagerImpl; 40 import org.apache.cocoon.forms.util.DomHelper; 41 import org.apache.cocoon.forms.util.SimpleServiceSelector; 42 import org.apache.excalibur.source.Source; 43 import org.apache.excalibur.source.SourceResolver; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 import org.xml.sax.InputSource ; 47 48 53 public class DefaultFormManager 54 extends AbstractLogEnabled 55 implements FormManager, Contextualizable, ThreadSafe, Serviceable, Disposable, Configurable, Component, Initializable { 56 58 protected static final String PREFIX = "CocoonForm:"; 59 protected ServiceManager manager; 60 protected Configuration configuration; 61 protected SimpleServiceSelector widgetDefinitionBuilderSelector; 62 protected CacheManager cacheManager; 63 64 protected LibraryManagerImpl libraryManager; 65 66 private Context avalonContext; 67 public void contextualize(Context context) throws ContextException { 68 this.avalonContext = context; 69 } 70 71 72 public Context getAvalonContext() { 74 return this.avalonContext; 75 } 76 77 public void service(ServiceManager manager) throws ServiceException { 78 this.manager = manager; 79 this.cacheManager = (CacheManager)manager.lookup(CacheManager.ROLE); 80 } 81 82 85 public void configure(Configuration configuration) throws ConfigurationException { 86 this.configuration = configuration; 87 } 88 89 public void initialize() throws Exception { 90 91 libraryManager = new LibraryManagerImpl(); 92 libraryManager.enableLogging(getLogger().getChildLogger("library")); 93 libraryManager.service(new FormServiceManager()); 94 libraryManager.configure(configuration.getChild("libraries")); 95 96 widgetDefinitionBuilderSelector = new SimpleServiceSelector("widget", WidgetDefinitionBuilder.class); 97 widgetDefinitionBuilderSelector.contextualize(avalonContext); 98 widgetDefinitionBuilderSelector.service(new FormServiceManager()); 99 widgetDefinitionBuilderSelector.configure(configuration.getChild("widgets")); 100 101 libraryManager.initialize(); 102 } 103 104 public ServiceSelector getWidgetDefinitionBuilderSelector() { 105 return this.widgetDefinitionBuilderSelector; 106 } 107 108 public Form createForm(Source source) throws Exception { 109 FormDefinition formDefinition = getFormDefinition(source); 110 Form form = (Form)formDefinition.createInstance(); 111 form.initialize(); 112 return form; 113 } 114 115 public Form createForm(String uri) throws Exception { 116 SourceResolver sourceResolver = null; 117 Source source = null; 118 119 try { 120 sourceResolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); 121 122 source = sourceResolver.resolveURI(uri); 123 Form form = createForm(source); 124 return form; 125 } finally { 126 if (source != null) { 127 sourceResolver.release(source); 128 } 129 if (sourceResolver != null) { 130 manager.release(sourceResolver); 131 } 132 } 133 } 134 135 public Form createForm(Element formElement) throws Exception { 136 Form form = (Form)getFormDefinition(formElement).createInstance(); 137 form.initialize(); 138 return form; 139 } 140 141 public FormDefinition createFormDefinition(Element formElement) throws Exception { 142 return getFormDefinition(formElement); 143 } 144 145 public FormDefinition getFormDefinition(Source source) throws Exception { 146 FormDefinition formDefinition = (FormDefinition)this.cacheManager.get(source, PREFIX); 147 148 if(formDefinition != null && formDefinition.getLocalLibrary().dependenciesHaveChanged()) 149 formDefinition = null; 151 if (formDefinition == null) { 152 153 if(getLogger().isDebugEnabled()) 154 getLogger().debug("Building Form: "+source.getURI()); 155 156 Document formDocument; 157 try { 158 InputSource inputSource = new InputSource (source.getInputStream()); 159 inputSource.setSystemId(source.getURI()); 160 formDocument = DomHelper.parse(inputSource, this.manager); 161 } catch (Exception e) { 162 throw new CascadingException("Could not parse form definition from " + 163 source.getURI(), e); 164 } 165 166 Element formElement = formDocument.getDocumentElement(); 167 formDefinition = getFormDefinition(formElement); 168 this.cacheManager.set(formDefinition, source, PREFIX); 169 } 170 return formDefinition; 171 } 172 173 public FormDefinition getFormDefinition(Element formElement) throws Exception { 174 if (!(formElement.getLocalName().equals("form") || FormsConstants.DEFINITION_NS.equals(formElement.getNamespaceURI()))) 176 throw new Exception ("Expected a Cocoon Forms form element at " + DomHelper.getLocation(formElement)); 177 178 FormDefinitionBuilder formDefinitionBuilder = (FormDefinitionBuilder)widgetDefinitionBuilderSelector.select("form"); 179 return (FormDefinition)formDefinitionBuilder.buildWidgetDefinition(formElement); 180 } 181 182 public FormDefinition createFormDefinition(String uri) throws Exception { 183 SourceResolver sourceResolver = null; 184 Source source = null; 185 Document formDocument = null; 186 187 try { 188 sourceResolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); 189 source = sourceResolver.resolveURI(uri); 190 191 try { 192 InputSource inputSource = new InputSource (source.getInputStream()); 193 inputSource.setSystemId(source.getURI()); 194 formDocument = DomHelper.parse(inputSource, this.manager); 195 } catch (Exception e) { 196 throw new CascadingException("Could not parse form definition from " + 197 source.getURI(), e); 198 } 199 200 } finally { 201 if (source != null) 202 sourceResolver.release(source); 203 if (sourceResolver != null) 204 manager.release(sourceResolver); 205 } 206 207 Element formElement = formDocument.getDocumentElement(); 208 return getFormDefinition(formElement); 209 } 210 211 214 public void dispose() { 215 if (this.widgetDefinitionBuilderSelector != null) { 216 this.widgetDefinitionBuilderSelector.dispose(); 217 this.widgetDefinitionBuilderSelector = null; 218 } 219 if(this.libraryManager != null) { 220 this.libraryManager.dispose(); 221 this.libraryManager = null; 222 } 223 this.manager.release(this.cacheManager); 224 this.cacheManager = null; 225 this.manager = null; 226 } 227 228 229 public class FormServiceManager implements ServiceManager { 230 final String WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE = WidgetDefinitionBuilder.class.getName() + "Selector"; 231 final String LIBRARY_MANAGER_ROLE = LibraryManager.ROLE; 232 233 public Object lookup(String name) throws ServiceException { 234 if (WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE.equals(name)) 235 return widgetDefinitionBuilderSelector; 236 else if(LIBRARY_MANAGER_ROLE.equals(name)) 237 return libraryManager; 238 else 239 return manager.lookup(name); 240 } 241 242 public boolean hasService(String name) { 243 if (WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE.equals(name)) 244 return true; 245 else if(LIBRARY_MANAGER_ROLE.equals(name)) 246 return true; 247 else 248 return manager.hasService(name); 249 } 250 251 public void release(Object service) { 252 if (service != widgetDefinitionBuilderSelector 253 && service != libraryManager) 254 manager.release(service); 255 } 256 } 257 } 258 | Popular Tags |