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.batch.bootstrap;
021    
022    import com.google.common.collect.Lists;
023    import org.apache.commons.io.FileUtils;
024    import org.apache.commons.lang.CharUtils;
025    import org.apache.commons.lang.StringUtils;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    import org.sonar.api.BatchComponent;
029    import org.sonar.api.utils.HttpDownloader;
030    import org.sonar.api.utils.SonarException;
031    import org.sonar.batch.ServerMetadata;
032    import org.sonar.core.plugins.RemotePlugin;
033    
034    import java.io.File;
035    import java.net.URI;
036    import java.net.URISyntaxException;
037    import java.util.List;
038    
039    public class ArtifactDownloader implements BatchComponent {
040    
041      private static final Logger LOG = LoggerFactory.getLogger(ArtifactDownloader.class);
042    
043      private HttpDownloader httpDownloader;
044      private TempDirectories workingDirectories;
045      private String baseUrl;
046    
047      public ArtifactDownloader(HttpDownloader httpDownloader, TempDirectories workingDirectories, ServerMetadata server) {
048        this.httpDownloader = httpDownloader;
049        this.workingDirectories = workingDirectories;
050        this.baseUrl = server.getURL();
051      }
052    
053      public File downloadJdbcDriver() {
054        String url = baseUrl + "/deploy/jdbc-driver.jar";
055        try {
056          File jdbcDriver = new File(workingDirectories.getRoot(), "jdbc-driver.jar");
057          LOG.debug("Downloading JDBC driver to " + jdbcDriver);
058          httpDownloader.download(new URI(url), jdbcDriver);
059          return jdbcDriver;
060    
061        } catch (URISyntaxException e) {
062          throw new SonarException("Fail to download the JDBC driver from : " + url, e);
063        }
064      }
065    
066      public List<File> downloadPlugin(RemotePlugin remote) {
067        try {
068          File targetDir = workingDirectories.getDir("plugins/" + remote.getKey());
069          FileUtils.forceMkdir(targetDir);
070          LOG.debug("Downloading plugin " + remote.getKey() + " into " + targetDir);
071    
072          List<File> files = Lists.newArrayList();
073          for (String filename : remote.getFilenames()) {
074            String url = baseUrl + "/deploy/plugins/" + remote.getKey() + "/" + filename;
075            File toFile = new File(targetDir, filename);
076            httpDownloader.download(new URI(url), toFile);
077            files.add(toFile);
078          }
079    
080    
081          return files;
082    
083        } catch (Exception e) {
084          throw new SonarException("Fail to download plugin: " + remote.getKey(), e);
085        }
086      }
087    
088      public List<RemotePlugin> downloadPluginIndex() {
089        String url = baseUrl + "/deploy/plugins/index.txt";
090        try {
091          LOG.debug("Downloading index of plugins");
092          String indexContent = httpDownloader.downloadPlainText(new URI(url), "UTF-8");
093          String[] rows = StringUtils.split(indexContent, CharUtils.LF);
094          List<RemotePlugin> remoteLocations = Lists.newArrayList();
095          for (String row : rows) {
096            remoteLocations.add(RemotePlugin.unmarshal(row));
097          }
098          return remoteLocations;
099    
100        } catch (Exception e) {
101          throw new SonarException("Fail to download plugins index: " + url, e);
102        }
103      }
104    
105    }