1 22 package org.jboss.mx.interceptor; 23 24 import javax.management.Attribute ; 25 import javax.management.Descriptor ; 26 import javax.management.InvalidAttributeValueException ; 27 import javax.management.ObjectName ; 28 29 import org.jboss.logging.Logger; 30 import org.jboss.mx.modelmbean.ModelMBeanConstants; 31 import org.jboss.mx.modelmbean.ModelMBeanInvoker; 32 import org.jboss.mx.server.Invocation; 33 import org.jboss.util.UnreachableStatementException; 34 35 36 44 public class ModelMBeanAttributeInterceptor 45 extends AbstractInterceptor 46 implements ModelMBeanConstants 47 { 48 50 private static final Logger log = Logger.getLogger(ModelMBeanAttributeInterceptor.class); 51 52 54 private boolean trace = log.isTraceEnabled(); 55 56 58 public ModelMBeanAttributeInterceptor() 59 { 60 super("ModelMBean Attribute Interceptor"); 61 } 62 63 64 66 public Object invoke(Invocation invocation) throws Throwable 67 { 68 Descriptor d = invocation.getDescriptor(); 70 Class clazz = invocation.getAttributeTypeClass(); 71 72 String name = null; 73 ObjectName objectName = null; 74 if (trace) 75 { 76 name = (String ) d.getFieldValue(NAME); 77 objectName = invocation.getInvoker().getObjectName(); 78 } 79 80 if (invocation.getType().equals(Invocation.OP_SETATTRIBUTE)) 82 { 83 Object value = invocation.getArgs() [0]; 85 if (trace) 86 log.trace("Setting objectName=" + objectName + " attr=" + name + " value=" + value); 87 88 checkAssignable("Set attribute ", clazz, value); 89 90 Object oldValue = d.getFieldValue(ATTRIBUTE_VALUE); 92 if (trace) 93 log.trace("Setting objectName=" + objectName + " attr=" + name + " oldValue=" + value); 94 95 String setMethod = (String ) d.getFieldValue(SET_METHOD); 97 if (trace) 98 log.trace("Setting objectName=" + objectName + " attr=" + name + " setMethod=" + setMethod); 99 100 if (setMethod != null) 101 { 102 invocation.invoke(); 104 } 105 106 String timeLimit = (String ) d.getFieldValue(CURRENCY_TIME_LIMIT); 108 long limit = (timeLimit == null) ? CACHE_NEVER_LIMIT : Long.parseLong(timeLimit); 109 String timestamp = Long.toString(System.currentTimeMillis()/1000); 110 111 if (limit != CACHE_NEVER_LIMIT) 112 { 113 if (trace) 114 log.trace("Setting objectName=" + objectName + " attr=" + name + " value=" + value + " timestamp=" + timestamp); 115 d.setField(CACHED_VALUE, value); 116 d.setField(LAST_UPDATED_TIME_STAMP, timestamp); 117 } 118 119 d.setField(ATTRIBUTE_VALUE, value); 125 d.setField(LAST_UPDATED_TIME_STAMP2, timestamp); 126 127 ModelMBeanInvoker invoker = (ModelMBeanInvoker) invocation.getInvoker(); 129 invoker.sendAttributeChangeNotification( 130 new Attribute (invocation.getName(), oldValue), 131 new Attribute (invocation.getName(), value) 132 ); 133 return null; 134 } 135 else if (invocation.getType().equals(Invocation.OP_GETATTRIBUTE)) 136 { 137 if (trace) 138 log.trace("Getting objectName=" + objectName + " attr=" + name); 139 140 String timeLimit = (String )d.getFieldValue(CURRENCY_TIME_LIMIT); 141 long limit = (timeLimit == null) ? CACHE_NEVER_LIMIT : Long.parseLong(timeLimit); 142 143 if (limit == CACHE_ALWAYS_LIMIT) 145 { 146 String timeStamp = (String )d.getFieldValue(LAST_UPDATED_TIME_STAMP); 147 if (timeStamp != null) 148 { 149 Object value = d.getFieldValue(CACHED_VALUE); 150 if (trace) 151 log.trace("Always cache objectName=" + objectName + " attr=" + name + " value=" + value); 152 checkAssignable("Cached value in descriptor ", clazz, value); 153 return value; 154 } 155 } 156 157 if (limit != CACHE_NEVER_LIMIT) 159 { 160 String timeStamp = (String )d.getFieldValue(LAST_UPDATED_TIME_STAMP); 161 long lastUpdate = (timeStamp == null) ? 0 : Long.parseLong(timeStamp); 162 163 long now = System.currentTimeMillis(); 165 long expires = lastUpdate * 1000 + limit * 1000; 166 if (now < expires) 167 { 168 Object value = d.getFieldValue(CACHED_VALUE); 169 if (trace) 170 log.trace("Using cache objectName=" + objectName + " attr=" + name + " value=" + value + " now=" + now + " expires=" + expires); 171 checkAssignable("Cached value in descriptor ", clazz, value); 172 return value; 173 } 174 else 175 { 176 if (trace) 177 log.trace("Cache expired objectName=" + objectName + " attr=" + name + " now=" + now + " expires=" + expires); 178 d.removeField(CACHED_VALUE); 179 } 180 } 181 else 182 { 183 if (trace) 185 log.trace("Removing any cached value objectName=" + objectName + " attr=" + name + " descriptor=" + d); 186 d.removeField(CACHED_VALUE); 187 } 188 189 String getMethod = (String )d.getFieldValue(GET_METHOD); 191 if (trace) 192 log.trace("Get attribute objectName=" + objectName + " attr=" + name + " getMethod=" + getMethod); 193 194 if (getMethod != null) 195 { 196 Object value = invocation.invoke(); 198 if (trace) 199 log.trace("Got attribute objectName=" + objectName + " attr=" + name + " value=" + value); 200 201 if (limit != CACHE_NEVER_LIMIT) 203 { 204 String timestamp = Long.toString(System.currentTimeMillis()/1000); 205 if (trace) 206 log.trace("Cache attribute objectName=" + objectName + " attr=" + name + " value=" + value + " timestamp=" + timestamp); 207 d.setField(CACHED_VALUE, value); 208 d.setField(LAST_UPDATED_TIME_STAMP, timestamp); 209 } 210 return value; 211 } 212 else 213 { 214 Object value = d.getFieldValue(DEFAULT); 216 if (trace) 217 log.trace("Get attribute use default objectName=" + objectName + " attr=" + name + " default=" + value); 218 checkAssignable("Default value ", clazz, value); 219 return value; 220 } 221 } 222 else 223 throw new UnreachableStatementException(invocation.getType()); 224 } 225 226 protected void checkAssignable(String context, Class clazz, Object value) throws InvalidAttributeValueException , ClassNotFoundException 227 { 228 if (value != null && clazz.isAssignableFrom(value.getClass()) == false) 229 throw new InvalidAttributeValueException (context + " has class " + value.getClass() + " loaded from " + value.getClass().getClassLoader() + 230 " that is not assignable to attribute class " + clazz + " loaded from " + clazz.getClassLoader()); 231 } 232 } 233 234 235 236 237 | Popular Tags |