KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > Listener


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.webapp;
30
31 import com.caucho.config.BuilderProgram;
32 import com.caucho.config.Config;
33 import com.caucho.config.ConfigException;
34 import com.caucho.config.NodeBuilderProgram;
35 import com.caucho.config.types.InitProgram;
36 import com.caucho.util.L10N;
37
38 import javax.servlet.ServletContextAttributeListener JavaDoc;
39 import javax.servlet.ServletContextListener JavaDoc;
40 import javax.servlet.ServletRequestAttributeListener JavaDoc;
41 import javax.servlet.ServletRequestListener JavaDoc;
42 import javax.servlet.http.HttpSessionActivationListener JavaDoc;
43 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
44 import javax.servlet.http.HttpSessionListener JavaDoc;
45
46 /**
47  * Configuration for the listener
48  */

49 public class Listener {
50   static L10N L = new L10N(Listener.class);
51
52   // The listener class
53
private Class JavaDoc _listenerClass;
54
55   // The listener object
56
private Object JavaDoc _object;
57   
58   private InitProgram _init;
59
60   /**
61    * Sets the listener class.
62    */

63   public void setListenerClass(Class JavaDoc cl)
64     throws ConfigException
65   {
66     Config.checkCanInstantiate(cl);
67     
68     if (ServletContextListener JavaDoc.class.isAssignableFrom(cl)) {
69     }
70     else if (ServletContextAttributeListener JavaDoc.class.isAssignableFrom(cl)) {
71     }
72     else if (ServletRequestListener JavaDoc.class.isAssignableFrom(cl)) {
73     }
74     else if (ServletRequestAttributeListener JavaDoc.class.isAssignableFrom(cl)) {
75     }
76     else if (HttpSessionListener JavaDoc.class.isAssignableFrom(cl)) {
77     }
78     else if (HttpSessionAttributeListener JavaDoc.class.isAssignableFrom(cl)) {
79     }
80     else if (HttpSessionActivationListener JavaDoc.class.isAssignableFrom(cl)) {
81     }
82     else
83       throw new ConfigException(L.l("listener-class '{0}' does not implement any web-app listener interface.",
84                     cl.getName()));
85     
86     _listenerClass = cl;
87   }
88
89   /**
90    * Gets the listener class.
91    */

92   public Class JavaDoc getListenerClass()
93   {
94     return _listenerClass;
95   }
96
97   /**
98    * Sets the init block
99    */

100   public void setInit(InitProgram init)
101   {
102     _init = init;
103   }
104
105   /**
106    * Gets the init block
107    */

108   public InitProgram getInit()
109   {
110     return _init;
111   }
112
113   /**
114    * Initialize.
115    */

116   public Object JavaDoc createListenerObject()
117     throws Exception JavaDoc
118   {
119     if (_object != null)
120       return _object;
121     
122       _object = _listenerClass.newInstance();
123     
124     InitProgram init = getInit();
125     BuilderProgram program;
126
127     if (init != null)
128       program = init.getBuilderProgram();
129     else
130       program = NodeBuilderProgram.NULL;
131
132     program.configure(_object);
133     program.init(_object);
134
135     return _object;
136   }
137
138   public String JavaDoc toString()
139   {
140     return "Listener[" + _listenerClass + "]";
141   }
142 }
143
Popular Tags