KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > servlet > FormViewServlet


1 package net.sourceforge.formview.servlet;
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.ServletConfig JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.UnavailableException JavaDoc;
14 import javax.servlet.http.HttpServlet JavaDoc;
15
16 import net.sourceforge.formview.FormViewResources;
17 import net.sourceforge.formview.displayer.DisplayersConfigResources;
18 import net.sourceforge.formview.util.WEBFormViewUtil;
19 import net.sourceforge.formview.validator.ExtendedValidatorResources;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.xml.sax.SAXException JavaDoc;
24
25 /**
26  *
27  * Description : FormView Servlet wich store FormView resources
28  * into Servlet Context. This Servlet must be used if you
29  * want use Formview without Struts (otherwise use Plguin Struts)
30  * Exemple of config :
31  * <!-- Servlet FormView which load config of FormView -->
32   <servlet>
33       <servlet-name>formview</servlet-name>
34         <servlet-class>net.sourceforge.formview.servlet.FormViewServlet</servlet-class>
35         <!-- FormView Config files (Required) -->
36         <init-param>
37             <param-name>formViewConfig</param-name>
38             <param-value>/WEB-INF/form-view.xml,/WEB-INF/form-view2.xml</param-value>
39         </init-param>
40         <!-- Validator Config files (Not required) -->
41         <init-param>
42             <param-name>validatorConfig</param-name>
43             <param-value>/WEB-INF/validation.xml</param-value>
44         </init-param>
45         <!-- Displayer Config files (Not required) -->
46         <init-param>
47             <param-name>displayerConfig</param-name>
48             <param-value>/WEB-INF/displayers-config.xml</param-value>
49         </init-param>
50   </servlet>
51  * @version 1.0.0
52  * @author <a HREF="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
53  *
54  */

55 public class FormViewServlet extends HttpServlet JavaDoc {
56
57     public static final long serialVersionUID = 1L;
58     
59     /**
60      * Commons Logging instance.
61      */

62     private static Log log = LogFactory.getLog(FormViewServlet.class);
63
64     /**
65      * Delimitter for Validator resources.
66      */

67     private final static String JavaDoc RESOURCE_DELIM = ",";
68
69     /**
70      * The set of Form instances that have been created and initialized,
71      * keyed by the struts form name.
72      */

73     protected FormViewResources resources = null;
74     
75     // ------------------------------------------------------------- Properties
76

77     /**
78      * A comma delimitted list of FormView resource.
79      */

80     private String JavaDoc formViewConfig = null;
81
82     /**
83      * A comma delimitted list of Validator resource.
84      */

85     private String JavaDoc validatorConfig = null;
86
87  
88     /**
89      * A comma delimitted list of Displayer Config resource.
90      */

91     private String JavaDoc displayerConfig = null;
92     
93     public void init()
94             throws ServletException JavaDoc {
95         ServletConfig JavaDoc servletConfig = getServletConfig();
96         // get FormView config
97
this.formViewConfig = servletConfig.getInitParameter("formViewConfig");
98         if (formViewConfig == null && formViewConfig.length() < 1) {
99             log.error("Error FormViewServlet : The parameter with name 'formViewConfig' (see section <init-param> of your web.xml) is required.");
100             throw new UnavailableException JavaDoc("Error FormViewServlet : The parameter with name 'formViewConfig' (see section <init-param> of your web.xml) is required.");
101         }
102         // get Displayer config
103
this.displayerConfig = servletConfig.getInitParameter("displayerConfig");
104         
105         // get Validator config
106
this.validatorConfig = servletConfig.getInitParameter("validatorConfig");
107
108         // Load our database from persistent storage
109
try {
110             this.initFormViewResources();
111             // Save FormViewResources into Servlet Context
112
WEBFormViewUtil.saveFormViewResources(getServletContext(), resources);
113             
114         } catch (Exception JavaDoc e) {
115             log.error(e.getMessage(), e);
116             throw new UnavailableException JavaDoc("Cannot load a form view resource from '" + formViewConfig + "'");
117         }
118         // Merge with validator if it exist
119
if (validatorConfig != null && validatorConfig.length() > 0) {
120             try {
121                 this.mergeWithValidator();
122             } catch (Exception JavaDoc e) {
123                 log.error(e.getMessage(), e);
124                 throw new UnavailableException JavaDoc("Cannot merge validator with form view resource from '" + validatorConfig + "'");
125             }
126         }
127         // Merge with displayerConfig
128
try {
129             this.mergeWithDisplayerConfig();
130         } catch (Exception JavaDoc e) {
131             log.error(e.getMessage(), e);
132             throw new UnavailableException JavaDoc("Cannot merge displayer config with form view resource from '" + displayerConfig + "'");
133         }
134     }
135     
136     
137     public void destroy() {
138         if (log.isDebugEnabled()) {
139             log.debug("Destroying FormViewServlet");
140         }
141     }
142     
143     /**
144      * Initialize the form view resources for this module.
145      *
146      * @throws IOException if an input/output error is encountered
147      * @throws ServletException if we cannot initialize these resources
148      */

149     protected void initFormViewResources() throws IOException JavaDoc, ServletException JavaDoc {
150
151         if (formViewConfig == null || formViewConfig.length() <= 0) {
152             return;
153         }
154         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(formViewConfig, RESOURCE_DELIM);
155
156         List JavaDoc streamList = new ArrayList JavaDoc();
157         try {
158             while (st.hasMoreTokens()) {
159                 String JavaDoc formViewRules = st.nextToken().trim();
160                 if (log.isInfoEnabled()) {
161                     log.info("Loading form-view rules file from '" + formViewRules + "'");
162                 }
163
164                 InputStream JavaDoc input = getServletContext().getResourceAsStream(formViewRules);
165                 
166                 // If the config isn't in the servlet context, try the class loader
167
// which allows the config files to be stored in a jar
168
if (input == null) {
169                     input = getClass().getResourceAsStream(formViewRules);
170                 }
171
172                 if (input != null) {
173                     BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(input);
174                     streamList.add(bis);
175                 } else {
176                     throw new ServletException JavaDoc("Skipping form-view rules file from '"
177                               + formViewRules + "'. No stream could be opened.");
178                 }
179             }
180             int streamSize = streamList.size();
181             InputStream JavaDoc[] streamArray = new InputStream JavaDoc[streamSize];
182             for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) {
183                 InputStream JavaDoc is = (InputStream JavaDoc) streamList.get(streamIndex);
184                 streamArray[streamIndex] = is;
185             }
186
187             this.resources = new FormViewResources(streamArray);
188         } catch (SAXException JavaDoc sex) {
189             log.error("Skipping all form-view",sex);
190             throw new ServletException JavaDoc(sex);
191         } finally {
192             Iterator JavaDoc streamIterator = streamList.iterator();
193             while (streamIterator.hasNext()) {
194                 InputStream JavaDoc is = (InputStream JavaDoc) streamIterator.next();
195                 is.close();
196             }
197         }
198     }
199     
200
201     /**
202      * Initialize the validator resources for this module.
203      *
204      * @throws IOException if an input/output error is encountered
205      * @throws ServletException if we cannot initialize these resources
206      */

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