1 22 package org.jboss.mx.persistence; 23 24 import javax.management.Attribute ; 25 import javax.management.AttributeList ; 26 import javax.management.Descriptor ; 27 import javax.management.InstanceNotFoundException ; 28 import javax.management.MBeanAttributeInfo ; 29 import javax.management.MBeanException ; 30 import javax.management.MBeanInfo ; 31 import javax.management.MBeanServer ; 32 import javax.management.MalformedObjectNameException ; 33 import javax.management.ObjectName ; 34 import javax.management.ReflectionException ; 35 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 36 import javax.management.modelmbean.ModelMBeanInfo ; 37 38 import org.jboss.logging.Logger; 39 import org.jboss.mx.modelmbean.ModelMBeanConstants; 40 import org.jboss.mx.modelmbean.ModelMBeanInvoker; 41 42 52 public class DelegatingPersistenceManager 53 implements PersistenceManager 54 { 55 57 private static Logger log = Logger.getLogger(DelegatingPersistenceManager.class); 58 59 60 private AttributePersistenceManager persistor; 61 62 63 private String persistName; 64 65 66 private boolean isLoading; 67 68 70 public DelegatingPersistenceManager() 71 { 72 } 74 75 77 82 public void load(ModelMBeanInvoker invoker, MBeanInfo metadata) 83 throws MBeanException 84 { 85 if (this.persistor == null) { 86 init(invoker, metadata); 89 } 90 91 if (log.isDebugEnabled()) 92 log.debug("load() called for: '" + this.persistName + "'"); 93 94 AttributeList attrs = null; 95 96 try { 98 attrs = this.persistor.load(this.persistName); 99 } 100 catch (Exception e) { 101 log.warn("Caught exception while loading", e); 103 throw new MBeanException (e); 104 } 105 106 if (attrs != null) { 107 109 try { 110 setIsLoading(true); 113 114 if (log.isDebugEnabled()) 115 log.debug("loading attributes: " + attrs); 116 invoker.setAttributes(attrs); 117 } 118 finally { 119 setIsLoading(false); 120 } 121 } 122 else { 123 if (log.isDebugEnabled()) 124 log.debug("No attributes to load"); 125 } 126 } 127 128 139 public void store(MBeanInfo metadata) 140 throws MBeanException 141 { 142 if (this.persistor == null) { 143 throw new MBeanException (new Exception ("store() called before instance initialized")); 145 } 146 147 if (isLoading()) { 149 return; } 151 else { 152 if (log.isDebugEnabled()) 153 log.debug("store() called for: '" + this.persistName + "'"); 154 155 AttributeList attributes = new AttributeList (); 157 158 MBeanAttributeInfo [] attrs = metadata.getAttributes(); 160 161 if (log.isDebugEnabled() && attrs.length > 0) 162 log.debug("store() --- ModelMBeanAttributeInfo[] ---"); 163 164 for (int i = 0; i < attrs.length; i++) 165 { 166 ModelMBeanAttributeInfo attributeInfo = (ModelMBeanAttributeInfo )attrs[i]; 169 170 if (log.isDebugEnabled()) 171 log.debug(" attr (#" + i + ") - " + attributeInfo); 172 173 if (attributeInfo.isWritable()) { 174 Descriptor attrDesc = attributeInfo.getDescriptor(); 175 176 Object name = attrDesc.getFieldValue(ModelMBeanConstants.NAME); 177 Object value = attrDesc.getFieldValue(ModelMBeanConstants.ATTRIBUTE_VALUE); 178 Object updated = attrDesc.getFieldValue(ModelMBeanConstants.LAST_UPDATED_TIME_STAMP2); 179 Object pPolicy = attrDesc.getFieldValue(ModelMBeanConstants.PERSIST_POLICY); 180 181 boolean noPersistPolicy = 182 pPolicy != null && 183 ((String )pPolicy).equalsIgnoreCase(ModelMBeanConstants.PP_NEVER) ? true : false; 184 185 if (updated != null && noPersistPolicy == false) { 191 attributes.add(new Attribute (name.toString(), value)); 192 } 193 } 194 } 195 try { 196 if (!attributes.isEmpty()) { 197 198 if (log.isDebugEnabled()) 199 log.debug("calling persistor.store(" + this.persistName + ") attrs=" + attributes); 200 201 persistor.store(this.persistName, attributes); 202 } 203 else { 204 if (log.isDebugEnabled()) 205 log.debug("nothing to persist"); 206 } 207 } 208 catch (Exception e) { 209 log.warn("cought exception during store()", e); 210 } 211 } 212 } 213 214 216 222 protected void init(ModelMBeanInvoker invoker, MBeanInfo metadata) 223 throws MBeanException 224 { 225 Descriptor desc = ((ModelMBeanInfo )metadata).getMBeanDescriptor(); 226 227 if (log.isDebugEnabled()) { 228 log.debug("init() --- ModelMBeanInfo Descriptor --- "); 229 log.debug(desc); 230 } 231 232 234 String name = (String )desc.getFieldValue(ModelMBeanConstants.PERSIST_NAME); 236 237 if (name != null) { 238 this.persistName = name; 239 } 240 else { 241 ObjectName objectName = (ObjectName )desc.getFieldValue(ModelMBeanConstants.OBJECT_NAME); 243 244 if (objectName != null) { 245 this.persistName = objectName.toString(); 246 } 247 else { 248 throw new MBeanException (new Exception ("must specify a value for: " + ModelMBeanConstants.PERSIST_NAME)); 249 } 250 } 251 252 if (log.isDebugEnabled()) 253 log.debug("chosen persistent id: '" + this.persistName + "'"); 254 255 String service = (String )desc.getFieldValue(ModelMBeanConstants.DELEGATING_PM_SERVICE_DESCRIPTOR); 258 if (service == null) 259 { 260 service = ModelMBeanConstants.DELEGATING_PM_SERVICE_DEFAULT_VALUE; 262 } 263 264 String operation = (String )desc.getFieldValue(ModelMBeanConstants.DELEGATING_PM_OPERATION_DESCRIPTOR); 266 if (operation == null) 267 { 268 operation = ModelMBeanConstants.DELEGATING_PM_OPERATION_DEFAULT_VALUE; 270 } 271 272 try { 274 ObjectName objName = new ObjectName (service); 275 MBeanServer server = invoker.getServer(); 276 277 this.persistor = (AttributePersistenceManager)server.invoke(objName, 278 operation, 279 new Object [] {}, 280 new String [] {}); 281 if (this.persistor == null) { 282 throw new MBeanException (new NullPointerException ("null AttributePersistenceManager from: " + service)); 283 } 284 } 285 catch (MalformedObjectNameException e) { 286 throw new MBeanException (e, "not a valid ObjectName: " + service); 287 } 288 catch (InstanceNotFoundException e) { 289 throw new MBeanException (e, "service not registered: " + service); 290 } 291 catch (ReflectionException e) { 292 throw new MBeanException (e); 293 } 294 295 if (log.isDebugEnabled()) 296 log.debug("using AttributePersistenceManager: " + this.persistor.getClass().getName()); 297 } 298 299 302 protected boolean isLoading() 303 { 304 return isLoading; 305 } 306 307 312 protected void setIsLoading(boolean newIsLoading) 313 { 314 isLoading = newIsLoading; 315 } 316 317 } 318 | Popular Tags |