0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.management.snmp.util;
0N/A
0N/Aimport com.sun.jmx.snmp.SnmpOid;
0N/Aimport com.sun.jmx.mbeanserver.Util;
0N/A
0N/Aimport java.io.Serializable;
0N/A
0N/Aimport java.util.Comparator;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Map;
0N/Aimport java.util.TreeMap;
0N/Aimport java.util.List;
0N/Aimport java.util.Iterator;
0N/A
0N/Aimport java.lang.ref.WeakReference;
0N/A
0N/A
0N/A/**
0N/A * This abstract class implements a weak cache that holds table data, for
0N/A * a table whose data is obtained from a list where a name can be obtained
0N/A * for each item in the list.
0N/A * <p>This object maintains a map between an entry name and its associated
0N/A * SnmpOid index, so that a given entry is always associated to the same
0N/A * index.</p>
0N/A * <p><b>NOTE: This class is not synchronized, subclasses must implement
0N/A * the appropriate synchronization whwn needed.</b></p>
0N/A **/
0N/Apublic abstract class SnmpNamedListTableCache extends SnmpListTableCache {
0N/A
0N/A /**
0N/A * This map associate an entry name with the SnmpOid index that's
0N/A * been allocated for it.
0N/A **/
0N/A protected TreeMap names = new TreeMap();
0N/A
0N/A /**
0N/A * The last allocate index.
0N/A **/
0N/A protected long last = 0;
0N/A
0N/A /**
0N/A * true if the index has wrapped.
0N/A **/
0N/A boolean wrapped = false;
0N/A
0N/A /**
0N/A * Returns the key to use as name for the given <var>item</var>.
0N/A * <br>This method is called by {@link #getIndex(Object,List,int,Object)}.
0N/A * The given <var>item</var> is expected to be always associated with
0N/A * the same name.
0N/A * @param context The context passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rawDatas Raw table datas passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rank Rank of the given <var>item</var> in the
0N/A * <var>rawDatas</var> list iterator.
0N/A * @param item The raw data object for which a key name must be determined.
0N/A **/
0N/A protected abstract String getKey(Object context, List rawDatas,
0N/A int rank, Object item);
0N/A
0N/A /**
0N/A * Find a new index for the entry corresponding to the
0N/A * given <var>item</var>.
0N/A * <br>This method is called by {@link #getIndex(Object,List,int,Object)}
0N/A * when a new index needs to be allocated for an <var>item</var>. The
0N/A * index returned must not be already in used.
0N/A * @param context The context passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rawDatas Raw table datas passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rank Rank of the given <var>item</var> in the
0N/A * <var>rawDatas</var> list iterator.
0N/A * @param item The raw data object for which an index must be determined.
0N/A **/
0N/A protected SnmpOid makeIndex(Object context, List rawDatas,
0N/A int rank, Object item) {
0N/A
0N/A // check we are in the limits of an unsigned32.
0N/A if (++last > 0x00000000FFFFFFFFL) {
0N/A // we just wrapped.
0N/A log.debug("makeIndex", "Index wrapping...");
0N/A last = 0;
0N/A wrapped=true;
0N/A }
0N/A
0N/A // If we never wrapped, we can safely return last as new index.
0N/A if (!wrapped) return new SnmpOid(last);
0N/A
0N/A // We wrapped. We must look for an unused index.
0N/A for (int i=1;i < 0x00000000FFFFFFFFL;i++) {
0N/A if (++last > 0x00000000FFFFFFFFL) last = 1;
0N/A final SnmpOid testOid = new SnmpOid(last);
0N/A
0N/A // Was this index already in use?
0N/A if (names == null) return testOid;
0N/A if (names.containsValue(testOid)) continue;
0N/A
0N/A // Have we just used it in a previous iteration?
0N/A if (context == null) return testOid;
0N/A if (((Map)context).containsValue(testOid)) continue;
0N/A
0N/A // Ok, not in use.
0N/A return testOid;
0N/A }
0N/A // all indexes are in use! we're stuck.
0N/A // // throw new IndexOutOfBoundsException("No index available.");
0N/A // better to return null and log an error.
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Call {@link #getKey(Object,List,int,Object)} in order to get
0N/A * the item name. Then check whether an index was already allocated
0N/A * for the entry by that name. If yes return it. Otherwise, call
0N/A * {@link #makeIndex(Object,List,int,Object)} to compute a new
0N/A * index for that entry.
0N/A * Finally store the association between
0N/A * the name and index in the context TreeMap.
0N/A * @param context The context passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * It is expected to
0N/A * be an instance of {@link TreeMap}.
0N/A * @param rawDatas Raw table datas passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rank Rank of the given <var>item</var> in the
0N/A * <var>rawDatas</var> list iterator.
0N/A * @param item The raw data object for which an index must be determined.
0N/A **/
0N/A protected SnmpOid getIndex(Object context, List rawDatas,
0N/A int rank, Object item) {
0N/A final String key = getKey(context,rawDatas,rank,item);
0N/A final Object index = (names==null||key==null)?null:names.get(key);
0N/A final SnmpOid result =
0N/A ((index != null)?((SnmpOid)index):makeIndex(context,rawDatas,
0N/A rank,item));
0N/A if ((context != null) && (key != null) && (result != null)) {
0N/A Map<Object, Object> map = Util.cast(context);
0N/A map.put(key,result);
0N/A }
0N/A log.debug("getIndex","key="+key+", index="+result);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Allocate a new {@link TreeMap} to serve as context, then
0N/A * call {@link SnmpListTableCache#updateCachedDatas(Object,List)}, and
0N/A * finally replace the {@link #names} TreeMap by the new allocated
0N/A * TreeMap.
0N/A * @param rawDatas The table datas from which the cached data will be
0N/A * computed.
0N/A **/
0N/A protected SnmpCachedData updateCachedDatas(Object context, List rawDatas) {
0N/A TreeMap ctxt = new TreeMap();
0N/A final SnmpCachedData result =
0N/A super.updateCachedDatas(context,rawDatas);
0N/A names = ctxt;
0N/A return result;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Load a list of raw data from which to build the cached data.
0N/A * This method is called when nothing is found in the request
0N/A * contextual cache.
0N/A * @param userData The request contextual cache allocated by
0N/A * the {@link JvmContextFactory}.
0N/A *
0N/A **/
0N/A protected abstract List loadRawDatas(Map userData);
0N/A
0N/A /**
0N/A *The name under which the raw data is to be found/put in
0N/A * the request contextual cache.
0N/A **/
0N/A protected abstract String getRawDatasKey();
0N/A
0N/A /**
0N/A * Get a list of raw data from which to build the cached data.
0N/A * Obtains a list of raw data by first looking it up in the
0N/A * request contextual cache <var>userData</var> under the given
0N/A * <var>key</var>. If nothing is found in the cache, calls
0N/A * {@link #loadRawDatas(Map)} to obtain a new rawData list,
0N/A * and cache the result in <var>userData</var> under <var>key</var>.
0N/A * @param userData The request contextual cache allocated by
0N/A * the {@link JvmContextFactory}.
0N/A * @param key The name under which the raw data is to be found/put in
0N/A * the request contextual cache.
0N/A *
0N/A **/
0N/A protected List getRawDatas(Map<Object, Object> userData, String key) {
0N/A List rawDatas = null;
0N/A
0N/A // Look for memory manager list in request contextual cache.
0N/A if (userData != null)
0N/A rawDatas = (List) userData.get(key);
0N/A
0N/A if (rawDatas == null) {
0N/A // No list in contextual cache, get it from API
0N/A rawDatas = loadRawDatas(userData);
0N/A
0N/A
0N/A // Put list in cache...
0N/A if (rawDatas != null && userData != null)
0N/A userData.put(key, rawDatas);
0N/A }
0N/A
0N/A return rawDatas;
0N/A }
0N/A
0N/A /**
0N/A * Update cahed datas.
0N/A * Obtains a {@link List} of raw datas by calling
0N/A * {@link #getRawDatas(Map,String) getRawDatas((Map)context,getRawDatasKey())}.<br>
0N/A * Then allocate a new {@link TreeMap} to serve as temporary map between
0N/A * names and indexes, and call {@link #updateCachedDatas(Object,List)}
0N/A * with that temporary map as context.<br>
0N/A * Finally replaces the {@link #names} TreeMap by the temporary
0N/A * TreeMap.
0N/A * @param context The request contextual cache allocated by the
0N/A * {@link JvmContextFactory}.
0N/A **/
0N/A protected SnmpCachedData updateCachedDatas(Object context) {
0N/A
0N/A final Map<Object, Object> userData =
0N/A (context instanceof Map)?Util.<Map<Object, Object>>cast(context):null;
0N/A
0N/A // Look for memory manager list in request contextual cache.
0N/A final List rawDatas = getRawDatas(userData,getRawDatasKey());
0N/A
0N/A log.debug("updateCachedDatas","rawDatas.size()=" +
0N/A ((rawDatas==null)?"<no data>":""+rawDatas.size()));
0N/A
0N/A TreeMap ctxt = new TreeMap();
0N/A final SnmpCachedData result =
0N/A super.updateCachedDatas(ctxt,rawDatas);
0N/A names = ctxt;
0N/A return result;
0N/A }
0N/A
0N/A static final MibLogger log = new MibLogger(SnmpNamedListTableCache.class);
0N/A}