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

com.marvelution.jira.plugins.sonar.services.servers.SonarServerManagerService Maven / Gradle / Ivy

/*
 * 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.jira.plugins.sonar.services.servers;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.java.ao.DBParam;
import net.java.ao.Query;

import com.atlassian.activeobjects.external.ActiveObjects;
import com.atlassian.activeobjects.tx.Transactional;
import com.atlassian.jira.bc.whitelist.WhitelistManager;
import com.google.common.collect.Lists;

/**
 * {@link SonarServer} manager implementation
 * 
 * @author Mark Rekveld
 *
 * @since 2.4.0
 */
@Transactional
public class SonarServerManagerService implements SonarServerManager {

	private final ActiveObjects objects;
	private final WhitelistManager whitelistManager;

	/**
	 * Constructor
	 * 
	 * @param objects the {@link ActiveObjects} implementation
	 * @param whitelistManager the {@link WhitelistManager} implementation
	 */
	public SonarServerManagerService(ActiveObjects objects, WhitelistManager whitelistManager) {
		this.objects = checkNotNull(objects, "activeObjects");
		this.whitelistManager = checkNotNull(whitelistManager, "whitelistManager");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isConfigured() {
		return hasServers();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean hasServers() {
		return objects.count(SonarServer.class) > 0;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean hasServer(int serverId) {
		return getServer(serverId) != null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean hasServer(String name) {
		return getServer(name) != null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Collection getServers() {
		return Lists.newArrayList(objects.find(SonarServer.class));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer getServer(int serverId) {
		return objects.get(SonarServer.class, serverId);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer getServer(String name) {
		SonarServer[] matches = objects.find(SonarServer.class, Query.select().where("NAME = ?", name).limit(1));
		if (matches != null && matches.length == 1) {
			return matches[0];
		} else {
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer addServer(String name, String description, String host) {
		return addServer(name, description, host, null, null, true);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer addServer(String name, String description, String host, String username, String password) {
		return addServer(name, description, host, username, password, true);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer addServer(String name, String description, String host, String username, String password,
					boolean includeInStreams) {
		SonarServer server = objects.create(SonarServer.class, new DBParam("NAME", name), new DBParam("HOST", host));
		server.setDescription(description);
		server.setUsername(username);
		server.setPassword(password);
		server.setIncludeInStreams(includeInStreams);
		server.save();
		List rules = whitelistManager.getRules();
		if (!rules.contains(SonarServerUtils.getHostWhitelistUrl(server))) {
			List newRules = new ArrayList(rules);
			newRules.add(SonarServerUtils.getHostWhitelistUrl(server));
			whitelistManager.updateRules(newRules, whitelistManager.isDisabled());
		}
		return server;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer addServer(SonarServer server) {
		checkNotNull(server, "server argument may NOT be null");
		return addServer(server.getName(), server.getDescription(), server.getHost(), server.getUsername(),
			server.getPassword(), true);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer updateServer(int serverId, String name, String description, String host, String username,
					String password, boolean includeInStreams) {
		SonarServer server = getServer(serverId);
		checkNotNull(server, "server");
		server.setName(name);
		server.setDescription(description);
		server.setHost(host);
		server.setUsername(username);
		server.setPassword(password);
		server.setIncludeInStreams(includeInStreams);
		server.save();
		return server;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SonarServer updateServer(SonarServer server) {
		checkNotNull(server, "server argument may NOT be null");
		// This one is easy, just save and return
		server.save();
		return server;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void removeServer(int serverId) {
		SonarServer server = getServer(serverId);
		checkNotNull(server, "No server configured with Id: " + serverId);
		removeServer(server);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void removeServer(SonarServer server) {
		checkNotNull(server, "sonarServer");
		List rules = whitelistManager.getRules();
		if (rules.contains(SonarServerUtils.getHostWhitelistUrl(server))) {
			List newRules = new ArrayList(rules);
			newRules.remove(SonarServerUtils.getHostWhitelistUrl(server));
			whitelistManager.updateRules(newRules, whitelistManager.isDisabled());
		}
		objects.delete(server);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy