1 22 package org.jboss.mx.persistence; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import javax.management.Attribute ; 31 import javax.management.AttributeList ; 32 import javax.management.Descriptor ; 33 import javax.management.MBeanAttributeInfo ; 34 import javax.management.MBeanException ; 35 import javax.management.MBeanInfo ; 36 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 37 import javax.management.modelmbean.ModelMBeanInfo ; 38 39 import org.jboss.mx.modelmbean.ModelMBeanConstants; 40 import org.jboss.mx.modelmbean.ModelMBeanInvoker; 41 import org.jboss.mx.persistence.PersistenceManager; 42 43 import org.jboss.logging.Logger; 44 import org.jboss.util.StringPropertyReplacer; 45 46 62 public class ObjectStreamPersistenceManager 63 extends Object 64 implements PersistenceManager 65 { 66 protected static Logger log = Logger.getLogger(ObjectStreamPersistenceManager.class); 67 70 protected boolean isLoading; 71 72 74 public ObjectStreamPersistenceManager() 75 { 76 super(); 77 } 78 79 80 82 89 public void load(ModelMBeanInvoker mbean, MBeanInfo metadata) throws MBeanException 90 { 91 log.debug("load, resource:"+mbean.getResource()); 92 93 if (metadata == null) 94 { 95 return; 96 } 97 if( log.isTraceEnabled() ) 98 log.trace("metadata: "+metadata); 99 100 File storeFile = getStoreFile(metadata, false); 101 if (storeFile == null) 102 { 103 return; 104 } 105 106 try 107 { 108 FileInputStream fis = new FileInputStream (storeFile); 109 ObjectInputStream ois = new ObjectInputStream (fis); 110 ModelMBeanInfo storeMetadata = (ModelMBeanInfo ) ois.readObject(); 111 ois.close(); 112 log.debug("metadata deserialized"); 113 if( log.isTraceEnabled() ) 114 log.trace("storeMetadata: "+storeMetadata); 115 loadFromMetadata(mbean, storeMetadata); 116 } 117 catch (Exception e) 118 { 119 log.error("Error loading MBean state", e); 120 } 121 } 122 123 132 public void store(MBeanInfo metadata) throws MBeanException 133 { 134 if (isLoading()) 135 { 136 return; 137 } 138 139 log.debug("store"); 140 if( log.isTraceEnabled() ) 141 log.trace("metadata: " + metadata); 142 File storeFile = getStoreFile(metadata, true); 143 if( storeFile == null ) 144 { 145 return; 146 } 147 148 try 149 { 150 log.debug("Storing to file: "+storeFile.getAbsolutePath()); 151 FileOutputStream fos = new FileOutputStream (storeFile); 152 ObjectOutputStream oos = new ObjectOutputStream (fos); 153 oos.writeObject(metadata); 154 } 155 catch (IOException e) 156 { 157 throw new MBeanException (e, "Error in persisting MBean."); 158 } 159 } 160 161 163 169 protected void loadFromMetadata(ModelMBeanInvoker mbean, ModelMBeanInfo metadata) 170 { 171 AttributeList attributes = new AttributeList (); 172 MBeanAttributeInfo [] attrs = metadata.getAttributes(); 174 for (int i = 0; i < attrs.length; i++) 175 { 176 ModelMBeanAttributeInfo attributeInfo = (ModelMBeanAttributeInfo )attrs[i]; 178 Descriptor attrDesc = attributeInfo.getDescriptor(); 179 Object name = attrDesc.getFieldValue(ModelMBeanConstants.NAME); 180 Object value = attrDesc.getFieldValue(ModelMBeanConstants.ATTRIBUTE_VALUE); 181 Object updated = attrDesc.getFieldValue(ModelMBeanConstants.LAST_UPDATED_TIME_STAMP2); 182 183 if (updated != null) 186 { 187 log.debug("loading attribute name: " + name + ", value: " + value); 188 Attribute curAttribute = new Attribute (name.toString(), value); 189 attributes.add(curAttribute); 190 } 191 } 192 193 try 194 { 195 setIsLoading(true); 196 mbean.setAttributes(attributes); 197 } 198 finally 199 { 200 setIsLoading(false); 201 } 202 } 203 204 protected boolean isLoading() 205 { 206 return isLoading; 207 } 208 209 protected void setIsLoading(boolean newIsLoading) 210 { 211 isLoading = newIsLoading; 212 } 213 protected File getStoreFile(MBeanInfo metadata, boolean createFile) 214 throws MBeanException 215 { 216 Descriptor d = ((ModelMBeanInfo )metadata).getMBeanDescriptor(); 217 String dirPath = (String )d.getFieldValue(ModelMBeanConstants.PERSIST_LOCATION); 218 String file = (String ) d.getFieldValue(ModelMBeanConstants.PERSIST_NAME); 219 if( dirPath == null ) 220 { 221 log.debug("No "+ModelMBeanConstants.PERSIST_LOCATION 222 +" descriptor value found, using '.'"); 223 dirPath = "."; 224 } 225 if( file == null ) 226 { 227 log.debug("No "+ModelMBeanConstants.PERSIST_NAME+" descriptor value found"); 228 return null; 229 } 230 231 dirPath = StringPropertyReplacer.replaceProperties(dirPath); 232 file = StringPropertyReplacer.replaceProperties(file); 233 File dir = new File (dirPath); 234 File storeFile = new File (dir, file); 235 boolean exists = storeFile.exists(); 236 log.debug("Store file is: "+storeFile.getAbsolutePath()); 237 if( exists == false && createFile == true ) 238 { 239 dir.mkdirs(); 240 try 241 { 242 storeFile.createNewFile(); 243 } 244 catch(IOException e) 245 { 246 throw new MBeanException (e, "Failed to create store file"); 247 } 248 } 249 else if( exists == false ) 250 { 251 storeFile = null; 252 } 253 return storeFile; 254 } 255 } 256 | Popular Tags |