jdk/src/jdk.snmp/share/classes/sun/management/snmp/util/SnmpTableCache.java
author sundar
Mon, 15 Sep 2014 15:18:13 +0530
changeset 26647 a6acc63c2a31
parent 25859 3317bb8137f4
permissions -rw-r--r--
8058422: Users should be able to overwrite "context" and "engine" variables Reviewed-by: lagergren, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23711
95fb268275e3 8039022: Fix serial lint warnings in sun.tools.java
darcy
parents: 14342
diff changeset
     2
 * Copyright (c) 2003, 2014, 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
 **/
23711
95fb268275e3 8039022: Fix serial lint warnings in sun.tools.java
darcy
parents: 14342
diff changeset
    48
@SuppressWarnings("serial") // JDK implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public abstract class SnmpTableCache implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Interval of time in ms during which the cached table data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * is considered valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected long validity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * A weak refernce holding cached table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected transient WeakReference<SnmpCachedData> datas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * true if the given cached table data is obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected boolean isObsolete(SnmpCachedData cached) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (cached   == null) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (validity < 0)     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return ((System.currentTimeMillis() - cached.lastUpdated) > validity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Returns the cached table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns null if the cached data is obsolete, or if there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * cached data, or if the cached data was garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return a still valid cached data or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected SnmpCachedData getCachedDatas() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (datas == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        final SnmpCachedData cached = datas.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if ((cached == null) || isObsolete(cached)) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Returns the cached table data, if it is still valid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * or recompute it if it is obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * When cache data is recomputed, store it in the weak reference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * unless {@link #validity} is 0: then the data will not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * at all.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * This method calls {@link #isObsolete(SnmpCachedData)} to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * whether the cached data is obsolete, and {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * {@link #updateCachedDatas(Object)} to recompute it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param context A context object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return the valid cached data, or the recomputed table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected synchronized SnmpCachedData getTableDatas(Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        final SnmpCachedData cached   = getCachedDatas();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (cached != null) return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        final SnmpCachedData computedDatas = updateCachedDatas(context);
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   102
        if (validity != 0) datas = new WeakReference<>(computedDatas);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return computedDatas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Recompute cached data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param context A context object, as passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *        {@link #getTableDatas(Object)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected abstract SnmpCachedData updateCachedDatas(Object context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Return a table handler that holds the table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * This method should return the cached table data if it is still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * valid, recompute it and cache the new value if it's not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public abstract SnmpTableHandler getTableHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}