1 22 package org.jboss.mx.interceptor; 23 24 import java.util.HashMap ; 25 import java.util.Timer ; 26 import java.util.TimerTask ; 27 28 import javax.management.Descriptor ; 29 import javax.management.PersistentMBean ; 30 import javax.management.MBeanException ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.modelmbean.ModelMBeanInfo ; 33 34 import org.jboss.mx.modelmbean.ModelMBeanConstants; 35 import org.jboss.mx.service.ServiceConstants; 36 import org.jboss.mx.server.Invocation; 37 import org.jboss.mx.server.MBeanInvoker; 38 39 50 public class PersistenceInterceptor 51 extends AbstractInterceptor 52 implements ModelMBeanConstants, ServiceConstants 53 { 54 55 private HashMap attrPersistencePolicies = new HashMap (); 56 57 private HashMap timerTaskMap = new HashMap (); 58 59 private String mbeanPersistencePolicy; 60 61 private PersistentMBean callback; 62 63 public PersistenceInterceptor() 64 { 65 super("Default Persistence Interceptor"); 66 } 67 68 public Object invoke(Invocation invocation) throws Throwable 70 { 71 if( callback == null ) 72 { 73 lazyInit(invocation); 74 } 75 76 Object returnValue = invocation.nextInterceptor().invoke(invocation); 77 String type = invocation.getType(); 78 if (type != Invocation.OP_SETATTRIBUTE ) 79 return returnValue; 80 81 String attrName = invocation.getName(); 82 String policy = (String )attrPersistencePolicies.get(attrName); 83 if (policy == null) 84 policy = mbeanPersistencePolicy; 85 86 if (policy.equalsIgnoreCase(PP_ON_UPDATE) == true) 87 { 88 MBeanInvoker invoker = invocation.getInvoker(); 89 Descriptor attrDesc = invocation.getDescriptor(); 90 invoker.updateAttributeInfo(attrDesc); 91 callback.store(); 92 } 93 else if(policy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN) == true) 94 { 95 PersistenceTimerTask task = (PersistenceTimerTask) timerTaskMap.get(attrName); 96 if( task != null ) 97 task.setHasUpdated(true); 98 } 99 return returnValue; 100 } 101 102 106 private synchronized void lazyInit(Invocation invocation) throws MBeanException 107 { 108 MBeanInvoker invoker = invocation.getInvoker(); 110 callback = (PersistentMBean ) invocation.getInvoker(); 111 ModelMBeanInfo info = (ModelMBeanInfo ) invoker.getMetaData(); 112 Descriptor mbeanDesc = info.getMBeanDescriptor(); 113 114 String policy = (String ) mbeanDesc.getFieldValue(PERSIST_POLICY); 115 String persistPeriod = (String )mbeanDesc.getFieldValue(PERSIST_PERIOD); 116 117 mbeanPersistencePolicy = PP_NEVER; 118 if (policy != null) 119 { 120 mbeanPersistencePolicy = policy; 121 if (mbeanPersistencePolicy.equalsIgnoreCase(PP_ON_TIMER) || 122 mbeanPersistencePolicy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN)) 123 { 124 boolean isNoMoreOftenThan = mbeanPersistencePolicy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN); 125 schedulePersistenceNotifications(Long.parseLong(persistPeriod), MBEAN_DESCRIPTOR, isNoMoreOftenThan); 126 } 127 } 128 129 Descriptor [] attrDescs = info.getDescriptors(ATTRIBUTE_DESCRIPTOR); 130 for (int i = 0; i < attrDescs.length; ++i) 131 { 132 policy = (String ) attrDescs[i].getFieldValue(PERSIST_POLICY); 133 persistPeriod = (String )attrDescs[i].getFieldValue(PERSIST_PERIOD); 134 135 if (policy != null) 136 { 137 String name = (String ) attrDescs[i].getFieldValue(NAME); 138 attrPersistencePolicies.put(name, policy); 139 140 if(policy.equalsIgnoreCase(PP_ON_TIMER) || 141 policy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN)) 142 { 143 boolean isNoMoreOftenThan = policy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN); 144 schedulePersistenceNotifications(Long.parseLong(persistPeriod), name, isNoMoreOftenThan); 145 } 146 } 147 } 148 } 149 150 private void schedulePersistenceNotifications(long persistPeriod, String name, 151 boolean isNoMoreOftenThan) 152 { 153 PersistenceTimerTask task = new PersistenceTimerTask(name, isNoMoreOftenThan); 155 Timer timer = new Timer (true); 156 timer.scheduleAtFixedRate(task, 0, persistPeriod); 157 timerTaskMap.put(name, task); 158 } 159 160 private class PersistenceTimerTask extends TimerTask 162 { 163 boolean noMoreOftenThan; 164 boolean hasUpdated; 165 String name; 166 PersistenceTimerTask(String name, boolean noMoreOftenThan) 167 { 168 this.name = name; 169 this.noMoreOftenThan = noMoreOftenThan; 170 } 171 synchronized void setHasUpdated(boolean flag) 172 { 173 hasUpdated = flag; 174 } 175 public void run() 176 { 177 try 178 { 179 boolean doStore = (noMoreOftenThan == true && hasUpdated == true) 183 || noMoreOftenThan == false; 184 if( doStore == true ) 185 { 186 callback.store(); 187 setHasUpdated(false); 188 } 189 } 190 catch (MBeanException e) 191 { 192 } 194 catch (InstanceNotFoundException e) 195 { 196 } 198 } 199 } 200 } 201 | Popular Tags |