001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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     */
020    package org.sonar.server.mavendeployer;
021    
022    import org.apache.commons.io.FileUtils;
023    import org.apache.commons.lang.StringUtils;
024    import org.sonar.api.utils.ZipUtils;
025    
026    import java.io.File;
027    import java.io.IOException;
028    
029    public final class Mojo extends Artifact {
030    
031      private Mojo(String artifactId, String version, File jar) {
032        super(BASE_GROUP_ID, artifactId, version, "maven-plugin", jar);
033      }
034    
035      public static Mojo createMaven2Plugin(String version, File jar) {
036        return new Mojo("sonar-core-maven-plugin", version, jar);
037      }
038    
039      @Override
040      protected String getTemplatePath() {
041        return "/org/sonar/server/mavendeployer/" + getArtifactId() + ".template";
042      }
043    
044      @Override
045      protected void copyTo(File toDir) throws IOException {
046        File tmpDir = prepareTmpDir();
047        try {
048          copyTo(toDir, tmpDir);
049    
050        } finally {
051          destroyTmpDir(tmpDir);
052        }
053      }
054    
055      protected void copyTo(File toDir, File temporaryDir) throws IOException {
056        ZipUtils.unzip(jar, temporaryDir);
057    
058        File metadataFile = new File(temporaryDir, "META-INF/maven/plugin.xml");
059        String metadata = FileUtils.readFileToString(metadataFile);
060        metadata = updateVersion(metadata, version);
061    
062        FileUtils.writeStringToFile(metadataFile, metadata);
063        ZipUtils.zipDir(temporaryDir, new File(toDir, jar.getName()));
064      }
065    
066      protected String updateVersion(String metadata, String version) {
067        String before = StringUtils.substringBefore(metadata, "<version>");
068        String after = StringUtils.substringAfter(metadata, "</version>");
069        return before + "<version>" + version + "</version>" + after;
070      }
071    
072      private File prepareTmpDir() throws IOException {
073        File tmpDir = new File(System.getProperty("java.io.tmpdir"), "sonar-" + version);
074        if (tmpDir.exists()) {
075          FileUtils.cleanDirectory(tmpDir);
076        } else {
077          FileUtils.forceMkdir(tmpDir);
078        }
079        return tmpDir;
080      }
081    
082      private void destroyTmpDir(File tmpDir) {
083        if (tmpDir != null) {
084          FileUtils.deleteQuietly(tmpDir);
085        }
086      }
087    }