SMSEventListenerManager.java revision 083ce1f7c46d83d177124cf39bf2e25b15c70745
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: SMSEventListenerManager.java,v 1.12 2009/01/28 05:35:03 ww203982 Exp $
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey * Portions Copyright 2015-2016 ForgeRock AS.
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.SearchScope;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Receives notifications for all SMS object changes from
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <class>SMSNotificationManager</class> and dispatches the same to <class>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * CachedSMSEntry</class> and <class>CachedSubEntries</class>. This class
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * also handles the case of sending deleted event for recursive deletes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterclass SMSEventListenerManager implements SMSObjectListener {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // All Notification Objects list
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey private static final Map<String, NotificationObject> notificationObjects =
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington Collections.synchronizedMap(new HashMap<String, NotificationObject>());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // CachedSMSEntry objects
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey private static final Map<String, Set<NotificationObject>> nodeChanges =
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey Collections.synchronizedMap(new HashMap<String, Set<NotificationObject>>());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // CachedSubEntries objects
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey private static final Map<String, Set<NotificationObject>> subNodeChanges =
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey Collections.synchronizedMap(new HashMap<String, Set<NotificationObject>>());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Static Initialization variables
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Debug debug = SMSEntry.eventDebug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected static boolean initialized;
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey static void initialize() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .registerCallbackHandler(new SMSEventListenerManager());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("Initialized SMS Event listner");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.error("SMSEventListenerManager::initialize " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "Unable to intialize SMS listener: " + e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // do nothing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Processes object changed notifications
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void objectChanged(String odn, int event) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Processes object changed notifications. The flag isLocal is used
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // distingush the self generated DELETE notifications for recursive
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // deletes, especially when data store notifications are disabled.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // In which case, delete notifications will never be generated.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void objectChanged(String odn, int event, boolean isLocal) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("SMSEventListener::entry changed for: " + odn +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Normalize the DN
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // If event is delete, need to send notifications for sub-entries
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Even if backend datastore notification is enabled, they woould
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // arrive much later causing write-through cache issues.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!isLocal && (event == SMSObjectListener.DELETE)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Collect the immediate children of the current sdn
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // from nodeChanges. All "subNodeChanges" entries would
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // have an entry in "nodeChanges", hence donot have to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // iterate throught it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (nodeChanges) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington if (DN.valueOf(cdn).isInScopeOf(sdn, SearchScope.SUBORDINATES)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send the notifications
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("SMSEventListener::objectChanged: Sending " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "delete event of: " + dn + " to child nodes: " + childDNs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send notifications to external listeners also if
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // data store notification is not enabled
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!SMSNotificationManager.isDataStoreNotificationEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMSNotificationManager.getInstance().sendNotifications(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send notifications to CachedSMSEntries
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey sendNotifications(nodeChanges.get(dn), odn, event);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Process sub-entry changed events, not interested in attribute mods
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send notifications to CachedSubEntries
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("SMSEventListener::entry changed for: " + dn +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " sending notifications to its parents");
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey sendNotifications(subNodeChanges.get(DN.valueOf(dn).parent().toString().toLowerCase()), odn, event);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("SMSEventListenerManager::allObjectsChanged called");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Collect all the DNs from "nodeChanges" and send notifications
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (nodeChanges) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send MODIFY notifications
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington objectChanged(item, SMSObjectListener.MODIFY);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Registers notification for changes to nodes
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey static String registerForNotifyChangesToNode(String dn, SMSEventListener eventListener) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington String ndn = DN.valueOf(dn).toString().toLowerCase();
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey return (addNotificationObject(nodeChanges, ndn, eventListener));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Registers notification for changes to its sub-nodes
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey static String registerForNotifyChangesToSubNodes(String dn, SMSEventListener eventListener) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington String ndn = DN.valueOf(dn).toString().toLowerCase();
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey return (addNotificationObject(subNodeChanges, ndn, eventListener));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes notification objects
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey static void removeNotification(String notificationID) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington NotificationObject no = notificationObjects.get(notificationID);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds notification method to the map
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey private static String addNotificationObject(Map<String, Set<NotificationObject>> nChangesMap, String dn,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (nChangesMap) {
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey nObjects = Collections.synchronizedSet(new HashSet<NotificationObject>());
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey NotificationObject no = new NotificationObject(eventListener, nObjects);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Sends notification to methods and objects within the set
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey private static void sendNotifications(Set<NotificationObject> nObjects, String dn, int event) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((nObjects == null) || (nObjects.isEmpty())) {
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey Set<NotificationObject> nobjs = new HashSet<>(2);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (nObjects) {
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey debug.error("SMSEvent notification: Unable to send notification: ", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static class NotificationObject {
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey NotificationObject(SMSEventListener eventListener, Set<NotificationObject> s) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public int hashCode() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hash = 13 * hash + (id != null ? id.hashCode() : 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (o instanceof NotificationObject) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NotificationObject no = (NotificationObject) o;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (false);