001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 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.plugins.cpd.index;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import org.apache.commons.lang.StringUtils;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    import org.sonar.api.BatchExtension;
027    import org.sonar.api.CoreProperties;
028    import org.sonar.api.config.Settings;
029    import org.sonar.api.resources.Project;
030    import org.sonar.batch.index.ResourcePersister;
031    import org.sonar.core.duplication.DuplicationDao;
032    
033    public class IndexFactory implements BatchExtension {
034    
035      private static final Logger LOG = LoggerFactory.getLogger(IndexFactory.class);
036    
037      private final Settings settings;
038      private final ResourcePersister resourcePersister;
039      private final DuplicationDao dao;
040    
041      /**
042       * For dry run, where is no access to database.
043       */
044      public IndexFactory(Settings settings) {
045        this.settings = settings;
046        this.resourcePersister = null;
047        this.dao = null;
048      }
049    
050      public IndexFactory(Settings settings, ResourcePersister resourcePersister, DuplicationDao dao) {
051        this.settings = settings;
052        this.resourcePersister = resourcePersister;
053        this.dao = dao;
054      }
055    
056      public SonarDuplicationsIndex create(Project project) {
057        if (isCrossProject(project)) {
058          LOG.info("Cross-project analysis enabled");
059          return new SonarDuplicationsIndex(new DbDuplicationsIndex(resourcePersister, project, dao));
060        } else {
061          LOG.info("Cross-project analysis disabled");
062          return new SonarDuplicationsIndex();
063        }
064      }
065    
066      /**
067       * @return true, if was enabled by user and database is available
068       */
069      @VisibleForTesting
070      boolean isCrossProject(Project project) {
071        return settings.getBoolean(CoreProperties.CPD_CROSS_RPOJECT)
072          && resourcePersister != null && dao != null
073          && StringUtils.isBlank(project.getBranch());
074      }
075    
076    }