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.server.platform; 021 022import org.apache.commons.io.IOUtils; 023import org.apache.commons.lang.StringUtils; 024import org.sonar.api.CoreProperties; 025import org.sonar.api.config.Settings; 026import org.sonar.api.platform.Server; 027 028import java.io.IOException; 029import java.io.InputStream; 030import java.text.SimpleDateFormat; 031import java.util.Date; 032import java.util.Properties; 033 034public final class ServerImpl extends Server { 035 036 private String id; 037 private String version; 038 private final Date startedAt; 039 private Settings settings; 040 041 public ServerImpl(Settings settings) { 042 this(settings, new Date()); 043 } 044 045 ServerImpl(Settings settings, Date startedAt) { 046 this.settings = settings; 047 this.startedAt = startedAt; 048 } 049 050 public void start() { 051 try { 052 id = new SimpleDateFormat("yyyyMMddHHmmss").format(startedAt); 053 version = loadVersionFromManifest("/META-INF/maven/org.codehaus.sonar/sonar-plugin-api/pom.properties"); 054 if (StringUtils.isBlank(version)) { 055 throw new ServerStartException("Unknown Sonar version"); 056 } 057 058 } catch (IOException e) { 059 throw new ServerStartException("Can not load metadata", e); 060 } 061 } 062 063 @Override 064 public String getPermanentServerId() { 065 return settings.getString(CoreProperties.PERMANENT_SERVER_ID); 066 } 067 068 @Override 069 public String getId() { 070 return id; 071 } 072 073 @Override 074 public String getVersion() { 075 return version; 076 } 077 078 @Override 079 public Date getStartedAt() { 080 return startedAt; 081 } 082 083 String loadVersionFromManifest(String pomFilename) throws IOException { 084 InputStream pomFileStream = getClass().getResourceAsStream(pomFilename); 085 try { 086 return readVersion(pomFileStream); 087 088 } finally { 089 IOUtils.closeQuietly(pomFileStream); 090 } 091 } 092 093 protected static String readVersion(InputStream pomFileStream) throws IOException { 094 String result = null; 095 if (pomFileStream != null) { 096 Properties pomProp = new Properties(); 097 pomProp.load(pomFileStream); 098 result = pomProp.getProperty("version"); 099 } 100 return StringUtils.defaultIfEmpty(result, ""); 101 } 102 103 @Override 104 public String getURL() { 105 return null; 106 } 107 108 @Override 109 public boolean equals(Object o) { 110 if (this == o) { 111 return true; 112 } 113 if (o == null || getClass() != o.getClass()) { 114 return false; 115 } 116 117 ServerImpl other = (ServerImpl) o; 118 return id.equals(other.id); 119 } 120 121 @Override 122 public int hashCode() { 123 return id.hashCode(); 124 } 125}