1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import static org.junit.Assert.assertTrue;
21
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.FileWriter;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarOutputStream;
31 import java.util.jar.Manifest;
32
33 import javax.tools.JavaCompiler;
34 import javax.tools.JavaFileObject;
35 import javax.tools.StandardJavaFileManager;
36 import javax.tools.ToolProvider;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.hadoop.fs.Path;
41
42
43
44
45 public class ClassLoaderTestHelper {
46 private static final Log LOG = LogFactory.getLog(ClassLoaderTestHelper.class);
47
48
49
50
51
52
53
54 private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
55 try {
56 byte buffer[] = new byte[4096];
57
58 FileOutputStream stream = new FileOutputStream(archiveFile);
59 JarOutputStream out = new JarOutputStream(stream, new Manifest());
60
61 for (int i = 0; i < tobeJared.length; i++) {
62 if (tobeJared[i] == null || !tobeJared[i].exists()
63 || tobeJared[i].isDirectory()) {
64 continue;
65 }
66
67
68 JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
69 jarAdd.setTime(tobeJared[i].lastModified());
70 out.putNextEntry(jarAdd);
71
72
73 FileInputStream in = new FileInputStream(tobeJared[i]);
74 while (true) {
75 int nRead = in.read(buffer, 0, buffer.length);
76 if (nRead <= 0)
77 break;
78 out.write(buffer, 0, nRead);
79 }
80 in.close();
81 }
82 out.close();
83 stream.close();
84 LOG.info("Adding classes to jar file completed");
85 return true;
86 } catch (Exception ex) {
87 LOG.error("Error: " + ex.getMessage());
88 return false;
89 }
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public static File buildJar(String testDir,
105 String className, String code) throws Exception {
106 return buildJar(testDir, className, code, testDir);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120 public static File buildJar(String testDir,
121 String className, String code, String folder) throws Exception {
122 String javaCode = code != null ? code : "public class " + className + " {}";
123 Path srcDir = new Path(testDir, "src");
124 File srcDirPath = new File(srcDir.toString());
125 srcDirPath.mkdirs();
126 File sourceCodeFile = new File(srcDir.toString(), className + ".java");
127 BufferedWriter bw = new BufferedWriter(new FileWriter(sourceCodeFile));
128 bw.write(javaCode);
129 bw.close();
130
131
132 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
133 ArrayList<String> srcFileNames = new ArrayList<String>();
134 srcFileNames.add(sourceCodeFile.toString());
135 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null,
136 null);
137 Iterable<? extends JavaFileObject> cu =
138 fm.getJavaFileObjects(sourceCodeFile);
139 List<String> options = new ArrayList<String>();
140 options.add("-classpath");
141
142
143 String currentDir = new File(".").getAbsolutePath();
144 String classpath = currentDir + File.separator + "target"+ File.separator
145 + "classes" + System.getProperty("path.separator")
146 + System.getProperty("java.class.path") + System.getProperty("path.separator")
147 + System.getProperty("surefire.test.class.path");
148 options.add(classpath);
149 LOG.debug("Setting classpath to: " + classpath);
150
151 JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null,
152 options, null, cu);
153 assertTrue("Compile file " + sourceCodeFile + " failed.", task.call());
154
155
156 String jarFileName = className + ".jar";
157 File jarFile = new File(folder, jarFileName);
158 if (!createJarArchive(jarFile,
159 new File[]{new File(srcDir.toString(), className + ".class")})){
160 assertTrue("Build jar file failed.", false);
161 }
162 return jarFile;
163 }
164 }