KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > struts > plugin > FormViewPlugIn


1 package net.sourceforge.formview.struts.plugin;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.UnavailableException JavaDoc;
13
14 import net.sourceforge.formview.FormViewResources;
15 import net.sourceforge.formview.displayer.DisplayersConfigResources;
16 import net.sourceforge.formview.util.WEBFormViewUtil;
17 import net.sourceforge.formview.validator.ExtendedValidatorResources;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.struts.action.ActionServlet;
22 import org.apache.struts.action.PlugIn;
23 import org.apache.struts.config.ModuleConfig;
24 import org.xml.sax.SAXException JavaDoc;
25
26 /**
27  * Description :
28  * Created on 19 janv. 2006
29  * @author azerr
30  */

31 public class FormViewPlugIn implements PlugIn {
32
33     /**
34      * Commons Logging instance.
35      */

36     private static Log log = LogFactory.getLog(FormViewPlugIn.class);
37
38     /**
39      * The {@link ActionServlet} owning this application.
40      */

41     private ActionServlet servlet = null;
42
43     /**
44      * Delimitter for Validator resources.
45      */

46     private final static String JavaDoc RESOURCE_DELIM = ",";
47
48     /**
49      * The set of Form instances that have been created and initialized,
50      * keyed by the struts form name.
51      */

52     protected FormViewResources resources = null;
53     
54     // ------------------------------------------------------------- Properties
55

56     /**
57      * A comma delimitted list of FormView resource.
58      */

59     private String JavaDoc formViewConfig = null;
60
61     /**
62      * Gets a comma delimitted list of FormView resources.
63      *
64      * @return comma delimited list of FormView resource path names
65      */

66     public String JavaDoc getFormViewConfig() {
67         return formViewConfig;
68     }
69
70     /**
71      * Sets a comma delimitted list of FormView resources.
72      *
73      * @param formViewConfig delimited list of FormView resource path names
74      */

75     public void setFormViewConfig(String JavaDoc formViewConfig) {
76         this.formViewConfig = formViewConfig;
77     }
78     
79     /**
80      * A comma delimitted list of Validator resource.
81      */

82     private String JavaDoc validatorConfig = null;
83
84     /**
85      * Gets a comma delimitted list of Validator resources.
86      *
87      * @return comma delimited list of Validator resource path names
88      */

89     public String JavaDoc getValidatorConfig() {
90         return validatorConfig;
91     }
92
93     /**
94      * Sets a comma delimitted list of Validator resources.
95      *
96      * @param validatorConfig delimited list of Validator resource path names
97      */

98     public void setValidatorConfig(String JavaDoc validatorConfig) {
99         this.validatorConfig = validatorConfig;
100     }
101  
102     /**
103      * A comma delimitted list of Displayer Config resource.
104      */

105     private String JavaDoc displayerConfig = null;
106
107     /**
108      * Gets a comma delimitted list of DisplayerConfig resources.
109      *
110      * @return comma delimited list of DisplayerConfig resource path names
111      */

112     public String JavaDoc getDisplayerConfig() {
113         return displayerConfig;
114     }
115
116     /**
117      * Sets a comma delimitted list of DisplayerConfig resources.
118      *
119      * @param displayerConfig delimited list of DisplayerConfig resource path names
120      */

121     public void setDisplayerConfig(String JavaDoc displayerConfig) {
122         this.displayerConfig = displayerConfig;
123     }
124     
125     public void init(ActionServlet servlet, ModuleConfig config)
126             throws ServletException JavaDoc {
127           // Remember our associated configuration and servlet
128

129         this.servlet = servlet;
130
131         // Load our database from persistent storage
132
try {
133             this.initFormViewResources();
134             // Save FormViewResources into Servlet Context
135
WEBFormViewUtil.saveFormViewResources(servlet.getServletContext(), resources);
136             
137         } catch (Exception JavaDoc e) {
138             log.error(e.getMessage(), e);
139             throw new UnavailableException JavaDoc("Cannot load a form view resource from '" + formViewConfig + "'");
140         }
141         // Merge with validator if it exist
142
if (validatorConfig != null && validatorConfig.length() > 0) {
143             try {
144                 this.mergeWithValidator();
145             } catch (Exception JavaDoc e) {
146                 log.error(e.getMessage(), e);
147                 throw new UnavailableException JavaDoc("Cannot merge validator with form view resource from '" + validatorConfig + "'");
148             }
149         }
150         // Merge with displayerConfig
151
try {
152             this.mergeWithDisplayerConfig();
153         } catch (Exception JavaDoc e) {
154             log.error(e.getMessage(), e);
155             throw new UnavailableException JavaDoc("Cannot merge displayer config with form view resource from '" + displayerConfig + "'");
156         }
157     }
158     
159     
160     public void destroy() {
161         if (log.isDebugEnabled()) {
162             log.debug("Destroying FormViewPlugIn");
163         }
164     }
165     
166     /**
167      * Initialize the form view resources for this module.
168      *
169      * @throws IOException if an input/output error is encountered
170      * @throws ServletException if we cannot initialize these resources
171      */

172     protected void initFormViewResources() throws IOException JavaDoc, ServletException JavaDoc {
173
174         if (formViewConfig == null || formViewConfig.length() <= 0) {
175             return;
176         }
177         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(formViewConfig, RESOURCE_DELIM);
178
179         List JavaDoc streamList = new ArrayList JavaDoc();
180         try {
181             while (st.hasMoreTokens()) {
182                 String JavaDoc formViewRules = st.nextToken().trim();
183                 if (log.isInfoEnabled()) {
184                     log.info("Loading form-view rules file from '" + formViewRules + "'");
185                 }
186
187                 InputStream JavaDoc input = servlet.getServletContext().getResourceAsStream(formViewRules);
188                 
189                 // If the config isn't in the servlet context, try the class loader
190
// which allows the config files to be stored in a jar
191
if (input == null) {
192                     input = getClass().getResourceAsStream(formViewRules);
193                 }
194
195                 if (input != null) {
196                     BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(input);
197                     streamList.add(bis);
198                 } else {
199                     throw new ServletException JavaDoc("Skipping form-view rules file from '"
200                               + formViewRules + "'. No stream could be opened.");
201                 }
202             }
203             int streamSize = streamList.size();
204             InputStream JavaDoc[] streamArray = new InputStream JavaDoc[streamSize];
205             for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) {
206                 InputStream JavaDoc is = (InputStream JavaDoc) streamList.get(streamIndex);
207                 streamArray[streamIndex] = is;
208             }
209
210             this.resources = new FormViewResources(streamArray);
211         } catch (SAXException JavaDoc sex) {
212             log.error("Skipping all form-view",sex);
213             throw new ServletException JavaDoc(sex);
214         } finally {
215             Iterator JavaDoc streamIterator = streamList.iterator();
216             while (streamIterator.hasNext()) {
217                 InputStream JavaDoc is = (InputStream JavaDoc) streamIterator.next();
218                 is.close();
219             }
220         }
221     }
222     
223
224     /**
225      * Initialize the validator resources for this module.
226      *
227      * @throws IOException if an input/output error is encountered
228      * @throws ServletException if we cannot initialize these resources
229      */

