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.util.config;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.josso.SSOConfigurationEventListener;
026:
027: import javax.management.AttributeChangeNotification;
028: import java.util.EventObject;
029:
030: /**
031: * This ConfigurationHandler receives SSOEvents and uses them to update JOSSO configuration files.
032: *
033: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
034: * @version $Id: SSOConfigurationEventHandler.java 508 2008-02-18 13:32:29Z sgonzalez $
035: */
036: public class SSOConfigurationEventHandler extends
037: XUpdateConfigurationHandler implements
038: SSOConfigurationEventListener {
039:
040: private static final Log logger = LogFactory
041: .getLog(SSOConfigurationEventHandler.class);
042:
043: private Object source;
044:
045: private String[] ignoredAttrs;
046:
047: /**
048: * @param ctx the ConfigurationContext used by this handler.
049: * @param elementsBaseLocation a XPath expression used to determine where elements to be updated are found in the configuration file.
050: * @param newElementsBaseLocation a XPath expression used to determine where new elements will be inserted as siblings in the configuration file.
051: * @param source the event source this handler uses
052: */
053: public SSOConfigurationEventHandler(ConfigurationContext ctx,
054: String elementsBaseLocation,
055: String newElementsBaseLocation, Object source,
056: String[] ignoredAttrs) {
057: super (ctx, elementsBaseLocation, newElementsBaseLocation);
058: this .source = source;
059: this .ignoredAttrs = ignoredAttrs;
060: }
061:
062: /**
063: * Handles an BaseSSOEvent and updates JOSSO config if necessary.
064: * The event object must be instance of javax.management.AttributeChangeNotification
065: *
066: * @param eventType the event type (this may be a JMX event type)
067: * @param event the event object. Only AttributeChangeNotification events are supported by this handler.
068: */
069: public void handleEvent(String eventType, EventObject event) {
070:
071: // An attribute has change in the SSOAgent ... update config file.
072: if (event instanceof AttributeChangeNotification) {
073: AttributeChangeNotification notification = (AttributeChangeNotification) event;
074: String attrName = notification.getAttributeName();
075:
076: if (ignore(attrName))
077: return;
078:
079: // This should cover longs, ints and of course, strings.
080: String newValue = notification.getNewValue().toString();
081: String oldValue = (notification.getOldValue() != null ? notification
082: .getOldValue().toString()
083: : null);
084:
085: this .saveElement(attrName, oldValue, newValue);
086: }
087:
088: }
089:
090: /**
091: * Only handles events of type : "jmx.attribute.change" for the supported resource.
092: */
093: public boolean isEventEnabled(String eventType, EventObject event) {
094: // This will only handle SSOAgent related events :
095: return source.equals(event.getSource());
096: }
097:
098: /**
099: * Util to determine if an attribute must be ignored.
100: * Subclasses may add different behavior.
101: *
102: */
103: protected boolean ignore(String attrName) {
104: for (int i = 0; i < ignoredAttrs.length; i++) {
105: String ignoredAttr = ignoredAttrs[i];
106: if (ignoredAttr.equals(attrName))
107: return true;
108: }
109: return false;
110: }
111:
112: }
|