1 package org.apache.turbine.util; 2 3 18 19 import java.io.BufferedInputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.InputStream ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.Vector ; 31 import javax.servlet.RequestDispatcher ; 32 import javax.servlet.Servlet ; 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletContext ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.apache.avalon.framework.activity.Disposable; 39 import org.apache.avalon.framework.activity.Initializable; 40 import org.apache.turbine.Turbine; 41 42 74 public class TurbineConfig 75 implements ServletConfig , ServletContext , Initializable, Disposable 76 { 77 81 public static final String CONFIGURATION_PATH_KEY = "configuration"; 82 83 87 public static final String PROPERTIES_PATH_KEY = "properties"; 88 89 93 public static final String PROPERTIES_PATH_DEFAULT = 94 "/WEB-INF/conf/TurbineResources.properties"; 95 96 97 protected File root; 98 99 100 protected Map attributes; 101 102 103 protected Map initParams; 104 105 106 private Turbine turbine; 107 108 109 private Log log = LogFactory.getLog(this.getClass()); 110 111 125 public TurbineConfig(String path, Map attributes, Map initParams) 126 { 127 root = new File (path); 128 this.attributes = attributes; 129 this.initParams = initParams; 130 } 131 132 135 public TurbineConfig(String path, Map initParams) 136 { 137 this(path, new HashMap (0), initParams); 138 } 139 140 149 public TurbineConfig(String path, String properties) 150 { 151 this(path, new HashMap (1)); 152 initParams.put(PROPERTIES_PATH_KEY, properties); 153 } 154 155 161 public void initialize() 162 { 163 try 164 { 165 turbine = new Turbine(); 166 turbine.init(this); 167 } 168 catch (Exception e) 169 { 170 log.error("TurbineConfig: Initialization failed", e); 171 } 172 } 173 174 177 public void init(RunData data) 178 { 179 if (turbine != null) 180 { 181 turbine.init(data); 182 } 183 } 184 185 189 public void dispose() 190 { 191 if (turbine != null) 192 { 193 turbine.destroy(); 194 } 195 } 196 197 202 public ServletContext getServletContext() 203 { 204 return this; 205 } 206 207 215 public String getRealPath(String path) 216 { 217 String result = null; 218 File f = new File (root, path); 219 220 if (log.isDebugEnabled()) 221 { 222 StringBuffer sb = new StringBuffer (); 223 224 sb.append("TurbineConfig.getRealPath: path '"); 225 sb.append(path); 226 sb.append("' translated to '"); 227 sb.append(f.getPath()); 228 sb.append("' "); 229 sb.append(f.exists() ? "" : "not "); 230 sb.append("found"); 231 log.debug(sb.toString()); 232 } 233 234 if (f.exists()) 235 { 236 result = f.getPath(); 237 } 238 else 239 { 240 log.error("getRealPath(\"" + path + "\") is undefined, returning null"); 241 } 242 243 return result; 244 } 245 246 252 public String getInitParameter(String name) 253 { 254 return (String ) initParams.get(name); 255 } 256 257 262 public Enumeration getInitParameterNames() 263 { 264 return new Vector (initParams.keySet()).elements(); 265 } 266 267 274 public String getServletName() 275 { 276 return "Turbine"; 277 } 278 279 286 public String getServletContextName() 287 { 288 return "Turbine"; 289 } 290 291 300 public URL getResource(String s) 301 throws MalformedURLException 302 { 303 return new URL ("file://" + getRealPath(s)); 304 } 305 306 313 public InputStream getResourceAsStream(String s) 314 { 315 try 316 { 317 FileInputStream fis = new FileInputStream (getRealPath(s)); 318 return new BufferedInputStream (fis); 319 } 320 catch (FileNotFoundException e) 321 { 322 return null; 323 } 324 } 325 326 333 public void log(Exception e, String m) 334 { 335 log.info(m, e); 336 } 337 338 343 public void log(String m) 344 { 345 log.info(m); 346 } 347 348 354 public void log(String m, Throwable t) 355 { 356 log.info(m, t); 357 } 358 359 363 public Object getAttribute(String s) 364 { 365 return attributes.get(s); 366 } 367 368 372 public Enumeration getAttributeNames() 373 { 374 return new Vector (attributes.keySet()).elements(); 375 } 376 377 379 386 public ServletContext getContext(String s) 387 { 388 throw new UnsupportedOperationException (); 389 } 390 391 398 public int getMajorVersion() 399 { 400 throw new UnsupportedOperationException (); 401 } 402 403 410 public String getMimeType(String s) 411 { 412 throw new UnsupportedOperationException (); 413 } 414 415 422 public int getMinorVersion() 423 { 424 throw new UnsupportedOperationException (); 425 } 426 427 434 public RequestDispatcher getNamedDispatcher(String s) 435 { 436 throw new UnsupportedOperationException (); 437 } 438 439 446 public RequestDispatcher getRequestDispatcher(String s) 447 { 448 throw new UnsupportedOperationException (); 449 } 450 451 457 public Set getResourcePaths(String s) 458 { 459 throw new UnsupportedOperationException (); 460 } 461 462 468 public String getServerInfo() 469 { 470 throw new UnsupportedOperationException (); 471 } 472 473 480 public Servlet getServlet(String s) 481 { 482 throw new UnsupportedOperationException (); 483 } 484 485 492 public Enumeration getServletNames() 493 { 494 throw new UnsupportedOperationException (); 495 } 496 497 504 public Enumeration getServlets() 505 { 506 throw new UnsupportedOperationException (); 507 } 508 509 515 public void removeAttribute(String s) 516 { 517 throw new UnsupportedOperationException (); 518 } 519 520 526 public void setAttribute(String s, Object o) 527 { 528 throw new UnsupportedOperationException (); 529 } 530 } 531 | Popular Tags |