
fr.ird.observe.services.ObserveServiceMainFactory Maven / Gradle / Ivy
package fr.ird.observe.services;
/*
* #%L
* ObServe Toolkit :: Common Service
* %%
* Copyright (C) 2008 - 2017 IRD, Ultreia.io
* %%
* This program 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.
*
* This program 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 this program. If not, see
* .
* #L%
*/
import fr.ird.observe.services.configuration.ObserveDataSourceConfiguration;
import fr.ird.observe.services.configuration.ObserveDataSourceConnection;
import fr.ird.observe.services.service.ObserveService;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Created on 16/08/15.
*
* @author Tony Chemit - [email protected]
*/
public class ObserveServiceMainFactory implements ObserveServiceFactory {
/** Logger. */
private static final Log log = LogFactory.getLog(ObserveServiceMainFactory.class);
private static final ObserveServiceMainFactory GET = new ObserveServiceMainFactory();
private final Set delegateFactories;
public static ObserveServiceMainFactory get() {
return GET;
}
@Override
public ObserveServiceFactory getMainServiceFactory() {
return this;
}
@Override
public void setMainServiceFactory(ObserveServiceFactory mainServiceFactory) {
throw new IllegalStateException("You can't set main factory inside the main factory");
}
@Override
public boolean accept(ObserveDataSourceConfiguration dataSourceConfiguration, Class serviceType) {
getFactory(dataSourceConfiguration, serviceType);
return true;
}
@Override
public boolean accept(ObserveDataSourceConnection dataSourceConnection, Class serviceType) {
getFactory(dataSourceConnection, serviceType);
return true;
}
@Override
public S newService(ObserveServiceInitializer observeServiceInitializer, Class serviceType) {
Objects.requireNonNull(observeServiceInitializer, "observeServiceInitializerContext can't be null.");
Objects.requireNonNull(serviceType, "serviceType can't be null.");
ObserveServiceFactory factory;
if (observeServiceInitializer.withDataSourceConnection()) {
factory = getFactory(observeServiceInitializer.getDataSourceConnection().orElseThrow(IllegalStateException::new), serviceType);
} else if (observeServiceInitializer.withDataSourceConfiguration()) {
factory = getFactory(observeServiceInitializer.getDataSourceConfiguration().orElseThrow(IllegalStateException::new), serviceType);
} else {
throw new IllegalStateException("No dataSourceConnection, nor dataSourceConfiguration given.");
}
Objects.requireNonNull(factory, "factory can't be null.");
if (log.isDebugEnabled()) {
log.debug("Using factory: " + factory);
}
S service = factory.newService(observeServiceInitializer, serviceType);
if (log.isInfoEnabled()) {
log.info("New service created: " + service);
}
return service;
}
@Override
public void close() throws IOException {
delegateFactories.forEach(IOUtils::closeQuietly);
}
private ObserveServiceMainFactory() {
log.info("Init MainServiceFactory.");
Set builder = new LinkedHashSet<>();
for (ObserveServiceFactory factory : ServiceLoader.load(ObserveServiceFactory.class)) {
log.info("Discover service factory: " + factory);
factory.setMainServiceFactory(this);
builder.add(factory);
}
if (builder.isEmpty()) {
throw new IllegalStateException("No service factory found.");
}
log.info(String.format("Discover %d service factories.", builder.size()));
delegateFactories = Collections.unmodifiableSet(builder);
}
private ObserveServiceFactory getFactory(ObserveDataSourceConfiguration dataSourceConfiguration, Class serviceType) {
Objects.requireNonNull(dataSourceConfiguration);
Objects.requireNonNull(serviceType);
Optional result = delegateFactories.stream()
.filter(f -> f.accept(dataSourceConfiguration, serviceType))
.findFirst();
if (!result.isPresent()) {
throw new NullPointerException(String.format("No factory found for dataSourceConfiguration: %s and serviceType: %s", dataSourceConfiguration, serviceType.getName()));
}
return result.get();
}
private ObserveServiceFactory getFactory(ObserveDataSourceConnection dataSourceConnection, Class serviceType) {
Objects.requireNonNull(dataSourceConnection);
Objects.requireNonNull(serviceType);
Optional result = delegateFactories.stream()
.filter(f -> f.accept(dataSourceConnection, serviceType))
.findFirst();
if (!result.isPresent()) {
throw new NullPointerException(String.format("No factory found for dataSourceConnection: %s and serviceType: %s", dataSourceConnection, serviceType.getName()));
}
return result.get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy