001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.impl.api.event;
023:
024: import org.jboss.mx.util.MBeanProxy;
025: import org.jboss.mx.util.MBeanProxyCreationException;
026: import org.jboss.mx.util.MBeanServerLocator;
027: import org.jboss.mx.util.ObjectNameFactory;
028: import org.jboss.portal.api.event.PortalEvent;
029: import org.jboss.portal.api.event.PortalEventContext;
030: import org.jboss.portal.api.event.PortalEventListener;
031: import org.jboss.portal.api.session.event.PortalSessionEvent;
032: import org.jboss.portal.api.user.event.UserAuthenticationEvent;
033: import org.jboss.portal.core.event.PortalEventListenerRegistry;
034: import org.jboss.portal.core.impl.api.PortalRuntimeContextImpl;
035:
036: import javax.servlet.ServletContextEvent;
037: import javax.servlet.ServletContextListener;
038: import javax.servlet.http.HttpSessionAttributeListener;
039: import javax.servlet.http.HttpSessionBindingEvent;
040: import javax.servlet.http.HttpSessionEvent;
041: import javax.servlet.http.HttpSessionListener;
042: import java.util.Iterator;
043:
044: /**
045: * Bridge servlet event to portal events.
046: *
047: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
048: * @version $Revision: 8786 $
049: */
050: public class ServletEventBridge implements HttpSessionListener,
051: ServletContextListener, HttpSessionAttributeListener {
052:
053: /** . */
054: private PortalEventListenerRegistry listenerRegistry;
055:
056: // ServletContextListener implementation ****************************************************************************
057:
058: public void contextInitialized(ServletContextEvent event) {
059: try {
060: listenerRegistry = (PortalEventListenerRegistry) MBeanProxy
061: .get(
062: PortalEventListenerRegistry.class,
063: ObjectNameFactory
064: .create("portal:service=ListenerRegistry"),
065: MBeanServerLocator.locateJBoss());
066: } catch (MBeanProxyCreationException e) {
067: e.printStackTrace();
068: }
069: }
070:
071: public void contextDestroyed(ServletContextEvent event) {
072: listenerRegistry = null;
073: }
074:
075: // HttpSessionListener implementation *******************************************************************************
076:
077: public void sessionCreated(HttpSessionEvent event) {
078: PortalRuntimeContextImpl rt = new PortalRuntimeContextImpl(
079: event.getSession());
080: PortalEventContextImpl uec = new PortalEventContextImpl(rt);
081: PortalSessionEvent use = new PortalSessionEvent(
082: PortalSessionEvent.SESSION_CREATED);
083: fireEvent(uec, use);
084: }
085:
086: public void sessionDestroyed(HttpSessionEvent event) {
087: PortalRuntimeContextImpl rt = new PortalRuntimeContextImpl(
088: event.getSession());
089: PortalEventContextImpl uec = new PortalEventContextImpl(rt);
090: PortalSessionEvent use = new PortalSessionEvent(
091: PortalSessionEvent.SESSION_DESTROYED);
092: fireEvent(uec, use);
093: }
094:
095: // HttpSessionAttributeListener implementation **********************************************************************
096:
097: public void attributeAdded(HttpSessionBindingEvent event) {
098: if ("PRINCIPAL_TOKEN".equals(event.getName())) {
099: String userId = (String) event.getValue();
100: PortalRuntimeContextImpl rt = new PortalRuntimeContextImpl(
101: event.getSession(), userId);
102: PortalEventContextImpl uec = new PortalEventContextImpl(rt);
103: UserAuthenticationEvent uae = new UserAuthenticationEvent(
104: userId, UserAuthenticationEvent.SIGN_IN);
105: fireEvent(uec, uae);
106: }
107: }
108:
109: public void attributeRemoved(HttpSessionBindingEvent event) {
110: if ("PRINCIPAL_TOKEN".equals(event.getName())) {
111: String userId = (String) event.getValue();
112: PortalRuntimeContextImpl rt = new PortalRuntimeContextImpl(
113: event.getSession(), userId);
114: PortalEventContextImpl uec = new PortalEventContextImpl(rt);
115: UserAuthenticationEvent uae = new UserAuthenticationEvent(
116: userId, UserAuthenticationEvent.SIGN_OUT);
117: fireEvent(uec, uae);
118: }
119: }
120:
121: public void attributeReplaced(HttpSessionBindingEvent event) {
122: }
123:
124: private void fireEvent(PortalEventContext eventContext,
125: PortalEvent event) {
126: for (Iterator i = listenerRegistry.getListeners().iterator(); i
127: .hasNext();) {
128: Object o = i.next();
129: if (o instanceof PortalEventListener) {
130: PortalEventListener listener = (PortalEventListener) o;
131: try {
132: listener.onEvent(eventContext, event);
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136: }
137: }
138: }
139: }
|