KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > test > AbstractURLDeploymentScannerTest


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.test;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import javax.management.MBeanServerConnection JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38
39 import org.jboss.mx.util.ObjectNameFactory;
40 import org.jboss.test.JBossTestCase;
41 import org.jboss.test.JBossTestSetup;
42 import org.jboss.util.file.Files;
43 import org.jboss.util.file.JarUtils;
44 import org.jboss.util.stream.Streams;
45
46 /**
47  * Base test for url deployment scanner
48  *
49  * @author adrian@jboss.org
50  * @author dimitris@jboss.org
51  * @version $Revision: 44464 $
52  */

53 public abstract class AbstractURLDeploymentScannerTest extends JBossTestCase
54 {
55    protected static ObjectName JavaDoc scanner = ObjectNameFactory.create("jboss.test:type=DeploymentScanner,flavor=URL");
56    
57    private static URL JavaDoc scanDir;
58    
59    public AbstractURLDeploymentScannerTest(String JavaDoc name)
60    {
61       super(name);
62    }
63
64    public static Test getTestSuite(Class JavaDoc clazz) throws Exception JavaDoc
65    {
66       TestSuite suite = new TestSuite();
67       suite.addTest(new TestSuite(clazz));
68       JBossTestSetup setup = new JBossTestSetup(suite)
69       {
70          protected void setUp() throws Exception JavaDoc
71          {
72             MBeanServerConnection JavaDoc server = getServer();
73             scanDir = getScanURL();
74             getLog().debug("Scan Directory=" + scanDir);
75             server.invoke(scanner, "addURL", new Object JavaDoc[] { scanDir }, new String JavaDoc[] { URL JavaDoc.class.getName() });
76          }
77       };
78       return getDeploySetup(setup, "jbosstest-urlscanner-service.xml");
79    }
80
81    public void startScanner() throws Exception JavaDoc
82    {
83       getServer().invoke(scanner, "start", null, null);
84    }
85
86    public void stopScanner() throws Exception JavaDoc
87    {
88       getServer().invoke(scanner, "stop", null, null);
89    }
90
91    /**
92     * The deployment URL of a filename, inside the scan directory
93     */

94    public URL JavaDoc getTargetURL(String JavaDoc fileName) throws Exception JavaDoc
95    {
96       return new URL JavaDoc(scanDir, fileName);
97    }
98    
99    /**
100     * Delegate to Scanner
101     */

102    public void suspendDeployment(URL JavaDoc url) throws Exception JavaDoc
103    {
104       getServer().invoke(
105             scanner,
106             "suspendDeployment",
107             new Object JavaDoc[] { url },
108             new String JavaDoc[] { URL JavaDoc.class.getName() });
109    }
110    
111    /**
112     * Delegate to Scanner
113     */

114    public void resumeDeployment(URL JavaDoc url, boolean markUpToDate) throws Exception JavaDoc
115    {
116       getServer().invoke(
117             scanner,
118             "resumeDeployment",
119             new Object JavaDoc[] { url, new Boolean JavaDoc(markUpToDate) },
120             new String JavaDoc[] { URL JavaDoc.class.getName(), boolean.class.getName() });
121    }
122    
123    public void hotDeploy(String JavaDoc fileName) throws Exception JavaDoc
124    {
125       hotDeploy(fileName, 2000);
126    }
127
128    public void hotDeploy(String JavaDoc fileName, long wait) throws Exception JavaDoc
129    {
130       URL JavaDoc url = getDeployURL(fileName);
131       URL JavaDoc destURL = getTargetURL(fileName);
132       copy(url, new File JavaDoc(destURL.getFile()));
133       // TODO something better than a sleep
134
if (wait > 0)
135          Thread.sleep(wait);
136    }
137
138    public void hotUndeploy(String JavaDoc fileName) throws Exception JavaDoc
139    {
140       hotUndeploy(fileName, 2000);
141    }
142
143    public void hotUndeploy(String JavaDoc fileName, long wait) throws Exception JavaDoc
144    {
145       URL JavaDoc destURL = new URL JavaDoc(scanDir, fileName);
146       delete(new File JavaDoc(destURL.getFile()));
147       // TODO something better than a sleep
148
if (wait > 0)
149          Thread.sleep(wait);
150    }
151
152    protected void copy(URL JavaDoc src, File JavaDoc dest) throws IOException JavaDoc
153    {
154       log.debug("Copying " + src + " -> " + dest);
155       
156       // Validate that the dest parent directory structure exists
157
File JavaDoc dir = dest.getParentFile();
158       if (!dir.exists())
159       {
160          boolean created = dir.mkdirs();
161          if( created == false )
162             throw new IOException JavaDoc("mkdirs failed for: "+dir.getAbsolutePath());
163       }
164
165       // Remove any existing dest content
166
if( dest.exists() == true )
167       {
168          boolean deleted = Files.delete(dest);
169          if( deleted == false )
170             throw new IOException JavaDoc("delete of previous content failed for: "+dest.getAbsolutePath());
171       }
172
173       if (src.getProtocol().equals("file"))
174       {
175          File JavaDoc srcFile = new File JavaDoc(src.getFile());
176          if (srcFile.isDirectory())
177          {
178             log.debug("Making zip copy of: " + srcFile);
179             // make a jar archive of the directory
180
OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(dest));
181             JarUtils.jar(out, srcFile.listFiles());
182             out.close();
183             return;
184          }
185       }
186
187       InputStream JavaDoc in = new BufferedInputStream JavaDoc(src.openStream());
188       OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(dest));
189       Streams.copy(in, out);
190       out.flush();
191       out.close();
192       in.close();
193    }
194
195    protected void delete(File JavaDoc dest) throws IOException JavaDoc
196    {
197       log.debug("Deleting " + dest);
198
199       // Remove any existing dest content
200
if( dest.exists() == true )
201       {
202          boolean deleted = Files.delete(dest);
203          if( deleted == false )
204             throw new IOException JavaDoc("delete of previous content failed for: "+dest.getAbsolutePath());
205       }
206    }
207
208    private static URL JavaDoc getScanURL() throws Exception JavaDoc
209    {
210       String JavaDoc deployDir = System.getProperty("jbosstest.deploy.dir");
211       if (deployDir == null)
212          deployDir = "../lib";
213       File JavaDoc file = new File JavaDoc(deployDir);
214       File JavaDoc scanDir = new File JavaDoc(file, "urlscannertest");
215       URL JavaDoc url = scanDir.toURL();
216       return url;
217    }
218 }
219
Popular Tags