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.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.jar.JarEntry;
32 import java.util.jar.JarOutputStream;
33 import java.util.jar.Manifest;
34
35 import javax.tools.JavaCompiler;
36 import javax.tools.JavaFileObject;
37 import javax.tools.StandardJavaFileManager;
38 import javax.tools.ToolProvider;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.apache.hadoop.conf.Configuration;
43 import org.apache.hadoop.fs.Path;
44
45
46
47
48 public class ClassLoaderTestHelper {
49 private static final Log LOG = LogFactory.getLog(ClassLoaderTestHelper.class);
50
51 private static final int BUFFER_SIZE = 4096;
52
53
54
55
56
57
58
59
60 private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
61 try {
62 byte buffer[] = new byte[BUFFER_SIZE];
63
64 FileOutputStream stream = new FileOutputStream(archiveFile);
65 JarOutputStream out = new JarOutputStream(stream, new Manifest());
66
67 for (int i = 0; i < tobeJared.length; i++) {
68 if (tobeJared[i] == null || !tobeJared[i].exists()
69 || tobeJared[i].isDirectory()) {
70 continue;
71 }
72
73
74 JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
75 jarAdd.setTime(tobeJared[i].lastModified());
76 out.putNextEntry(jarAdd);
77
78
79 FileInputStream in = new FileInputStream(tobeJared[i]);
80 while (true) {
81 int nRead = in.read(buffer, 0, buffer.length);
82 if (nRead <= 0)
83 break;
84 out.write(buffer, 0, nRead);
85 }
86 in.close();
87 }
88 out.close();
89 stream.close();
90 LOG.info("Adding classes to jar file completed");
91 return true;
92 } catch (Exception ex) {
93 LOG.error("Error: " + ex.getMessage());
94 return false;
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public static File buildJar(String testDir,
111 String className, String code) throws Exception {
112 return buildJar(testDir, className, code, testDir);
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126 public static File buildJar(String testDir,
127 String className, String code, String folder) throws Exception {
128 String javaCode = code != null ? code : "public class " + className + " {}";
129 Path srcDir = new Path(testDir, "src");
130 File srcDirPath = new File(srcDir.toString());
131 srcDirPath.mkdirs();
132 File sourceCodeFile = new File(srcDir.toString(), className + ".java");
133 BufferedWriter bw = Files.newBufferedWriter(sourceCodeFile.toPath(), StandardCharsets.UTF_8);
134 bw.write(javaCode);
135 bw.close();
136
137
138 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
139 ArrayList<String> srcFileNames = new ArrayList<String>();
140 srcFileNames.add(sourceCodeFile.toString());
141 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null,
142 null);
143 Iterable<? extends JavaFileObject> cu =
144 fm.getJavaFileObjects(sourceCodeFile);
145 List<String> options = new ArrayList<String>();
146 options.add("-classpath");
147
148
149 String currentDir = new File(".").getAbsolutePath();
150 String classpath = currentDir + File.separator + "target"+ File.separator
151 + "classes" + System.getProperty("path.separator")
152 + System.getProperty("java.class.path") + System.getProperty("path.separator")
153 + System.getProperty("surefire.test.class.path");
154
155 options.add(classpath);
156 LOG.debug("Setting classpath to: " + classpath);
157
158 JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null,
159 options, null, cu);
160 assertTrue("Compile file " + sourceCodeFile + " failed.", task.call());
161
162
163 String jarFileName = className + ".jar";
164 File jarFile = new File(folder, jarFileName);
165 jarFile.getParentFile().mkdirs();
166 if (!createJarArchive(jarFile,
167 new File[]{new File(srcDir.toString(), className + ".class")})){
168 assertTrue("Build jar file failed.", false);
169 }
170 return jarFile;
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public static void addJarFilesToJar(File targetJar,
185 String libPrefix, File... srcJars) throws Exception {
186 FileOutputStream stream = new FileOutputStream(targetJar);
187 JarOutputStream out = new JarOutputStream(stream, new Manifest());
188 byte buffer[] = new byte[BUFFER_SIZE];
189
190 for (File jarFile: srcJars) {
191
192 JarEntry jarAdd = new JarEntry(libPrefix + jarFile.getName());
193 jarAdd.setTime(jarFile.lastModified());
194 out.putNextEntry(jarAdd);
195
196
197 FileInputStream in = new FileInputStream(jarFile);
198 while (true) {
199 int nRead = in.read(buffer, 0, buffer.length);
200 if (nRead <= 0)
201 break;
202 out.write(buffer, 0, nRead);
203 }
204 in.close();
205 }
206 out.close();
207 stream.close();
208 LOG.info("Adding jar file to outer jar file completed");
209 }
210
211 static String localDirPath(Configuration conf) {
212 return conf.get(ClassLoaderBase.LOCAL_DIR_KEY)
213 + File.separator + "jars" + File.separator;
214 }
215
216 }