1 8 9 package 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 import javax.management.MBeanParameterInfo ; 27 28 import com.sleepycat.je.DatabaseException; 29 import com.sleepycat.je.Environment; 30 import com.sleepycat.je.jmx.JEMBeanHelper; 31 32 66 67 public class JEApplicationMBean implements DynamicMBean { 68 69 private static final String DESCRIPTION = 70 "A MBean for an application which uses JE. Provides open and close " + 71 "operations which configure and open a JE environment as part of the "+ 72 "applications's resources. Also supports general JE monitoring."; 73 74 private MBeanInfo mbeanInfo; private JEMBeanHelper jeHelper; private Environment targetEnv; 78 81 public static final String OP_OPEN = "openJE"; 82 83 87 public static final String OP_CLOSE = "closeJE"; 88 89 94 public JEApplicationMBean(String environmentHome) { 95 96 File environmentDirectory = new File (environmentHome); 97 jeHelper = new JEMBeanHelper(environmentDirectory, true); 98 resetMBeanInfo(); 99 } 100 101 104 public Object getAttribute(String attributeName) 105 throws AttributeNotFoundException , 106 MBeanException { 107 108 return jeHelper.getAttribute(targetEnv, attributeName); 109 } 110 111 114 public void setAttribute(Attribute attribute) 115 throws AttributeNotFoundException , 116 InvalidAttributeValueException { 117 118 jeHelper.setAttribute(targetEnv, attribute); 119 } 120 121 124 public AttributeList getAttributes(String [] attributes) { 125 126 127 if (attributes == null) { 128 throw new IllegalArgumentException ("Attributes cannot be null"); 129 } 130 131 132 AttributeList results = new AttributeList (); 133 for (int i = 0; i < attributes.length; i++) { 134 try { 135 String name = attributes[i]; 136 Object value = jeHelper.getAttribute(targetEnv, name); 137 results.add(new Attribute (name, value)); 138 } catch (Exception e) { 139 e.printStackTrace(); 140 } 141 } 142 return results; 143 } 144 145 148 public AttributeList setAttributes(AttributeList attributes) { 149 150 151 if (attributes == null) { 152 throw new IllegalArgumentException ("attribute list can't be null"); 153 } 154 155 156 AttributeList results = new AttributeList (); 157 for (int i = 0; i < attributes.size(); i++) { 158 Attribute attr = (Attribute ) attributes.get(i); 159 try { 160 161 jeHelper.setAttribute(targetEnv, attr); 162 163 170 String name = attr.getName(); 171 Object newValue = jeHelper.getAttribute(targetEnv, name); 172 results.add(new Attribute (name, newValue)); 173 } catch (Exception e) { 174 e.printStackTrace(); 175 } 176 } 177 return results; 178 } 179 180 183 public Object invoke(String actionName, 184 Object [] params, 185 String [] signature) 186 throws MBeanException { 187 188 Object result = null; 189 190 if (actionName == null) { 191 throw new IllegalArgumentException ("actionName cannot be null"); 192 } 193 194 if (actionName.equals(OP_OPEN)) { 195 openEnvironment(); 196 return null; 197 } else if (actionName.equals(OP_CLOSE)) { 198 closeEnvironment(); 199 return null; 200 } else { 201 result = jeHelper.invoke(targetEnv, actionName, params, signature); 202 } 203 204 return result; 205 } 206 207 210 public MBeanInfo getMBeanInfo() { 211 return mbeanInfo; 212 } 213 214 220 private synchronized void resetMBeanInfo() { 221 222 227 228 229 List attributeList = jeHelper.getAttributeList(targetEnv); 230 MBeanAttributeInfo [] attributeInfo = 231 new MBeanAttributeInfo [attributeList.size()]; 232 attributeList.toArray(attributeInfo); 233 234 235 Constructor [] constructors = this.getClass().getConstructors(); 236 MBeanConstructorInfo [] constructorInfo = 237 new MBeanConstructorInfo [constructors.length]; 238 for (int i = 0; i < constructors.length; i++) { 239 constructorInfo[i] = 240 new MBeanConstructorInfo (this.getClass().getName(), 241 constructors[i]); 242 } 243 244 245 246 250 List operationList = jeHelper.getOperationList(targetEnv); 251 if (targetEnv == null) { 252 operationList.add( 253 new MBeanOperationInfo (OP_OPEN, 254 "Configure and open the JE environment.", 255 new MBeanParameterInfo [0], "java.lang.Boolean", 257 MBeanOperationInfo.ACTION_INFO)); 258 } else { 259 operationList.add( 260 new MBeanOperationInfo (OP_CLOSE, 261 "Close the JE environment.", 262 new MBeanParameterInfo [0], "void", 264 MBeanOperationInfo.ACTION_INFO)); 265 } 266 267 MBeanOperationInfo [] operationInfo = 268 new MBeanOperationInfo [operationList.size()]; 269 operationList.toArray(operationInfo); 270 271 272 MBeanNotificationInfo [] notificationInfo = 273 jeHelper.getNotificationInfo(targetEnv); 274 275 276 mbeanInfo = new MBeanInfo (this.getClass().getName(), 277 DESCRIPTION, 278 attributeInfo, 279 constructorInfo, 280 operationInfo, 281 notificationInfo); 282 } 283 284 288 private void openEnvironment() 289 throws MBeanException { 290 291 try { 292 if (targetEnv == null) { 293 297 targetEnv = 298 new Environment(jeHelper.getEnvironmentHome(), 299 jeHelper.getEnvironmentOpenConfig()); 300 resetMBeanInfo(); 301 } 302 } catch (DatabaseException e) { 303 throw new MBeanException (e); 304 } 305 } 306 307 311 private void closeEnvironment() 312 throws MBeanException { 313 314 try { 315 if (targetEnv != null) { 316 targetEnv.close(); 317 targetEnv = null; 318 resetMBeanInfo(); 319 } 320 } catch (DatabaseException e) { 321 throw new MBeanException (e); 322 } 323 } 324 } 325 | Popular Tags |