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

is.codion.common.rmi.server.AuxiliaryServerFactory Maven / Gradle / Ivy

/*
 * This file is part of Codion.
 *
 * Codion 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.
 *
 * Codion 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 Codion.  If not, see .
 *
 * Copyright (c) 2020 - 2024, Björn Darri Sigurðsson.
 */
package is.codion.common.rmi.server;

import java.rmi.Remote;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

import static java.util.Objects.requireNonNull;
import static java.util.stream.StreamSupport.stream;

/**
 * Provides a {@link AuxiliaryServer} implementation.
 * @param  the remote connection type provided by the parent server
 * @param  the admin connection type provided by the parent server
 * @param  the parent server type
 */
public interface AuxiliaryServerFactory {

	/**
	 * Creates a server instance using the given configuration.
	 * @param server the parent server
	 * @return a server
	 */
	T createServer(Server server);

	/**
	 * Returns the {@link AuxiliaryServerFactory} implementation found by the {@link ServiceLoader} of the given type.
	 * @param classname the classname of the required auxiliary server factory
	 * @param  the remote connection type provided by the parent server
	 * @param  the admin connection type provided by the parent server
	 * @param  the auxiliary server type
	 * @return a {@link AuxiliaryServerFactory} implementation of the given type from the {@link ServiceLoader}.
	 * @throws IllegalStateException in case no such {@link AuxiliaryServerFactory} implementation is available.
	 */
	static  AuxiliaryServerFactory instance(String classname) {
		requireNonNull(classname);
		try {
			return stream(ServiceLoader.load(AuxiliaryServerFactory.class).spliterator(), false)
							.filter(serverFactory -> serverFactory.getClass().getName().equals(classname))
							.findFirst()
							.orElseThrow(() -> new IllegalStateException("No auxiliary server factory of type: " + classname + " available"));
		}
		catch (ServiceConfigurationError e) {
			Throwable cause = e.getCause();
			if (cause instanceof RuntimeException) {
				throw (RuntimeException) cause;
			}
			throw new RuntimeException(cause);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy