View Javadoc
1   package auxtestlib;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   /**
7    * Class that provides utility methods to work with jars.
8    */
9   public final class JarUtils {
10  	/**
11  	 * Utility class: no constructor
12  	 */
13  	private JarUtils() {
14  		/*
15  		 * Nothing to do.
16  		 */
17  	}
18  
19  	/**
20  	 * Creates a jar from a directory. If the file already exists, this method
21  	 * will throw an exception.
22  	 * 
23  	 * @param directory the jar's base directory
24  	 * @param jarFile the file to create. If it is <code>null</code> the jar's
25  	 * name will be the directory name followed by <code>.jar</code> (the file
26  	 * is created in the same parent directory as the base directory)
27  	 * 
28  	 * @return o the file with the jar
29  	 * 
30  	 * @throws IOException the file already exists
31  	 */
32  	public static File makeJar(File directory, File jarFile) throws IOException {
33  		if (directory == null) {
34  			throw new IllegalArgumentException("directory == null");
35  		}
36  
37  		if (!directory.exists()) {
38  			throw new IllegalArgumentException("Directory '"
39  					+ directory.getAbsolutePath() + "' doesnt't exist.");
40  		}
41  
42  		if (!directory.isDirectory()) {
43  			throw new IllegalArgumentException("File '"
44  					+ directory.getAbsolutePath() + "' is not a directory.");
45  		}
46  
47  		File parent = directory.getParentFile();
48  		if (parent == null) {
49  			throw new IllegalArgumentException("Directory '"
50  					+ directory.getAbsolutePath() + "' does not name a parent.");
51  		}
52  
53  		File jfile = jarFile;
54  
55  		if (jfile == null) {
56  			jfile = new File(parent, directory.getName() + ".jar");
57  		}
58  
59  		if (jfile.exists()) {
60  			throw new IllegalArgumentException("Jar file '"
61  					+ jfile.getAbsolutePath() + "' already exists.");
62  		}
63  
64  		String args[] = new String[] { "jar", "cf", jfile.getAbsolutePath(),
65  				directory.getName() };
66  
67  		CommandRunner cr = new CommandRunner();
68  
69  		// TODO: We must find some way to handle the timeout.
70  		cr.run_command(args, parent, 60);
71  		return jfile;
72  	}
73  
74  	/**
75  	 * Creates a jar from a directory contents (not the directory itself). If
76  	 * teh file already exists, the method will throw an exception.
77  	 * 
78  	 * @param directory the jar's base directory
79  	 * @param jarFile jar file to create. If <code>null</code> the jar is
80  	 * created with the same parent as the directory and it's name is the
81  	 * directory name followed by <code>.jar</code>
82  	 * 
83  	 * @return the created jar file
84  	 * 
85  	 * @throws IOException file already exists
86  	 */
87  	public static File makeFullJar(File directory, File jarFile)
88  			throws IOException {
89  		if (directory == null) {
90  			throw new IllegalArgumentException("directory == null");
91  		}
92  
93  		if (jarFile == null) {
94  			throw new IllegalArgumentException("jarFile == null");
95  		}
96  
97  		if (!directory.exists()) {
98  			throw new IllegalArgumentException("Directory '"
99  					+ directory.getAbsolutePath() + "' doesnt't exist.");
100 		}
101 
102 		if (!directory.isDirectory()) {
103 			throw new IllegalArgumentException("File '"
104 					+ directory.getAbsolutePath() + "' is not a directory.");
105 		}
106 
107 		if (jarFile.exists()) {
108 			throw new IllegalArgumentException("Jar file '"
109 					+ jarFile.getAbsolutePath() + "' already exists.");
110 		}
111 
112 		File files[] = directory.listFiles();
113 		String args[] = new String[3 + files.length];
114 		args[0] = "jar";
115 		args[1] = "cf";
116 		args[2] = jarFile.getAbsolutePath();
117 		for (int i = 0; i < files.length; i++) {
118 			args[3 + i] = files[i].getName();
119 		}
120 
121 		CommandRunner cr = new CommandRunner();
122 
123 		// TODO: We must handle the timeout
124 		cr.run_command(args, directory, 60);
125 		return jarFile;
126 	}
127 }