1 22 package org.jboss.mx.interceptor; 23 24 import java.util.Arrays ; 25 26 import javax.management.Descriptor ; 27 import javax.management.InvalidAttributeValueException ; 28 import javax.management.ObjectName ; 29 30 import org.jboss.mx.modelmbean.ModelMBeanConstants; 31 import org.jboss.mx.server.Invocation; 32 33 34 40 public class ModelMBeanOperationInterceptor 41 extends AbstractInterceptor 42 implements ModelMBeanConstants 43 { 44 46 48 private boolean trace; 49 50 52 public ModelMBeanOperationInterceptor() 53 { 54 super("ModelMBean Operation Interceptor"); 55 trace = log.isTraceEnabled(); 56 } 57 58 59 61 public Object invoke(Invocation invocation) throws Throwable 62 { 63 Descriptor d = invocation.getDescriptor(); 65 Class clazz = invocation.getReturnTypeClass(); 66 67 String name = null; 68 ObjectName objectName = null; 69 if (trace) 70 { 71 if (d != null) 72 name = (String ) d.getFieldValue(NAME); 73 objectName = invocation.getInvoker().getObjectName(); 74 } 75 76 if (trace) 77 { 78 Object args = invocation.getArgs(); 79 if (args != null) 80 args = Arrays.asList((Object []) args); 81 log.trace("Invoking objectName=" + objectName + " oper=" + name + " args=" + args + " desc=" + d); 82 } 83 84 long limit = CACHE_NEVER_LIMIT; 85 86 if (d != null && clazz != null) 87 { 88 String timeLimit = (String ) d.getFieldValue(CURRENCY_TIME_LIMIT); 89 if (timeLimit != null) 90 limit = Long.parseLong(timeLimit); 91 92 if (limit == CACHE_ALWAYS_LIMIT) 94 { 95 String timeStamp = (String )d.getFieldValue(LAST_UPDATED_TIME_STAMP); 96 if (timeStamp != null) 97 { 98 Object value = d.getFieldValue(CACHED_VALUE); 99 if (trace) 100 log.trace("Always cache objectName=" + objectName + " oper=" + name + " value=" + value); 101 checkAssignable("Cached value in descriptor ", clazz, value); 102 return value; 103 } 104 } 105 106 if (limit != CACHE_NEVER_LIMIT) 108 { 109 String timeStamp = (String ) d.getFieldValue(LAST_UPDATED_TIME_STAMP); 110 long lastUpdate = (timeStamp == null) ? 0 : Long.parseLong(timeStamp); 111 112 long now = System.currentTimeMillis(); 114 long expires = lastUpdate * 1000 + limit * 1000; 115 if (now < expires) 116 { 117 Object value = d.getFieldValue(CACHED_VALUE); 118 if (trace) 119 log.trace("Using cache objectName=" + objectName + " oper=" + name + " value=" + value + " now=" + now + " expires=" + expires); 120 checkAssignable("Cached value in descriptor ", clazz, value); 121 return value; 122 } 123 else 124 { 125 if (trace) 126 log.trace("Cache expired objectName=" + objectName + " oper=" + name + " now=" + now + " expires=" + expires); 127 d.removeField(CACHED_VALUE); 128 } 129 } 130 else 131 { 132 if (trace) 134 log.trace("Removing any cached value objectName=" + objectName + " oper=" + name + " descriptor=" + d); 135 d.removeField(CACHED_VALUE); 136 } 137 } 138 139 Object value = invocation.invoke(); 141 if (trace) 142 log.trace("Got result objectName=" + objectName + " oper=" + name + " value=" + value); 143 144 if (d !=null && limit != CACHE_NEVER_LIMIT) 146 { 147 String timestamp = Long.toString(System.currentTimeMillis()/1000); 148 if (trace) 149 log.trace("Cache result objectName=" + objectName + " oper=" + name + " value=" + value + " timestamp=" + timestamp); 150 d.setField(CACHED_VALUE, value); 151 d.setField(LAST_UPDATED_TIME_STAMP, timestamp); 152 } 153 return value; 154 } 155 156 protected void checkAssignable(String context, Class clazz, Object value) throws InvalidAttributeValueException , ClassNotFoundException 157 { 158 if (value != null && clazz.isAssignableFrom(value.getClass()) == false) 159 throw new InvalidAttributeValueException (context + " has class " + value.getClass() + " loaded from " + value.getClass().getClassLoader() + 160 " that is not assignable to attribute class " + clazz + " loaded from " + clazz.getClassLoader()); 161 } 162 } 163 164 165 166 167 | Popular Tags |