1 package de.webman.template.jsp; 2 3 import java.util.Enumeration ; 4 import java.io.*; 5 import java.util.Properties ; 6 import javax.servlet.*; 7 import javax.servlet.http.*; 8 import javax.xml.parsers.DocumentBuilder ; 9 import javax.xml.parsers.DocumentBuilderFactory ; 10 import com.teamkonzept.lib.*; 11 import com.teamkonzept.web.TKEvent; 12 import com.teamkonzept.lib.templates.*; 13 import org.apache.jasper.*; 14 import org.w3c.dom.Document ; 15 import org.w3c.dom.Element ; 16 import org.apache.log4j.Category; 17 import org.apache.log4j.Priority; 18 import javax.servlet.jsp.*; 19 import com.teamkonzept.webman.ProcessStarter; 20 21 27 public class JSPTemplate 28 implements TemplateBasic, 29 TemplateTypes 30 31 { 32 33 34 private static final WebManJspFactory JSP_FACTORY = new WebManJspFactory(); 35 36 37 private static final String POSTFIX = "_webmanTemplate"; 38 39 private DOMTemplateData data; 40 41 42 private static Category cat = Category.getInstance(JSPTemplate.class); 43 44 45 private String name; 46 47 48 private long lastModified; 49 50 51 private HttpJspPage jspPage; 52 53 54 private static final String COMPILE_DIR = "compiled"; 55 56 57 private String text; 58 59 60 private String rootDir; 61 62 63 private boolean editor; 64 65 66 private HttpServletRequest originalRequest; 67 68 69 private WebManRequest request; 70 71 79 public JSPTemplate (String _docRoot, 80 String _name, 81 boolean _editor) 82 throws TKTemplateSyntaxException 83 { 84 try 85 { 86 rootDir = _docRoot; 87 name = _name; 88 editor = _editor; 89 data = new DOMTemplateData(); 90 } 91 catch (Exception e) 92 { 93 cat.error("JSPTemplate:constructor " , e); 94 } 95 init(); 96 } 97 98 public void setOriginalRequest(HttpServletRequest request) 99 { 100 originalRequest = request; 101 } 102 103 public String getType() 104 { 105 return JSP_TEMPLATE; 106 } 107 108 public HttpServletRequest getRequest() 109 { 110 return request; 111 } 112 113 public DOMTemplateData getDOMData() 114 { 115 return data; 116 } 117 118 protected void resetData() 119 { 120 try 121 { 122 data = new DOMTemplateData(); 123 originalRequest = null; 124 } 125 catch (Exception e) 126 { 127 cat.error("Unable to reset Data", e); 128 } 129 } 130 131 134 void init() throws TKTemplateSyntaxException 135 { 136 try 137 { 138 String [] args = new String [8]; 140 args[0] = "-uriroot"; 141 if (editor) 142 args[1] = rootDir + TemplateUtils.getWebmanTemplateDirectory() + File.separator; 143 else 144 args[1] = rootDir + TemplateUtils.getGenerationDirectory(); 145 146 149 Priority prio = cat.getPriority(); 150 if (prio == null) 151 args[2] = "-v0"; 152 else if (prio.equals(Priority.ERROR)) 153 args[2] = "-v2"; 154 else if (prio.equals(Priority.WARN)) 155 args[2] = "-v4"; 156 else if (prio.equals(Priority.INFO)) 157 args[2] = "-v6"; 158 else 159 args[2] = "-v9"; 160 args[3] = "-d"; 162 args[4] = rootDir + COMPILE_DIR; args[5] ="-c"; 164 String className = name.substring(0, name.indexOf(".")) + POSTFIX; 165 args[6] = className; String fileName = ""; 167 if (editor) 168 fileName = rootDir + TemplateUtils.getWebmanTemplateDirectory() + File.separator + name; 169 else 170 fileName = rootDir + TemplateUtils.getGenerationDirectory() +name; 171 args[7] = fileName; 172 String pathToClass = rootDir + COMPILE_DIR + File.separator + className + ".class"; 173 174 long preCompiled = new File(pathToClass).lastModified(); 176 File f = new File(fileName); 177 lastModified = f.lastModified(); 178 180 Properties props = System.getProperties(); 182 String path = props.getProperty("java.class.path"); 183 if (path == null) 184 path = ""; 185 String append = ProcessStarter.getWMClasspath(rootDir); 186 props.setProperty("java.class.path", path + ";"+ append); 187 System.setProperties(props); 188 189 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 190 PrintStream ps = new PrintStream(bout); 191 192 JspC jspc = new JspC(args, ps); 193 jspc.parseFiles(ps); 194 ps.flush(); 195 long postCompiled = new File(pathToClass).lastModified(); 197 if (! (postCompiled > preCompiled)) throw new TKTemplateSyntaxException("Unable to compile !" + bout.toString()); 199 } 200 catch (TKTemplateSyntaxException e) 201 { 202 throw e; 203 } 204 catch (Throwable t) 205 { 206 cat.error("JSPTemplate:init " , t); 207 } 208 } 209 210 213 public void set( TKHashtable aSubst ) 214 { 215 data.set(aSubst); 216 } 217 218 219 public void set( String key, Object val ) 220 { 221 data.set(key, val); 222 } 223 224 227 public void initRequest() 228 { 229 if (jspPage == null) 230 { 231 try 232 { 233 initJspPage(); 234 } 235 catch (ServletException e) 236 { 237 cat.error("initRequest: " + e.getMessage(), e); 238 } 239 } 240 request = new WebManRequest(jspPage.getServletConfig().getServletContext()); 241 } 242 243 private Class loadClass() 244 { 245 try 246 { 247 JSPClassLoader loader = new JSPClassLoader(rootDir + File.separator + COMPILE_DIR, getClass().getClassLoader()); 248 Class c = loader.loadClass(name.substring(0, name.indexOf("."))+ POSTFIX); 249 return c; 250 } 251 catch (ClassNotFoundException e) 252 { 253 cat.error("JSPTemplate:loadClass" , e); 254 return null; 255 } 256 } 257 258 261 private boolean isOutdated() throws TKTemplateSyntaxException 262 { 263 String fileName = ""; 264 if (editor) 265 fileName = rootDir + TemplateUtils.getWebmanTemplateDirectory() + File.separator + name; 266 else 267 fileName = rootDir + TemplateUtils.getGenerationDirectory() +name; 268 File f = new File(fileName); 269 long l = f.lastModified(); 270 if ( l > lastModified) 272 { 273 cat.debug("Reload Template " + name); 274 init(); 275 jspPage = null; 276 return true; 277 } 278 return false; 279 } 280 281 public synchronized void sendData(Element node, PrintWriter out) { 283 try 284 { 285 if (jspPage == null || isOutdated()) 286 { 287 initJspPage(); 288 } 289 if (request == null) 290 request = new WebManRequest(jspPage.getServletConfig().getServletContext()); request.setData(node); 292 request.setOriginalRequest(originalRequest); 293 WebManResponse response = new WebManResponse(out); 294 jspPage._jspService(request, response); 295 296 out.flush(); 297 } 298 catch (ServletException ee) 299 { 300 Throwable t = ee.getRootCause() ; 301 cat.error("JSPTemplate.sendData: " + t.getMessage() , t); 302 } 303 catch (Exception e) 304 { 305 cat.error("JSPTemplate.sendData: " + e.getMessage() , e); 306 } 307 finally 308 { 309 resetData(); 310 } 311 } 312 313 private void initJspPage() throws ServletException 314 { 315 try 316 { 317 Class cl = loadClass(); 318 if (cl == null) 319 { 320 cat.error("Class not found !!!! " + name); return; 322 } 323 Object obj = cl.newInstance(); 324 jspPage = (HttpJspPage)obj; 325 javax.servlet.jsp.JspFactory.setDefaultFactory(JSP_FACTORY); 326 WebManServletConfig config = new WebManServletConfig(); 328 TKEvent evt = TKEvent.getEventForThread(); 329 if (evt != null) 330 { 331 ((WebManServletContext)config.getServletContext()).setDocRoot(evt.getHttpInterface().getDocumentRoot()); 332 } 333 jspPage.init(config); 334 jspPage.jspInit(); 335 } 336 catch (IllegalAccessException e) 337 { 338 cat.error("JSPTemplate.initJspPage: " + e.getMessage() , e); 339 } 340 catch (InstantiationException ee) 341 { 342 cat.error("JSPTemplate.initJspPage: " + ee.getMessage() , ee); 343 } 344 } 345 352 public String getText() 353 { 354 return text; 355 } 356 357 364 public void printTemplate( PrintStream out ) 365 { 366 sendData(data.getRoot(), new PrintWriter(out)); 367 } 368 369 376 public void printTemplate( Writer writer ) throws IOException 377 { 378 sendData(data.getRoot(), new PrintWriter(writer)); 379 } 380 381 388 public void printTemplate() 389 { 390 throw new RuntimeException ("Methode not implemented !"); 391 } 392 393 406 public void printTemplate(String file) throws IOException 407 { 408 FileWriter w = new FileWriter(file); 409 sendData(data.getRoot(), new PrintWriter(w)); 410 } 411 412 413 422 public void doTagSubstitution() throws TKTemplateSyntaxException 423 { 424 doMainSubstitutions(); 425 } 426 427 432 public void doMainSubstitutions() throws TKTemplateSyntaxException 433 { 434 438 } 439 440 } 441 | Popular Tags |