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

org.appdapter.gui.util.ClassLoadingNamingResolver Maven / Gradle / Ivy

Go to download

Appdapter Maven project including Java and Scala, produces jar, not bundle. Excludes concrete SLF4J binding.

The newest version!
package org.appdapter.gui.util;

import java.util.Collection;
import java.util.ServiceLoader;

import org.appdapter.api.trigger.AnyOper;
import org.appdapter.gui.util.ObjectFinder.Found;
import org.appdapter.gui.util.ObjectFinder.FoundObject;

public class ClassLoadingNamingResolver implements NamingResolver {

	public ClassLoadingNamingResolver() {
		InitialBoxedContext.installNamingResolver(this);
	}

	@Override public  Found lookup(String name, Class cls, boolean createIfNotFound, Collection dontUse) {
		if (dontUse.contains(this))
			return null;
		dontUse.add(this);
		if (!createIfNotFound)
			return null;
		if (cls == null) {
			try {
				cls = PromiscuousClassUtilsA.forName(name);
			} catch (ClassNotFoundException e) {
				cls = null;
			}
		} else {
			PromiscuousClassUtilsA.ensureOntoligized(cls);
		}

		Class clo = (Class) cls;
		final ServiceLoader sl = (ServiceLoader) ServiceLoader.load(clo);
		if (sl != null)
			return new FoundObject(null, name, new ObjectFinder.GetF() {
				@SuppressWarnings("unchecked") @Override public O getValue() {
					return (O) sl.iterator().next();
				}
			});
		return null;
	}

}