001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020package org.sonar.plugins.jacoco; 021 022import org.apache.commons.io.FileUtils; 023import org.jacoco.agent.AgentJar; 024import org.jacoco.core.JaCoCo; 025import org.sonar.api.BatchExtension; 026import org.sonar.api.utils.SonarException; 027 028import java.io.File; 029import java.io.IOException; 030 031/** 032 * @author Evgeny Mandrikov 033 */ 034public class JaCoCoAgentDownloader implements BatchExtension { 035 036 /** 037 * Dirty hack, but it allows to extract agent only once during Sonar analyzes for multi-module project. 038 */ 039 private static File agentJarFile; 040 041 public JaCoCoAgentDownloader() { 042 } 043 044 public synchronized File getAgentJarFile() { 045 if (agentJarFile == null) { 046 agentJarFile = extractAgent(); 047 } 048 return agentJarFile; 049 } 050 051 private File extractAgent() { 052 try { 053 File agent = File.createTempFile("jacocoagent", ".jar"); 054 AgentJar.extractTo(agent); 055 FileUtils.forceDeleteOnExit(agent); // TODO evil method 056 JaCoCoUtils.LOG.info("JaCoCo agent (version " + JaCoCo.VERSION + ") extracted: {}", agent); 057 return agent; 058 } catch (IOException e) { 059 throw new SonarException(e); 060 } 061 } 062}