feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,110 @@
/*
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.util;
import com.sun.jmx.mbeanserver.Util;
import com.sun.jmx.snmp.agent.SnmpUserDataFactory;
import com.sun.jmx.snmp.SnmpPdu;
import com.sun.jmx.snmp.SnmpStatusException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class JvmContextFactory implements SnmpUserDataFactory {
/**
* Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
* Allocate a contextual object containing some user data. This method
* is called once for each incoming SNMP request. The scope
* of this object will be the whole request. Since the request can be
* handled in several threads, the user should make sure that this
* object can be accessed in a thread-safe manner. The SNMP framework
* will never access this object directly - it will simply pass
* it to the <code>SnmpMibAgent</code> within
* <code>SnmpMibRequest</code> objects - from where it can be retrieved
* through the {@link com.sun.jmx.snmp.agent.SnmpMibRequest#getUserData() getUserData()} accessor.
* <code>null</code> is considered to be a valid return value.
*
* This method is called just after the SnmpPduPacket has been
* decoded.
*
* @param requestPdu The SnmpPduPacket received from the SNMP manager.
* <b>This parameter is owned by the SNMP framework and must be
* considered as transient.</b> If you wish to keep some of its
* content after this method returns (by storing it in the
* returned object for instance) you should clone that
* information.
*
* @return A newly allocated user-data contextual object, or
* <code>null</code>
* @exception SnmpStatusException If an SnmpStatusException is thrown,
* the request will be aborted.
*
* @since Java DMK 5.0
**/
public Object allocateUserData(SnmpPdu requestPdu)
throws SnmpStatusException {
return Collections.synchronizedMap(new HashMap<Object, Object>());
}
/**
* Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
* Release a previously allocated contextual object containing user-data.
* This method is called just before the responsePdu is sent back to the
* manager. It gives the user a chance to alter the responsePdu packet
* before it is encoded, and to free any resources that might have
* been allocated when creating the contextual object.
*
* @param userData The contextual object being released.
* @param responsePdu The SnmpPduPacket that will be sent back to the
* SNMP manager.
* <b>This parameter is owned by the SNMP framework and must be
* considered as transient.</b> If you wish to keep some of its
* content after this method returns you should clone that
* information.
*
* @exception SnmpStatusException If an SnmpStatusException is thrown,
* the responsePdu is dropped and nothing is returned to
* to the manager.
*
* @since Java DMK 5.0
**/
public void releaseUserData(Object userData, SnmpPdu responsePdu)
throws SnmpStatusException {
((Map<?, ?>)userData).clear();
}
public static Map<Object, Object> getUserData() {
final Object userData =
com.sun.jmx.snmp.ThreadContext.get("SnmpUserData");
if (userData instanceof Map<?, ?>) return Util.cast(userData);
else return null;
}
}

View File

