KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > AbstractModule


1 package dinamica;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import javax.servlet.ServletContext JavaDoc;
6 import javax.servlet.http.*;
7 import java.sql.Connection JavaDoc;
8
9 /**
10  * Base class that factorizes reusable behavior
11  * for Output and Transaction classes
12  * <br>
13  * Creation date: 4/10/2003<br>
14  * Last Update: 4/10/2003<br>
15  * (c) 2003 Martin Cordova<br>
16  * This code is released under the LGPL license<br>
17  * @author Martin Cordova
18  */

19 public abstract class AbstractModule
20 {
21
22     ServletContext JavaDoc _ctx = null;
23     HttpServletRequest _req = null;
24     HttpServletResponse _res = null;
25     Connection JavaDoc _conn = null;
26     Config _config = null;
27     PrintWriter JavaDoc _pw = null;
28     
29
30     /**
31      * Initialize this object
32      * @param ctx
33      * @param req
34      */

35     public void init(ServletContext JavaDoc ctx, HttpServletRequest req, HttpServletResponse res)
36     {
37         _ctx = ctx;
38         _req = req;
39         _res = res;
40     }
41
42     /**
43      * Set log writer for Db object (to log jdbc operations)
44      * @param pw
45      */

46     public void setLogWriter(PrintWriter JavaDoc pw)
47     {
48         _pw = pw;
49     }
50
51     /**
52      * Return framework Db object (wrapper for JDBC operations)
53      * using the connection and the LogWriter passed to this object
54      * @return Db object
55      * @throws Throwable
56      */

57     protected Db getDb() throws Throwable JavaDoc
58     {
59         
60         if (_conn==null)
61             throw new Throwable JavaDoc("Connection object is null!");
62         
63         Db db = new Db(_conn);
64         if (_pw!=null)
65             db.setLogWriter(_pw);
66         return db;
67         
68     }
69
70     /**
71      * Set transaction configuration
72      * @param config
73      */

74     public void setConfig(Config config)
75     {
76         _config = config;
77     }
78
79     /**
80      * Set database connection
81      * @param connection
82      */

83     public void setConnection(Connection JavaDoc connection)
84     {
85         _conn = connection;
86     }
87
88     /**
89      * Return UserID for the current security session, if any,
90      * otherwise returns null
91      * @return
92      */

93     protected String JavaDoc getUserName()
94     {
95         return _req.getRemoteUser();
96     }
97
98     /**
99      * Returns true if user belongs to role
100      * @param roleName Name of the role as defined in WEB.XML
101      * @return
102      */

103     protected boolean isUserInRole(String JavaDoc roleName)
104     {
105         return _req.isUserInRole(roleName);
106     }
107
108     /**
109      * Return HTTP Session, force session creation if necessary
110      * @return HttpSession reference
111      */

112     protected HttpSession getSession()
113     {
114         HttpSession s = _req.getSession(true);
115         return s;
116     }
117
118     /**
119      * The application uses a default DataSource.
120      * A connection from this pool is made available
121      * for Transaction modules
122      * @return Default Database Connection
123      */

124     protected Connection JavaDoc getConnection()
125     {
126         return _conn;
127     }
128
129     /**
130      * @return Servlet Context
131      */

132     protected ServletContext JavaDoc getContext()
133     {
134         return _ctx;
135     }
136
137     /**
138      * @return Servlet Request object
139      */

140     protected HttpServletRequest getRequest()
141     {
142         return _req;
143     }
144
145     /**
146      * @return Servlet Response object
147      */

148     protected HttpServletResponse getResponse()
149     {
150         return _res;
151     }
152
153     /**
154      *
155      * @return Config object
156      */

157     protected Config getConfig()
158     {
159         return _config;
160     }
161
162     /**
163      * Write message to the log writer
164      * @param message Message to log
165      */

166     protected void log(String JavaDoc message)
167     {
168         _pw.println(message);
169     }
170
171     /**
172      * Load a text resource from the Action path
173      * (where config.xml is located) or from a relative
174      * path inside the context. The resource may be
175      * a SQL or HTML template, any text based file.
176      * @param fileName Resource file name; if starts with "/" then
177      * it is interpreted as a path relative to the context, otherwise
178      * the Action's path is used.
179      * @return A String containing the resource
180      * @throws Throwable
181      */

182     public String JavaDoc getResource(String JavaDoc fileName) throws Throwable JavaDoc
183     {
184         
185         //ALL CODE PATCHED 2005-02-18 - encoding support
186

187         String JavaDoc path = null;
188         
189         //relative to the context?
190
if (fileName.startsWith("/"))
191         {
192             path = fileName;
193             
194             //PATCH 2005-08-31 support for ${def:actionroot} on config.xml template element
195
String JavaDoc actionPath = (String JavaDoc)_req.getAttribute("dinamica.action.path");
196             actionPath = actionPath.substring(0, actionPath.lastIndexOf("/"));
197             path = StringUtil.replace(path, "${def:actionroot}", actionPath);
198             //END PATCH
199
}
200         else
201         {
202             path = _config.path + fileName;
203         }
204         
205         //global encoding?
206
String JavaDoc encoding = getContext().getInitParameter("file-encoding");
207         if (encoding!=null && encoding.trim().equals(""))
208             encoding = null;
209         
210         //load resource with appropiate encoding if defined
211
if (_config.templateEncoding!=null)
212             return StringUtil.getResource(_ctx, path, _config.templateEncoding);
213         else if (encoding!=null)
214             return StringUtil.getResource(_ctx, path, encoding);
215         else
216             return StringUtil.getResource(_ctx, path);
217         
218     }
219
220     /**
221      * Load a text resource relative to the class location
222      * @param path Pathname of the resource, if it is a filename
223      * then the resource location is assumed in the same path as the class
224      * otherwise may be a sybdirectory relative to the class directory.
225      * @return A String containing the resource
226      * @throws Throwable
227      */

228     protected String JavaDoc getLocalResource(String JavaDoc path) throws Throwable JavaDoc
229     {
230
231         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(5000);
232         byte[] data = new byte[5000];
233
234         InputStream JavaDoc in = null;
235         
236         in = this.getClass().getResourceAsStream(path);
237         
238         try
239         {
240             if (in!=null)
241             {
242                 while (true)
243                 {
244                     int len = in.read(data);
245                     if (len!=-1)
246                     {
247                         buf.append(new String JavaDoc(data,0,len));
248                     }
249                     else
250                     {
251                         break;
252                     }
253                 }
254                 return buf.toString();
255             }
256             else
257             {
258                 throw new Throwable JavaDoc("Invalid path to resource: " + path);
259             }
260             
261         }
262         catch (Throwable JavaDoc e)
263         {
264             throw e;
265         }
266         finally
267         {
268             if (in!=null)
269             {
270                 try{
271                     in.close();
272                 } catch (Exception JavaDoc e){}
273             }
274         }
275                 
276     }
277
278 }
279
Popular Tags