1 package dinamica; 2 3 import java.io.InputStream ; 4 import java.io.PrintWriter ; 5 import javax.servlet.ServletContext ; 6 import javax.servlet.http.*; 7 import java.sql.Connection ; 8 9 19 public abstract class AbstractModule 20 { 21 22 ServletContext _ctx = null; 23 HttpServletRequest _req = null; 24 HttpServletResponse _res = null; 25 Connection _conn = null; 26 Config _config = null; 27 PrintWriter _pw = null; 28 29 30 35 public void init(ServletContext ctx, HttpServletRequest req, HttpServletResponse res) 36 { 37 _ctx = ctx; 38 _req = req; 39 _res = res; 40 } 41 42 46 public void setLogWriter(PrintWriter pw) 47 { 48 _pw = pw; 49 } 50 51 57 protected Db getDb() throws Throwable  58 { 59 60 if (_conn==null) 61 throw new Throwable ("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 74 public void setConfig(Config config) 75 { 76 _config = config; 77 } 78 79 83 public void setConnection(Connection connection) 84 { 85 _conn = connection; 86 } 87 88 93 protected String getUserName() 94 { 95 return _req.getRemoteUser(); 96 } 97 98 103 protected boolean isUserInRole(String roleName) 104 { 105 return _req.isUserInRole(roleName); 106 } 107 108 112 protected HttpSession getSession() 113 { 114 HttpSession s = _req.getSession(true); 115 return s; 116 } 117 118 124 protected Connection getConnection() 125 { 126 return _conn; 127 } 128 129 132 protected ServletContext getContext() 133 { 134 return _ctx; 135 } 136 137 140 protected HttpServletRequest getRequest() 141 { 142 return _req; 143 } 144 145 148 protected HttpServletResponse getResponse() 149 { 150 return _res; 151 } 152 153 157 protected Config getConfig() 158 { 159 return _config; 160 } 161 162 166 protected void log(String message) 167 { 168 _pw.println(message); 169 } 170 171 182 public String getResource(String fileName) throws Throwable  183 { 184 185 187 String path = null; 188 189 if (fileName.startsWith("/")) 191 { 192 path = fileName; 193 194 String actionPath = (String )_req.getAttribute("dinamica.action.path"); 196 actionPath = actionPath.substring(0, actionPath.lastIndexOf("/")); 197 path = StringUtil.replace(path, "${def:actionroot}", actionPath); 198 } 200 else 201 { 202 path = _config.path + fileName; 203 } 204 205 String encoding = getContext().getInitParameter("file-encoding"); 207 if (encoding!=null && encoding.trim().equals("")) 208 encoding = null; 209 210 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 228 protected String getLocalResource(String path) throws Throwable  229 { 230 231 StringBuffer buf = new StringBuffer (5000); 232 byte[] data = new byte[5000]; 233 234 InputStream 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 (data,0,len)); 248 } 249 else 250 { 251 break; 252 } 253 } 254 return buf.toString(); 255 } 256 else 257 { 258 throw new Throwable ("Invalid path to resource: " + path); 259 } 260 261 } 262 catch (Throwable e) 263 { 264 throw e; 265 } 266 finally 267 { 268 if (in!=null) 269 { 270 try{ 271 in.close(); 272 } catch (Exception e){} 273 } 274 } 275 276 } 277 278 } 279
| Popular Tags
|