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

com.marvelution.bamboo.plugins.sonar.upgrade.SonarFixSonarServersUpgradeTask Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.marvelution.bamboo.plugins.sonar.upgrade;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.atlassian.bamboo.bandana.PlanAwareBandanaContext;
import com.atlassian.bamboo.build.BuildManager;
import com.atlassian.bandana.BandanaManager;
import com.atlassian.sal.api.message.Message;
import com.atlassian.sal.api.upgrade.PluginUpgradeTask;
import com.marvelution.bamboo.plugins.sonar.BambooSonarServer;
import com.marvelution.bamboo.plugins.sonar.BandanaUtils;
import com.marvelution.bamboo.plugins.sonar.SonarServer;

/**
 * Upgrade task to fix the issue with loading the SoanrServersList using Bandana
 * 
 * @author Mark Rekveld
 */
public class SonarFixSonarServersUpgradeTask implements PluginUpgradeTask {

	private final Logger logger = Logger.getLogger(SonarFixSonarServersUpgradeTask.class);

	private BuildManager buildManager;

	private BandanaManager bandanaManager;

	/**
	 * Constructor
	 * 
	 * @param buildManager the {@link BuildManager} implementation
	 * @param bandanaManager the {@link BandanaManager} implementation
	 */
	public SonarFixSonarServersUpgradeTask(BuildManager buildManager, BandanaManager bandanaManager) {
		this.buildManager = buildManager;
		this.bandanaManager = bandanaManager;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public Collection doUpgrade() throws Exception {
		boolean requireCleanup = false;
		final Object bandanaValue = bandanaManager.getValue(PlanAwareBandanaContext.GLOBAL_CONTEXT,
			BandanaUtils.SONAR_SERVER_LIST_KEY);
		final Map sonarServers = new HashMap();
		if (bandanaValue != null && bandanaValue instanceof Map) {
			final Map objects = (Map) bandanaValue;
			for (final Entry entry : objects.entrySet()) {
				if (entry.getValue() instanceof BambooSonarServer) {
					sonarServers.put(entry.getKey(), (BambooSonarServer) entry.getValue());
				} else if (entry.getValue() instanceof SonarServer) {
					logger.info("Detected old Sonar Server definition. Upgrading it...");
					final SonarServer oldServer = (SonarServer) entry.getValue();
					final BambooSonarServer server = new BambooSonarServer();
					server.setServerId(oldServer.getServerId());
					server.setName(oldServer.getName());
					server.setDescription(oldServer.getDescription());
					server.setHost(oldServer.getHost());
					server.setDatabaseUrl(oldServer.getDatabaseUrl());
					server.setDatabaseDriver(oldServer.getDatabaseDriver());
					server.setDatabaseUsername(oldServer.getDatabaseUsername());
					server.setDatabasePassword(oldServer.getDatabasePassword());
					server.setAdditionalArguments(oldServer.getAdditionalArguments());
					server.setDisabled(oldServer.isDisabled());
					sonarServers.put(entry.getKey(), server);
					requireCleanup = true;
				} else {
					requireCleanup = true;
					logger.warn("Detected unknown object in the Soanr Server List. Removing it...");
				}
			}
		}
		bandanaManager.setValue(PlanAwareBandanaContext.GLOBAL_CONTEXT, BandanaUtils.SONAR_SERVER_LIST_KEY,
			sonarServers);
		if (requireCleanup) {
			// Rerun the clean up task
			final SonarCleanupUpgradeTask cleanupTask = new SonarCleanupUpgradeTask(buildManager, bandanaManager);
			cleanupTask.doUpgrade();
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public int getBuildNumber() {
		return 2;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getPluginKey() {
		return "com.marvelution.bamboo.plugins.sonar.gadgets";
	}

	/**
	 * {@inheritDoc}
	 */
	public String getShortDescription() {
		return "Upgrade task to fix the SonarServersList loading issue";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy