AgentJar.java

    1/*******************************************************************************
    2 * Copyright (c) 2010 Mountainminds GmbH & Co. KG and others
    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 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.agent;
   14
   15import java.io.InputStream;
   16import java.net.URL;
   17
   18/**
   19 * API to access the agent JAR file as a resource.
   20 * 
   21 * @author Marc R. Hoffmann
   22 * @version $Revision: $
   23 */
   24public class AgentJar {
   25
   26    /**
   27     * Name of the agent JAR file resource within this bunde.
   28     */
   29    public static final String RESOURCE = "/jacocoagent.jar";
   30
   31    private AgentJar() {
   32    }
   33
   34    /**
   35     * Returns a URL pointing to the JAR file.
   36     * 
   37     * @return URL of the JAR file
   38     */
   39    public static URL getResource() {
   40        final URL url = AgentJar.class.getResource(RESOURCE);
   41        if (url == null) {
   42            throw new RuntimeException("Resource not found: " + RESOURCE);
   43        }
   44        return url;
   45    }
   46
   47    /**
   48     * Returns the content of the JAR file as a stream.
   49     * 
   50     * @return content of the JAR file
   51     */
   52    public static InputStream getResourceAsStream() {
   53        final InputStream stream = AgentJar.class.getResourceAsStream(RESOURCE);
   54        if (stream == null) {
   55            throw new RuntimeException("Resource not found: " + RESOURCE);
   56        }
   57        return stream;
   58    }
   59
   60}