3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
23 import java.io.IOException;
24 import java.util.Optional;
25 import org.sonar.api.utils.MessageException;
26 import org.sonar.api.utils.TempFolder;
27 import org.sonar.api.utils.ZipUtils;
28 import org.sonar.api.utils.log.Logger;
29 import org.sonar.api.utils.log.Loggers;
30 import org.sonar.ce.queue.CeTask;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.ce.CeTaskDataDao;
34 import org.sonar.server.computation.task.projectanalysis.batch.MutableBatchReportDirectoryHolder;
35 import org.sonar.server.computation.task.step.ComputationStep;
38 * Extracts the content zip file of the {@link CeTask} to a temp directory and adds a {@link File}
39 * representing that temp directory to the {@link MutableBatchReportDirectoryHolder}.
41 public class ExtractReportStep implements ComputationStep {
42 private static final Logger LOG = Loggers.get(ExtractReportStep.class);
44 private final DbClient dbClient;
45 private final CeTask task;
46 private final TempFolder tempFolder;
47 private final MutableBatchReportDirectoryHolder reportDirectoryHolder;
49 public ExtractReportStep(DbClient dbClient, CeTask task, TempFolder tempFolder,
50 MutableBatchReportDirectoryHolder reportDirectoryHolder) {
51 this.dbClient = dbClient;
53 this.tempFolder = tempFolder;
54 this.reportDirectoryHolder = reportDirectoryHolder;
58 public void execute() {
59 try (DbSession dbSession = dbClient.openSession(false)) {
60 Optional<CeTaskDataDao.DataStream> opt = dbClient.ceTaskDataDao().selectData(dbSession, task.getUuid());
61 if (opt.isPresent()) {
62 File unzippedDir = tempFolder.newDir();
63 try (CeTaskDataDao.DataStream reportStream = opt.get()) {
64 ZipUtils.unzip(reportStream.getInputStream(), unzippedDir);
65 } catch (IOException e) {
66 throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e);
68 reportDirectoryHolder.setDirectory(unzippedDir);
69 LOG.info("Analysis report extracted");
71 throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database");
77 public String getDescription() {
78 return "Extract report";