@@ -0,0 +1,203 @@
/*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.util;
import java.util.logging.Logger;
import java.util.logging.Level;
public class MibLogger {
final Logger logger;
final String className;
static String getClassName(Class<?> clazz) {
if (clazz == null) return null;
if (clazz.isArray())
return getClassName(clazz.getComponentType()) + "[]";
final String fullname = clazz.getName();
final int lastpoint = fullname.lastIndexOf('.');
final int len = fullname.length();
if ((lastpoint < 0) || (lastpoint >= len))
return fullname;
else return fullname.substring(lastpoint+1,len);
}
static String getLoggerName(Class<?> clazz) {
if (clazz == null) return "sun.management.snmp.jvminstr";
Package p = clazz.getPackage();
if (p == null) return "sun.management.snmp.jvminstr";
final String pname = p.getName();
if (pname == null) return "sun.management.snmp.jvminstr";
else return pname;
}
public MibLogger(Class<?> clazz) {
this(getLoggerName(clazz),getClassName(clazz));
}
public MibLogger(Class<?> clazz, String postfix) {
this(getLoggerName(clazz)+((postfix==null)?"":"."+postfix),
getClassName(clazz));
}
public MibLogger(String className) {
this("sun.management.snmp.jvminstr",className);
}
public MibLogger(String loggerName, String className) {
Logger l = null;
try {
l = Logger.getLogger(loggerName);
} catch (Exception x) {
// OK. Should not happen
}
logger = l;
this.className=className;
}
protected Logger getLogger() {
return logger;
}
public boolean isTraceOn() {
final Logger l = getLogger();
if (l==null) return false;
return l.isLoggable(Level.FINE);
}
public boolean isDebugOn() {
final Logger l = getLogger();
if (l==null) return false;
return l.isLoggable(Level.FINEST);
}
public boolean isInfoOn() {
final Logger l = getLogger();
if (l==null) return false;
return l.isLoggable(Level.INFO);
}
public boolean isConfigOn() {
final Logger l = getLogger();
if (l==null) return false;
return l.isLoggable(Level.CONFIG);
}
public void config(String func, String msg) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.CONFIG,className,
func,msg);
}
public void config(String func, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.CONFIG,className,
func,t.toString(),t);
}
public void config(String func, String msg, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.CONFIG,className,
func,msg,t);
}
public void error(String func, String msg) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.SEVERE,className,
func,msg);
}
public void info(String func, String msg) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.INFO,className,
func,msg);
}
public void info(String func, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.INFO,className,
func,t.toString(),t);
}
public void info(String func, String msg, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.INFO,className,
func,msg,t);
}
public void warning(String func, String msg) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.WARNING,className,
func,msg);
}
public void warning(String func, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.WARNING,className,
func,t.toString(),t);
}
public void warning(String func, String msg, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.WARNING,className,
func,msg,t);
}
public void trace(String func, String msg) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.FINE,className,
func,msg);
}
public void trace(String func, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.FINE,className,
func,t.toString(),t);
}
public void trace(String func, String msg, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.FINE,className,
func,msg,t);
}
public void debug(String func, String msg) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.FINEST,className,
func,msg);
}
public void debug(String func, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.FINEST,className,
func,t.toString(),t);
}
public void debug(String func, String msg, Throwable t) {
final Logger l = getLogger();
if (l!=null) l.logp(Level.FINEST,className,
func,msg,t);
}
}

View File

@@ -0,0 +1,168 @@
/*
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.util;
import com.sun.jmx.snmp.SnmpOid;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.List;
import java.util.Iterator;
import java.lang.ref.WeakReference;
/**
* This class is used to cache table data.
**/
public class SnmpCachedData implements SnmpTableHandler {
/**
* Compares two SnmpOid.
**/
public static final Comparator<SnmpOid> oidComparator =
new Comparator<SnmpOid>() {
public int compare(SnmpOid o1, SnmpOid o2) {
return o1.compareTo(o2);
}
public boolean equals(Object o1, Object o2) {
if (o1 == o2) return true;
else return o1.equals(o2);
}
};
/**
* Constructs a new instance of SnmpCachedData. Instances are
* immutable.
* @param lastUpdated Time stamp as returned by
* {@link System#currentTimeMillis System.currentTimeMillis()}
* @param indexes The table entry indexes, sorted in ascending order.
* @param datas The table datas, sorted according to the
* order in <code>indexes</code>: <code>datas[i]</code>
* is the data that corresponds to
* <code>indexes[i]</code>
**/
public SnmpCachedData(long lastUpdated, SnmpOid indexes[],
Object datas[]) {
this.lastUpdated = lastUpdated;
this.indexes = indexes;
this.datas = datas;
}
/**
* Constructs a new instance of SnmpCachedData. Instances are
* immutable.
* @param lastUpdated Time stamp as returned by
* {@link System#currentTimeMillis System.currentTimeMillis()}
* @param indexMap The table indexed table data, sorted in ascending
* order by {@link #oidComparator}. The keys must be
* instances of {@link SnmpOid}.
**/
public SnmpCachedData(long lastUpdated, TreeMap<SnmpOid, Object> indexMap) {
this(lastUpdated, indexMap, true);
}
/**
* Constructs a new instance of SnmpCachedData. Instances are
* immutable.
* @param lastUpdated Time stamp as returned by
* {@link System#currentTimeMillis System.currentTimeMillis()}
* @param indexMap The table indexed table data, sorted in ascending
* order by {@link #oidComparator}. The keys must be
* instances of {@link SnmpOid}.
**/
public SnmpCachedData(long lastUpdated, TreeMap<SnmpOid, Object> indexMap,
boolean b) {
final int size = indexMap.size();
this.lastUpdated = lastUpdated;
this.indexes = new SnmpOid[size];
this.datas = new Object[size];
if(b) {
indexMap.keySet().toArray(this.indexes);
indexMap.values().toArray(this.datas);
} else
indexMap.values().toArray(this.datas);
}
/**
* Time stamp as returned by
* {@link System#currentTimeMillis System.currentTimeMillis()}
**/
public final long lastUpdated;
/**
* The table entry indexes, sorted in ascending order.
**/
public final SnmpOid indexes[];
/**
* The table datas, sorted according to the
* order in <code>indexes</code>: <code>datas[i]</code>
* is the data that corresponds to <code>indexes[i]</code>
**/
public final Object datas[];
/**
* The position of the given <var>index</var>, as returned by
* <code>java.util.Arrays.binarySearch()</code>
**/
public final int find(SnmpOid index) {
return Arrays.binarySearch(indexes,index,oidComparator);
}
// SnmpTableHandler.getData()
public Object getData(SnmpOid index) {
final int pos = find(index);
if ((pos < 0)||(pos >= datas.length)) return null;
return datas[pos];
}
// SnmpTableHandler.getNext()
public SnmpOid getNext(SnmpOid index) {
if (index == null) {
if (indexes.length>0) return indexes[0];
else return null;
}
final int pos = find(index);
if (pos > -1) {
if (pos < (indexes.length -1) ) return indexes[pos+1];
else return null;
}
final int insertion = -pos -1;
if ((insertion > -1) && (insertion < indexes.length))
return indexes[insertion];
else return null;
}
// SnmpTableHandler.contains()
public boolean contains(SnmpOid index) {
final int pos = find(index);
return ((pos > -1)&&(pos < indexes.length));
}
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.util;
import com.sun.jmx.snmp.SnmpOid;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.List;
import java.util.Iterator;
import java.lang.ref.WeakReference;
/**
* This abstract class implements a weak cache for a table whose data
* is obtained from a {@link List}.
*
* <p><b>NOTE: This class is not synchronized, subclasses must implement
* the appropriate synchronization whwn needed.</b></p>
**/
public abstract class SnmpListTableCache extends SnmpTableCache {
/**
* The index of the entry corresponding to the given <var>item</var>.
* <br>This method is called by {@link #updateCachedDatas(Object,List)}.
* The given <var>item</var> is expected to be always associated with
* the same index.
* @param context The context passed to
* {@link #updateCachedDatas(Object,List)}.
* @param rawDatas Raw table datas passed to
* {@link #updateCachedDatas(Object,List)}.
* @param rank Rank of the given <var>item</var> in the
* <var>rawDatas</var> list iterator.
* @param item The raw data object for which an index must be determined.
**/
protected abstract SnmpOid getIndex(Object context, List<?> rawDatas,
int rank, Object item);
/**
* The data for the entry corresponding to the given <var>item</var>.
* <br>This method is called by {@link #updateCachedDatas(Object,List)}.
* @param context The context passed to
* {@link #updateCachedDatas(Object,List)}.
* @param rawDatas Raw table datas passed to
* {@link #updateCachedDatas(Object,List)}.
* @param rank Rank of the given <var>item</var> in the
* <var>rawDatas</var> list iterator.
* @param item The raw data object from which the entry data must be
* extracted.
* @return By default <var>item</var> is returned.
**/
protected Object getData(Object context, List<?> rawDatas,
int rank, Object item) {
return item;
}
/**
* Recompute cached data.
* @param context A context object, valid during the duration of
* of the call to this method, and that will be passed to
* {@link #getIndex} and {@link #getData}. <br>
* This method is intended to be called by
* {@link #updateCachedDatas(Object)}. It is assumed that
* the context is be allocated by before this method is called,
* and released just after this method has returned.<br>
* This class does not use the context object: it is a simple
* hook for subclassed.
* @param rawDatas The table datas from which the cached data will be
* computed.
* @return the computed cached data.
**/
protected SnmpCachedData updateCachedDatas(Object context, List<?> rawDatas) {
final int size = ((rawDatas == null)?0:rawDatas.size());
if (size == 0) return null;
final long time = System.currentTimeMillis();
final Iterator<?> it = rawDatas.iterator();
final TreeMap<SnmpOid, Object> map =
new TreeMap<>(SnmpCachedData.oidComparator);
for (int rank=0; it.hasNext() ; rank++) {
final Object item = it.next();
final SnmpOid index = getIndex(context, rawDatas, rank, item);
final Object data = getData(context, rawDatas, rank, item);
if (index == null) continue;
map.put(index,data);
}
return new SnmpCachedData(time,map);
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.util;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpStatusException;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.List;
import java.util.Iterator;
import java.lang.ref.WeakReference;
/**
* This class is used to cache LoadedClass table data.
* WARNING : MUST IMPLEMENT THE SnmpTableHandler directly. Some changes in daniel classes.
**/
public final class SnmpLoadedClassData extends SnmpCachedData {
/**
* Constructs a new instance of SnmpLoadedClassData. Instances are
* immutable.
* @param lastUpdated Time stamp as returned by
* {@link System#currentTimeMillis System.currentTimeMillis()}
* @param indexMap The table indexed table data, sorted in ascending
* order by {@link #oidComparator}. The keys must be
* instances of {@link SnmpOid}.
**/
public SnmpLoadedClassData(long lastUpdated, TreeMap<SnmpOid, Object> indexMap) {
super(lastUpdated, indexMap, false);
}
// SnmpTableHandler.getData()
public final Object getData(SnmpOid index) {
int pos = 0;
try {
pos = (int) index.getOidArc(0);
}catch(SnmpStatusException e) {
return null;
}
if (pos >= datas.length) return null;
return datas[pos];
}
// SnmpTableHandler.getNext()
public final SnmpOid getNext(SnmpOid index) {
int pos = 0;
if (index == null) {
if( (datas!= null) && (datas.length >= 1) )
return new SnmpOid(0);
}
try {
pos = (int) index.getOidArc(0);
}catch(SnmpStatusException e) {
return null;
}
if(pos < (datas.length - 1))
return new SnmpOid(pos+1);
else
return null;
}
// SnmpTableHandler.contains()
public final boolean contains(SnmpOid index) {
int pos = 0;
try {
pos = (int) index.getOidArc(0);
}catch(SnmpStatusException e) {
return false;
}
return (pos < datas.length);
}
}

View File

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

View File

@@ -0,0 +1,119 @@
/*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.util;
import com.sun.jmx.snmp.SnmpOid;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.List;
import java.util.Iterator;
import java.lang.ref.WeakReference;
/**
* This abstract class implements a weak cache that holds table data.
* <p>The table data is stored in an instance of
* {@link SnmpCachedData}, which is kept in a {@link WeakReference}.
* If the WeakReference is null or empty, the cached data is recomputed.</p>
*
* <p><b>NOTE: This class is not synchronized, subclasses must implement
* the appropriate synchronization when needed.</b></p>
**/
public abstract class SnmpTableCache implements Serializable {
/**
* Interval of time in ms during which the cached table data
* is considered valid.
**/
protected long validity;
/**
* A weak refernce holding cached table data.
**/
protected transient WeakReference<SnmpCachedData> datas;
/**
* true if the given cached table data is obsolete.
**/
protected boolean isObsolete(SnmpCachedData cached) {
if (cached == null) return true;
if (validity < 0) return false;
return ((System.currentTimeMillis() - cached.lastUpdated) > validity);
}
/**
* Returns the cached table data.
* Returns null if the cached data is obsolete, or if there is no
* cached data, or if the cached data was garbage collected.
* @return a still valid cached data or null.
**/
protected SnmpCachedData getCachedDatas() {
if (datas == null) return null;
final SnmpCachedData cached = datas.get();
if ((cached == null) || isObsolete(cached)) return null;
return cached;
}
/**
* Returns the cached table data, if it is still valid,
* or recompute it if it is obsolete.
* <p>
* When cache data is recomputed, store it in the weak reference,
* unless {@link #validity} is 0: then the data will not be stored
* at all.<br>
* This method calls {@link #isObsolete(SnmpCachedData)} to determine
* whether the cached data is obsolete, and {
* {@link #updateCachedDatas(Object)} to recompute it.
* </p>
* @param context A context object.
* @return the valid cached data, or the recomputed table data.
**/
protected synchronized SnmpCachedData getTableDatas(Object context) {
final SnmpCachedData cached = getCachedDatas();
if (cached != null) return cached;
final SnmpCachedData computedDatas = updateCachedDatas(context);
if (validity != 0) datas = new WeakReference<>(computedDatas);
return computedDatas;
}
/**
* Recompute cached data.
* @param context A context object, as passed to
* {@link #getTableDatas(Object)}
**/
protected abstract SnmpCachedData updateCachedDatas(Object context);
/**
* Return a table handler that holds the table data.
* This method should return the cached table data if it is still
* valid, recompute it and cache the new value if it's not.
**/
public abstract SnmpTableHandler getTableHandler();
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.util;
import com.sun.jmx.snmp.SnmpOid;
/**
* Defines the interface implemented by an object that holds
* table data.
**/
public interface SnmpTableHandler {
/**
* Returns the data associated with the given index.
* If the given index is not found, null is returned.
* Note that returning null does not necessarily means that
* the index was not found.
**/
public Object getData(SnmpOid index);
/**
* Returns the index that immediately follows the given
* <var>index</var>. The returned index is strictly greater
* than the given <var>index</var>, and is contained in the table.
* <br>If the given <var>index</var> is null, returns the first
* index in the table.
* <br>If there are no index after the given <var>index</var>,
* returns null.
**/
public SnmpOid getNext(SnmpOid index);
/**
* Returns true if the given <var>index</var> is present.
**/
public boolean contains(SnmpOid index);
}