package org.jboss.test.jmx.xmbean;
import java.beans.PropertyEditor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.Descriptor;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanInfo;
import org.jboss.mx.metadata.MBeanInfoConversion;
import org.jboss.mx.modelmbean.ModelMBeanConstants;
import org.jboss.mx.modelmbean.ModelMBeanInvoker;
import org.jboss.mx.persistence.PersistenceManager;
import org.jboss.logging.Logger;
import org.jboss.util.StringPropertyReplacer;
import org.jboss.util.propertyeditor.PropertyEditors;
public class XMLFilePersistenceManager extends Object
implements PersistenceManager
{
protected static Logger log = Logger.getLogger(XMLFilePersistenceManager.class);
protected boolean isLoading;
public XMLFilePersistenceManager()
{
super();
}
public void load(ModelMBeanInvoker mbean, MBeanInfo metadata)
throws MBeanException
{
log.debug("load, resource:" + mbean.getResource());
if (metadata == null)
{
return;
}
if (log.isTraceEnabled())
log.trace("metadata: " + metadata);
File storeFile = getStoreFile(metadata, false);
if (storeFile == null)
{
return;
}
try
{
AttributeList attributes = new AttributeList();
FileInputStream fis = new FileInputStream(storeFile);
BufferedReader buf = new BufferedReader(new InputStreamReader(fis));
String line, line2;
while ((line = buf.readLine()) != null)
{
log.trace("Line: " + line);
if (line.equals("<attributes>"))
continue;
if (line.equals("</attributes>"))
continue;
line = line.substring(line.indexOf("<"));
if (!line.startsWith("<attribute"))
{
log.warn("Unknown line, skipping " + line);
continue;
}
int pos = line.indexOf("name=\"") + 6; line2 = line.substring(pos);
int pos2 = line2.indexOf("\"");
String name = line2.substring(0, pos2);
log.debug("name: " + name);
pos = line.indexOf("type=\"") + 6; line2 = line.substring(pos);
pos2 = line2.indexOf("\"");
String type = line2.substring(0, pos2);
log.debug("type: " + type);
pos = line.indexOf(">");
pos2 = line.lastIndexOf("<");
String value = line.substring(pos + 1, pos2);
log.debug("value :" + value);
Object oVal = convert(value, type);
Attribute att = new Attribute(name, oVal);
attributes.add(att);
}
mbean.setAttributes(attributes);
}
catch (Exception e)
{
log.error("Error loading MBean state", e);
}
setIsLoading(false);
}
public void store(MBeanInfo metadata) throws MBeanException
{
if (isLoading())
{
return;
}
ModelMBeanInfo mmeta =
MBeanInfoConversion.toModelMBeanInfo(metadata, true);
log.debug("store");
if (log.isTraceEnabled())
log.trace("metadata: " + metadata);
File storeFile = getStoreFile(metadata, true);
if (storeFile == null)
{
return;
}
try
{
log.debug("Storing to file: " + storeFile.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(storeFile);
MBeanAttributeInfo[] mais = mmeta.getAttributes();
StringBuffer buf = new StringBuffer();
buf.append("<attributes>\n");
for (int i = 0; i < mais.length; i++)
{
ModelMBeanAttributeInfo mai = (ModelMBeanAttributeInfo) mais[i];
buf.append(" <attribute name=\"" + mai.getName() + "\" ");
buf.append("type=\"" + mai.getType() + "\">");
log.debug("Trying to load " + mai.getName());
Descriptor aDesc = mai.getDescriptor();
if (aDesc==null)
throw new Exception("aDesc is null");
log.debug(aDesc.toString());
Object att = aDesc.getFieldValue(ModelMBeanConstants.ATTRIBUTE_VALUE);
if (att!=null)
buf.append(att.toString());
else
log.warn("att was null");
buf.append("</attribute>\n");
}
buf.append("</attributes>");
log.trace(buf.toString());
fos.write(buf.toString().getBytes());
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
throw new MBeanException(e, "Error in persisting MBean.");
}
}
protected boolean isLoading()
{
return isLoading;
}
protected void setIsLoading(boolean newIsLoading)
{
isLoading = newIsLoading;
}
protected File getStoreFile(MBeanInfo metadata, boolean createFile)
throws MBeanException
{
Descriptor d = ((ModelMBeanInfo) metadata).getMBeanDescriptor();
String dirPath =
(String) d.getFieldValue(ModelMBeanConstants.PERSIST_LOCATION);
String file = (String) d.getFieldValue(ModelMBeanConstants.PERSIST_NAME);
if (dirPath == null)
{
log.debug(
"No "
+ ModelMBeanConstants.PERSIST_LOCATION
+ " descriptor value found, using '.'");
dirPath = ".";
}
if (file == null)
{
log.debug(
"No "
+ ModelMBeanConstants.PERSIST_NAME
+ " descriptor value found");
return null;
}
dirPath = StringPropertyReplacer.replaceProperties(dirPath);
file = StringPropertyReplacer.replaceProperties(file);
if (!file.endsWith(".xml"))
file = file + ".xml";
File dir = new File(dirPath);
File storeFile = new File(dir, file);
boolean exists = storeFile.exists();
log.debug("Store file is: " + storeFile.getAbsolutePath());
if (exists == false && createFile == true)
{
dir.mkdirs();
try
{
storeFile.createNewFile();
}
catch (IOException e)
{
throw new MBeanException(e, "Failed to create store file");
}
}
else if (exists == false)
{
storeFile = null;
}
return storeFile;
}
private Object convert(String val, String oType) throws Exception
{
PropertyEditor editor = PropertyEditors.getEditor(oType);
editor.setAsText(val);
return editor.getValue();
}
}