1 22 package org.jboss.test.jmx.test; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.net.URL ; 32 33 import javax.management.MBeanServerConnection ; 34 import javax.management.ObjectName ; 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 53 public abstract class AbstractURLDeploymentScannerTest extends JBossTestCase 54 { 55 protected static ObjectName scanner = ObjectNameFactory.create("jboss.test:type=DeploymentScanner,flavor=URL"); 56 57 private static URL scanDir; 58 59 public AbstractURLDeploymentScannerTest(String name) 60 { 61 super(name); 62 } 63 64 public static Test getTestSuite(Class clazz) throws Exception 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 71 { 72 MBeanServerConnection server = getServer(); 73 scanDir = getScanURL(); 74 getLog().debug("Scan Directory=" + scanDir); 75 server.invoke(scanner, "addURL", new Object [] { scanDir }, new String [] { URL .class.getName() }); 76 } 77 }; 78 return getDeploySetup(setup, "jbosstest-urlscanner-service.xml"); 79 } 80 81 public void startScanner() throws Exception 82 { 83 getServer().invoke(scanner, "start", null, null); 84 } 85 86 public void stopScanner() throws Exception 87 { 88 getServer().invoke(scanner, "stop", null, null); 89 } 90 91 94 public URL getTargetURL(String fileName) throws Exception 95 { 96 return new URL (scanDir, fileName); 97 } 98 99 102 public void suspendDeployment(URL url) throws Exception 103 { 104 getServer().invoke( 105 scanner, 106 "suspendDeployment", 107 new Object [] { url }, 108 new String [] { URL .class.getName() }); 109 } 110 111 114 public void resumeDeployment(URL url, boolean markUpToDate) throws Exception 115 { 116 getServer().invoke( 117 scanner, 118 "resumeDeployment", 119 new Object [] { url, new Boolean (markUpToDate) }, 120 new String [] { URL .class.getName(), boolean.class.getName() }); 121 } 122 123 public void hotDeploy(String fileName) throws Exception 124 { 125 hotDeploy(fileName, 2000); 126 } 127 128 public void hotDeploy(String fileName, long wait) throws Exception 129 { 130 URL url = getDeployURL(fileName); 131 URL destURL = getTargetURL(fileName); 132 copy(url, new File (destURL.getFile())); 133 if (wait > 0) 135 Thread.sleep(wait); 136 } 137 138 public void hotUndeploy(String fileName) throws Exception 139 { 140 hotUndeploy(fileName, 2000); 141 } 142 143 public void hotUndeploy(String fileName, long wait) throws Exception 144 { 145 URL destURL = new URL (scanDir, fileName); 146 delete(new File (destURL.getFile())); 147 if (wait > 0) 149 Thread.sleep(wait); 150 } 151 152 protected void copy(URL src, File dest) throws IOException 153 { 154 log.debug("Copying " + src + " -> " + dest); 155 156 File dir = dest.getParentFile(); 158 if (!dir.exists()) 159 { 160 boolean created = dir.mkdirs(); 161 if( created == false ) 162 throw new IOException ("mkdirs failed for: "+dir.getAbsolutePath()); 163 } 164 165 if( dest.exists() == true ) 167 { 168 boolean deleted = Files.delete(dest); 169 if( deleted == false ) 170 throw new IOException ("delete of previous content failed for: "+dest.getAbsolutePath()); 171 } 172 173 if (src.getProtocol().equals("file")) 174 { 175 File srcFile = new File (src.getFile()); 176 if (srcFile.isDirectory()) 177 { 178 log.debug("Making zip copy of: " + srcFile); 179 OutputStream out = new BufferedOutputStream (new FileOutputStream (dest)); 181 JarUtils.jar(out, srcFile.listFiles()); 182 out.close(); 183 return; 184 } 185 } 186 187 InputStream in = new BufferedInputStream (src.openStream()); 188 OutputStream out = new BufferedOutputStream (new FileOutputStream (dest)); 189 Streams.copy(in, out); 190 out.flush(); 191 out.close(); 192 in.close(); 193 } 194 195 protected void delete(File dest) throws IOException 196 { 197 log.debug("Deleting " + dest); 198 199 if( dest.exists() == true ) 201 { 202 boolean deleted = Files.delete(dest); 203 if( deleted == false ) 204 throw new IOException ("delete of previous content failed for: "+dest.getAbsolutePath()); 205 } 206 } 207 208 private static URL getScanURL() throws Exception 209 { 210 String deployDir = System.getProperty("jbosstest.deploy.dir"); 211 if (deployDir == null) 212 deployDir = "../lib"; 213 File file = new File (deployDir); 214 File scanDir = new File (file, "urlscannertest"); 215 URL url = scanDir.toURL(); 216 return url; 217 } 218 } 219 | Popular Tags |