KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > DefaultFormManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47
48 /**
49  * Component implementing the {@link FormManager} role.
50  *
51  * @version $Id: DefaultFormManager.java 326838 2005-10-20 06:26:53Z sylvain $
52  */

53 public class DefaultFormManager
54   extends AbstractLogEnabled
55   implements FormManager, Contextualizable, ThreadSafe, Serviceable, Disposable, Configurable, Component, Initializable {
56 // FIXME: Component is there to allow this block to also run in the 2.1 branch
57

58     protected static final String JavaDoc 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     /** Temporary internal method, don't rely on it's existence! Needed to access the context from flowscript. */
72     // FIXME (SW). Extending the FOM is needed.
73
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     /**
83      * Configurable
84      */

85     public void configure(Configuration configuration) throws ConfigurationException {
86         this.configuration = configuration;
87     }
88
89     public void initialize() throws Exception JavaDoc {
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 JavaDoc {
109         FormDefinition formDefinition = getFormDefinition(source);
110         Form form = (Form)formDefinition.createInstance();
111         form.initialize();
112         return form;
113     }
114
115     public Form createForm(String JavaDoc uri) throws Exception JavaDoc {
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 JavaDoc formElement) throws Exception JavaDoc {
136         Form form = (Form)getFormDefinition(formElement).createInstance();
137         form.initialize();
138         return form;
139     }
140
141     public FormDefinition createFormDefinition(Element JavaDoc formElement) throws Exception JavaDoc {
142         return getFormDefinition(formElement);
143     }
144
145     public FormDefinition getFormDefinition(Source source) throws Exception JavaDoc {
146         FormDefinition formDefinition = (FormDefinition)this.cacheManager.get(source, PREFIX);
147         
148         if(formDefinition != null && formDefinition.getLocalLibrary().dependenciesHaveChanged())
149             formDefinition = null; // invalidate
150

151         if (formDefinition == null) {
152             
153             if(getLogger().isDebugEnabled())
154                 getLogger().debug("Building Form: "+source.getURI());
155             
156             Document JavaDoc formDocument;
157             try {
158                 InputSource JavaDoc inputSource = new InputSource JavaDoc(source.getInputStream());
159                 inputSource.setSystemId(source.getURI());
160                 formDocument = DomHelper.parse(inputSource, this.manager);
161             } catch (Exception JavaDoc e) {
162                 throw new CascadingException("Could not parse form definition from " +
163                                              source.getURI(), e);
164             }
165
166             Element JavaDoc 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 JavaDoc formElement) throws Exception JavaDoc {
174         // check that the root element is a fd:form element
175
if (!(formElement.getLocalName().equals("form") || FormsConstants.DEFINITION_NS.equals(formElement.getNamespaceURI())))
176             throw new Exception JavaDoc("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 JavaDoc uri) throws Exception JavaDoc {
183         SourceResolver sourceResolver = null;
184         Source source = null;
185         Document JavaDoc formDocument = null;
186
187         try {
188             sourceResolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
189             source = sourceResolver.resolveURI(uri);
190
191             try {
192                 InputSource JavaDoc inputSource = new InputSource JavaDoc(source.getInputStream());
193                 inputSource.setSystemId(source.getURI());
194                 formDocument = DomHelper.parse(inputSource, this.manager);
195             } catch (Exception JavaDoc 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 JavaDoc formElement = formDocument.getDocumentElement();
208         return getFormDefinition(formElement);
209     }
210
211     /**
212      * Disposable
213      */

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 JavaDoc WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE = WidgetDefinitionBuilder.class.getName() + "Selector";
231         final String JavaDoc LIBRARY_MANAGER_ROLE = LibraryManager.ROLE;
232
233         public Object JavaDoc lookup(String JavaDoc 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 JavaDoc 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 JavaDoc service) {
252             if (service != widgetDefinitionBuilderSelector
253                     && service != libraryManager)
254                 manager.release(service);
255         }
256     }
257 }
258
Popular Tags