001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.io;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.externalizer.ExternalizedResource;
018: import org.wings.session.Session;
019: import org.wings.session.SessionManager;
020:
021: import java.io.IOException;
022:
023: /**
024: * The factory creating the output devies for externalized resources.
025: * To declare and use your own device factory (i.e. to compress returned output streams or
026: * log the device output) declare an init property <code>wings.device.factory</code>
027: * in your web xml.
028: * <p>Example:<br/>
029: * <pre> <init-param>
030: <param-name>wings.device.factory</param-name>
031: <param-value>com.mycompany.MyDeviceFactory</param-value>
032: </init-param>
033: </pre>
034: */
035: public abstract class DeviceFactory {
036: private final transient static Log log = LogFactory
037: .getLog(DeviceFactory.class);
038:
039: private static String DEFAULT_DEVICE_FACTORY = "org.wings.io.DeviceFactory$Default";
040:
041: private static DeviceFactory factory;
042:
043: /**
044: * Overrides the current device factory.
045: */
046: public static void setDeviceFactory(DeviceFactory factory) {
047: DeviceFactory.factory = factory;
048: }
049:
050: /**
051: * Returns or lazily creates the current device factory. Use {@link #setDeviceFactory(DeviceFactory)} or
052: * an <code>web.xml</code> init property <code>wings.device.factory</code> to declare an alternative deivce factory.
053: * @return The current device factory.
054: */
055: public static DeviceFactory getDeviceFactory() {
056: if (factory == null) {
057: synchronized (DeviceFactory.class) {
058: if (factory == null) {
059: String className = (String) SessionManager
060: .getSession().getProperty(
061: "wings.device.factory");
062: if (className == null) {
063: className = DEFAULT_DEVICE_FACTORY;
064: }
065:
066: try {
067: Class factoryClass = null;
068: try {
069: factoryClass = Class.forName(className,
070: true, Thread.currentThread()
071: .getContextClassLoader());
072: } catch (ClassNotFoundException e) {
073: // fallback, in case the servlet container fails to set the
074: // context class loader.
075: factoryClass = Class.forName(className);
076: }
077: factory = (DeviceFactory) factoryClass
078: .newInstance();
079: } catch (Exception e) {
080: log.fatal(
081: "could not load wings.device.factory: "
082: + className, e);
083: throw new RuntimeException(
084: "could not load wings.device.factory: "
085: + className + "("
086: + e.getMessage() + ")");
087: }
088: }
089: }
090: }
091: return factory;
092: }
093:
094: /**
095: * Creates a output device for the passed resource using the current device factory.
096: * @param externalizedResource The resource to ouput.
097: */
098: public static Device createDevice(
099: ExternalizedResource externalizedResource)
100: throws IOException {
101: return getDeviceFactory().create(externalizedResource);
102: }
103:
104: protected abstract Device create(
105: ExternalizedResource externalizedResource)
106: throws IOException;
107:
108: /**
109: * Default device factory.
110: */
111: static class Default extends DeviceFactory {
112: protected Device create(
113: ExternalizedResource externalizedResource)
114: throws IOException {
115: final Session session = SessionManager.getSession();
116: return new ServletDevice(session.getServletResponse()
117: .getOutputStream(), session.getCharacterEncoding());
118: /*if (externalizedResource.getExtension().equalsIgnoreCase("html"))
119: return new CachingDevice(new ServletDevice(SessionManager.getSession().getServletResponse().getOutputStream()));
120: else
121: return new ServletDevice(SessionManager.getSession().getServletResponse().getOutputStream());*/
122: }
123: }
124:
125: }
|