1 package com.eviware.soapui.support.jnlp;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.JarURLConnection;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.util.Enumeration;
12 import java.util.jar.JarEntry;
13 import java.util.jar.JarFile;
14
15 public class WebstartUtil {
16
17 protected static String createWebStartDirectory(String name, String jarUrl)
18 throws IOException {
19
20 String deploymentUserTmp = System.getProperty("deployment.user.tmp");
21 JarFile jar = getJar(jarUrl);
22 String dir = createDirectory(deploymentUserTmp, name);
23 extract(jar, dir);
24 return dir;
25 }
26
27 private static void extract(JarFile jar, String dir) throws IOException,
28 FileNotFoundException {
29 makeDirectories(jar, dir);
30 extractFiles(jar, dir);
31 }
32
33 @SuppressWarnings("unchecked")
34 private static void extractFiles(JarFile jar, String eviwareDir)
35 throws IOException, FileNotFoundException {
36 Enumeration entries = jar.entries();
37 while (entries.hasMoreElements()) {
38 JarEntry file = (JarEntry) entries.nextElement();
39 File f = new File(eviwareDir + File.separator + file.getName());
40
41 if (file.isDirectory()) {
42 continue;
43 }
44
45 InputStream is = jar.getInputStream(file);
46 FileOutputStream fos = new FileOutputStream(f);
47
48 while (is.available() > 0) {
49 fos.write(is.read());
50 }
51
52 fos.close();
53 is.close();
54 }
55 }
56
57 @SuppressWarnings("unchecked")
58 private static void makeDirectories(JarFile jar, String eviwareDir) {
59 Enumeration entries = jar.entries();
60 while (entries.hasMoreElements()) {
61 JarEntry file = (JarEntry) entries.nextElement();
62 File f = new File(eviwareDir + File.separator + file.getName());
63 if (file.isDirectory()) {
64 f.mkdir();
65
66 }
67 }
68 }
69
70 private static JarFile getJar(String jarUrl) throws MalformedURLException,
71 IOException {
72
73 URL url = new URL("jar:" + jarUrl + "!/");
74 JarURLConnection jarConnection = (JarURLConnection) url
75 .openConnection();
76 JarFile jar = jarConnection.getJarFile();
77 return jar;
78 }
79
80 private static String createDirectory(String deploymentUserTmp,
81 String folderName) {
82 File folder = new File(deploymentUserTmp + File.separator + folderName);
83 folder.mkdir();
84
85 return folder.getAbsolutePath();
86 }
87
88
89 protected static boolean isWebStart(){
90 String webstart = System.getProperty("com.eviware.soapui.webstart","false");
91 if ("true".equalsIgnoreCase(webstart)) {
92 return true;
93 } else
94 return false;
95
96 }
97
98
99
100 }