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.agent;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.josso.util.config.ConfigurationContext;
026: import org.josso.util.config.SSOConfigurationEventHandler;
027:
028: import java.util.EventObject;
029:
030: /**
031: * This ConfigurationHandler listens to SSOAgentMBean notifications to add or remove JOSSO partner application definitions
032: * from JOSSO agent configuration file.
033: *
034: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
035: * @version $Id: SSOAgentConfigurationEventHandler.java 508 2008-02-18 13:32:29Z sgonzalez $
036: */
037:
038: public class SSOAgentConfigurationEventHandler extends
039: SSOConfigurationEventHandler {
040:
041: private static final Log logger = LogFactory
042: .getLog(SSOAgentConfigurationEventHandler.class);
043:
044: /**
045: * @param ctx the configuration context used by this handler.
046: * @param elementsBaseLocation a XPath expression used to determine where elements to be updated are found in the configuration file.
047: * @param newElementsBaseLocation a XPath expression used to determine where new elements will be inserted as siblings in the configuration file.
048: * @param source the event source this handler uses
049: */
050: public SSOAgentConfigurationEventHandler(ConfigurationContext ctx,
051: String elementsBaseLocation,
052: String newElementsBaseLocation, Object source,
053: String[] ignoredAttrs) {
054: super (ctx, elementsBaseLocation, newElementsBaseLocation,
055: source, ignoredAttrs);
056: }
057:
058: /**
059: * @see SSOAgentMBean.SSOAgentMBeanNotification
060: *
061: * @return true only if the notification is instance of SSOAgentMBean.SSOAgentMBeanNotification
062: */
063: public boolean isEventEnabled(String eventType, EventObject event) {
064: return event instanceof SSOAgentMBean.SSOAgentMBeanNotification;
065: }
066:
067: /**
068: * This method expects events of type SSOAgentMBean.SSOAgentMBeanNotification
069: * It adds or removes a JOSSO partner application definition from the JOSSO agent configuration file.
070: */
071: public void handleEvent(String eventType, EventObject event) {
072:
073: // Get data needed to process this event.
074: SSOAgentMBean.SSOAgentMBeanNotification notification = (SSOAgentMBean.SSOAgentMBeanNotification) event;
075:
076: // Build a XUpdate query string.
077: if (eventType
078: .equals(SSOAgentMBean.JOSSO_AGENT_EVENT_ADD_PARTNER_APP)) {
079: SSOPartnerAppConfig cfg = (SSOPartnerAppConfig) notification
080: .getUserData();
081: addSSOPartnerAppConfig(cfg);
082:
083: } else if (eventType
084: .equals(SSOAgentMBean.JOSSO_AGENT_EVENT_REMOVE_PARTNER_APP)) {
085: String context = (String) notification.getUserData();
086: removeSSOPartnerAppConfig(context);
087: }
088:
089: }
090:
091: /**
092: * This method will add a new partner app definition to josso configuration file.
093: */
094: protected void addSSOPartnerAppConfig(SSOPartnerAppConfig cfg) {
095: String context = cfg.getContext();
096: if (context == null || context.equals("")) {
097: logger
098: .error("addSSOPartnerAppConfig : received context is null or empty");
099: return;
100: }
101:
102: String xml = " <context>" + cfg.getContext()
103: + "</context>\n";
104:
105: if (cfg.getIgnoredWebRources() != null
106: && cfg.getIgnoredWebRources().length > 0) {
107: xml += " <security-constraint>\n";
108: for (int i = 0; i < cfg.getIgnoredWebRources().length; i++) {
109: String s = cfg.getIgnoredWebRources()[i];
110: xml += " <ignore-web-resource-collection>"
111: + s + "</ignore-web-resource-collection>\n";
112: }
113: xml += " </security-constraint>";
114: }
115:
116: String qry = this .buildXAppendElementXMLQueryString(
117: getElementsBaseLocation(), "partner-app", xml);
118: try {
119: updateConfiguration(qry);
120: } catch (Exception e) {
121: logger
122: .error("Can't add SSO partner application from to config ("
123: + cfg.getContext() + ")");
124: }
125: }
126:
127: /**
128: * This method will remove a partner app definition from josso configuration file.
129: */
130: protected void removeSSOPartnerAppConfig(String context) {
131: if (context == null || context.equals("")) {
132: logger
133: .error("removeSSOPartnerAppConfig : received context is null or empty");
134: return;
135: }
136: String qry = this .buildXDeleteElementQuery(
137: getElementsBaseLocation(), "partner-app[context='"
138: + context + "']");
139: try {
140: updateConfiguration(qry);
141: } catch (Exception e) {
142: logger
143: .error("Can't remove SSO partner application from config ("
144: + context + ")");
145: }
146: }
147: }
|