KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > xmbean > XMLAttributePersistenceManagerTestService


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jmx.xmbean;
23
24 import java.io.File JavaDoc;
25
26 import javax.management.Attribute JavaDoc;
27 import javax.management.AttributeList JavaDoc;
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38
39 /**
40  * A test service that wraps an XMLAttributePersistenceManager
41  * configured to write to a random directory containing a space
42  * in its name, e.g. "./tmp/XmlApmXXXXXTest .dir"
43  *
44  * @see org.jboss.test.jmx.test.MLAttributePersistenceManagerUnitTestCase
45  *
46  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
47  * @version $Revision: 37406 $
48  */

49 public class XMLAttributePersistenceManagerTestService
50    extends ServiceMBeanSupport
51 {
52    private AttributePersistenceManager apm;
53    private File JavaDoc storeDir;
54    
55    protected void startService()
56       throws Exception JavaDoc
57    {
58       File JavaDoc tmpDir = ServerConfigLocator.locate().getServerTempDir();
59       boolean result;
60       
61       // Get a temporary file in the server tmp dir, with a space in its name
62
storeDir = File.createTempFile("XmlApm", "Test .dir", tmpDir);
63       
64       // Remove the tmp file
65
result = storeDir.delete();
66
67       // Recreate it as directory
68
result = storeDir.mkdir();
69       log.info("Created 'bad' store dir: " + storeDir + ", " + result);
70       
71       String JavaDoc dirURL = storeDir.toURL().toString();
72       log.info("Dir URL: " + dirURL);
73       
74       apm = new XMLAttributePersistenceManager();
75
76       // Initialize an XMLAttributePeristenceManager and
77
// configure it to point to the "bad" directory
78
apm.create(null, prepareConfig(dirURL));
79    }
80    
81    protected void stopService()
82       throws Exception JavaDoc
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 JavaDoc id, AttributeList JavaDoc atlist) throws Exception JavaDoc
98    {
99       apm.store(id, atlist);
100    }
101    
102    public AttributeList JavaDoc load(String JavaDoc id) throws Exception JavaDoc
103    {
104       return apm.load(id);
105    }
106    
107    public void selftest() throws Exception JavaDoc
108    {
109       // Store some attributes under an id
110
AttributeList JavaDoc alist = new AttributeList JavaDoc();
111       String JavaDoc storeId = "bananarama";
112       
113       Integer JavaDoc anInteger = new Integer JavaDoc(666);
114       String JavaDoc aString = new String JavaDoc("Evil Test");
115       alist.add(new Attribute JavaDoc("Attr1", anInteger));
116       alist.add(new Attribute JavaDoc("Attr2", aString));
117       apm.store(storeId, alist);
118       
119       // Read them back
120
AttributeList JavaDoc alist2 = apm.load(storeId);
121    }
122    
123    private Element JavaDoc prepareConfig(String JavaDoc dir) throws Exception JavaDoc
124    {
125       // build the config XML Element in memory using DOM
126
DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
127       Document JavaDoc doc = builder.newDocument();
128
129       // Create config element
130
Element JavaDoc config = doc.createElement(XMLAttributePersistenceManager.DATA_DIR_ELEMENT);
131       
132       // Insert a text node with the directory name
133
Node JavaDoc text = doc.createTextNode(dir);
134       
135       config.appendChild(text);
136       
137       // Return the config
138
return config;
139    }
140 }
141
Popular Tags