1 22 package org.jboss.test.jmx.xmbean; 23 24 import java.io.File ; 25 26 import javax.management.Attribute ; 27 import javax.management.AttributeList ; 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 31 import org.jboss.mx.persistence.AttributePersistenceManager; 32 import org.jboss.system.ServiceMBeanSupport; 33 import org.jboss.system.pm.XMLAttributePersistenceManager; 34 import org.jboss.system.server.ServerConfigLocator; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 39 49 public class XMLAttributePersistenceManagerTestService 50 extends ServiceMBeanSupport 51 { 52 private AttributePersistenceManager apm; 53 private File storeDir; 54 55 protected void startService() 56 throws Exception 57 { 58 File tmpDir = ServerConfigLocator.locate().getServerTempDir(); 59 boolean result; 60 61 storeDir = File.createTempFile("XmlApm", "Test .dir", tmpDir); 63 64 result = storeDir.delete(); 66 67 result = storeDir.mkdir(); 69 log.info("Created 'bad' store dir: " + storeDir + ", " + result); 70 71 String dirURL = storeDir.toURL().toString(); 72 log.info("Dir URL: " + dirURL); 73 74 apm = new XMLAttributePersistenceManager(); 75 76 apm.create(null, prepareConfig(dirURL)); 79 } 80 81 protected void stopService() 82 throws Exception 83 { 84 if (apm != null) 85 { 86 apm.removeAll(); 87 apm.destroy(); 88 log.info("Destroyed AttributePersistenceManager"); 89 } 90 if (storeDir != null) 91 { 92 boolean result = storeDir.delete(); 93 log.info("Removed: " + storeDir + ", " + result); 94 } 95 } 96 97 public void store(String id, AttributeList atlist) throws Exception 98 { 99 apm.store(id, atlist); 100 } 101 102 public AttributeList load(String id) throws Exception 103 { 104 return apm.load(id); 105 } 106 107 public void selftest() throws Exception 108 { 109 AttributeList alist = new AttributeList (); 111 String storeId = "bananarama"; 112 113 Integer anInteger = new Integer (666); 114 String aString = new String ("Evil Test"); 115 alist.add(new Attribute ("Attr1", anInteger)); 116 alist.add(new Attribute ("Attr2", aString)); 117 apm.store(storeId, alist); 118 119 AttributeList alist2 = apm.load(storeId); 121 } 122 123 private Element prepareConfig(String dir) throws Exception 124 { 125 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 127 Document doc = builder.newDocument(); 128 129 Element config = doc.createElement(XMLAttributePersistenceManager.DATA_DIR_ELEMENT); 131 132 Node text = doc.createTextNode(dir); 134 135 config.appendChild(text); 136 137 return config; 139 } 140 } 141 | Popular Tags |