jdk/src/share/classes/sun/management/snmp/util/SnmpTableCache.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
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) 2003, 2006, 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
package sun.management.snmp.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import com.sun.jmx.snmp.SnmpOid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This abstract class implements a weak cache that holds table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>The table data is stored in an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@link SnmpCachedData}, which is kept in a {@link WeakReference}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * If the WeakReference is null or empty, the cached data is recomputed.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p><b>NOTE: This class is not synchronized, subclasses must implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *          the appropriate synchronization when needed.</b></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class SnmpTableCache implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Interval of time in ms during which the cached table data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * is considered valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected long validity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * A weak refernce holding cached table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected transient WeakReference<SnmpCachedData> datas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * true if the given cached table data is obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected boolean isObsolete(SnmpCachedData cached) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (cached   == null) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (validity < 0)     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return ((System.currentTimeMillis() - cached.lastUpdated) > validity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Returns the cached table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Returns null if the cached data is obsolete, or if there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * cached data, or if the cached data was garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @return a still valid cached data or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected SnmpCachedData getCachedDatas() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (datas == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        final SnmpCachedData cached = datas.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if ((cached == null) || isObsolete(cached)) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Returns the cached table data, if it is still valid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * or recompute it if it is obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * When cache data is recomputed, store it in the weak reference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * unless {@link #validity} is 0: then the data will not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * at all.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * This method calls {@link #isObsolete(SnmpCachedData)} to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * whether the cached data is obsolete, and {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * {@link #updateCachedDatas(Object)} to recompute it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param context A context object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @return the valid cached data, or the recomputed table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected synchronized SnmpCachedData getTableDatas(Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        final SnmpCachedData cached   = getCachedDatas();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (cached != null) return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        final SnmpCachedData computedDatas = updateCachedDatas(context);
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   101
        if (validity != 0) datas = new WeakReference<>(computedDatas);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return computedDatas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Recompute cached data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param context A context object, as passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *        {@link #getTableDatas(Object)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected abstract SnmpCachedData updateCachedDatas(Object context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Return a table handler that holds the table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * This method should return the cached table data if it is still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * valid, recompute it and cache the new value if it's not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public abstract SnmpTableHandler getTableHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}