001: package dinamica;
002:
003: import java.io.InputStream;
004: import java.io.PrintWriter;
005: import javax.servlet.ServletContext;
006: import javax.servlet.http.*;
007: import java.sql.Connection;
008:
009: /**
010: * Base class that factorizes reusable behavior
011: * for Output and Transaction classes
012: * <br>
013: * Creation date: 4/10/2003<br>
014: * Last Update: 4/10/2003<br>
015: * (c) 2003 Martin Cordova<br>
016: * This code is released under the LGPL license<br>
017: * @author Martin Cordova
018: */
019: public abstract class AbstractModule {
020:
021: ServletContext _ctx = null;
022: HttpServletRequest _req = null;
023: HttpServletResponse _res = null;
024: Connection _conn = null;
025: Config _config = null;
026: PrintWriter _pw = null;
027:
028: /**
029: * Initialize this object
030: * @param ctx
031: * @param req
032: */
033: public void init(ServletContext ctx, HttpServletRequest req,
034: HttpServletResponse res) {
035: _ctx = ctx;
036: _req = req;
037: _res = res;
038: }
039:
040: /**
041: * Set log writer for Db object (to log jdbc operations)
042: * @param pw
043: */
044: public void setLogWriter(PrintWriter pw) {
045: _pw = pw;
046: }
047:
048: /**
049: * Return framework Db object (wrapper for JDBC operations)
050: * using the connection and the LogWriter passed to this object
051: * @return Db object
052: * @throws Throwable
053: */
054: protected Db getDb() throws Throwable {
055:
056: if (_conn == null)
057: throw new Throwable("Connection object is null!");
058:
059: Db db = new Db(_conn);
060: if (_pw != null)
061: db.setLogWriter(_pw);
062: return db;
063:
064: }
065:
066: /**
067: * Set transaction configuration
068: * @param config
069: */
070: public void setConfig(Config config) {
071: _config = config;
072: }
073:
074: /**
075: * Set database connection
076: * @param connection
077: */
078: public void setConnection(Connection connection) {
079: _conn = connection;
080: }
081:
082: /**
083: * Return UserID for the current security session, if any,
084: * otherwise returns null
085: * @return
086: */
087: protected String getUserName() {
088: return _req.getRemoteUser();
089: }
090:
091: /**
092: * Returns true if user belongs to role
093: * @param roleName Name of the role as defined in WEB.XML
094: * @return
095: */
096: protected boolean isUserInRole(String roleName) {
097: return _req.isUserInRole(roleName);
098: }
099:
100: /**
101: * Return HTTP Session, force session creation if necessary
102: * @return HttpSession reference
103: */
104: protected HttpSession getSession() {
105: HttpSession s = _req.getSession(true);
106: return s;
107: }
108:
109: /**
110: * The application uses a default DataSource.
111: * A connection from this pool is made available
112: * for Transaction modules
113: * @return Default Database Connection
114: */
115: protected Connection getConnection() {
116: return _conn;
117: }
118:
119: /**
120: * @return Servlet Context
121: */
122: protected ServletContext getContext() {
123: return _ctx;
124: }
125:
126: /**
127: * @return Servlet Request object
128: */
129: protected HttpServletRequest getRequest() {
130: return _req;
131: }
132:
133: /**
134: * @return Servlet Response object
135: */
136: protected HttpServletResponse getResponse() {
137: return _res;
138: }
139:
140: /**
141: *
142: * @return Config object
143: */
144: protected Config getConfig() {
145: return _config;
146: }
147:
148: /**
149: * Write message to the log writer
150: * @param message Message to log
151: */
152: protected void log(String message) {
153: _pw.println(message);
154: }
155:
156: /**
157: * Load a text resource from the Action path
158: * (where config.xml is located) or from a relative
159: * path inside the context. The resource may be
160: * a SQL or HTML template, any text based file.
161: * @param fileName Resource file name; if starts with "/" then
162: * it is interpreted as a path relative to the context, otherwise
163: * the Action's path is used.
164: * @return A String containing the resource
165: * @throws Throwable
166: */
167: public String getResource(String fileName) throws Throwable {
168:
169: //ALL CODE PATCHED 2005-02-18 - encoding support
170:
171: String path = null;
172:
173: //relative to the context?
174: if (fileName.startsWith("/")) {
175: path = fileName;
176:
177: //PATCH 2005-08-31 support for ${def:actionroot} on config.xml template element
178: String actionPath = (String) _req
179: .getAttribute("dinamica.action.path");
180: actionPath = actionPath.substring(0, actionPath
181: .lastIndexOf("/"));
182: path = StringUtil.replace(path, "${def:actionroot}",
183: actionPath);
184: //END PATCH
185: } else {
186: path = _config.path + fileName;
187: }
188:
189: //global encoding?
190: String encoding = getContext()
191: .getInitParameter("file-encoding");
192: if (encoding != null && encoding.trim().equals(""))
193: encoding = null;
194:
195: //load resource with appropiate encoding if defined
196: if (_config.templateEncoding != null)
197: return StringUtil.getResource(_ctx, path,
198: _config.templateEncoding);
199: else if (encoding != null)
200: return StringUtil.getResource(_ctx, path, encoding);
201: else
202: return StringUtil.getResource(_ctx, path);
203:
204: }
205:
206: /**
207: * Load a text resource relative to the class location
208: * @param path Pathname of the resource, if it is a filename
209: * then the resource location is assumed in the same path as the class
210: * otherwise may be a sybdirectory relative to the class directory.
211: * @return A String containing the resource
212: * @throws Throwable
213: */
214: protected String getLocalResource(String path) throws Throwable {
215:
216: StringBuffer buf = new StringBuffer(5000);
217: byte[] data = new byte[5000];
218:
219: InputStream in = null;
220:
221: in = this .getClass().getResourceAsStream(path);
222:
223: try {
224: if (in != null) {
225: while (true) {
226: int len = in.read(data);
227: if (len != -1) {
228: buf.append(new String(data, 0, len));
229: } else {
230: break;
231: }
232: }
233: return buf.toString();
234: } else {
235: throw new Throwable("Invalid path to resource: " + path);
236: }
237:
238: } catch (Throwable e) {
239: throw e;
240: } finally {
241: if (in != null) {
242: try {
243: in.close();
244: } catch (Exception e) {
245: }
246: }
247: }
248:
249: }
250:
251: }
|