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 for a table whose data 41 * is obtained from a {@link List}. 42 * 43 * <p><b>NOTE: This class is not synchronized, subclasses must implement 44 * the appropriate synchronization whwn needed.</b></p> 45 **/ 46 @SuppressWarnings("serial") // JDK implementation class 47 public abstract class SnmpListTableCache extends SnmpTableCache { 48 49 50 /** 51 * The index of the entry corresponding to the given <var>item</var>. 52 * <br>This method is called by {@link #updateCachedDatas(Object,List)}. 53 * The given <var>item</var> is expected to be always associated with 54 * the same index. 55 * @param context The context passed to 56 * {@link #updateCachedDatas(Object,List)}. 57 * @param rawDatas Raw table datas passed to 58 * {@link #updateCachedDatas(Object,List)}. 59 * @param rank Rank of the given <var>item</var> in the 60 * <var>rawDatas</var> list iterator. 61 * @param item The raw data object for which an index must be determined. 62 **/ 63 protected abstract SnmpOid getIndex(Object context, List<?> rawDatas, 64 int rank, Object item); 65 66 /** 67 * The data for the entry corresponding to the given <var>item</var>. 68 * <br>This method is called by {@link #updateCachedDatas(Object,List)}. 69 * @param context The context passed to 70 * {@link #updateCachedDatas(Object,List)}. 71 * @param rawDatas Raw table datas passed to 72 * {@link #updateCachedDatas(Object,List)}. 73 * @param rank Rank of the given <var>item</var> in the 74 * <var>rawDatas</var> list iterator. 75 * @param item The raw data object from which the entry data must be 76 * extracted. 77 * @return By default <var>item</var> is returned. 78 **/ 79 protected Object getData(Object context, List<?> rawDatas, 80 int rank, Object item) { 81 return item; 82 } 83 84 /** 85 * Recompute cached data. 86 * @param context A context object, valid during the duration of 87 * of the call to this method, and that will be passed to 88 * {@link #getIndex} and {@link #getData}. <br> 89 * This method is intended to be called by 90 * {@link #updateCachedDatas(Object)}. It is assumed that 91 * the context is be allocated by before this method is called, 92 * and released just after this method has returned.<br> 93 * This class does not use the context object: it is a simple 94 * hook for subclassed. 95 * @param rawDatas The table datas from which the cached data will be 96 * computed. 97 * @return the computed cached data. 98 **/ 99 protected SnmpCachedData updateCachedDatas(Object context, List<?> rawDatas) { 100 final int size = ((rawDatas == null)?0:rawDatas.size()); 101 if (size == 0) return null; 102 103 final long time = System.currentTimeMillis(); 104 final Iterator<?> it = rawDatas.iterator(); 105 final TreeMap<SnmpOid, Object> map = 106 new TreeMap<>(SnmpCachedData.oidComparator); 107 for (int rank=0; it.hasNext() ; rank++) { 108 final Object item = it.next(); 109 final SnmpOid index = getIndex(context, rawDatas, rank, item); 110 final Object data = getData(context, rawDatas, rank, item); 111 if (index == null) continue; 112 map.put(index,data); 113 } 114 115 return new SnmpCachedData(time,map); 116 } 117 118 }