001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021:
022: package org.josso.spring;
023:
024: import org.josso.gateway.event.security.SSOSecurityEventManagerImpl;
025: import org.josso.gateway.event.security.SSOEventNotification;
026: import org.josso.gateway.event.SSOEvent;
027: import org.josso.gateway.event.SSOEventListener;
028: import org.josso.SSOConfigurationEventListener;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.springframework.jmx.export.notification.NotificationPublisherAware;
032: import org.springframework.jmx.export.notification.NotificationPublisher;
033:
034: import javax.management.*;
035: import java.util.List;
036: import java.util.ArrayList;
037:
038: /**
039: * JMX Based event manager. This manager is a SSOSecurityEventManager and a MBean.
040: * It uses JMX Notification scheme to deliver SSO Events.
041: *
042: * @author <a href="mailto:ggarcia@josso.org">Gustavo Garcia</a>
043: */
044:
045: public class SpringJMXSSOEventManagerImpl extends
046: SSOSecurityEventManagerImpl implements
047: NotificationPublisherAware, NotificationListener {
048:
049: public static final Log logger = LogFactory
050: .getLog(SpringJMXSSOEventManagerImpl.class);
051:
052: private NotificationPublisher publisher;
053:
054: private List _jossoEventListeners;
055:
056: private static long sequnece;
057:
058: public SpringJMXSSOEventManagerImpl() {
059: super ();
060: this ._jossoEventListeners = new ArrayList();
061: }
062:
063: protected Notification buildNotification(SSOEvent event) {
064: return new SSOEventNotification(event, sequnece++);
065: }
066:
067: public void fireSSOEvent(SSOEvent event) {
068: try {
069:
070: this .publisher.sendNotification(buildNotification(event));
071:
072: if (logger.isDebugEnabled())
073: logger.debug("Sent notification : " + event);
074:
075: } catch (Exception e) {
076: logger.error("Can't send JMX Notification : "
077: + e.getMessage(), e);
078: }
079: }
080:
081: public void setNotificationPublisher(
082: NotificationPublisher notificationPublisher) {
083: this .publisher = notificationPublisher;
084: }
085:
086: public void handleNotification(Notification notification,
087: Object handback) {
088: if (logger.isDebugEnabled())
089: logger.debug("Received notification : "
090: + notification.getType());
091:
092: if (notification instanceof SSOEventNotification) {
093: try {
094: String eventType = notification.getType();
095: // Submit this to all the chain of listeners.
096: for (int i = 0; i < _jossoEventListeners.size(); i++) {
097: if (_jossoEventListeners.get(i) instanceof SSOConfigurationEventListener) {
098: SSOConfigurationEventListener listener = (SSOConfigurationEventListener) _jossoEventListeners
099: .get(i);
100: if (listener.isEventEnabled(eventType,
101: notification))
102: listener.handleEvent(eventType,
103: notification);
104: } else {
105: SSOEventListener listener = (SSOEventListener) _jossoEventListeners
106: .get(i);
107: listener
108: .handleSSOEvent(((SSOEventNotification) notification)
109: .getEvent());
110: }
111: }
112:
113: } catch (Exception e) {
114: logger.error("Can't handle notification "
115: + notification + ": \n" + e.getMessage(), e);
116: }
117: }
118: }
119:
120: public void setEventListeners(List eventListeners) {
121: this ._jossoEventListeners = eventListeners;
122: }
123:
124: public List getEventListeners() {
125: return this._jossoEventListeners;
126: }
127: }
|