1 /* 2 * Copyright (c) 1997, 2012, 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 26 package com.sun.jmx.snmp.agent; 27 28 import java.io.Serializable; 29 import java.util.Enumeration; 30 import java.util.logging.Level; 31 import java.util.Vector; 32 33 import javax.management.ObjectName; 34 import javax.management.MBeanServer; 35 import javax.management.MalformedObjectNameException; 36 import javax.management.InstanceAlreadyExistsException; 37 import javax.management.MBeanRegistrationException; 38 import javax.management.NotCompliantMBeanException; 39 40 import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER; 41 import com.sun.jmx.snmp.SnmpOid; 42 import com.sun.jmx.snmp.SnmpVarBind; 43 import com.sun.jmx.snmp.SnmpDefinitions; 44 import com.sun.jmx.snmp.SnmpStatusException; 45 import com.sun.jmx.snmp.SnmpEngine; 46 import com.sun.jmx.snmp.SnmpUnknownModelException; 47 import com.sun.jmx.snmp.internal.SnmpAccessControlModel; 48 import com.sun.jmx.snmp.internal.SnmpEngineImpl; 49 50 /** 51 * This list is used in order to construct the OID during the getnext. 52 * The constructed oid is checked by the checker AcmChecker. 53 */ 54 final class LongList { 55 56 public static int DEFAULT_CAPACITY = 10; 57 58 public static int DEFAULT_INCREMENT = 10; 59 60 61 private final int DELTA; 62 private int size; 63 64 /** 65 * The list content. Any access to this variable must be protected 66 * by a synchronized block on the LongList object. 67 * Only read-only action should be performed on this object. 68 **/ 69 public long[] list; 70 71 LongList() { 72 this(DEFAULT_CAPACITY,DEFAULT_INCREMENT); 73 } 74 75 LongList(int initialCapacity) { 76 this(initialCapacity,DEFAULT_INCREMENT); 77 } 78 79 LongList(int initialCapacity, int delta) { 80 size = 0; 81 DELTA = delta; 82 list = allocate(initialCapacity); 83 } 84 85 /** 86 * Same behaviour than size() in {@link java.util.List}. 87 **/ 88 public final int size() { return size;} 89 90 /** 91 * Same behaviour than add(long o) in {@link java.util.List}. 92 * Any access to this method should be protected in a synchronized 93 * block on the LongList object. 94 **/ 95 public final boolean add(final long o) { 96 if (size >= list.length) 97 resize(); 98 list[size++]=o; 99 return true; 100 } 101 102 /** 103 * Same behaviour than add(int index, long o) in 104 * {@link java.util.List}. 105 * Any access to this method should be protected in a synchronized 106 * block on the LongList object. 107 **/ 108 public final void add(final int index, final long o) { 109 if (index > size) throw new IndexOutOfBoundsException(); 110 if (index >= list.length) resize(); 111 if (index == size) { 112 list[size++]=o; 113 return; 114 } 115 116 java.lang.System.arraycopy(list,index,list,index+1,size-index); 117 list[index]=o; 118 size++; 119 } 120 121 /** 122 * Adds <var>count</var> elements to the list. 123 * @param at index at which the elements must be inserted. The 124 * first element will be inserted at this index. 125 * @param src An array containing the elements we want to insert. 126 * @param from Index of the first element from <var>src</var> that 127 * must be inserted. 128 * @param count number of elements to insert. 129 * Any access to this method should be protected in a synchronized 130 * block on the LongList object. 131 **/ 132 public final void add(final int at,final long[] src, final int from, 133 final int count) { 134 if (count <= 0) return; 135 if (at > size) throw new IndexOutOfBoundsException(); 136 ensure(size+count); 137 if (at < size) { 138 java.lang.System.arraycopy(list,at,list,at+count,size-at); 139 } 140 java.lang.System.arraycopy(src,from,list,at,count); 141 size+=count; 142 } 143 144 /** 145 * Any access to this method should be protected in a synchronized 146 * block on the LongList object. 147 **/ 148 public final long remove(final int from, final int count) { 149 if (count < 1 || from < 0) return -1; 150 if (from+count > size) return -1; 151 152 final long o = list[from]; 153 final int oldsize = size; 154 size = size - count; 155 156 if (from == size) return o; 157 158 java.lang.System.arraycopy(list,from+count,list,from, 159 size-from); 160 return o; 161 } 162 163 /** 164 * Same behaviour than remove(int index) in {@link java.util.List}. 165 * Any access to this method should be protected in a synchronized 166 * block on the LongList object. 167 **/ 168 public final long remove(final int index) { 169 if (index >= size) return -1; 170 final long o = list[index]; 171 list[index]=0; 172 if (index == --size) return o; 173 174 java.lang.System.arraycopy(list,index+1,list,index, 175 size-index); 176 return o; 177 } 178 179 /** 180 * Same behaviour than the toArray(long[] a) method in 181 * {@link java.util.List}. 182 * Any access to this method should be protected in a synchronized 183 * block on the LongList object. 184 **/ 185 public final long[] toArray(long[] a) { 186 java.lang.System.arraycopy(list,0,a,0,size); 187 return a; 188 } 189 190 /** 191 * Same behaviour than the toArray() method in 192 * {@link java.util.List}. 193 * Any access to this method should be protected in a synchronized 194 * block on the LongList object. 195 **/ 196 public final long[] toArray() { 197 return toArray(new long[size]); 198 } 199 200 /** 201 * Resize the list. Increase its capacity by DELTA elements. 202 * Any call to this method must be protected by a synchronized 203 * block on this LongList. 204 **/ 205 private final void resize() { 206 final long[] newlist = allocate(list.length + DELTA); 207 java.lang.System.arraycopy(list,0,newlist,0,size); 208 list = newlist; 209 } 210 211 /** 212 * Resize the list. Insure that the new length will be at 213 * least equal to <var>length</var>. 214 * @param length new minimal length requested. 215 * Any call to this method must be protected by a synchronized 216 * block on this LongList. 217 **/ 218 private final void ensure(int length) { 219 if (list.length < length) { 220 final int min = list.length+DELTA; 221 length=(length<min)?min:length; 222 final long[] newlist = allocate(length); 223 java.lang.System.arraycopy(list,0,newlist,0,size); 224 list = newlist; 225 } 226 } 227 228 /** 229 * Allocate a new array of object of specified length. 230 **/ 231 private final long[] allocate(final int length) { 232 return new long[length]; 233 } 234 235 }