All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.clover.CloverMavenPlugin Maven / Gradle / Ivy

There is a newer version: 1.5M3
Show newest version
/*
 * Sonar, entreprise quality control tool.
 * Copyright (C) 2007-2008 Hortis-GRC SA
 * mailto:be_agile HAT hortis DOT ch
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.plugins.clover;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.logging.Log;
import org.sonar.plugins.api.maven.model.MavenPlugin;
import org.sonar.plugins.api.maven.model.MavenPom;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

public class CloverMavenPlugin {

  private int version;
  private MavenPlugin plugin;
  private Configuration configuration;
  private String licenseKey;
  private Log log;

  public CloverMavenPlugin(MavenPlugin plugin, int version, String licenseKey, Log log, Configuration configuration) {
    this.version = version;
    this.plugin = plugin;
    this.configuration = configuration;
    this.licenseKey = licenseKey;
    this.log = log;
  }

  public boolean shouldBeExecuted() {
    final boolean execute;
    if (plugin != null) {
      execute = (isLocalLicenseLocationExisting() || isLocalLicenseContentExisting() || isGlobalConfigurationExisting());
    } else {
      execute = (isDefaultVersionEqualsToCurrentVersion() && isGlobalConfigurationExisting());
    }
    if (!execute) {
      log.debug("license not found.");
    }
    return execute;
  }

  private boolean isDefaultVersionEqualsToCurrentVersion() {
    int defaultversion = configuration.getShort(CloverPlugin.PROP_DEFAULT_VERSION, CloverPlugin.DEFAULT_VERSION_VALUE);
    return defaultversion == version;
  }

  private boolean isLocalLicenseLocationExisting() {
    String localeLicenseLocation = getLicenseFromLicenseLocation();
    if (localeLicenseLocation != null) {
      File license = new File(localeLicenseLocation);
      if (license.exists()) {
        return true;
      } else {
        log.warn("clover license location is invalid : " + localeLicenseLocation);
        return false;
      }
    }
    return false;
  }

  public boolean isLocalLicenseContentExisting() {
    return (plugin.getConfigParameter("license") != null);
  }

  private boolean isGlobalConfigurationExisting() {
    return !"".equals(StringUtils.defaultString(getGlobalConfiguration()));
  }

  private String getGlobalConfiguration() {
    Object value = configuration.getProperty(licenseKey);
    if (value != null) {
      if (value instanceof String) {
        return (String)value;

      } else if (value instanceof Collection) {
        return StringUtils.join(((Collection)value), "");
      }
    }
    return null;
  }

  public String getOrCreateLicensePath(MavenPom pom) {
    try {
      if (plugin != null && isLocalLicenseLocationExisting()) {
        return getLicenseFromLicenseLocation();

      } else if (isGlobalConfigurationExisting()) {
        File license = writeLicenseFromGlobalLicense(pom);
        return license.getCanonicalPath();

      }
      return null;

    } catch (IOException e) {
      throw new RuntimeException("can not open the clover license", e);
    }
  }

  private String getLicenseFromLicenseLocation() {
    return plugin.getConfigParameter("licenseLocation");
  }

  private File writeLicenseFromGlobalLicense(MavenPom pom) {
    try {
      String globalLicense = getGlobalConfiguration();
      File license = new File(pom.getSonarWorkingDirectory(), "clover.license");
      FileUtils.writeStringToFile(license, globalLicense);
      return license;
    } catch (IOException e) {
      throw new RuntimeException("can not write the clover license", e);
    }
  }

  public MavenPlugin getPlugin() {
    return plugin;
  }

  protected void setPlugin(MavenPlugin plugin) {
    this.plugin = plugin;
  }

  protected String getLicenseKey() {
    return licenseKey;
  }

  protected int getVersion() {
    return version;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy