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