View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2WebGui library.
13   *
14   *  The Initial Owner of the Original Code is Power Of Two S.R.L.
15   *  Portions created by Power Of Two S.R.L. are Copyright (C) Power Of Two S.R.L.
16   *  All Rights Reserved.
17   *
18   * Contributor(s):
19   */
20  
21  package com.pow2.webgui.resources;
22  
23  import java.util.*;
24  
25  import javax.servlet.*;
26  import javax.servlet.http.*;
27  
28  import org.apache.log4j.*;
29  import org.apache.velocity.app.*;
30  
31  import com.pow2.util.*;
32  import com.pow2.webgui.*;
33  
34  
35  /***
36   *  webGui init servlet.
37   *  <br>
38   *  Register into the WidgetFactory all the prototype widgets
39   *  specified into the webGui-init-file.
40   *  <br>
41   *  This servlet SHOULD start after the log4j initialization servlet
42   *  (see com.pow2.resources.Log4jInit for further details).
43   *  <br>
44   *  Define the following servlet in the web.xml file for your web-application.
45   *  <br>
46   *
47   *  <servlet>
48   *    <servlet-name>webGui-init</servlet-name>
49   *    <servlet-class>com.pow2.webgui.resources.WebGuiInit</servlet-class>
50   *
51   *    <init-param>
52   *      <param-name>webGui.prototypes.properties</param-name>
53   *      <param-value>WEB-INF/wgPrototypes.properties</param-value>
54   *    </init-param>
55   *
56   *    <init-param>
57   *      <param-name>webGui.velocity.properties</param-name>
58   *      <param-value>WEB-INF/velocity.properties</param-value>
59   *    </init-param>
60   *
61   *    <load-on-startup>2</load-on-startup>
62   *  </servlet>
63   *
64   *
65   * @author  Luca Fossato
66   * @created  11 June 2002
67   */
68  public class WebGuiInit extends HttpServlet
69  {
70    protected Category cat = Category.getInstance(this.getClass());
71  
72  
73    /***
74     *  Initialize this servlet.
75     *  <br>
76     *  Register into the WidgetFactory all the prototype widgets
77     *  specified into the webGui-init-file.
78     */
79    public void init() throws ServletException
80    {
81      // set the log4j category;
82      cat = Category.getInstance(this.getClass());
83  
84      String prefix         = getServletContext().getRealPath("/");
85      String protoConfig    = getInitParameter("webGui.prototypes.properties");
86      String velocityConfig = getInitParameter("webGui.velocity.properties");
87  
88      // register the prototype widget classes provided by
89      // the 'webGui.prototypes' properties file into the
90      // WidgetFactory;
91      if (!Util.isNull(protoConfig))
92      {
93        WidgetFactory wf = WidgetFactory.instance();
94  
95        try
96        {
97          wf.setWidgets(prefix + protoConfig);
98        }
99        catch(Exception e)
100       {
101         cat.error("::init - cannot set a widget", e);
102         throw new ServletException(e.getMessage());
103       }
104 
105       cat.info("::init - webGui prototypes successfull registered.");
106     }
107     else
108       cat.error("::init - cannot configure webGui. Prototype file is: [" + (prefix + protoConfig) + "]");
109 
110 
111     // setting the properties for Velocity template engine;
112     Properties props = null;
113     velocityConfig = (prefix + velocityConfig);
114 
115     try
116     {
117       props = FileUtil.getProperties(velocityConfig);
118       String path = props.getProperty("file.resource.loader.path");
119 
120       // prepend the servlet context path to the
121       // file.resource.loader.path property value;
122       if (path != null)
123       {
124         path = getServletContext().getRealPath(path);
125         props.setProperty("file.resource.loader.path", path);
126       }
127 
128       Velocity.init(props);
129 
130       cat.info("::init - webGui Velocity template successfully configured using [" + velocityConfig + "] file.");
131       cat.info("::init - Velocity template path: [" + path + "]");
132     }
133     catch (Exception e)
134     {
135       cat.error("::init - cannot configure webGui Velcity engine. Velocity config file is: [" + velocityConfig + "]", e);
136     }
137   }
138 }