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 org.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
024    import org.sonar.api.BatchComponent;
025    import org.sonar.api.CoreProperties;
026    
027    import java.text.MessageFormat;
028    
029    /**
030     * @since 4.0
031     */
032    public class AnalysisMode implements BatchComponent {
033    
034      private static final Logger LOG = LoggerFactory.getLogger(AnalysisMode.class);
035    
036      private static final int DEFAULT_PREVIEW_READ_TIMEOUT_SEC = 60;
037    
038      private boolean preview;
039      private boolean incremental;
040      private int previewReadTimeoutSec;
041      private boolean sensorMode;
042    
043      public AnalysisMode(BootstrapProperties bootstrapProps) {
044        init(bootstrapProps);
045      }
046    
047      public boolean isPreview() {
048        return preview || incremental;
049      }
050    
051      public boolean isIncremental() {
052        return incremental;
053      }
054    
055      public boolean isSensorMode() {
056        return sensorMode;
057      }
058    
059      private void init(BootstrapProperties bootstrapProps) {
060        if (bootstrapProps.properties().containsKey(CoreProperties.DRY_RUN)) {
061          LOG.warn(MessageFormat.format("Property {0} is deprecated. Please use {1} instead.", CoreProperties.DRY_RUN, CoreProperties.ANALYSIS_MODE));
062          preview = "true".equals(bootstrapProps.property(CoreProperties.DRY_RUN));
063          incremental = false;
064          sensorMode = false;
065        } else {
066          String mode = bootstrapProps.property(CoreProperties.ANALYSIS_MODE);
067          preview = CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode);
068          incremental = CoreProperties.ANALYSIS_MODE_INCREMENTAL.equals(mode);
069          sensorMode = CoreProperties.ANALYSIS_MODE_SENSOR.equals(mode);
070        }
071        if (incremental) {
072          LOG.info("Incremental mode");
073        } else if (preview) {
074          LOG.info("Preview mode");
075        } else if (sensorMode) {
076          LOG.info("Sensor mode");
077        }
078        // To stay compatible with plugins that use the old property to check mode
079        if (incremental || preview) {
080          bootstrapProps.properties().put(CoreProperties.DRY_RUN, "true");
081          previewReadTimeoutSec = loadPreviewReadTimeout(bootstrapProps);
082        }
083      }
084    
085      // SONAR-4488 Allow to increase preview read timeout
086      private int loadPreviewReadTimeout(BootstrapProperties bootstrapProps) {
087        int readTimeoutSec;
088        if (bootstrapProps.property(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC) != null) {
089          LOG.warn("Property {} is deprecated. Please use {} instead.", CoreProperties.DRY_RUN_READ_TIMEOUT_SEC, CoreProperties.PREVIEW_READ_TIMEOUT_SEC);
090          readTimeoutSec = Integer.parseInt(bootstrapProps.property(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC));
091        } else if (bootstrapProps.property(CoreProperties.PREVIEW_READ_TIMEOUT_SEC) != null) {
092          readTimeoutSec = Integer.parseInt(bootstrapProps.property(CoreProperties.PREVIEW_READ_TIMEOUT_SEC));
093        } else {
094          readTimeoutSec = DEFAULT_PREVIEW_READ_TIMEOUT_SEC;
095        }
096        return readTimeoutSec;
097      }
098    
099      /**
100       * Read timeout used by HTTP request done in preview mode (SONAR-4488, SONAR-5028)
101       */
102      public int getPreviewReadTimeoutSec() {
103        return previewReadTimeoutSec;
104      }
105    
106    }