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

org.sonar.server.organization.DefaultOrganizationProviderImpl Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.organization;

import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.CheckForNull;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.property.InternalProperties;

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

public class DefaultOrganizationProviderImpl implements DefaultOrganizationProvider, DefaultOrganizationCache {
  private static final ThreadLocal CACHE = new ThreadLocal<>();

  private final DbClient dbClient;

  public DefaultOrganizationProviderImpl(DbClient dbClient) {
    this.dbClient = dbClient;
  }

  @Override
  public DefaultOrganization get() {
    Cache cache = CACHE.get();
    if (cache != null) {
      return cache.get(() -> getDefaultOrganization(dbClient));
    }

    return getDefaultOrganization(dbClient);
  }

  private static DefaultOrganization getDefaultOrganization(DbClient dbClient) {
    try (DbSession dbSession = dbClient.openSession(false)) {
      Optional uuid = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.DEFAULT_ORGANIZATION);
      checkState(uuid.isPresent() && !uuid.get().isEmpty(), "No Default organization uuid configured");
      Optional dto = dbClient.organizationDao().selectByUuid(dbSession, uuid.get());
      checkState(dto.isPresent(), "Default organization with uuid '%s' does not exist", uuid.get());
      return toDefaultOrganization(dto.get());
    }
  }

  private static DefaultOrganization toDefaultOrganization(OrganizationDto organizationDto) {
    return DefaultOrganization.newBuilder()
      .setUuid(organizationDto.getUuid())
      .setKey(organizationDto.getKey())
      .setName(organizationDto.getName())
      .setCreatedAt(organizationDto.getCreatedAt())
      .setUpdatedAt(organizationDto.getUpdatedAt())
      .build();
  }

  @Override
  public void load() {
    checkState(
      CACHE.get() == null,
      "load called twice for thread '%s' or state wasn't cleared last time it was used",
      Thread.currentThread().getName());
    CACHE.set(new Cache());
  }

  @Override
  public void unload() {
    CACHE.remove();
  }

  private static final class Cache {
    @CheckForNull
    private DefaultOrganization defaultOrganization;

    public DefaultOrganization get(Supplier supplier) {
      if (defaultOrganization == null) {
        defaultOrganization = supplier.get();
      }
      return defaultOrganization;
    }

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy