2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
package sun.management.snmp.util;
|
|
26 |
|
|
27 |
import com.sun.jmx.snmp.SnmpOid;
|
|
28 |
|
|
29 |
import java.io.Serializable;
|
|
30 |
|
|
31 |
import java.util.Comparator;
|
|
32 |
import java.util.Arrays;
|
|
33 |
import java.util.TreeMap;
|
|
34 |
import java.util.List;
|
|
35 |
import java.util.Iterator;
|
|
36 |
|
|
37 |
import java.lang.ref.WeakReference;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* This abstract class implements a weak cache that holds table data.
|
|
41 |
* <p>The table data is stored in an instance of
|
|
42 |
* {@link SnmpCachedData}, which is kept in a {@link WeakReference}.
|
|
43 |
* If the WeakReference is null or empty, the cached data is recomputed.</p>
|
|
44 |
*
|
|
45 |
* <p><b>NOTE: This class is not synchronized, subclasses must implement
|
|
46 |
* the appropriate synchronization when needed.</b></p>
|
|
47 |
**/
|
|
48 |
public abstract class SnmpTableCache implements Serializable {
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Interval of time in ms during which the cached table data
|
|
52 |
* is considered valid.
|
|
53 |
**/
|
|
54 |
protected long validity;
|
|
55 |
|
|
56 |
/**
|
|
57 |
* A weak refernce holding cached table data.
|
|
58 |
**/
|
|
59 |
protected transient WeakReference<SnmpCachedData> datas;
|
|
60 |
|
|
61 |
/**
|
|
62 |
* true if the given cached table data is obsolete.
|
|
63 |
**/
|
|
64 |
protected boolean isObsolete(SnmpCachedData cached) {
|
|
65 |
if (cached == null) return true;
|
|
66 |
if (validity < 0) return false;
|
|
67 |
return ((System.currentTimeMillis() - cached.lastUpdated) > validity);
|
|
68 |
}
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Returns the cached table data.
|
|
72 |
* Returns null if the cached data is obsolete, or if there is no
|
|
73 |
* cached data, or if the cached data was garbage collected.
|
|
74 |
* @return a still valid cached data or null.
|
|
75 |
**/
|
|
76 |
protected SnmpCachedData getCachedDatas() {
|
|
77 |
if (datas == null) return null;
|
|
78 |
final SnmpCachedData cached = datas.get();
|
|
79 |
if ((cached == null) || isObsolete(cached)) return null;
|
|
80 |
return cached;
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Returns the cached table data, if it is still valid,
|
|
85 |
* or recompute it if it is obsolete.
|
|
86 |
* <p>
|
|
87 |
* When cache data is recomputed, store it in the weak reference,
|
|
88 |
* unless {@link #validity} is 0: then the data will not be stored
|
|
89 |
* at all.<br>
|
|
90 |
* This method calls {@link #isObsolete(SnmpCachedData)} to determine
|
|
91 |
* whether the cached data is obsolete, and {
|
|
92 |
* {@link #updateCachedDatas(Object)} to recompute it.
|
|
93 |
* </p>
|
|
94 |
* @param context A context object.
|
|
95 |
* @return the valid cached data, or the recomputed table data.
|
|
96 |
**/
|
|
97 |
protected synchronized SnmpCachedData getTableDatas(Object context) {
|
|
98 |
final SnmpCachedData cached = getCachedDatas();
|
|
99 |
if (cached != null) return cached;
|
|
100 |
final SnmpCachedData computedDatas = updateCachedDatas(context);
|
|
101 |
if (validity != 0) datas = new WeakReference<SnmpCachedData>(computedDatas);
|
|
102 |
return computedDatas;
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Recompute cached data.
|
|
107 |
* @param context A context object, as passed to
|
|
108 |
* {@link #getTableDatas(Object)}
|
|
109 |
**/
|
|
110 |
protected abstract SnmpCachedData updateCachedDatas(Object context);
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Return a table handler that holds the table data.
|
|
114 |
* This method should return the cached table data if it is still
|
|
115 |
* valid, recompute it and cache the new value if it's not.
|
|
116 |
**/
|
|
117 |
public abstract SnmpTableHandler getTableHandler();
|
|
118 |
|
|
119 |
}
|