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

net.sf.jstuff.integration.serviceregistry.support.ServiceAsSpringBean Maven / Gradle / Ivy

/*
 * Copyright 2010-2022 by Sebastian Thomschke and contributors.
 * SPDX-License-Identifier: EPL-2.0
 */
package net.sf.jstuff.integration.serviceregistry.support;

import static net.sf.jstuff.core.validation.NullAnalysisHelper.*;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import net.sf.jstuff.core.validation.Args;
import net.sf.jstuff.core.validation.Assert;
import net.sf.jstuff.integration.serviceregistry.ServiceRegistry;

/**
 * Helper class to expose services registered with the {@link ServiceRegistry} as Spring beans.
 * 

* I.e. the service will be looked up in the given serviceRegistry made available for injection into other Spring-managed beans. * *

 * {@code
 * 
 *     
 *     
 *     
 *     
 * 
 * }
 * 
* * @author Sebastian Thomschke */ public class ServiceAsSpringBean<@NonNull T> implements FactoryBean, InitializingBean { private ServiceRegistry serviceRegistry = lazyNonNull(); /** * @optional by default the fully qualified name of the service interface is used */ private @Nullable String serviceEndpointId; private Class serviceInterface = lazyNonNull(); private T service = lazyNonNull(); @Override public synchronized void afterPropertiesSet() throws Exception { Assert.isNull(service, "Already initialized!"); Assert.notNull(serviceRegistry, "[serviceRegistry] must not be null!"); Assert.notNull(serviceInterface, "[serviceInterface] must not be null!"); service = serviceRegistry.getService(// serviceEndpointId != null ? serviceEndpointId : serviceInterface.getName(), // serviceInterface // ).get(); } @Override public T getObject() throws Exception { return service; } @Override public Class getObjectType() { return serviceInterface; } @Override public boolean isSingleton() { return true; } public synchronized void setServiceEndpointId(final String serviceEndpointId) { Args.notNull("serviceEndpointId", serviceEndpointId); Assert.isNull(service, "Already initialized!"); this.serviceEndpointId = serviceEndpointId; } public synchronized void setServiceInterface(final Class serviceInterface) { Args.notNull("serviceInterface", serviceInterface); Assert.isNull(service, "Already initialized!"); Assert.isTrue(serviceInterface.isInterface(), "[serviceInterface] must be an interface but is " + serviceInterface); this.serviceInterface = serviceInterface; } public synchronized void setServiceRegistry(final ServiceRegistry serviceRegistry) { Args.notNull("serviceRegistry", serviceRegistry); Assert.isNull(service, "Already initialized!"); this.serviceRegistry = serviceRegistry; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy