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

org.nuiton.topia.persistence.TopiaApplicationContextFactory Maven / Gradle / Ivy

The newest version!
package org.nuiton.topia.persistence;

/*-
 * #%L
 * ToPIA Extension :: API
 * %%
 * Copyright (C) 2018 - 2022 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import io.ultreia.java4all.util.ServiceLoaders;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
 * Created on 17/06/2022.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.0.5
 */
public abstract class TopiaApplicationContextFactory

> { private static final Logger log = LogManager.getLogger(TopiaApplicationContextFactory.class); public static TopiaApplicationContextFactory INSTANCE; final Map cache = new TreeMap<>(); public static TopiaApplicationContextFactory get() { if (INSTANCE == null) { INSTANCE = ServiceLoaders.loadUniqueService(TopiaApplicationContextFactory.class); } return INSTANCE; } public static

> A createTopiaApplicationContext(TopiaConfigurationExtension topiaConfiguration) { @SuppressWarnings("unchecked") TopiaApplicationContextFactory topiaApplicationContextFactory = (TopiaApplicationContextFactory) get(); return topiaApplicationContextFactory.createApplicationContext(topiaConfiguration, topiaApplicationContextFactory.cache); } public static

> A getTopiaApplicationContext(String authenticationToken) { @SuppressWarnings("unchecked") TopiaApplicationContextFactory topiaApplicationContextFactory = (TopiaApplicationContextFactory) get(); return topiaApplicationContextFactory.getTopiaApplicationContext0(authenticationToken); } public static void clearContexts(String authenticationToken) { get().clearContexts0(authenticationToken); } public static void close() { get().close0(); } protected static void close(TopiaApplicationContext topiaApplicationContext) { if (!topiaApplicationContext.isClosed()) { try { topiaApplicationContext.close(); } catch (Exception e) { log.error(String.format("Could not close topiaApplicationContext: %s", topiaApplicationContext), e); } } } protected abstract A createApplicationContext(TopiaConfigurationExtension topiaConfiguration, Map cache); public A getTopiaApplicationContext0(String authenticationToken) { A result = cache.get(authenticationToken); Objects.requireNonNull(result, "Did not find how to create A from: " + authenticationToken); return result; } public void clearContexts0(String authenticationToken) { A toRemove = cache.remove(authenticationToken); if (toRemove == null) { // nothing to clean return; } log.info(String.format("Close remaining topia application context for: %s", authenticationToken)); close(toRemove); log.info(String.format("Still using %d topia application context(s).", cache.size())); } public void close0() { for (A topiaApplicationContext : new LinkedHashSet<>(cache.values())) { close(topiaApplicationContext); } cache.clear(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy