1 9 10 package org.archive.util; 11 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 68 69 public class JEApplicationMBean implements DynamicMBean { 70 71 private static final String DESCRIPTION = 72 "A MBean for an application which uses JE. Provides open and close " + 73 "operations which configure and open a JE environment as part of the "+ 74 "applications's resources. Also supports general JE monitoring."; 75 76 private MBeanInfo mbeanInfo; private JEMBeanHelper jeHelper; private Environment targetEnv; 80 83 public static final String OP_OPEN = "openJE"; 84 85 89 public static final String OP_CLOSE = "closeJE"; 90 91 97 public JEApplicationMBean(Environment env) throws DatabaseException { 98 this.targetEnv = env; 99 jeHelper = new JEMBeanHelper(env.getConfig(), env.getHome(), true); 100 resetMBeanInfo(); 101 } 102 103 106 public Object getAttribute(String attributeName) 107 throws AttributeNotFoundException , 108 MBeanException { 109 110 return jeHelper.getAttribute(targetEnv, attributeName); 111 } 112 113 116 public void setAttribute(Attribute attribute) 117 throws AttributeNotFoundException , 118 InvalidAttributeValueException { 119 120 jeHelper.setAttribute(targetEnv, attribute); 121 } 122 123 126 public AttributeList getAttributes(String [] attributes) { 127 128 129 if (attributes == null) { 130 throw new IllegalArgumentException ("Attributes cannot be null"); 131 } 132 133 134 AttributeList results = new AttributeList (); 135 for (int i = 0; i < attributes.length; i++) { 136 try { 137 String name = attributes[i]; 138 Object value = jeHelper.getAttribute(targetEnv, name); 139 results.add(new Attribute (name, value)); 140 } catch (Exception e) { 141 e.printStackTrace(); 142 } 143 } 144 return results; 145 } 146 147 150 public AttributeList setAttributes(AttributeList attributes) { 151 152 153 if (attributes == null) { 154 throw new IllegalArgumentException ("attribute list can't be null"); 155 } 156 157 158 AttributeList results = new AttributeList (); 159 for (int i = 0; i < attributes.size(); i++) { 160 Attribute attr = (Attribute ) attributes.get(i); 161 try { 162 163 jeHelper.setAttribute(targetEnv, attr); 164 165 172 String name = attr.getName(); 173 Object newValue = jeHelper.getAttribute(targetEnv, name); 174 results.add(new Attribute (name, newValue)); 175 } catch (Exception e) { 176 e.printStackTrace(); 177 } 178 } 179 return results; 180 } 181 182 185 public Object invoke(String actionName, 186 Object [] params, 187 String [] signature) 188 throws MBeanException { 189 190 Object result = null; 191 192 if (actionName == null) { 193 throw new IllegalArgumentException ("actionName cannot be null"); 194 } 195 result = jeHelper.invoke(targetEnv, actionName, params, signature); 206 208 return result; 209 } 210 211 214 public MBeanInfo getMBeanInfo() { 215 return mbeanInfo; 216 } 217 218 224 private synchronized void resetMBeanInfo() { 225 226 231 232 233 List <MBeanAttributeInfo > attributeList = jeHelper.getAttributeList(targetEnv); 234 MBeanAttributeInfo [] attributeInfo = 235 new MBeanAttributeInfo [attributeList.size()]; 236 attributeList.toArray(attributeInfo); 237 238 239 Constructor [] constructors = this.getClass().getConstructors(); 240 MBeanConstructorInfo [] constructorInfo = 241 new MBeanConstructorInfo [constructors.length]; 242 for (int i = 0; i < constructors.length; i++) { 243 constructorInfo[i] = 244 new MBeanConstructorInfo (this.getClass().getName(), 245 constructors[i]); 246 } 247 248 249 250 254 List <MBeanOperationInfo > operationList = jeHelper.getOperationList(targetEnv); 255 272 273 MBeanOperationInfo [] operationInfo = 274 new MBeanOperationInfo [operationList.size()]; 275 operationList.toArray(operationInfo); 276 277 278 MBeanNotificationInfo [] notificationInfo = 279 jeHelper.getNotificationInfo(targetEnv); 280 281 282 mbeanInfo = new MBeanInfo (this.getClass().getName(), 283 DESCRIPTION, 284 attributeInfo, 285 constructorInfo, 286 operationInfo, 287 notificationInfo); 288 } 289 290 294 313 317 } 331 | Popular Tags |