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: package org.josso.gateway.event.security;
022:
023: import org.apache.commons.modeler.Registry;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.josso.gateway.event.SSOEvent;
027: import org.josso.gateway.event.SSOEventListener;
028:
029: import javax.management.MBeanServer;
030: import javax.management.Notification;
031: import javax.management.ObjectName;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * JMX Based event manager. This manager is a SSOSecurityEventManager and a MBean.
037: * It uses JMX Notification scheme to deliver SSO Events.
038: *
039: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
040: * @version $Id: JMXSSOEventManagerImpl.java 508 2008-02-18 13:32:29Z sgonzalez $
041: */
042: public class JMXSSOEventManagerImpl extends SSOSecurityEventManagerImpl {
043:
044: public static final Log logger = LogFactory
045: .getLog(JMXSSOEventManagerImpl.class);
046:
047: // Event sequence number ...
048: private static long sequnece;
049:
050: // This is the MBean used to fire notifications and to attach listeners..
051: private ObjectName mbeanOname;
052: private String oname;
053: private boolean initialized = false;
054:
055: private List preRegistered;
056:
057: /**
058: * Common Modelere MBean registry
059: */
060: private Registry registry;
061:
062: public JMXSSOEventManagerImpl() {
063: super ();
064: this .registry = Registry.getRegistry(null, null);
065: this .preRegistered = new ArrayList();
066: }
067:
068: public void initialize() {
069: super .initialize();
070:
071: if (initialized)
072: return;
073:
074: try {
075:
076: mbeanOname = new ObjectName(oname);
077: // this.registry.registerComponent(this, oname, null);
078:
079: if (logger.isDebugEnabled())
080: logger.debug("Using MBean for notifications : "
081: + mbeanOname);
082:
083: // Mark this manager as initialized.
084: initialized = true;
085:
086: for (int i = 0; i < preRegistered.size(); i++) {
087: SSOEventListener listener = (SSOEventListener) preRegistered
088: .get(i);
089: this .registerListener(listener);
090: }
091: } catch (Exception e) {
092: logger.error("Can't create MBean Objectname : "
093: + e.getMessage(), e);
094: }
095: }
096:
097: public void destroy() {
098: super .destroy();
099: }
100:
101: /**
102: * This method invokes the fireSSOEvent of the MBean registered under the "oname" name.
103: * @param event
104: */
105: public void fireSSOEvent(SSOEvent event) {
106: try {
107: // Invoke fireSSOEvent on MBean ...
108: MBeanServer server = getMBeanServer();
109:
110: // Build a JMX Notification based on the SSOEvent.
111: Notification notification = buildNotification(event);
112:
113: server.invoke(mbeanOname, "fireJMXSSOEvent",
114: new Object[] { notification },
115: new String[] { "javax.management.Notification" });
116:
117: } catch (Exception e) {
118: logger.error("Can't send SSO Event : " + e.getMessage(), e);
119: }
120: }
121:
122: /**
123: * Registers a new event listener.
124: * This implementation creates a NotificationSSOEventListener instance to wrapp the recieved listener.
125: * It registers the NotificationSSOEventListener as a JMX listener of the configured MBean.
126: */
127: public void registerListener(SSOEventListener listener) {
128:
129: // If this component has not been initialized, store the listener so we can register it on initialization.
130: if (!initialized) {
131: preRegistered.add(listener);
132: return;
133: }
134:
135: try {
136: // TODO : Keep track of all listeners !?
137: logger.info("Adding listener : " + listener + " for : "
138: + mbeanOname);
139: getMBeanServer().addNotificationListener(mbeanOname,
140: new NotificationSSOEventListener(listener), null,
141: null);
142:
143: } catch (Exception e) {
144: logger.error("Can't add listener : " + listener
145: + " to mbean : " + mbeanOname + "\n"
146: + e.getMessage(), e);
147: }
148: }
149:
150: /**
151: * Configuration parameter containing the MBean object name that this manager uses to send JMX notifications.
152: */
153: public String getOname() {
154: return oname;
155: }
156:
157: /**
158: * Configuration parameter containing the MBean object name that this manager uses to send JMX notifications.
159: */
160: public void setOname(String oname) {
161: this .oname = oname;
162: }
163:
164: /**
165: * Finds the propper MBeanServer instance.
166: */
167: protected MBeanServer getMBeanServer() {
168: return registry.getMBeanServer();
169: }
170:
171: /**
172: * This implementation builds a NotifiactionSSOEvent using received event information.
173: */
174: protected Notification buildNotification(SSOEvent event) {
175: return new SSOEventNotification(event, sequnece++);
176: }
177:
178: }
|