1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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  *
  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.
  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 @SuppressWarnings("serial") // JDK implementation class
  49 public abstract class SnmpTableCache implements Serializable {
  50 
  51     /**
  52      * Interval of time in ms during which the cached table data
  53      * is considered valid.
  54      **/
  55     protected long validity;
  56 
  57     /**
  58      * A weak refernce holding cached table data.
  59      **/
  60     protected transient WeakReference<SnmpCachedData> datas;
  61 
  62     /**
  63      * true if the given cached table data is obsolete.
  64      **/
  65     protected boolean isObsolete(SnmpCachedData cached) {
  66         if (cached   == null) return true;
  67         if (validity < 0)     return false;
  68         return ((System.currentTimeMillis() - cached.lastUpdated) > validity);
  69     }
  70 
  71     /**
  72      * Returns the cached table data.
  73      * Returns null if the cached data is obsolete, or if there is no
  74      * cached data, or if the cached data was garbage collected.
  75      * @return a still valid cached data or null.
  76      **/
  77     protected SnmpCachedData getCachedDatas() {
  78         if (datas == null) return null;
  79         final SnmpCachedData cached = datas.get();
  80         if ((cached == null) || isObsolete(cached)) return null;
  81         return cached;
  82     }
  83 
  84     /**
  85      * Returns the cached table data, if it is still valid,
  86      * or recompute it if it is obsolete.
  87      * <p>
  88      * When cache data is recomputed, store it in the weak reference,
  89      * unless {@link #validity} is 0: then the data will not be stored
  90      * at all.<br>
  91      * This method calls {@link #isObsolete(SnmpCachedData)} to determine
  92      * whether the cached data is obsolete, and {
  93      * {@link #updateCachedDatas(Object)} to recompute it.
  94      * </p>
  95      * @param context A context object.
  96      * @return the valid cached data, or the recomputed table data.
  97      **/
  98     protected synchronized SnmpCachedData getTableDatas(Object context) {
  99         final SnmpCachedData cached   = getCachedDatas();
 100         if (cached != null) return cached;
 101         final SnmpCachedData computedDatas = updateCachedDatas(context);
 102         if (validity != 0) datas = new WeakReference<>(computedDatas);
 103         return computedDatas;
 104     }
 105 
 106     /**
 107      * Recompute cached data.
 108      * @param context A context object, as passed to
 109      *        {@link #getTableDatas(Object)}
 110      **/
 111     protected abstract SnmpCachedData updateCachedDatas(Object context);
 112 
 113     /**
 114      * Return a table handler that holds the table data.
 115      * This method should return the cached table data if it is still
 116      * valid, recompute it and cache the new value if it's not.
 117      **/
 118     public abstract SnmpTableHandler getTableHandler();
 119 
 120 }