package org.jboss.system.pm;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.jboss.logging.Logger;
import org.jboss.mx.persistence.AttributePersistenceManager;
import org.jboss.system.server.ServerConfigLocator;
import org.jboss.util.file.Files;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class XMLAttributePersistenceManager
implements AttributePersistenceManager
{
public static final String DATA_DIR_ELEMENT = "data-directory";
public static final String DEFAULT_BASE_DIR = "data/xmbean-attrs";
public static final String AL_ROOT_ELEMENT = "attribute-list";
public static final String AL_ID_ATTRIBUTE = "id";
public static final String AL_DATE_ATTRIBUTE = "date";
public static final String AL_ATTRIBUTE_ELEMENT = "attribute";
public static final String AL_NAME_ATTRIBUTE = "name";
public static final String AL_TYPE_ATTRIBUTE = "type";
public static final String AL_NULL_ATTRIBUTE = "null";
public static final String AL_SERIALIZED_ATTRIBUTE = "serialized";
public static final String AL_TRUE_VALUE = "true";
public static final String AL_FALSE_VALUE = "false";
private static final Logger log = Logger.getLogger(XMLAttributePersistenceManager.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private static final char[] hexDigits = new char[]
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private File dataDir;
private boolean state;
private Map idMap;
public XMLAttributePersistenceManager()
{
if (log.isDebugEnabled())
log.debug("Constructed");
}
public void create(String version, Element config)
throws Exception
{
if (getState()) {
return;
}
String baseDir = null;
if (config == null) {
baseDir = DEFAULT_BASE_DIR;
}
else {
if (!config.getTagName().equals(DATA_DIR_ELEMENT)) {
throw new Exception("expected '" + DATA_DIR_ELEMENT +
"' XML configuration element, got '" +
config.getTagName() + "'");
}
else {
baseDir = getElementContent(config);
}
}
this.dataDir = initDataDir(baseDir, version);
if (log.isDebugEnabled()) {
log.debug("Using data directory: " + this.dataDir.getCanonicalPath());
}
this.idMap = Collections.synchronizedMap(new HashMap());
setState(true);
}
public boolean getState()
{
return this.state;
}
public void destroy()
{
setState(false);
this.dataDir = null;
this.idMap = null;
}
public void store(String id, AttributeList attrs)
throws Exception
{
if (log.isDebugEnabled())
log.debug("store(" + id + ") attrs=" + attrs);
checkActiveState();
String origId = id;
id = mapId(id);
if (attrs == null)
throw new Exception("store() called with null AttributeList");
File file = checkFileForWrite(id);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Comment comment = doc.createComment(" automatically produced by XMLAttributePersistenceManager ");
doc.appendChild(comment);
Element root = doc.createElement(AL_ROOT_ELEMENT);
root.setAttribute(AL_ID_ATTRIBUTE, origId);
root.setAttribute(AL_DATE_ATTRIBUTE, dateFormat.format(new Date()));
doc.appendChild(root);
for (int i = 0; i < attrs.size(); i++) {
Attribute attr = (Attribute)attrs.get(i);
String name = attr.getName();
Object value = attr.getValue();
Element element = doc.createElement(AL_ATTRIBUTE_ELEMENT);
element.setAttribute(AL_NAME_ATTRIBUTE, name);
if (value == null) {
element.setAttribute(AL_NULL_ATTRIBUTE, AL_TRUE_VALUE);
root.appendChild(element);
}
else if (value instanceof org.w3c.dom.Element) {
element.setAttribute(AL_TYPE_ATTRIBUTE, "org.w3c.dom.Element");
Node copy = doc.importNode((org.w3c.dom.Element)value, true);
element.appendChild(copy);
root.appendChild(element);
}
else {
Class clazz = value.getClass();
String type = clazz.getName();
PropertyEditor peditor = PropertyEditorManager.findEditor(clazz);
if (peditor != null) {
peditor.setValue(value);
element.setAttribute(AL_TYPE_ATTRIBUTE, type);
element.appendChild(doc.createTextNode(peditor.getAsText()));
root.appendChild(element);
}
else if (value instanceof Serializable) {
String encoded = encodeAsHexString((Serializable)value);
if (encoded != null) {
element.setAttribute(AL_TYPE_ATTRIBUTE, type);
element.setAttribute(AL_SERIALIZED_ATTRIBUTE, AL_TRUE_VALUE);
element.appendChild(doc.createTextNode(encoded));
root.appendChild(element);
}
else {
root.appendChild(doc.createComment(
" WARN <attribute name=\"" + name + "\" type=\"" + type +
"\"/> could not be serialized "));
log.warn("Could not serialize attribute '" + name +
"' of type '" + type + "' and value: " + value);
}
}
else {
root.appendChild(doc.createComment(
" WARN <attribute name=\"" + name + "\" type=\"" + type +
"\"/> could not be persisted "));
log.warn("Could not find a way to persist attribute '" + name +
"' of type '" + type + "' and value: " + value);
}
}
}
try {
outputXmlFile(doc, file);
}
catch (Exception e) {
log.warn("Cannot persist AttributeList to: \"" + id + "\"", e);
throw e;
}
}
public AttributeList load(String id)
throws Exception
{
if (log.isDebugEnabled())
log.debug("load(" + id + ")");
checkActiveState();
id = mapId(id);
if (!getState())
return null;
AttributeList attrs = null;
File file = checkFileForRead(id);
if (file != null) {
Document doc = parseXmlFile(file);
NodeList docList = doc.getChildNodes();
Element root = null;
for (int i = 0; i < docList.getLength(); i++) {
Node node = docList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals(AL_ROOT_ELEMENT)) {
root = (Element)node;
break; }
}
if (root == null) {
throw new Exception("Expected XML element: " + AL_ROOT_ELEMENT);
}
else {
attrs = new AttributeList();
NodeList rootList = root.getChildNodes();
for (int i = 0; i < rootList.getLength(); i++) {
Node node = rootList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals(AL_ATTRIBUTE_ELEMENT)) {
Element element = (Element)node;
String name = element.getAttribute(AL_NAME_ATTRIBUTE);
if (!(name.length() > 0)) {
throw new Exception("Attribute '" + AL_NAME_ATTRIBUTE +
"' must be specified for element '" + AL_ATTRIBUTE_ELEMENT + "'");
}
if (element.getAttribute(AL_NULL_ATTRIBUTE).toLowerCase().equals(AL_TRUE_VALUE)) {
attrs.add(new Attribute(name, null));
}
else if (element.getAttribute(AL_SERIALIZED_ATTRIBUTE).toLowerCase().equals(AL_TRUE_VALUE)) {
String hexStr = getElementContent(element);
Serializable obj = decodeFromHexString(hexStr);
if (obj == null) {
throw new Exception("Failed to deserialize attribute '" + name + "'");
}
else {
attrs.add(new Attribute(name, obj));
}
}
else {
String type = element.getAttribute(AL_TYPE_ATTRIBUTE);
if (!(type.length() > 0)) {
throw new Exception("Attribute '" + AL_TYPE_ATTRIBUTE +
"' must be specified for name='" + name + "'");
}
if (type.equals("org.w3c.dom.Element")) {
NodeList nlist = element.getChildNodes();
Element el = null;
for (int j = 0; j < nlist.getLength(); j++) {
Node n = nlist.item(j);
if (n.getNodeType() == Node.ELEMENT_NODE)
{
el = (Element)n;
break;
}
}
if (el != null) {
attrs.add(new Attribute(name, el.cloneNode(true)));
}
else {
attrs.add(new Attribute(name, null));
}
}
else {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class clazz = null;
try {
clazz = cl.loadClass(type);
}
catch (ClassNotFoundException e) {
throw new Exception("Class not found for attribute '" + name +
"' of type '" + type + "'");
}
PropertyEditor peditor = PropertyEditorManager.findEditor(clazz);
if (peditor != null) {
String value = getElementContent(element);
peditor.setAsText(value);
attrs.add(new Attribute(name, peditor.getValue()));
}
else {
throw new Exception("Cannot find a way to load attribute '" + name +
"' of type '" + type + "'");
}
}
}
}
} }
}
if (log.isDebugEnabled())
log.debug("load() returns with: " + attrs);
return attrs;
}
public boolean exists(String id)
throws Exception
{
checkActiveState();
return (new File(this.dataDir, mapId(id))).isFile();
}
public void remove(String id)
throws Exception
{
checkActiveState();
(new File(this.dataDir, mapId(id))).delete();
}
public void removeAll()
throws Exception
{
checkActiveState();
String[] files = this.dataDir.list(new XMLFilter());
if (files != null) {
for (int i = 0; i < files.length; i++) {
(new File(this.dataDir, files[i])).delete();
}
}
}
public String[] listAll()
throws Exception
{
checkActiveState();
String[] files = this.dataDir.list(new XMLFilter());
String[] result = null;
if (files != null) {
result = new String[files.length];
for (int i = 0; i < files.length; i++) {
result[i] = mapFile(files[i]);
}
}
return result;
}
private void setState(boolean state)
{
this.state = state;
}
private File initDataDir(String baseDir, String versionTag)
throws Exception
{
File dir = null;
try {
URL fileURL = new URL(baseDir);
File file = new File(fileURL.getFile());
if(file.isDirectory() && file.canRead() && file.canWrite()) {
dir = file;
}
}
catch(Exception e) {
File homeDir = ServerConfigLocator.locate().getServerHomeDir();
dir = new File(homeDir, baseDir);
dir.mkdirs();
if (!dir.isDirectory())
throw new Exception("The base directory is not valid: "
+ dir.getCanonicalPath());
}
if (versionTag != null && !versionTag.equals("")) {
dir = new File(dir, versionTag);
dir.mkdirs();
if (!dir.isDirectory())
throw new Exception("The data directory is not valid: "
+ dir.getCanonicalPath());
}
return dir;
}
private String encodeAsHexString(Serializable obj)
{
String retn = null;
if (obj != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
byte[] bytes = baos.toByteArray();
StringBuffer sbuf = new StringBuffer(1024);
for (int i = 0; i < bytes.length; i++) {
sbuf.append(hexDigits[ (bytes[i] >> 4) & 0xF ]); sbuf.append(hexDigits[ (bytes[i] ) & 0xF ]); }
retn = sbuf.toString();
}
catch (IOException e) {
}
}
return retn;
}
private Serializable decodeFromHexString(String hexStr)
{
int len = hexStr.length() / 2;
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++) {
char h1 = hexStr.charAt(i * 2); char h2 = hexStr.charAt(i * 2 + 1);
int d1 = (h1 >= 'a') ? (10 + h1 - 'a')
: ((h1 >= 'A') ? (10 + h1 - 'A')
: (h1 - '0'));
int d2 = (h2 >= 'a') ? (10 + h2 - 'a')
: ((h2 >= 'A') ? (10 + h2 - 'A')
: (h2 - '0'));
bytes[i] = (byte)(d1 * 16 + d2); }
Serializable retn = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
retn = (Serializable)ois.readObject();
}
catch (IOException e) {
log.warn("Cannot deserialize object", e); }
catch (ClassNotFoundException e) {
log.warn("Cannot deserialize object", e);
}
return retn;
}
private void outputXmlFile(Document doc, File file)
throws Exception
{
Source source = new DOMSource(doc);
Result result = new StreamResult(file);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
}
private Document parseXmlFile(File file)
throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
Document doc = factory.newDocumentBuilder().parse(file);
return doc;
}
private File checkFileForWrite(String filename)
throws Exception
{
File file = new File(this.dataDir, filename);
if (file.isFile()) {
if (file.canRead() && file.canWrite()) {
return file; }
else {
throw new Exception("file '" + filename + "' is not r/w"); }
}
else if (file.isDirectory()) {
throw new Exception(filename + " is a directory!"); }
else {
return file; }
}
private File checkFileForRead(String filename)
throws Exception
{
File file = new File(this.dataDir, filename);
if (file.isFile()) {
if (file.canRead() && file.canWrite()) {
return file; }
else {
throw new Exception("file '" + filename + "' is not r/w"); }
}
else if (file.isDirectory()) {
throw new Exception(filename + " is a directory!"); }
else {
return null; }
}
private String getElementContent(Element element)
{
NodeList nlist = element.getChildNodes();
StringBuffer sbuf = new StringBuffer(1024);
for (int i = 0; i < nlist.getLength(); i++) {
Node node = nlist.item(i);
if (node.getNodeType() == Node.TEXT_NODE) {
sbuf.append(((Text)node).getData());
}
}
return sbuf.toString();
}
private void checkActiveState()
{
if (!getState()) {
throw new IllegalStateException("AttributePersistenceManager not active");
}
}
private class XMLFilter
implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return name.endsWith(".xml");
}
}
private String mapId(String id)
throws Exception
{
if (id == null) {
throw new Exception("called with null id");
}
else {
String file = (String)this.idMap.get(id);
if (file == null) {
file = Files.encodeFileName(id) + ".xml";
this.idMap.put(id, file);
}
return file;
}
}
private String mapFile(String file)
{
if (file == null) {
return null;
}
else {
file = file.substring(0, file.length() - 4);
return Files.decodeFileName(file);
}
}
}