use of org.qi4j.api.service.ServiceImporterException in project qi4j-sdk by Qi4j.
the class ImportedServiceReferenceInstance method getInstance.
private T getInstance() throws ServiceImporterException {
// DCL that works with Java 1.5 volatile semantics
if (serviceInstance == null) {
synchronized (this) {
if (serviceInstance == null) {
serviceInstance = serviceModel.<T>importInstance(module);
instance = serviceInstance.instance();
try {
activation.activate(serviceModel.newActivatorsInstance(module), serviceInstance, new Runnable() {
@Override
public void run() {
active = true;
}
});
} catch (Exception e) {
serviceInstance = null;
throw new ServiceUnavailableException("Could not activate service " + serviceModel.identity(), e);
}
}
}
}
return instance;
}
use of org.qi4j.api.service.ServiceImporterException in project qi4j-sdk by Qi4j.
the class AbstractDataSourceServiceImporterMixin method importService.
@Override
public final synchronized DataSource importService(final ImportedServiceDescriptor importedServiceDescriptor) throws ServiceImporterException {
PooledDataSourceType pool = pools.get(importedServiceDescriptor.identity());
if (pool == null) {
try {
DataSourceConfigurationValue config = getConfiguration(importedServiceDescriptor.identity());
if (!config.enabled().get()) {
// Not started
throw new ServiceImporterException("DataSource not enabled");
}
// Instantiate pool
pool = setupDataSourcePool(config);
pools.put(importedServiceDescriptor.identity(), pool);
LOGGER.info("Starting up DataSource '" + importedServiceDescriptor.identity() + "' for: {}@{}", config.username().get(), config.url().get());
} catch (Exception e) {
throw new ServiceImporterException(e);
}
// Test the pool
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(null);
try {
pool.getConnection().close();
LOGGER.info("Database for DataSource is up!");
} catch (SQLException e) {
LOGGER.warn("Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available");
throw new ServiceImporterException("Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available", e);
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
// Check if circuitbreaker is used
final CircuitBreaker circuitBreaker = importedServiceDescriptor.metaInfo(CircuitBreaker.class);
if (circuitBreaker != null) {
DataSource wrappedDataSource = DataSources.wrapWithCircuitBreaker(importedServiceDescriptor.identity(), pool, circuitBreaker);
circuitBreakers.put(pool, circuitBreaker);
return wrappedDataSource;
} else {
return pool;
}
}
use of org.qi4j.api.service.ServiceImporterException in project qi4j-sdk by Qi4j.
the class DataSources method wrapWithCircuitBreaker.
public static DataSource wrapWithCircuitBreaker(final String dataSourceIdentity, final DataSource pool, final CircuitBreaker circuitBreaker) {
// Create wrapper
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!circuitBreaker.isOn()) {
Throwable throwable = circuitBreaker.lastThrowable();
if (throwable != null) {
throw throwable;
} else {
throw new ServiceImporterException("Circuit breaker for DataSource " + dataSourceIdentity + " is not on");
}
}
try {
Object result = method.invoke(pool, args);
circuitBreaker.success();
return result;
} catch (IllegalAccessException e) {
circuitBreaker.throwable(e);
throw e;
} catch (IllegalArgumentException e) {
circuitBreaker.throwable(e);
throw e;
} catch (InvocationTargetException e) {
circuitBreaker.throwable(e.getCause());
throw e.getCause();
}
}
};
// Create proxy with circuit breaker
return (DataSource) Proxy.newProxyInstance(DataSource.class.getClassLoader(), new Class[] { DataSource.class }, handler);
}
use of org.qi4j.api.service.ServiceImporterException in project qi4j-sdk by Qi4j.
the class ServiceSelectorImporter method importService.
@Override
@SuppressWarnings({ "raw", "unchecked" })
public T importService(ImportedServiceDescriptor serviceDescriptor) throws ServiceImporterException {
Specification<ServiceReference<?>> selector = serviceDescriptor.metaInfo(Specification.class);
Class serviceType = Iterables.first(serviceDescriptor.types());
Iterable<ServiceReference<T>> services = locator.findServices(serviceType);
List<ServiceReference<T>> filteredServices = new ArrayList<>();
for (ServiceReference<T> service : services) {
Specification selector1 = service.metaInfo(Specification.class);
if (selector1 != null && selector1 == selector) {
continue;
}
filteredServices.add(service);
}
T service = ServiceQualifier.firstService(selector, filteredServices);
if (service == null) {
throw new ServiceImporterException("Could not find any service to import that matches the given specification for " + serviceDescriptor.identity());
}
return service;
}
Aggregations