it.tidalwave.util.ContextManager Maven / Gradle / Ivy
/*
* *********************************************************************************************************************
*
* TheseFoolishThings: Miscellaneous utilities
* http://tidalwave.it/projects/thesefoolishthings
*
* Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed 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.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
* git clone https://github.com/tidalwave-it/thesefoolishthings-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.util;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Supplier;
import it.tidalwave.role.spi.ContextManagerProvider;
import static it.tidalwave.role.impl.ServiceLoaderLocator.lazySupplierOf;
/***********************************************************************************************************************
*
* A facility to register and unregister global and local DCI contexts.
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
public interface ContextManager
{
/*******************************************************************************************************************
*
* A locator for the {@link ContextManager} which uses the {@link ServiceLoader} facility to be independent of
* any DI framework.
*
* This locator caches the internal reference and this is ok for production use; during tests, since multiple
* contexts are typically created and destroyed for each test, you should call {@link #reset()} after each test
* has been completed.
*
******************************************************************************************************************/
static class Inner
{
private static final LazySupplier CONTEXT_MANAGER_REF =
LazySupplier.of(() -> Inner.CONTEXT_MANAGER_PROVIDER_REF.get().getContextManager());
private static final LazySupplier CONTEXT_MANAGER_PROVIDER_REF =
lazySupplierOf(ContextManagerProvider.class);
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Nonnull
public static ContextManager getInstance()
{
return Inner.CONTEXT_MANAGER_REF.get();
}
/*******************************************************************************************************************
*
* This method is for testing only. Sets the global {@link ContextManagerProvider}. See note about
* {@link #reset()}.
*
* @param provider the provider
* @see #reset()
*
******************************************************************************************************************/
public static void set (@Nonnull final ContextManagerProvider provider)
{
Inner.CONTEXT_MANAGER_REF.clear();
Inner.CONTEXT_MANAGER_PROVIDER_REF.set(provider);
}
/*******************************************************************************************************************
*
* This method is for testing only. Resets the global {@link ContextManagerProvider}; it must be called
* at the test completion whenever {@link #set(ContextManagerProvider)} has been called, to avoid polluting the
* context of further tests.
*
* @see #set(ContextManagerProvider)
*
******************************************************************************************************************/
public static void reset()
{
Inner.CONTEXT_MANAGER_REF.clear();
Inner.CONTEXT_MANAGER_PROVIDER_REF.clear();
}
@FunctionalInterface
public static interface RunnableWithException
{
public void run()
throws E;
}
@FunctionalInterface
public static interface SupplierWithException
{
public T get()
throws E;
}
/*******************************************************************************************************************
*
* Returns the list of current contexts, ordered by their priority.
*
* @return the list of current contexts
*
******************************************************************************************************************/
@Nonnull
public List © 2015 - 2025 Weber Informatics LLC | Privacy Policy