1 8 9 package com.sleepycat.je.jmx; 10 11 import java.io.File ; 12 import java.lang.reflect.Constructor ; 13 import java.util.List ; 14 15 import javax.management.Attribute ; 16 import javax.management.AttributeList ; 17 import javax.management.AttributeNotFoundException ; 18 import javax.management.DynamicMBean ; 19 import javax.management.InvalidAttributeValueException ; 20 import javax.management.MBeanAttributeInfo ; 21 import javax.management.MBeanConstructorInfo ; 22 import javax.management.MBeanException ; 23 import javax.management.MBeanInfo ; 24 import javax.management.MBeanNotificationInfo ; 25 import javax.management.MBeanOperationInfo ; 26 27 import com.sleepycat.je.DatabaseException; 28 import com.sleepycat.je.Environment; 29 30 76 public class JEMonitor implements DynamicMBean { 77 78 private static final String DESCRIPTION = 79 "Monitor an open Berkeley DB, Java Edition environment."; 80 81 private MBeanInfo mbeanInfo; private JEMBeanHelper jeHelper; 84 89 public JEMonitor(String environmentHome) 90 throws MBeanException { 91 92 File environmentDirectory = new File (environmentHome); 93 jeHelper = new JEMBeanHelper(environmentDirectory, false); 94 95 Environment targetEnv = getEnvironmentIfOpen(); 96 try { 97 resetMBeanInfo(targetEnv); 98 } finally { 99 closeEnvironment(targetEnv); 100 } 101 } 102 103 106 public Object getAttribute(String attributeName) 107 throws AttributeNotFoundException , 108 MBeanException { 109 110 Object result = null; 111 Environment targetEnv = getEnvironmentIfOpen(); 112 try { 113 result = jeHelper.getAttribute(targetEnv, attributeName); 114 targetEnv = checkForMBeanReset(targetEnv); 115 } finally { 116 117 closeEnvironment(targetEnv); 118 } 119 120 return result; 121 } 122 123 126 public void setAttribute(Attribute attribute) 127 throws AttributeNotFoundException , 128 InvalidAttributeValueException , 129 MBeanException { 130 131 Environment targetEnv = getEnvironmentIfOpen(); 132 try { 133 jeHelper.setAttribute(targetEnv, attribute); 134 } finally { 135 136 closeEnvironment(targetEnv); 137 } 138 } 139 140 143 public AttributeList getAttributes(String [] attributes) { 144 145 146 if (attributes == null) { 147 throw new IllegalArgumentException ("Attributes cannot be null"); 148 } 149 150 151 AttributeList results = new AttributeList (); 152 Environment targetEnv = getEnvironmentIfOpen(); 153 154 try { 155 for (int i = 0; i < attributes.length; i++) { 156 try { 157 String name = attributes[i]; 158 Object value = jeHelper.getAttribute(targetEnv, name); 159 160 164 targetEnv = checkForMBeanReset(targetEnv); 165 166 results.add(new Attribute (name, value)); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 } 171 return results; 172 } finally { 173 try { 174 175 closeEnvironment(targetEnv); 176 } catch (MBeanException ignore) { 177 178 } 179 } 180 } 181 182 185 public AttributeList setAttributes(AttributeList attributes) { 186 187 188 if (attributes == null) { 189 throw new IllegalArgumentException ("attribute list can't be null"); 190 } 191 192 193 AttributeList results = new AttributeList (); 194 Environment targetEnv = getEnvironmentIfOpen(); 195 196 try { 197 for (int i = 0; i < attributes.size(); i++) { 198 Attribute attr = (Attribute ) attributes.get(i); 199 try { 200 201 jeHelper.setAttribute(targetEnv, attr); 202 203 210 String name = attr.getName(); 211 Object newValue = jeHelper.getAttribute(targetEnv, name); 212 results.add(new Attribute (name, newValue)); 213 } catch (Exception e) { 214 e.printStackTrace(); 215 } 216 } 217 return results; 218 } finally { 219 try { 220 221 closeEnvironment(targetEnv); 222 } catch (MBeanException ignore) { 223 224 } 225 } 226 } 227 228 231 public Object invoke(String actionName, 232 Object [] params, 233 String [] signature) 234 throws MBeanException { 235 236 Object result = null; 237 Environment targetEnv = getEnvironmentIfOpen(); 238 try { 239 result = jeHelper.invoke(targetEnv, actionName, 240 params, signature); 241 } finally { 242 243 closeEnvironment(targetEnv); 244 } 245 246 return result; 247 } 248 249 252 public MBeanInfo getMBeanInfo() { 253 254 return mbeanInfo; 255 } 256 257 266 private Environment checkForMBeanReset(Environment targetEnv) 267 throws MBeanException { 268 269 Environment env = targetEnv; 270 if (jeHelper.getNeedReset()) { 271 272 273 closeEnvironment(env); 274 env = getEnvironmentIfOpen(); 275 resetMBeanInfo(env); 276 } 277 return env; 278 } 279 280 288 private void resetMBeanInfo(Environment targetEnv) { 289 290 295 296 297 List attributeList = jeHelper.getAttributeList(targetEnv); 298 MBeanAttributeInfo [] attributeInfo = 299 new MBeanAttributeInfo [attributeList.size()]; 300 attributeList.toArray(attributeInfo); 301 302 303 Constructor [] constructors = this.getClass().getConstructors(); 304 MBeanConstructorInfo [] constructorInfo = 305 new MBeanConstructorInfo [constructors.length]; 306 for (int i = 0; i < constructors.length; i++) { 307 constructorInfo[i] = 308 new MBeanConstructorInfo (this.getClass().getName(), 309 constructors[i]); 310 } 311 312 313 List operationList = jeHelper.getOperationList(targetEnv); 314 MBeanOperationInfo [] operationInfo = 315 new MBeanOperationInfo [operationList.size()]; 316 operationList.toArray(operationInfo); 317 318 319 MBeanNotificationInfo [] notificationInfo = 320 jeHelper.getNotificationInfo(targetEnv); 321 322 323 mbeanInfo = new MBeanInfo (this.getClass().getName(), 324 DESCRIPTION, 325 attributeInfo, 326 constructorInfo, 327 operationInfo, 328 notificationInfo); 329 } 330 331 338 protected Environment getEnvironmentIfOpen() { 339 340 return jeHelper.getEnvironmentIfOpen(); 341 } 342 343 349 protected void closeEnvironment(Environment targetEnv) 350 throws MBeanException { 351 352 try { 353 if (targetEnv != null) { 354 targetEnv.close(); 355 } 356 } catch (DatabaseException e) { 357 throw new MBeanException (e); 358 } 359 } 360 } 361 | Popular Tags |