void run() throws Exception { File classesDir = new File("classes"); classesDir.mkdirs(); writeFile(baseFile, baseText); String[] javacArgs = {"-d", classesDir.getPath(), baseFile.getPath()}; com.sun.tools.javac.Main.compile(javacArgs); writeFile(srcFile, srcText); String[] args = { "-d", "api", "-classpath", classesDir.getPath(), "-package", "p", srcFile.getPath() }; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); PrintStream prev = System.err; System.setErr(ps); try { int rc = com.sun.tools.javadoc.Main.execute(args); } finally { System.err.flush(); System.setErr(prev); } String out = baos.toString(); System.out.println(out); String errorMessage = "java.lang.IllegalArgumentException: <clinit>"; if (out.contains(errorMessage)) throw new Exception("error message found: " + errorMessage); }
String javac(String... args) throws Exception { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int rc = com.sun.tools.javac.Main.compile(args, pw); if (rc != 0) throw new Exception("javac failed, rc=" + rc); return sw.toString(); }
File compileTestFile(File f) { int rc = com.sun.tools.javac.Main.compile(new String[] {"-source", "1.7", "-g", f.getPath()}); if (rc != 0) throw new Error("compilation failed. rc=" + rc); String path = f.getPath(); return new File(path.substring(0, path.length() - 5) + ".class"); }
public void run() throws Exception { // Selection of files to be compiled File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A"); File relJar = createJar(new File("rel.jar"), "j.R"); File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A"); File relDir = createDir(new File("rel.dir"), "d.R"); File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }"); File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }"); File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }"); // This next class references other classes that will be found on the source path // and which will therefore need to be compiled as well. File mainFile = writeFile(new File("Main.java"), "class Main { j.A ja; j.R jr; d.A da; d.R dr; }" + ""); String sourcePath = createPath(absJar, relJar, absDir, relDir); File outDir = new File("classes"); outDir.mkdirs(); String[] args = { "-sourcepath", sourcePath, "-d", outDir.getPath(), absTestFile.getPath(), relTestFile.getPath(), relTest2File.getPath(), mainFile.getPath(), }; System.err.println("compile: " + Arrays.asList(args)); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int rc = com.sun.tools.javac.Main.compile(args, pw); pw.close(); if (rc != 0) { System.err.println(sw.toString()); throw new Exception("unexpected exit from javac: " + rc); } Set<File> expect = getFiles( outDir, "d/A.class", "d/A$Inner.class", "d/R.class", "d/R$Inner.class", "j/A.class", "j/A$Inner.class", "j/R.class", "j/R$Inner.class", "AbsTest.class", "AbsTest$Inner.class", "RelTest.class", "RelTest$Inner.class", "p/RelTest2.class", "p/RelTest2$Inner.class", "Main.class"); Set<File> found = findFiles(outDir); if (!found.equals(expect)) { if (found.containsAll(expect)) throw new Exception("unexpected files found: " + diff(found, expect)); else if (expect.containsAll(found)) throw new Exception("expected files not found: " + diff(expect, found)); } for (File f : found) verifySourceFileAttribute(f); if (errors > 0) throw new Exception(errors + " errors occurred"); }
private String createDefinition(Object runConfigNode) throws Exception { Element benchDefNode = (Element) xp.evaluate("fd:benchmarkDefinition", runConfigNode, XPathConstants.NODE); if (benchDefNode == null) { return null; } String definingClassName = xp.evaluate("fd:driverConfig/@name", runConfigNode); // Get the cycleTime annotation Element driverConfigNode = (Element) xp.evaluate("fd:driverConfig", runConfigNode, XPathConstants.NODE); String requestLagTime = getRequestLagTime(driverConfigNode); /** * Load the template from file Template file is small, but is there a better way to read this? */ String line = null; BufferedReader br = null; InputStream is = null; try { ClassLoader cl = this.getClass().getClassLoader(); is = cl.getResourceAsStream("driver_template"); br = new BufferedReader(new InputStreamReader(is)); } catch (Exception ex) { // what to do? ex.printStackTrace(); throw new ConfigurationException(ex); } StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { if (!line.equals("\n")) { sb.append(line).append("\n"); } } String template = sb.toString(); is.close(); // Obtain the provider. TransportProvider provider = TransportProvider.getProvider(); /** * Get the operation token out of the template. Use this to create multiple * operations/functions that will replace the token in the java file */ String opTemplate = template.substring((template.indexOf("#operation") + 10), template.indexOf("operation#")); StringBuilder operations = new StringBuilder(); Element operationNode = null; int i = 1; while ((operationNode = (Element) xp.evaluate( ("fd:driverConfig/fd:operation[" + i + "]"), runConfigNode, XPathConstants.NODE)) != null) { /* * There are many different options here. First, the operation * is either POST or GET. In either case, the subst element * indicates whether or not random strings are present in the * payload data (for backward compatibility, it is assumed * that random data is present). * * For POST, we can read the data from a file, and we can * encode the data as form-encoded or binary (octet-stream) */ boolean isPost = false; boolean isBinary = false; boolean isFile = false; boolean doSubst = true; String requestLagTimeOverride = getRequestLagTime(operationNode); String operationName = xp.evaluate("fd:name", operationNode); String url = xp.evaluate("fd:url", operationNode); String max90th = xp.evaluate("fd:max90th", operationNode); String kbps = xp.evaluate("fd:kbps", operationNode); String accept = xp.evaluate("fd:accept", operationNode); String requestString = ""; Element requestNode = (Element) xp.evaluate("fd:get", operationNode, XPathConstants.NODE); if (requestNode == null) { // Can't have both post & get either, but if both are there, // will assume you meant GET. requestNode = (Element) xp.evaluate("fd:post", operationNode, XPathConstants.NODE); if (requestNode != null) { isPost = true; if ("true".equalsIgnoreCase(requestNode.getAttributeNS(null, "file"))) { isFile = true; } if ("true".equalsIgnoreCase(requestNode.getAttributeNS(null, "binary"))) { isBinary = true; } } else { throw new ConfigurationException("<operation> " + "must have a either a get/post"); } // Can't have both post & get either, but if there, // will assume you meant GET. } if ("false".equalsIgnoreCase(requestNode.getAttributeNS(null, "subst"))) { doSubst = false; } if (isBinary && doSubst) { throw new ConfigurationException( "<operation> " + "cannot be both binary and perform substitution"); } requestString = requestNode.getNodeValue(); if (requestString == null) { CDATASection cDataNode = (CDATASection) requestNode.getFirstChild(); if (cDataNode != null) { requestString = cDataNode.getNodeValue(); } } if (requestString == null) { requestString = ""; } if (requestLagTimeOverride == null) { requestLagTimeOverride = ""; } if (operationName == null) { throw new ConfigurationException("<operation> must have a <name> "); } if (url == null) { throw new ConfigurationException("<operation> must have a <url>"); } RunInfoDefinition rid; if (isPost) { if (isFile) { rid = new RunInfoPostFileDefinition(); } else { rid = new RunInfoPostDataDefinition(); } } else { rid = new RunInfoGetDefinition(); } rid.init(doSubst, isBinary, url, requestString, kbps, accept); // Create the benchmark Operation annotation StringBuilder bmop = new StringBuilder("@BenchmarkOperation(name = \"").append(operationName); bmop.append("\", percentileLimits={ ") .append(max90th) .append(", ") .append(max90th) .append(", ") .append(max90th) .append("}, "); if (provider == TransportProvider.SUN && url.startsWith("https")) bmop.append("timing = com.sun.faban.driver.Timing.MANUAL"); else bmop.append("timing = com.sun.faban.driver.Timing.AUTO"); bmop.append(")"); String opTemplateClone = new String(opTemplate); // replace tokens with actual content opTemplateClone = opTemplateClone.replaceAll("@RequestLagTime@", requestLagTimeOverride); opTemplateClone = opTemplateClone.replaceAll("@Operations@", bmop.toString()); opTemplateClone = opTemplateClone.replaceAll("@operationName@", operationName); opTemplateClone = opTemplateClone.replaceAll("@url@", rid.getURL(i)); opTemplateClone = opTemplateClone.replaceAll("@postRequest@", rid.getPostRequest(i)); opTemplateClone = opTemplateClone.replaceAll("@Statics@", rid.getStatics(i)); opTemplateClone = opTemplateClone.replaceAll("@doKbps@", rid.getKbps(i)); opTemplateClone = opTemplateClone.replaceAll("@doHeaders@", rid.getHeaders(i)); opTemplateClone = opTemplateClone.replaceAll("@doTiming@", rid.doTiming(i, provider)); operations.append(opTemplateClone); i++; } String benchmarkDef = getBenchmarkDefinition(benchDefNode); // replace tokens in template template = template.replaceFirst("#operation(.*\\n*)*operation#", operations.toString()); template = template.replaceFirst("@RequestLagTime@", requestLagTime); template = template.replaceFirst("@BenchmarkDefinition@", benchmarkDef); template = template.replaceAll("@DriverClassName@", definingClassName); template = template.replaceFirst("@ProviderClass@", provider.providerClass); template = template.replaceFirst("@BenchmarkDriverName@", definingClassName); String tmpDir = System.getProperty("faban.tmpdir"); if (tmpDir == null) { tmpDir = System.getProperty("java.io.tmpdir"); } if (!tmpDir.endsWith(File.separator)) { tmpDir = tmpDir + File.separator; } String className = new StringBuilder(tmpDir).append(definingClassName).append(".java").toString(); try { // convert class name to filename? PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(className))); pw.print(template); pw.flush(); } catch (Exception ex) { ex.printStackTrace(); throw new ConfigurationException(ex); } String classpath = System.getProperty("java.class.path"); String arg[] = new String[] {"-classpath", classpath, className}; int errorCode = com.sun.tools.javac.Main.compile(arg); if (errorCode != 0) { throw new ConfigurationException( "unable to compile generated driver file. " + "check output for errors"); } return definingClassName; }