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.
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Maddenimport org.forgerock.guava.common.collect.HashMultimap;
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Maddenimport org.forgerock.guava.common.collect.Multimaps;
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Maddenimport org.forgerock.guava.common.collect.SetMultimap;
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Maddenimport org.forgerock.openam.utils.CollectionUtils;
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 // CachedSMSEntry objects
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private static final SetMultimap<DN, Subscription> nodeChanges = newSubscriberMultimap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // CachedSubEntries objects
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private static final SetMultimap<DN, Subscription> subNodeChanges = newSubscriberMultimap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Static Initialization variables
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private static final Debug debug = SMSEntry.eventDebug;
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden SMSNotificationManager.getInstance().registerCallbackHandler(new SMSEventListenerManager());
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden debug.message("Initialized SMS Event listener");
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden debug.error("SMSEventListenerManager::initialize Unable to initialize 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.
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private void objectChanged(DN dn, int event, boolean isLocal) {
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden debug.message("SMSEventListener::entry changed for: {} type: {}", dn, event);
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
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden // have an entry in "nodeChanges", hence do not have to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // iterate throught it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (nodeChanges) {
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden for (Iterator<DN> it = childDNs.iterator(); it.hasNext();) {
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden if (!it.next().isInScopeOf(dn, SearchScope.SUBORDINATES)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send the notifications
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden debug.message("SMSEventListener::objectChanged: Sending delete event of: {} to child nodes: {}", dn,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send notifications to external listeners also if
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // data store notification is not enabled
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!SMSNotificationManager.isDataStoreNotificationEnabled()) {
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden SMSNotificationManager.getInstance().sendNotifications(item.toString().toLowerCase(), event, true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send notifications to CachedSMSEntries
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden sendNotifications(nodeChanges.get(dn), dn, event);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Process sub-entry changed events, not interested in attribute mods
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden if (event == SMSObjectListener.ADD || event == SMSObjectListener.DELETE) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send notifications to CachedSubEntries
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden debug.message("SMSEventListener::entry changed for: {} sending notifications to its parents", dn);
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden sendNotifications(subNodeChanges.get(dn.parent()), dn, event);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("SMSEventListenerManager::allObjectsChanged called");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Collect all the DNs from "nodeChanges" and send notifications
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Send MODIFY notifications
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden objectChanged(item, SMSObjectListener.MODIFY, false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Registers notification for changes to nodes
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden static Subscription registerForNotifyChangesToNode(String dn, SMSEventListener eventListener) {
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden return addNotificationObject(nodeChanges, DN.valueOf(dn), eventListener);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Registers notification for changes to its sub-nodes
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden static Subscription registerForNotifyChangesToSubNodes(String dn, SMSEventListener eventListener) {
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden return addNotificationObject(subNodeChanges, DN.valueOf(dn), eventListener);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds notification method to the map
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private static Subscription addNotificationObject(SetMultimap<DN, Subscription> nodeChangeSubscribers, DN dn,
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden final Subscription subscription = new Subscription(eventListener, dn, nodeChangeSubscribers);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Sends notification to methods and objects within the set
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private static void sendNotifications(Set<Subscription> subscribers, DN dn, int event) {
083ce1f7c46d83d177124cf39bf2e25b15c70745Tom Rumsey debug.error("SMSEvent notification: Unable to send notification: ", e);
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private final SetMultimap<DN, Subscription> map;
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden Subscription(SMSEventListener eventListener, DN dn,
a7587037ca2237ee8a201dd776998171e8cfc00aNeil Madden private static SetMultimap<DN, Subscription> newSubscriberMultimap() {