1 package org.apache.turbine; 2 3 56 57 import java.io.BufferedInputStream ; 58 import java.io.File ; 59 import java.io.FileInputStream ; 60 import java.io.FileNotFoundException ; 61 import java.io.InputStream ; 62 import java.net.MalformedURLException ; 63 import java.net.URL ; 64 import java.util.Enumeration ; 65 import java.util.Hashtable ; 66 import java.util.HashMap ; 67 import java.util.Map ; 68 import java.util.Set ; 69 import java.util.Vector ; 70 71 import javax.servlet.RequestDispatcher ; 72 import javax.servlet.Servlet ; 73 import javax.servlet.ServletConfig ; 74 import javax.servlet.ServletContext ; 75 76 import org.apache.turbine.Turbine; 77 import org.apache.turbine.RunData; 78 import org.apache.commons.logging.LogFactory; 79 import org.apache.commons.logging.Log; 80 81 108 public class TurbineConfig implements ServletConfig , ServletContext 109 { 110 private static final Log log 111 = LogFactory.getLog( TurbineConfig.class ); 112 113 117 public static final String CONFIGURATION_PATH_KEY = "configuration"; 118 119 123 public static final String PROPERTIES_KEY = "properties"; 124 125 126 protected final static boolean DEBUG = false; 127 128 129 protected File root; 130 131 132 protected Map attributes; 133 134 135 protected Map initParams; 136 137 138 protected Turbine turbine; 139 140 153 public TurbineConfig(String path, Map attributes, Map initParams) 154 { 155 root = new File (path); 156 this.attributes = (attributes instanceof Hashtable ? attributes : 159 new Hashtable (attributes)); 160 this.initParams = initParams; 161 } 162 163 166 public TurbineConfig(String path, Map initParams) 167 { 168 this(path, new Hashtable (0), initParams); 169 } 170 171 178 public TurbineConfig(String path, String properties) 179 { 180 this(path, new HashMap (1)); 181 initParams.put(PROPERTIES_KEY, properties); 182 } 183 184 188 public void init() 189 { 190 try 191 { 192 turbine = new Turbine(); 193 turbine.init(this); 194 } 195 catch (Exception e) 196 { 197 String msg = "TurbineConfig: Initialization failed"; 200 try 201 { 202 log.error(msg, e); 203 } 204 catch (Throwable t) 205 { 206 System.err.println(msg); 207 e.printStackTrace(); 208 System.err.println("Couldn't write to log"); 209 t.printStackTrace(); 210 } 211 } 212 } 213 214 217 public void init(RunData data) 218 { 219 if (turbine != null) 220 { 221 turbine.init(data); 222 } 223 } 224 225 230 public ServletContext getServletContext() 231 { 232 return this; 233 } 234 235 243 public String getRealPath( String path ) 244 { 245 File f = new File (root, path); 246 if (DEBUG) 247 { 248 System.err.println("TurbineConfig.getRealPath: path '" + path + 249 "' translated to '" + f.getPath() + "' " + 250 (f.exists() ? "" : "not ") + "found"); 251 } 252 return (f.exists() ? f.getPath() : null); 253 } 254 255 261 public String getInitParameter(String name) 262 { 263 return (String )initParams.get(name); 264 } 265 266 271 public Enumeration getInitParameterNames() 272 { 273 return new Vector (initParams.keySet()).elements(); 274 } 275 276 283 public String getServletName() 284 { 285 return "Turbine"; 286 } 287 288 295 public String getServletContextName() 296 { 297 return "Turbine"; 298 } 299 300 309 public URL getResource( String s ) 310 throws MalformedURLException 311 { 312 return new URL ("file://" + getRealPath(s)); 313 } 314 315 322 public InputStream getResourceAsStream( String s ) 323 { 324 try 325 { 326 FileInputStream fis = new FileInputStream (getRealPath(s)); 327 return new BufferedInputStream (fis); 328 } 329 catch (FileNotFoundException e) 330 { 331 return null; 332 } 333 } 334 335 342 public void log(Exception e, String m) 343 { 344 System.err.println(m); 346 e.printStackTrace(); 347 } 348 349 354 public void log(String m) 355 { 356 System.out.println(m); 358 } 359 360 366 public void log( String m, Throwable t ) 367 { 368 System.err.println(m); 370 t.printStackTrace(); 371 } 372 373 377 public Object getAttribute(String name) 378 { 379 return attributes.get(name); 380 } 381 382 386 public Enumeration getAttributeNames() 387 { 388 return new Vector (attributes.keySet()).elements(); 389 } 390 391 393 400 public ServletContext getContext(String s) 401 { 402 throw new UnsupportedOperationException (); 403 } 404 405 412 public int getMajorVersion() 413 { 414 throw new UnsupportedOperationException (); 415 } 416 417 424 public String getMimeType(String s) 425 { 426 throw new UnsupportedOperationException (); 427 } 428 429 436 public int getMinorVersion() 437 { 438 throw new UnsupportedOperationException (); 439 } 440 441 448 public RequestDispatcher getNamedDispatcher( String s) 449 { 450 throw new UnsupportedOperationException (); 451 } 452 453 460 public RequestDispatcher getRequestDispatcher( String s ) 461 { 462 throw new UnsupportedOperationException (); 463 } 464 465 473 public Set getResourcePaths() 474 { 475 throw new UnsupportedOperationException (); 476 } 477 478 486 public Set getResourcePaths(String s) 487 { 488 throw new UnsupportedOperationException (); 489 } 490 491 498 public String getServerInfo() 499 { 500 throw new UnsupportedOperationException (); 501 } 502 503 511 public Servlet getServlet(String s) 512 { 513 throw new UnsupportedOperationException (); 514 } 515 516 524 public Enumeration getServletNames() 525 { 526 throw new UnsupportedOperationException (); 527 } 528 529 537 public Enumeration getServlets() 538 { 539 throw new UnsupportedOperationException (); 540 } 541 542 545 public void removeAttribute(String name) 546 { 547 attributes.remove(name); 548 } 549 550 553 public void setAttribute(String name, Object obj) 554 { 555 if (obj != null) 556 { 557 attributes.put(name, obj); 558 } 559 else 560 { 561 removeAttribute(name); 562 } 563 } 564 } 565 | Popular Tags |