Search in sources :

Example 1 with ServiceImporterException

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;
}
Also used : ServiceUnavailableException(org.qi4j.api.service.ServiceUnavailableException) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ServiceUnavailableException(org.qi4j.api.service.ServiceUnavailableException)

Example 2 with ServiceImporterException

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;
    }
}
Also used : CircuitBreaker(org.qi4j.library.circuitbreaker.CircuitBreaker) SQLException(java.sql.SQLException) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) IOException(java.io.IOException) SQLException(java.sql.SQLException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) DataSource(javax.sql.DataSource)

Example 3 with ServiceImporterException

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);
}
Also used : ServiceImporterException(org.qi4j.api.service.ServiceImporterException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSource(javax.sql.DataSource)

Example 4 with ServiceImporterException

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;
}
Also used : ArrayList(java.util.ArrayList) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) Specification(org.qi4j.functional.Specification) ServiceReference(org.qi4j.api.service.ServiceReference)

Aggregations

ServiceImporterException (org.qi4j.api.service.ServiceImporterException)4 DataSource (javax.sql.DataSource)2 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 ActivationException (org.qi4j.api.activation.ActivationException)1 PassivationException (org.qi4j.api.activation.PassivationException)1 ServiceReference (org.qi4j.api.service.ServiceReference)1 ServiceUnavailableException (org.qi4j.api.service.ServiceUnavailableException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 Specification (org.qi4j.functional.Specification)1 CircuitBreaker (org.qi4j.library.circuitbreaker.CircuitBreaker)1