jdk/src/share/classes/com/sun/jndi/ldap/NotifierArgs.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 10324 e28265130e4f
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.naming.directory.SearchControls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.naming.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class holds the information in an event registration/deregistration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * request. This includes the name, filter, search controls and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * the different interfaces that the listener implements. This last piece
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of information determines which event(s) the listener is interested in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * It overrides equals() and hashCode() to use all these pieces of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * information so that it can be used correctly in a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
final class NotifierArgs {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final int ADDED_MASK = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final int REMOVED_MASK = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int CHANGED_MASK = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final int RENAMED_MASK = 0x8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // these fields are package private; used by NamingEventNotifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    String filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    SearchControls controls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    NotifierArgs(String name, int scope, NamingListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this(name, "(objectclass=*)", null, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // if scope is not default, create search ctl and set it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (scope != EventContext.ONELEVEL_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            controls = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            controls.setSearchScope(scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    NotifierArgs(String name, String filter, SearchControls ctls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        NamingListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.controls = ctls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (l instanceof NamespaceChangeListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            mask |= ADDED_MASK|REMOVED_MASK|RENAMED_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (l instanceof ObjectChangeListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            mask |= CHANGED_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // checks name, filter, controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (obj instanceof NotifierArgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            NotifierArgs target = (NotifierArgs)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return mask == target.mask &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                name.equals(target.name) && filter.equals(target.filter) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                checkControls(target.controls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private boolean checkControls(SearchControls ctls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if ((controls == null || ctls == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return ctls == controls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // ctls are nonempty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return (controls.getSearchScope() == ctls.getSearchScope()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            (controls.getTimeLimit() == ctls.getTimeLimit()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            (controls.getDerefLinkFlag() == ctls.getDerefLinkFlag()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            (controls.getReturningObjFlag() == ctls.getReturningObjFlag()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            (controls.getCountLimit() == ctls.getCountLimit()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            checkStringArrays(controls.getReturningAttributes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                ctls.getReturningAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static boolean checkStringArrays(String[] s1, String[] s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if ((s1 == null) || (s2 == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return s1 == s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // both are nonnull
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (s1.length != s2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        for (int i = 0; i < s1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (!s1[i].equals(s2[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // save from having to recalculate each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private int sum = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (sum == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            sum = mask + name.hashCode() + filter.hashCode() + controlsCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // used in calculating hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private int controlsCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (controls == null) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int total = (int)controls.getTimeLimit() + (int)controls.getCountLimit() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            (controls.getDerefLinkFlag() ? 1 : 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            (controls.getReturningObjFlag() ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        String[] attrs = controls.getReturningAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (attrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            for (int i = 0; i < attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                total += attrs[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}