001: /*
002: * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.jndi.ldap;
027:
028: import javax.naming.directory.SearchControls;
029: import javax.naming.event.*;
030:
031: /**
032: * This class holds the information in an event registration/deregistration
033: * request. This includes the name, filter, search controls and
034: * the different interfaces that the listener implements. This last piece
035: * of information determines which event(s) the listener is interested in.
036: *<p>
037: * It overrides equals() and hashCode() to use all these pieces of
038: * information so that it can be used correctly in a hashtable.
039: *
040: * @author Rosanna Lee
041: */
042: final class NotifierArgs {
043: static final int ADDED_MASK = 0x1;
044: static final int REMOVED_MASK = 0x2;
045: static final int CHANGED_MASK = 0x4;
046: static final int RENAMED_MASK = 0x8;
047:
048: // these fields are package private; used by NamingEventNotifier
049: String name;
050: String filter;
051: SearchControls controls;
052: int mask;
053:
054: // package private
055: NotifierArgs(String name, int scope, NamingListener l) {
056: this (name, "(objectclass=*)", null, l);
057:
058: // if scope is not default, create search ctl and set it
059: if (scope != EventContext.ONELEVEL_SCOPE) {
060: controls = new SearchControls();
061: controls.setSearchScope(scope);
062: }
063: }
064:
065: // package private
066: NotifierArgs(String name, String filter, SearchControls ctls,
067: NamingListener l) {
068: this .name = name;
069: this .filter = filter;
070: this .controls = ctls;
071:
072: if (l instanceof NamespaceChangeListener) {
073: mask |= ADDED_MASK | REMOVED_MASK | RENAMED_MASK;
074: }
075: if (l instanceof ObjectChangeListener) {
076: mask |= CHANGED_MASK;
077: }
078: }
079:
080: // checks name, filter, controls
081: public boolean equals(Object obj) {
082: if (obj instanceof NotifierArgs) {
083: NotifierArgs target = (NotifierArgs) obj;
084: return mask == target.mask && name.equals(target.name)
085: && filter.equals(target.filter)
086: && checkControls(target.controls);
087: }
088: return false;
089: }
090:
091: private boolean checkControls(SearchControls ctls) {
092: if ((controls == null || ctls == null)) {
093: return ctls == controls;
094: }
095: // ctls are nonempty
096:
097: return (controls.getSearchScope() == ctls.getSearchScope())
098: && (controls.getTimeLimit() == ctls.getTimeLimit())
099: && (controls.getDerefLinkFlag() == ctls
100: .getDerefLinkFlag())
101: && (controls.getReturningObjFlag() == ctls
102: .getReturningObjFlag())
103: && (controls.getCountLimit() == ctls.getCountLimit())
104: && checkStringArrays(controls.getReturningAttributes(),
105: ctls.getReturningAttributes());
106: }
107:
108: private static boolean checkStringArrays(String[] s1, String[] s2) {
109: if ((s1 == null) || (s2 == null)) {
110: return s1 == s2;
111: }
112:
113: // both are nonnull
114: if (s1.length != s2.length) {
115: return false;
116: }
117:
118: for (int i = 0; i < s1.length; i++) {
119: if (!s1[i].equals(s2[i])) {
120: return false;
121: }
122: }
123: return true;
124: }
125:
126: // save from having to recalculate each time
127: private int sum = -1;
128:
129: public int hashCode() {
130: if (sum == -1)
131: sum = mask + name.hashCode() + filter.hashCode()
132: + controlsCode();
133: return sum;
134: }
135:
136: // used in calculating hash code
137: private int controlsCode() {
138: if (controls == null)
139: return 0;
140:
141: int total = (int) controls.getTimeLimit()
142: + (int) controls.getCountLimit()
143: + (controls.getDerefLinkFlag() ? 1 : 0)
144: + (controls.getReturningObjFlag() ? 1 : 0);
145:
146: String[] attrs = controls.getReturningAttributes();
147: if (attrs != null) {
148: for (int i = 0; i < attrs.length; i++) {
149: total += attrs[i].hashCode();
150: }
151: }
152:
153: return total;
154: }
155: }
|