1 22 package org.jboss.test.jmx.xmbean; 23 24 import java.beans.PropertyEditor ; 25 import java.io.BufferedReader ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStreamReader ; 31 import javax.management.Attribute ; 32 import javax.management.AttributeList ; 33 import javax.management.Descriptor ; 34 import javax.management.MBeanAttributeInfo ; 35 import javax.management.MBeanException ; 36 import javax.management.MBeanInfo ; 37 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 38 import javax.management.modelmbean.ModelMBeanInfo ; 39 40 import org.jboss.mx.metadata.MBeanInfoConversion; 41 import org.jboss.mx.modelmbean.ModelMBeanConstants; 42 import org.jboss.mx.modelmbean.ModelMBeanInvoker; 43 import org.jboss.mx.persistence.PersistenceManager; 44 45 import org.jboss.logging.Logger; 46 import org.jboss.util.StringPropertyReplacer; 47 import org.jboss.util.propertyeditor.PropertyEditors; 48 49 68 public class XMLFilePersistenceManager extends Object 69 implements PersistenceManager 70 { 71 protected static Logger log = Logger.getLogger(XMLFilePersistenceManager.class); 72 75 protected boolean isLoading; 76 77 79 public XMLFilePersistenceManager() 80 { 81 super(); 82 } 83 84 86 93 public void load(ModelMBeanInvoker mbean, MBeanInfo metadata) 94 throws MBeanException 95 { 96 log.debug("load, resource:" + mbean.getResource()); 97 98 if (metadata == null) 99 { 100 return; 101 } 102 if (log.isTraceEnabled()) 103 log.trace("metadata: " + metadata); 104 105 File storeFile = getStoreFile(metadata, false); 106 if (storeFile == null) 107 { 108 return; 109 } 110 111 try 112 { 113 AttributeList attributes = new AttributeList (); 114 115 FileInputStream fis = new FileInputStream (storeFile); 116 BufferedReader buf = new BufferedReader (new InputStreamReader (fis)); 117 118 String line, line2; 119 while ((line = buf.readLine()) != null) 120 { 121 log.trace("Line: " + line); 122 if (line.equals("<attributes>")) 123 continue; 124 if (line.equals("</attributes>")) 125 continue; 126 127 line = line.substring(line.indexOf("<")); 129 130 if (!line.startsWith("<attribute")) 131 { 132 log.warn("Unknown line, skipping " + line); 133 continue; 134 } 135 136 int pos = line.indexOf("name=\"") + 6; line2 = line.substring(pos); 139 int pos2 = line2.indexOf("\""); 140 String name = line2.substring(0, pos2); 141 log.debug("name: " + name); 142 143 pos = line.indexOf("type=\"") + 6; line2 = line.substring(pos); 146 pos2 = line2.indexOf("\""); 147 String type = line2.substring(0, pos2); 148 log.debug("type: " + type); 149 150 pos = line.indexOf(">"); 152 pos2 = line.lastIndexOf("<"); 153 String value = line.substring(pos + 1, pos2); 154 log.debug("value :" + value); 155 156 Object oVal = convert(value, type); 157 Attribute att = new Attribute (name, oVal); 158 attributes.add(att); 159 160 } 162 mbean.setAttributes(attributes); 163 } 164 catch (Exception e) 165 { 166 log.error("Error loading MBean state", e); 167 } 168 setIsLoading(false); 169 } 170 171 180 public void store(MBeanInfo metadata) throws MBeanException 181 { 182 if (isLoading()) 183 { 184 return; 185 } 186 187 ModelMBeanInfo mmeta = 188 MBeanInfoConversion.toModelMBeanInfo(metadata, true); 189 190 log.debug("store"); 191 if (log.isTraceEnabled()) 192 log.trace("metadata: " + metadata); 193 File storeFile = getStoreFile(metadata, true); 194 if (storeFile == null) 195 { 196 return; 197 } 198 199 try 200 { 201 log.debug("Storing to file: " + storeFile.getAbsolutePath()); 202 FileOutputStream fos = new FileOutputStream (storeFile); 203 MBeanAttributeInfo [] mais = mmeta.getAttributes(); 204 StringBuffer buf = new StringBuffer (); 205 buf.append("<attributes>\n"); 206 for (int i = 0; i < mais.length; i++) 207 { 208 ModelMBeanAttributeInfo mai = (ModelMBeanAttributeInfo ) mais[i]; 209 buf.append(" <attribute name=\"" + mai.getName() + "\" "); 210 buf.append("type=\"" + mai.getType() + "\">"); 211 log.debug("Trying to load " + mai.getName()); 212 Descriptor aDesc = mai.getDescriptor(); 213 if (aDesc==null) 214 throw new Exception ("aDesc is null"); 215 log.debug(aDesc.toString()); 216 Object att = aDesc.getFieldValue(ModelMBeanConstants.ATTRIBUTE_VALUE); 217 if (att!=null) 218 buf.append(att.toString()); 219 else 220 log.warn("att was null"); 221 buf.append("</attribute>\n"); 222 } 223 buf.append("</attributes>"); 224 log.trace(buf.toString()); 225 fos.write(buf.toString().getBytes()); 226 fos.close(); 227 } 228 catch (Exception e) 229 { 230 e.printStackTrace(); 231 throw new MBeanException (e, "Error in persisting MBean."); 232 } 233 } 234 235 237 protected boolean isLoading() 238 { 239 return isLoading; 240 } 241 242 protected void setIsLoading(boolean newIsLoading) 243 { 244 isLoading = newIsLoading; 245 } 246 247 256 protected File getStoreFile(MBeanInfo metadata, boolean createFile) 257 throws MBeanException 258 { 259 Descriptor d = ((ModelMBeanInfo ) metadata).getMBeanDescriptor(); 260 String dirPath = 261 (String ) d.getFieldValue(ModelMBeanConstants.PERSIST_LOCATION); 262 String file = (String ) d.getFieldValue(ModelMBeanConstants.PERSIST_NAME); 263 if (dirPath == null) 264 { 265 log.debug( 266 "No " 267 + ModelMBeanConstants.PERSIST_LOCATION 268 + " descriptor value found, using '.'"); 269 dirPath = "."; 270 } 271 if (file == null) 272 { 273 log.debug( 274 "No " 275 + ModelMBeanConstants.PERSIST_NAME 276 + " descriptor value found"); 277 return null; 278 } 279 280 dirPath = StringPropertyReplacer.replaceProperties(dirPath); 281 file = StringPropertyReplacer.replaceProperties(file); 282 if (!file.endsWith(".xml")) 284 file = file + ".xml"; 285 File dir = new File (dirPath); 286 File storeFile = new File (dir, file); 287 boolean exists = storeFile.exists(); 288 log.debug("Store file is: " + storeFile.getAbsolutePath()); 289 if (exists == false && createFile == true) 290 { 291 dir.mkdirs(); 292 try 293 { 294 storeFile.createNewFile(); 295 } 296 catch (IOException e) 297 { 298 throw new MBeanException (e, "Failed to create store file"); 299 } 300 } 301 else if (exists == false) 302 { 303 storeFile = null; 304 } 305 return storeFile; 306 } 307 308 315 private Object convert(String val, String oType) throws Exception 316 { 317 PropertyEditor editor = PropertyEditors.getEditor(oType); 318 editor.setAsText(val); 319 return editor.getValue(); 320 } 321 } 322 | Popular Tags |