KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > io > DeviceFactory


1 /*
2  * $Id: DeviceFactory.java,v 1.4 2004/12/01 07:54:08 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
22
23 public abstract class DeviceFactory {
24     private final transient static Log log = LogFactory.getLog(DeviceFactory.class);
25
26     private static String JavaDoc 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 JavaDoc className = (String JavaDoc) SessionManager.getSession().getProperty("wings.device.factory");
39                     if (className == null)
40                         className = DEFAULT_DEVICE_FACTORY;
41
42                     try {
43                         Class JavaDoc factoryClass = null;
44                         try {
45                             factoryClass = Class.forName(className, true,
46                                     Thread.currentThread()
47                                     .getContextClassLoader());
48                         } catch (ClassNotFoundException JavaDoc e) {
49                             // fallback, in case the servlet container fails to set the
50
// context class loader.
51
factoryClass = Class.forName(className);
52                         }
53                         factory = (DeviceFactory) factoryClass.newInstance();
54                     } catch (Exception JavaDoc e) {
55                         log.fatal("could not load wings.device.factory: " +
56                                 className, e);
57                         throw new RuntimeException JavaDoc("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 JavaDoc {
68         return getDeviceFactory().create(externalizedResource);
69     }
70
71     protected abstract Device create(ExternalizedResource externalizedResource) throws IOException JavaDoc;
72
73     static class Default
74             extends DeviceFactory {
75         protected Device create(ExternalizedResource externalizedResource)
76                 throws IOException JavaDoc {
77             return new ServletDevice(SessionManager.getSession().getServletResponse().getOutputStream());
78         }
79     }
80 }
81
Popular Tags