1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package jdk.test.lib.util; |
|
25 |
|
26 import java.io.File; |
|
27 import java.io.IOException; |
|
28 import java.nio.file.Path; |
|
29 import java.nio.file.Paths; |
|
30 import java.util.Arrays; |
|
31 import java.util.jar.Attributes; |
|
32 import java.util.jar.Manifest; |
|
33 |
|
34 import jdk.test.lib.Utils; |
|
35 import jdk.test.lib.util.JarUtils; |
|
36 |
|
37 /** |
|
38 * A builder for a common Java agent. |
|
39 * Can be used directly from the jtreg test header to |
|
40 * build a java agent before the test is executed. |
|
41 * |
|
42 * E.g.: |
|
43 * @run driver jdk.test.lib.util.JavaAgentBuilder |
|
44 * jdk.jfr.javaagent.EventEmitterAgent EventEmitterAgent.jar |
|
45 * |
|
46 */ |
|
47 public class JavaAgentBuilder { |
|
48 |
|
49 /** |
|
50 * Build a java agent jar file with a given agent class. |
|
51 * |
|
52 * @param args[0] fully qualified name of an agent class |
|
53 * @param args[1] file name of the agent jar to be created |
|
54 * @throws IOException |
|
55 */ |
|
56 public static void main(String... args) throws IOException { |
|
57 String agentClass = args[0]; |
|
58 String agentJar = args[1]; |
|
59 System.out.println("Building " + agentJar + " with agent class " + agentClass); |
|
60 build(agentClass, agentJar); |
|
61 } |
|
62 |
|
63 /** |
|
64 * Build a java agent jar file with a given agent class. |
|
65 * The agent class will be added as both premain class and agent class. |
|
66 * |
|
67 * @param agentClass fully qualified name of an agent class |
|
68 * @param agentJar file name of the agent jar to be created |
|
69 * the file will be placed in a current work directory |
|
70 * @throws IOException |
|
71 */ |
|
72 public static void build(String agentClass, String agentJar) throws IOException { |
|
73 Manifest mf = new Manifest(); |
|
74 Attributes attrs = mf.getMainAttributes(); |
|
75 attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
|
76 attrs.putValue("Premain-Class", agentClass); |
|
77 attrs.putValue("Agent-Class", agentClass); |
|
78 |
|
79 Path jarFile = Paths.get(".", agentJar); |
|
80 String testClasses = Utils.TEST_CLASSES; |
|
81 String agentPath = agentClass.replace(".", File.separator) + ".class"; |
|
82 Path agentFile = Paths.get(testClasses, agentPath); |
|
83 Path dir = Paths.get(testClasses); |
|
84 JarUtils.createJarFile(jarFile, mf, dir, agentFile); |
|
85 System.out.println("Agent built:" + jarFile.toAbsolutePath()); |
|
86 } |
|
87 } |
|