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

com.mageddo.togglefirst.FeatureContext Maven / Gradle / Ivy

There is a newer version: 1.4.9
Show newest version
package com.mageddo.togglefirst;

import com.mageddo.togglefirst.repository.InMemoryFeatureRepository;
import com.mageddo.togglefirst.spring.ApplicationContextProvider;
import org.springframework.context.ApplicationContext;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * Responsible to find a {@link FeatureManager} instance at the classpath
 */
public final class FeatureContext {

	private FeatureContext() {
	}

	public static FeatureManager getFeatureManager(){

		final FeatureManager featureManager = getSpringFeatureManager();
		if (featureManager != null){
			return featureManager;
		}

		final FeatureManager serviceLoaderFeatureManager = getServiceLoaderFeatureManager();
		if(serviceLoaderFeatureManager != null){
			return serviceLoaderFeatureManager;
		}

		return new DefaultFeatureManager()
			.featureRepository(new InMemoryFeatureRepository())
			.featureMetadataProvider(new EnumFeatureMetadataProvider())
		;
	}

	static boolean existsOnClasspath(String name){
		try {
			Class.forName(name);
			return true;
		} catch (ClassNotFoundException ignored) {
			return false;
		}
	}

	/**
	 * Create a serviceloader file at the path /META-INF/services/com.mageddo.togglefirst.FeatureManager with a content like
	 * 
	 * com.mageddo.togglefirst.DefaultFeatureManager
	 * 
	 */
	private static FeatureManager getServiceLoaderFeatureManager() {
		final Iterator it = ServiceLoader.load(FeatureManager.class).iterator();
		if(it.hasNext()){
			return it.next();
		}
		return null;
	}

	/**
	 * To integrate with spring just create a bean of the type {@link FeatureManager}
	 */
	private static FeatureManager getSpringFeatureManager() {
		if(existsOnClasspath("org.springframework.context.ApplicationContext")){
			final ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
			if(ctx != null){
				return ctx.getBean(FeatureManager.class);
			}
		}
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy