AgentJar.java
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.agent;
13
14import java.io.Closeable;
15import java.io.File;
16import java.io.FileOutputStream;
17import java.io.IOException;
18import java.io.InputStream;
19import java.io.OutputStream;
20import java.net.URL;
21
22/**
23 * API to access the agent JAR file as a resource.
24 *
25 * @author Marc R. Hoffmann
26 * @version 0.4.1.20101007204400
27 */
28public class AgentJar {
29
30 /**
31 * Name of the agent JAR file resource within this bundle.
32 */
33 private static final String RESOURCE = "/jacocoagent.jar";
34
35 private AgentJar() {
36 }
37
38 /**
39 * Returns a URL pointing to the JAR file.
40 *
41 * @return URL of the JAR file
42 */
43 public static URL getResource() {
44 final URL url = AgentJar.class.getResource(RESOURCE);
45 if (url == null) {
46 throw new AssertionError(ERRORMSG);
47 }
48 return url;
49 }
50
51 /**
52 * Returns the content of the JAR file as a stream.
53 *
54 * @return content of the JAR file
55 */
56 public static InputStream getResourceAsStream() {
57 final InputStream stream = AgentJar.class.getResourceAsStream(RESOURCE);
58 if (stream == null) {
59 throw new AssertionError(ERRORMSG);
60 }
61 return stream;
62 }
63
64 /**
65 * Extract the JaCoCo agent jar from the classpath and put it into a
66 * temporary location. This file should be deleted on exit, but may not if
67 * the VM is terminated
68 *
69 * @return Location of the Agent Jar file in the local file system. The file
70 * should exist and be readable.
71 * @throws IOException
72 * Unable to unpack agent jar
73 */
74 public static File extractToTempLocation() throws IOException {
75 final File agentJar = File.createTempFile("jacocoagent", ".jar");
76 agentJar.deleteOnExit();
77
78 extractTo(agentJar);
79
80 return agentJar;
81 }
82
83 /**
84 * Extract the JaCoCo agent jar from the classpath and put it into the
85 * specified location.
86 *
87 * @param destination
88 * Location to write JaCoCo Agent Jar to. Must be writeable
89 * @throws IOException
90 * Unable to unpack agent jar
91 */
92 public static void extractTo(File destination) throws IOException {
93 InputStream inputJarStream = getResourceAsStream();
94 OutputStream outputJarStream = null;
95
96 try {
97
98 outputJarStream = new FileOutputStream(destination);
99
100 final byte[] buffer = new byte[8192];
101
102 int bytesRead;
103 while ((bytesRead = inputJarStream.read(buffer)) != -1) {
104 outputJarStream.write(buffer, 0, bytesRead);
105 }
106 } finally {
107 safeClose(inputJarStream);
108 safeClose(outputJarStream);
109 }
110 }
111
112 /**
113 * Close a stream ignoring any error
114 *
115 * @param closeable
116 * stream to be closed
117 */
118 private static void safeClose(Closeable closeable) {
119 try {
120 if (closeable != null) {
121 closeable.close();
122 }
123 } catch (IOException e) {
124 }
125 }
126
127 private static final String ERRORMSG = String.format(
128 "The resource %s has not been found. Please see "
129 + "/org.jacoco.agent/README.TXT for details.", RESOURCE);
130
131}