1 14 package org.wings.io; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.externalizer.ExternalizedResource; 19 import org.wings.session.SessionManager; 20 21 import java.io.IOException ; 22 23 public abstract class DeviceFactory { 24 private final transient static Log log = LogFactory.getLog(DeviceFactory.class); 25 26 private static String DEFAULT_DEVICE_FACTORY = "org.wings.io.DeviceFactory$Default"; 27 28 private static DeviceFactory factory; 29 30 public static void setDeviceFactory(DeviceFactory factory) { 31 DeviceFactory.factory = factory; 32 } 33 34 public static DeviceFactory getDeviceFactory() { 35 if (factory == null) { 36 synchronized (DeviceFactory.class) { 37 if (factory == null) { 38 String className = (String ) SessionManager.getSession().getProperty("wings.device.factory"); 39 if (className == null) 40 className = DEFAULT_DEVICE_FACTORY; 41 42 try { 43 Class factoryClass = null; 44 try { 45 factoryClass = Class.forName(className, true, 46 Thread.currentThread() 47 .getContextClassLoader()); 48 } catch (ClassNotFoundException e) { 49 factoryClass = Class.forName(className); 52 } 53 factory = (DeviceFactory) factoryClass.newInstance(); 54 } catch (Exception e) { 55 log.fatal("could not load wings.device.factory: " + 56 className, e); 57 throw new RuntimeException ("could not load wings.device.factory: " + 58 className + "(" + e.getMessage() + ")"); 59 } 60 } 61 } 62 } 63 return factory; 64 } 65 66 public static Device createDevice(ExternalizedResource externalizedResource) 67 throws IOException { 68 return getDeviceFactory().create(externalizedResource); 69 } 70 71 protected abstract Device create(ExternalizedResource externalizedResource) throws IOException ; 72 73 static class Default 74 extends DeviceFactory { 75 protected Device create(ExternalizedResource externalizedResource) 76 throws IOException { 77 return new ServletDevice(SessionManager.getSession().getServletResponse().getOutputStream()); 78 } 79 } 80 } 81 | Popular Tags |