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     */
020    package org.sonar.server.platform;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.apache.commons.lang.StringUtils;
024    import org.sonar.api.CoreProperties;
025    import org.sonar.api.config.Settings;
026    import org.sonar.api.platform.Server;
027    
028    import java.io.IOException;
029    import java.io.InputStream;
030    import java.text.SimpleDateFormat;
031    import java.util.Date;
032    import java.util.Properties;
033    
034    public 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      public String getPermanentServerId() {
064        return settings.getString(CoreProperties.PERMANENT_SERVER_ID);
065      }
066    
067      public String getId() {
068        return id;
069      }
070    
071      public String getVersion() {
072        return version;
073      }
074    
075      public Date getStartedAt() {
076        return startedAt;
077      }
078    
079      String loadVersionFromManifest(String pomFilename) throws IOException {
080        InputStream pomFileStream = getClass().getResourceAsStream(pomFilename);
081        try {
082          return readVersion(pomFileStream);
083    
084        } finally {
085          IOUtils.closeQuietly(pomFileStream);
086        }
087      }
088    
089      protected static String readVersion(InputStream pomFileStream) throws IOException {
090        String result = null;
091        if (pomFileStream != null) {
092          Properties pomProp = new Properties();
093          pomProp.load(pomFileStream);
094          result = pomProp.getProperty("version");
095        }
096        return StringUtils.defaultIfEmpty(result, "");
097      }
098    
099      public String getURL() {
100        return null;
101      }
102    
103      @Override
104      public boolean equals(Object o) {
105        if (this == o) {
106          return true;
107        }
108        if (o == null || getClass() != o.getClass()) {
109          return false;
110        }
111    
112        ServerImpl other = (ServerImpl) o;
113        return id.equals(other.id);
114      }
115    
116      @Override
117      public int hashCode() {
118        return id.hashCode();
119      }
120    }