001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.webapp;
030:
031: import com.caucho.config.program.ConfigProgram;
032: import com.caucho.config.Config;
033: import com.caucho.config.ConfigException;
034: import com.caucho.config.program.ContainerProgram;
035: import com.caucho.config.types.DescriptionGroupConfig;
036: import com.caucho.util.L10N;
037: import com.caucho.webbeans.manager.*;
038:
039: import javax.servlet.ServletContextAttributeListener;
040: import javax.servlet.ServletContextListener;
041: import javax.servlet.ServletRequestAttributeListener;
042: import javax.servlet.ServletRequestListener;
043: import javax.servlet.http.HttpSessionActivationListener;
044: import javax.servlet.http.HttpSessionAttributeListener;
045: import javax.servlet.http.HttpSessionListener;
046:
047: /**
048: * Configuration for the listener
049: */
050: public class Listener extends DescriptionGroupConfig {
051: static L10N L = new L10N(Listener.class);
052:
053: // The listener class
054: private Class _listenerClass;
055:
056: // The listener object
057: private Object _object;
058:
059: private ContainerProgram _init;
060:
061: /**
062: * Sets the listener class.
063: */
064: public void setListenerClass(Class cl) throws ConfigException {
065: Config.checkCanInstantiate(cl);
066:
067: if (ServletContextListener.class.isAssignableFrom(cl)) {
068: } else if (ServletContextAttributeListener.class
069: .isAssignableFrom(cl)) {
070: } else if (ServletRequestListener.class.isAssignableFrom(cl)) {
071: } else if (ServletRequestAttributeListener.class
072: .isAssignableFrom(cl)) {
073: } else if (HttpSessionListener.class.isAssignableFrom(cl)) {
074: } else if (HttpSessionAttributeListener.class
075: .isAssignableFrom(cl)) {
076: } else if (HttpSessionActivationListener.class
077: .isAssignableFrom(cl)) {
078: } else
079: throw new ConfigException(
080: L
081: .l(
082: "listener-class '{0}' does not implement any web-app listener interface.",
083: cl.getName()));
084:
085: _listenerClass = cl;
086: }
087:
088: /**
089: * Gets the listener class.
090: */
091: public Class getListenerClass() {
092: return _listenerClass;
093: }
094:
095: /**
096: * Sets the init block
097: */
098: public void setInit(ContainerProgram init) {
099: _init = init;
100: }
101:
102: /**
103: * Gets the init block
104: */
105: public ContainerProgram getInit() {
106: return _init;
107: }
108:
109: /**
110: * Initialize.
111: */
112: public Object createListenerObject() throws Exception {
113: if (_object != null)
114: return _object;
115:
116: WebBeansContainer webBeans = WebBeansContainer.create();
117:
118: _object = webBeans.getEnvironmentObject(_listenerClass);
119:
120: if (_init != null)
121: _init.configure(_object);
122:
123: return _object;
124: }
125:
126: public String toString() {
127: return "Listener[" + _listenerClass + "]";
128: }
129: }
|