/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.test.jmx.xmbean;

import java.io.File;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jboss.mx.persistence.AttributePersistenceManager;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.system.pm.XMLAttributePersistenceManager;
import org.jboss.system.server.ServerConfigLocator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * A test service that wraps an XMLAttributePersistenceManager
 * configured to write to a random directory containing a space
 * in its name, e.g. "./tmp/XmlApmXXXXXTest .dir"
 * 
 * @see org.jboss.test.jmx.test.MLAttributePersistenceManagerUnitTestCase
 * 
 * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
 * @version $Revision: 1.1.2.1 $
 */
public class XMLAttributePersistenceManagerTestService
   extends ServiceMBeanSupport
{
   private AttributePersistenceManager apm;
   private File storeDir;
   
   protected void startService()
      throws Exception
   {
      File tmpDir = ServerConfigLocator.locate().getServerTempDir();
      boolean result;
      
      // Get a temporary file in the server tmp dir, with a space in its name
      storeDir = File.createTempFile("XmlApm", "Test .dir", tmpDir);
      
      // Remove the tmp file
      result = storeDir.delete();

      // Recreate it as directory
      result = storeDir.mkdir();
      log.info("Created 'bad' store dir: " + storeDir + ", " + result);
      
      String dirURL = storeDir.toURL().toString();
      log.info("Dir URL: " + dirURL);      
      
      apm = new XMLAttributePersistenceManager();

      // Initialize an XMLAttributePeristenceManager and
      // configure it to point to the "bad" directory
      apm.create(null, prepareConfig(dirURL));
   }
   
   protected void stopService()
      throws Exception
   {
      if (apm != null)
      {
         apm.removeAll();
         apm.destroy();
         log.info("Destroyed AttributePersistenceManager");
      }
      if (storeDir != null)
      {
         boolean result = storeDir.delete();
         log.info("Removed: " +  storeDir + ", " + result);
      }
   }
   
   public void store(String id, AttributeList atlist) throws Exception
   {
      apm.store(id, atlist);
   }
   
   public AttributeList load(String id) throws Exception
   {
      return apm.load(id);
   }
   
   public void selftest() throws Exception
   {
      // Store some attributes under an id
      AttributeList alist = new AttributeList();
      String storeId = "bananarama";
      
      Integer anInteger = new Integer(666);
      String aString = new String("Evil Test");
      alist.add(new Attribute("Attr1", anInteger));
      alist.add(new Attribute("Attr2", aString));
      apm.store(storeId, alist);
      
      // Read them back
      AttributeList alist2 = apm.load(storeId);      
   }
   
   private Element prepareConfig(String dir) throws Exception
   {
      // build the config XML Element in memory using DOM
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.newDocument();

      // Create config element
      Element config = doc.createElement(XMLAttributePersistenceManager.DATA_DIR_ELEMENT);
      
      // Insert a text node with the directory name
      Node text = doc.createTextNode(dir);
      
      config.appendChild(text);
      
      // Return the config
      return config;
   }   
}
