Source Code Cross Referenced for AbstractModule.java in  » J2EE » Dinamica » dinamica » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Dinamica » dinamica 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.