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