001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.bootstrap;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import org.apache.commons.lang.StringUtils;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.BatchComponent;
026    import org.sonar.api.CoreProperties;
027    import org.sonar.api.config.Settings;
028    import org.sonar.api.platform.Server;
029    
030    import java.io.File;
031    import java.text.ParseException;
032    import java.text.SimpleDateFormat;
033    import java.util.Date;
034    
035    /**
036     * @deprecated in 3.4. Replaced by {@link org.sonar.batch.bootstrap.ServerClient}
037     */
038    @Deprecated
039    public class ServerMetadata extends Server implements BatchComponent {
040    
041      private Settings settings;
042      private ServerClient client;
043    
044      public ServerMetadata(Settings settings, ServerClient client) {
045        this.settings = settings;
046        this.client = client;
047      }
048    
049      @Override
050      public String getId() {
051        return settings.getString(CoreProperties.SERVER_ID);
052      }
053    
054      @Override
055      public String getVersion() {
056        return settings.getString(CoreProperties.SERVER_VERSION);
057      }
058    
059      @Override
060      public Date getStartedAt() {
061        String dateString = settings.getString(CoreProperties.SERVER_STARTTIME);
062        if (dateString != null) {
063          try {
064            return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
065    
066          } catch (ParseException e) {
067            LoggerFactory.getLogger(getClass()).error("The property " + CoreProperties.SERVER_STARTTIME + " is badly formatted.", e);
068          }
069        }
070        return null;
071      }
072    
073      @Override
074      public File getRootDir() {
075        return null;
076      }
077    
078      @Override
079      public File getDeployDir() {
080        return null;
081      }
082    
083      @Override
084      public String getContextPath() {
085        return null;
086      }
087    
088      @Override
089      public String getURL() {
090        return client.getURL();
091      }
092    
093      @Override
094      public String getPermanentServerId() {
095        return settings.getString(CoreProperties.PERMANENT_SERVER_ID);
096      }
097    
098      public String getServerId() {
099        String remoteServerInfo = client.request("/api/server");
100        // don't use JSON utilities to extract ID from such a small string
101        return extractServerId(remoteServerInfo);
102      }
103    
104      @VisibleForTesting
105      String extractServerId(String remoteServerInfo) {
106        String partialId = StringUtils.substringAfter(remoteServerInfo, "\"id\":\"");
107        return StringUtils.substringBefore(partialId, "\"");
108      }
109    }