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.codec.digest.DigestUtils; 023 import org.apache.commons.io.FileUtils; 024 import org.apache.commons.io.IOUtils; 025 import org.apache.commons.lang.CharEncoding; 026 import org.apache.commons.lang.StringUtils; 027 028 import java.io.File; 029 import java.io.IOException; 030 import java.io.InputStream; 031 032 public class Artifact { 033 034 public static final String BASE_GROUP_ID = "org.codehaus.sonar.runtime"; 035 036 private String groupId; 037 private String artifactId; 038 protected String version; 039 protected File jar; 040 private String packaging; 041 042 public Artifact(String groupId, String artifactId, String version, String packaging, File jar) { 043 this.artifactId = artifactId; 044 this.groupId = groupId; 045 this.version = version; 046 this.jar = jar; 047 this.packaging = packaging; 048 } 049 050 public void deployTo(File rootDir) throws IOException { 051 File dir = createDir(rootDir); 052 savePom(dir); 053 saveMetadata(dir); 054 saveJar(dir); 055 } 056 057 protected File createDir(File rootDir) throws IOException { 058 String path = StringUtils.replace(groupId, ".", "/") + "/" + artifactId + "/" + version; 059 File dir = new File(rootDir, path); 060 FileUtils.forceMkdir(dir); 061 return dir; 062 } 063 064 protected String getArtifactName() { 065 return artifactId + "-" + version; 066 } 067 068 public String getGroupId() { 069 return groupId; 070 } 071 072 public String getArtifactId() { 073 return artifactId; 074 } 075 076 public String getVersion() { 077 return version; 078 } 079 080 private void saveJar(File dir) throws IOException { 081 if (jar != null) { 082 copyTo(dir); 083 File newJar = new File(dir, jar.getName()); 084 File target = new File(dir, getArtifactName() + ".jar"); 085 newJar.renameTo(target); 086 saveDigests(target); 087 } 088 } 089 090 protected void copyTo(File dir) throws IOException { 091 FileUtils.copyFileToDirectory(jar, dir); 092 } 093 094 private void savePom(File dir) throws IOException { 095 File pom = new File(dir, getArtifactName() + ".pom"); 096 FileUtils.writeStringToFile(pom, getPom(), CharEncoding.UTF_8); 097 saveDigests(pom); 098 } 099 100 private void saveDigests(File file) throws IOException { 101 String path = file.getAbsolutePath(); 102 byte[] content = FileUtils.readFileToByteArray(file); 103 FileUtils.writeStringToFile(new File(path + ".md5"), DigestUtils.md5Hex(content), CharEncoding.UTF_8); 104 FileUtils.writeStringToFile(new File(path + ".sha1"), DigestUtils.shaHex(content), CharEncoding.UTF_8); 105 } 106 107 public String getPom() throws IOException { 108 return transformFromTemplatePath(getTemplatePath()); 109 } 110 111 protected String getTemplatePath() { 112 return "/org/sonar/server/mavendeployer/pom.template"; 113 } 114 115 @Override 116 public boolean equals(Object o) { 117 if (this == o) { 118 return true; 119 } 120 if (o == null || getClass() != o.getClass()) { 121 return false; 122 } 123 124 Artifact that = (Artifact) o; 125 if (!artifactId.equals(that.artifactId)) { 126 return false; 127 } 128 return groupId.equals(that.groupId); 129 } 130 131 @Override 132 public int hashCode() { 133 int result = groupId.hashCode(); 134 result = 31 * result + artifactId.hashCode(); 135 return result; 136 } 137 138 @Override 139 public String toString() { 140 return new StringBuilder().append(groupId).append(":").append(artifactId).toString(); 141 } 142 143 public String getMetadata() throws IOException { 144 return transformFromTemplatePath("/org/sonar/server/mavendeployer/maven-metadata.template"); 145 } 146 147 protected void saveMetadata(File dir) throws IOException { 148 File metadataFile = new File(dir.getParentFile(), "maven-metadata.xml"); 149 FileUtils.writeStringToFile(metadataFile, getMetadata(), CharEncoding.UTF_8); 150 } 151 152 protected final String transformFromTemplatePath(String templatePath) throws IOException { 153 InputStream template = this.getClass().getResourceAsStream(templatePath); 154 try { 155 String content = IOUtils.toString(template); 156 content = StringUtils.replace(content, "$groupId", groupId); 157 content = StringUtils.replace(content, "$artifactId", artifactId); 158 content = StringUtils.replace(content, "$version", version); 159 content = StringUtils.replace(content, "$timestamp", version); 160 content = StringUtils.replace(content, "$packaging", packaging); 161 return content; 162 163 } finally { 164 IOUtils.closeQuietly(template); 165 } 166 } 167 168 public String getXmlDefinition() throws IOException { 169 return transformFromTemplatePath("/org/sonar/server/mavendeployer/dependency.template"); 170 } 171 }