230     protected void mergeWithValidator() throws IOException JavaDoc, ServletException JavaDoc {
231
232         if (validatorConfig == null || validatorConfig.length() <= 0) {
233             return;
234         }
235         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(validatorConfig, RESOURCE_DELIM);
236
237         List JavaDoc streamList = new ArrayList JavaDoc();
238         try {
239             while (st.hasMoreTokens()) {
240                 String JavaDoc validatorRules = st.nextToken().trim();
241                 if (log.isInfoEnabled()) {
242                     log.info("Loading validation rules file from '" + validatorRules + "'");
243                 }
244
245                 InputStream JavaDoc input = servlet.getServletContext().getResourceAsStream(validatorRules);
246                 
247                 // If the config isn't in the servlet context, try the class loader
248
// which allows the config files to be stored in a jar
249
if (input == null) {
250                     input = getClass().getResourceAsStream(validatorRules);
251                 }
252
253                 if (input != null) {
254                     BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(input);
255                     streamList.add(bis);
256                 } else {
257                     throw new ServletException JavaDoc("Skipping validation rules file from '"
258                               + validatorRules + "'. No stream could be opened.");
259                 }
260             }
261             int streamSize = streamList.size();
262             InputStream JavaDoc[] streamArray = new InputStream JavaDoc[streamSize];
263             for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) {
264                 InputStream JavaDoc is = (InputStream JavaDoc) streamList.get(streamIndex);
265                 streamArray[streamIndex] = is;
266             }
267
268             ExtendedValidatorResources validatorResources = new ExtendedValidatorResources(streamArray);
269             this.resources.mergeFormSetWithValidator(validatorResources);
270             
271         } catch (SAXException JavaDoc sex) {
272             log.error("Skipping all validation",sex);
273             throw new ServletException JavaDoc(sex);
274         } finally {
275             Iterator JavaDoc streamIterator = streamList.iterator();
276             while (streamIterator.hasNext()) {
277                 InputStream JavaDoc is = (InputStream JavaDoc) streamIterator.next();
278                 is.close();
279             }
280         }
281
282     }
283     
284     public void mergeWithDisplayerConfig() throws IOException JavaDoc, ServletException JavaDoc {
285         // Merge with DisplayersConfigResources
286
DisplayersConfigResources displayersResources = null;
287         if (displayerConfig == null || displayerConfig.length() <= 0) {
288             InputStream JavaDoc inDisplayerConfig = null;
289             try {
290                 // Load default Displayer Config
291
inDisplayerConfig = DisplayersConfigResources.class.getResourceAsStream("displayers-config.xml");
292                 displayersResources = new DisplayersConfigResources(inDisplayerConfig);
293             } catch (SAXException JavaDoc sex) {
294                 log.error("Skipping default displayers config",sex);
295                 throw new ServletException JavaDoc(sex);
296              } finally {
297 // Make sure we close the input stream.
298
if (inDisplayerConfig != null) {
299                     inDisplayerConfig.close();
300                  }
301              }
302         }
303         else {
304             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(displayerConfig, RESOURCE_DELIM);
305     
306             List JavaDoc streamList = new ArrayList JavaDoc();
307             try {
308                 while (st.hasMoreTokens()) {
309                     String JavaDoc displayerConfigRules = st.nextToken().trim();
310                     if (log.isInfoEnabled()) {
311                         log.info("Loading displayer-config rules file from '" + displayerConfigRules + "'");
312                     }
313     
314                     InputStream JavaDoc input = servlet.getServletContext().getResourceAsStream(displayerConfigRules);
315                     
316                     // If the config isn't in the servlet context, try the class loader
317
// which allows the config files to be stored in a jar
318
if (input == null) {
319                         input = getClass().getResourceAsStream(displayerConfigRules);
320                     }
321     
322                     if (input != null) {
323                         BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(input);
324                         streamList.add(bis);
325                     } else {
326                         throw new ServletException JavaDoc("Skipping displayer-config rules file from '"
327                                   + displayerConfigRules + "'. No stream could be opened.");
328                     }
329                 }
330                 int streamSize = streamList.size();
331                 InputStream JavaDoc[] streamArray = new InputStream JavaDoc[streamSize];
332                 for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) {
333                     InputStream JavaDoc is = (InputStream JavaDoc) streamList.get(streamIndex);
334                     streamArray[streamIndex] = is;
335                 }
336     
337                 displayersResources = new DisplayersConfigResources(streamArray);
338             } catch (SAXException JavaDoc sex) {
339                 log.error("Skipping all form-view",sex);
340                 throw new ServletException JavaDoc(sex);
341             } finally {
342                 Iterator JavaDoc streamIterator = streamList.iterator();
343                 while (streamIterator.hasNext()) {
344                     InputStream JavaDoc is = (InputStream JavaDoc) streamIterator.next();
345                     is.close();
346                 }
347             }
348         }
349         // Merge with FormView Resources
350
if (displayersResources != null)
351             resources.mergeDisplayersWithDisplayerConfig(displayersResources);
352          
353     }
354     
355     /**
356      * Destroy <code>FormViewResources</code>.
357      */

358     protected void destroyResources() {
359         resources = null;
360     }
361     
362 }
363
Popular Tags