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