View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 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.HashMap; 66 import java.util.Map; 67 import java.util.Set; 68 import java.util.Vector; 69 import javax.servlet.RequestDispatcher; 70 import javax.servlet.Servlet; 71 import javax.servlet.ServletConfig; 72 import javax.servlet.ServletContext; 73 import org.apache.turbine.Turbine; 74 import org.apache.turbine.services.TurbineServices; 75 76 /*** 77 * A class used for initalization of Turbine without a servlet container. 78 * 79 * If you need to use Turbine outside of a servlet container, you can 80 * use this class for initalization of the Turbine servlet.<br> 81 * 82 * <blockquote><code><pre> 83 * TurbineConfig config = new TurbineConfig(".", "/conf/TurbineResources.properties"); 84 * </pre></code></blockquote> 85 * 86 * All paths referenced in TurbineResources.properties and the path to 87 * the properties file itself (the second argument) will be resolved 88 * relative to the directory given as the first argument of the constructor, 89 * here - the directory where application was started. Don't worry about 90 * discarding the references to objects created above. They are not needed, 91 * once everything is initialized. 92 * 93 * In order to initialize the Services Framework outside of the Turbine Servlet, 94 * you need to call the <code>init()</code> method. By default, this will 95 * initialize the Resource and Logging Services and any other services you 96 * have defined in your TurbineResources.properties file. 97 * 98 * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a> 99 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 100 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a> 101 * @version $Id: TurbineConfig.java,v 1.2 2002/07/11 16:53:21 mpoeschl Exp $ 102 */ 103 public class TurbineConfig implements ServletConfig, ServletContext 104 { 105 /*** Enables output of debug messages (compile time option). */ 106 private final static boolean DEBUG = false; 107 108 /*** Filenames are looked up in this directory. */ 109 private File root; 110 111 /*** Servlet container (or emulator) attributes. */ 112 private Map attributes; 113 114 /*** Turbine servlet initialization parameters. */ 115 private Map initParams; 116 117 /*** The Turbine servlet instance used for initialization. */ 118 private Turbine turbine; 119 120 /*** 121 * Constructs a new TurbineConfig. 122 * 123 * This is the general form of the constructor. You can provide 124 * a path to search for files, and a name-value map of init 125 * parameters. 126 * 127 * <p> For the list of recognized init parameters, see 128 * {@link org.apache.turbine.Turbine} class. 129 * 130 * @param path The web application root (i.e. the path for file lookup). 131 * @param attributes Servlet container (or emulator) attributes. 132 * @param initParams initialization parameters. 133 */ 134 public TurbineConfig(String path, Map attributes, Map initParams) 135 { 136 root = new File(path); 137 this.attributes = attributes; 138 this.initParams = initParams; 139 } 140 141 /*** 142 * @see #TurbineConfig(String path, Map attributes, Map initParams) 143 */ 144 public TurbineConfig(String path, Map initParams) 145 { 146 this(path, new HashMap(0), initParams); 147 } 148 149 /*** 150 * Constructs a TurbineConfig. 151 * 152 * This is a specialized constructor that allows to configure 153 * Turbine easiliy in the common setups. 154 * 155 * @param path The web application root (i.e. the path for file lookup). 156 * @param properties the relative path to TurbineResources.properties file 157 */ 158 public TurbineConfig(String path, String properties) 159 { 160 this(path, new HashMap(1)); 161 initParams.put(TurbineServices.PROPERTIES_PATH_KEY, properties); 162 } 163 164 /*** 165 * Causes this class to initialize itself which in turn initializes 166 * all of the Turbine Services that need to be initialized. 167 */ 168 public void init() 169 { 170 try 171 { 172 turbine = new Turbine(); 173 turbine.init(this); 174 } 175 catch (Exception e) 176 { 177 Log.error("TurbineConfig: Initialization failed", e); 178 } 179 } 180 181 /*** 182 * Initialization requiring a HTTP <code>GET</code> request. 183 */ 184 public void init(RunData data) 185 { 186 if (turbine != null) 187 { 188 turbine.init(data); 189 } 190 } 191 192 /*** 193 * Returns a reference to the object cast onto ServletContext type. 194 * 195 * @return a ServletContext reference 196 */ 197 public ServletContext getServletContext() 198 { 199 return this; 200 } 201 202 /*** 203 * Translates a path relative to the web application root into an 204 * absolute path. 205 * 206 * @param path A path relative to the web application root. 207 * @return An absolute version of the supplied path, or <code>null</code> 208 * if the translated path doesn't map to a file or directory. 209 */ 210 public String getRealPath( String path ) 211 { 212 File f = new File(root, path); 213 if (DEBUG) 214 { 215 System.err.println("TurbineConfig.getRealPath: path '" + path + 216 "' translated to '" + f.getPath() + "' " + 217 (f.exists() ? "" : "not ") + "found"); 218 } 219 return (f.exists() ? f.getPath() : null); 220 } 221 222 /*** 223 * Retrieves an initialization parameter. 224 * 225 * @param name the name of the parameter. 226 * @return the value of the parameter. 227 */ 228 public String getInitParameter(String name) 229 { 230 return (String)initParams.get(name); 231 } 232 233 /*** 234 * Retrieves an Enumeration of initialization parameter names. 235 * 236 * @return an Enumeration of initialization parameter names. 237 */ 238 public Enumeration getInitParameterNames() 239 { 240 return new Vector(initParams.keySet()).elements(); 241 } 242 243 /*** 244 * Returns the servlet name. 245 * 246 * Fixed value "Turbine" is returned. 247 * 248 * @return the servlet name. 249 */ 250 public String getServletName() 251 { 252 return "Turbine"; 253 } 254 255 /*** 256 * Returns the context name. 257 * 258 * Fixed value "Turbine" is returned 259 * 260 * @return the context name 261 */ 262 public String getServletContextName() 263 { 264 return "Turbine"; 265 } 266 267 /*** 268 * Returns a URL to the resource that is mapped to a specified 269 * path. The path must begin with a "/" and is interpreted 270 * as relative to the current context root. 271 * 272 * @param s the path to the resource 273 * @return a URL pointing to the resource 274 * @exception MalformedURLException 275 */ 276 public URL getResource( String s ) 277 throws MalformedURLException 278 { 279 return new URL("file://" + getRealPath(s)); 280 } 281 282 /*** 283 * Returns the resource located at the named path as 284 * an <code>InputStream</code> object. 285 * 286 * @param s the path to the resource 287 * @return an InputStream object from which the resource can be read 288 */ 289 public InputStream getResourceAsStream( String s ) 290 { 291 try 292 { 293 FileInputStream fis = new FileInputStream(getRealPath(s)); 294 return new BufferedInputStream(fis); 295 } 296 catch(FileNotFoundException e) 297 { 298 return null; 299 } 300 } 301 302 /*** 303 * Logs an error message. 304 * 305 * @param e an Exception. 306 * @param m a message. 307 * @deprecated 308 */ 309 public void log(Exception e, String m) 310 { 311 // cannot use Turbine logging yet. 312 System.err.println(m); 313 e.printStackTrace(); 314 } 315 316 /*** 317 * Logs a message. 318 * 319 * @param m a message. 320 */ 321 public void log(String m) 322 { 323 // cannot use Turbine logging yet. 324 System.out.println(m); 325 } 326 327 /*** 328 * Logs an error message. 329 * 330 * @param t a Throwable object. 331 * @param m a message. 332 */ 333 public void log( String m, Throwable t ) 334 { 335 // cannot use Turbine logging yet. 336 System.err.println(m); 337 t.printStackTrace(); 338 } 339 340 /*** 341 * Returns the servlet container attribute with the given name, or 342 * null if there is no attribute by that name. 343 */ 344 public Object getAttribute(String s) 345 { 346 return attributes.get(s); 347 } 348 349 /*** 350 * Returns an Enumeration containing the attribute names available 351 * within this servlet context. 352 */ 353 public Enumeration getAttributeNames() 354 { 355 return new Vector(attributes.keySet()).elements(); 356 } 357 358 // Unimplemented methods follow 359 360 /*** 361 * Not implemented. 362 * 363 * A method in ServletConfig or ServletContext interface that is not 364 * implemented and will throw <code>UnsuportedOperationException</code> 365 * upon invocation 366 */ 367 public ServletContext getContext(String s) 368 { 369 throw new UnsupportedOperationException(); 370 } 371 372 /*** 373 * Not implemented. 374 * 375 * A method in ServletConfig or ServletContext interface that is not 376 * implemented and will throw <code>UnsuportedOperationException</code> 377 * upon invocation 378 */ 379 public int getMajorVersion() 380 { 381 throw new UnsupportedOperationException(); 382 } 383 384 /*** 385 * Not implemented. 386 * 387 * A method in ServletConfig or ServletContext interface that is not 388 * implemented and will throw <code>UnsuportedOperationException</code> 389 * upon invocation 390 */ 391 public String getMimeType(String s) 392 { 393 throw new UnsupportedOperationException(); 394 } 395 396 /*** 397 * Not implemented. 398 * 399 * A method in ServletConfig or ServletContext interface that is not 400 * implemented and will throw <code>UnsuportedOperationException</code> 401 * upon invocation 402 */ 403 public int getMinorVersion() 404 { 405 throw new UnsupportedOperationException(); 406 } 407 408 /*** 409 * Not implemented. 410 * 411 * A method in ServletConfig or ServletContext interface that is not 412 * implemented and will throw <code>UnsuportedOperationException</code> 413 * upon invocation 414 */ 415 public RequestDispatcher getNamedDispatcher( String s) 416 { 417 throw new UnsupportedOperationException(); 418 } 419 420 /*** 421 * Not implemented. 422 * 423 * A method in ServletConfig or ServletContext interface that is not 424 * implemented and will throw <code>UnsuportedOperationException</code> 425 * upon invocation 426 */ 427 public RequestDispatcher getRequestDispatcher( String s ) 428 { 429 throw new UnsupportedOperationException(); 430 } 431 432 /*** 433 * Not implemented. 434 * 435 * A method in ServletConfig or ServletContext interface that is not 436 * implemented and will throw <code>UnsuportedOperationException</code> 437 * upon invocation 438 * @deprecated 439 */ 440 public Set getResourcePaths() 441 { 442 throw new UnsupportedOperationException(); 443 } 444 445 /*** 446 * Not implemented. 447 * 448 * A method in ServletConfig or ServletContext interface that is not 449 * implemented and will throw <code>UnsuportedOperationException</code> 450 * upon invocation 451 * @deprecated 452 */ 453 public Set getResourcePaths(String s) 454 { 455 throw new UnsupportedOperationException(); 456 } 457 458 /*** 459 * Not implemented. 460 * 461 * A method in ServletConfig or ServletContext interface that is not 462 * implemented and will throw <code>UnsuportedOperationException</code> 463 * upon invocation 464 */ 465 public String getServerInfo() 466 { 467 throw new UnsupportedOperationException(); 468 } 469 470 /*** 471 * Not implemented. 472 * 473 * A method in ServletConfig or ServletContext interface that is not 474 * implemented and will throw <code>UnsuportedOperationException</code> 475 * upon invocation 476 * @deprecated 477 */ 478 public Servlet getServlet(String s) 479 { 480 throw new UnsupportedOperationException(); 481 } 482 483 /*** 484 * Not implemented. 485 * 486 * A method in ServletConfig or ServletContext interface that is not 487 * implemented and will throw <code>UnsuportedOperationException</code> 488 * upon invocation 489 * @deprecated 490 */ 491 public Enumeration getServletNames() 492 { 493 throw new UnsupportedOperationException(); 494 } 495 496 /*** 497 * Not implemented. 498 * 499 * A method in ServletConfig or ServletContext interface that is not 500 * implemented and will throw <code>UnsuportedOperationException</code> 501 * upon invocation 502 * @deprecated 503 */ 504 public Enumeration getServlets() 505 { 506 throw new UnsupportedOperationException(); 507 } 508 509 /*** 510 * Not implemented. 511 * 512 * A method in ServletConfig or ServletContext interface that is not 513 * implemented and will throw <code>UnsuportedOperationException</code> 514 * upon invocation 515 */ 516 public void removeAttribute( String s ) 517 { 518 throw new UnsupportedOperationException(); 519 } 520 521 /*** 522 * Not implemented. 523 * 524 * A method in ServletConfig or ServletContext interface that is not 525 * implemented and will throw <code>UnsuportedOperationException</code> 526 * upon invocation 527 */ 528 public void setAttribute( String s, Object o ) 529 { 530 throw new UnsupportedOperationException(); 531 } 532 }

This page was automatically generated by Maven