Example usage for org.springframework.beans.factory DisposableBean destroy

List of usage examples for org.springframework.beans.factory DisposableBean destroy

Introduction

In this page you can find the example usage for org.springframework.beans.factory DisposableBean destroy.

Prototype

void destroy() throws Exception;

Source Link

Document

Invoked by the containing BeanFactory on destruction of a bean.

Usage

From source file:org.guicerecipes.spring.support.DisposableBeanCloser.java

public void close(Object object) throws Throwable {
    if (object instanceof DisposableBean) {
        DisposableBean disposableBean = (DisposableBean) object;
        disposableBean.destroy();
    }//  w  w  w  .j a  v  a2s  . c o m
}

From source file:org.terracotta.quartz.tests.spring.SpringClientBase.java

@Override
protected final void test(Scheduler scheduler) throws Throwable {

    try {/*ww  w.  j  av a  2 s .c om*/
        test0(scheduler);
    } finally {
        if (context.containsBean("taskExecutor")) {
            DisposableBean exec = (DisposableBean) context.getBean("taskExecutor");
            exec.destroy();
        }
    }
}

From source file:io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.java

@Override
public void destroy() throws Exception {
    if (executor instanceof DisposableBean) {
        DisposableBean bean = (DisposableBean) executor;
        bean.destroy();
    }/*from  w w  w.  ja  v a2  s  .c  om*/
}

From source file:org.brekka.stillingar.spring.snapshot.SnapshotDeltaValueInterceptor.java

protected void destroyBean(Object value) {
    if (value instanceof DisposableBean) {
        DisposableBean disposableBean = (DisposableBean) value;
        try {//from   w ww  . j  ava2  s .  co m
            disposableBean.destroy();
        } catch (Exception e) {
            throw new ConfigurationException(String.format("Error while disposing bean '%s'", value), e);
        }
        if (log.isInfoEnabled()) {
            log.info(String.format("Stopped DisposableBean: %s", value));
        }
    } else if (value instanceof Lifecycle) {
        Lifecycle lifecycle = (Lifecycle) value;
        lifecycle.stop();
        if (log.isInfoEnabled()) {
            log.info(String.format("Stopped Lifecycle bean: %s", value));
        }
    }
}

From source file:guru.qas.martini.scope.ScenarioScope.java

protected void dispose(String name, Object bean) {
    if (DisposableBean.class.isInstance(bean)) {
        DisposableBean disposable = DisposableBean.class.cast(bean);
        try {/*from ww  w . java  2s.  com*/
            disposable.destroy();
        } catch (Exception e) {
            logger.warn("unable to dispose of bean {}", name, bean);
        }
    }
}

From source file:net.firejack.platform.model.config.hibernate.HibernateFactoryBean.java

public void destroy() {
    for (String name : databases.keySet()) {
        try {/*  w ww . j  a v a 2  s  .co m*/
            BasicDataSource dataSource = context.getBean(name + HIBERNATE_DATA_SOURCE_SUFFIX,
                    BasicDataSource.class);
            dataSource.close();
        } catch (Exception e) {
            logger.error(e);
        }
    }

    for (String name : xaDatabases.keySet()) {
        try {
            AtomikosDataSourceBean dataSource = context.getBean(name + HIBERNATE_DATA_SOURCE_SUFFIX,
                    AtomikosDataSourceBean.class);
            dataSource.close();
        } catch (Exception e) {
            logger.error(e);
        }
    }

    for (DisposableBean bean : disposableBeans) {
        try {
            bean.destroy();
        } catch (Exception e) {
            logger.error(e);
        }
    }
}

From source file:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.java

/**
 * Destroy the given bean. Must destroy beans that depend on the given
 * bean before the bean itself. Should not throw any exceptions.
 * @param beanName the name of the bean//from  w w  w.ja va2  s  .  co  m
 * @param bean the bean instance to destroy
 */
protected void destroyBean(String beanName, @Nullable DisposableBean bean) {
    // Trigger destruction of dependent beans first...
    Set<String> dependencies = this.dependentBeanMap.remove(beanName);
    if (dependencies != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Retrieved dependent beans for bean '" + beanName + "': " + dependencies);
        }
        for (String dependentBeanName : dependencies) {
            destroySingleton(dependentBeanName);
        }
    }

    // Actually destroy the bean now...
    if (bean != null) {
        try {
            bean.destroy();
        } catch (Throwable ex) {
            logger.error("Destroy method on bean with name '" + beanName + "' threw an exception", ex);
        }
    }

    // Trigger destruction of contained beans...
    Set<String> containedBeans = this.containedBeanMap.remove(beanName);
    if (containedBeans != null) {
        for (String containedBeanName : containedBeans) {
            destroySingleton(containedBeanName);
        }
    }

    // Remove destroyed bean from other beans' dependencies.
    synchronized (this.dependentBeanMap) {
        for (Iterator<Map.Entry<String, Set<String>>> it = this.dependentBeanMap.entrySet().iterator(); it
                .hasNext();) {
            Map.Entry<String, Set<String>> entry = it.next();
            Set<String> dependenciesToClean = entry.getValue();
            dependenciesToClean.remove(beanName);
            if (dependenciesToClean.isEmpty()) {
                it.remove();
            }
        }
    }

    // Remove destroyed bean's prepared dependency information.
    this.dependenciesForBeanMap.remove(beanName);
}

From source file:org.springframework.data.hadoop.config.common.annotation.configuration.AutowireBeanFactoryObjectPostProcessor.java

@Override
public void destroy() throws Exception {
    for (DisposableBean disposable : disposableBeans) {
        try {/*from  w  ww .  j  a va2 s .  c om*/
            disposable.destroy();
        } catch (Exception error) {
            log.error(error);
        }
    }
}

From source file:org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor.java

public void destroy() throws Exception {
    for (DisposableBean disposable : this.disposableBeans) {
        try {//from www.  j  a v a2 s. c  om
            disposable.destroy();
        } catch (Exception error) {
            this.logger.error(error);
        }
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalFileTestExecutionListener.java

@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    if (isTestClassAssignableFromOwncloudLocalFileTest(testContext)) {
        ApplicationContext applicationContext = testContext.getApplicationContext();

        Class<?> testClass = testContext.getTestClass();
        Method testMethod = testContext.getTestMethod();

        DisposableBean localDataService = applicationContext.getBean(OwncloudLocalUserDataServiceImpl.class);
        localDataService.destroy();

        ResourceLoader resourceLoader = applicationContext;
        OwncloudProperties properties = applicationContext.getBean(OwncloudProperties.class);
        Resource target = resourceLoader.getResource(properties.getLocation());

        boolean hasSpecificResourceTest = false;
        for (Method method : testContext.getTestClass().getMethods()) {
            // is this Method annotated by @CompareResourceAfter
            CompareResourceAfter compareResourceAfter = AnnotationUtils.findAnnotation(method,
                    CompareResourceAfter.class);
            if (compareResourceAfter == null
                    || !StringUtils.equals(compareResourceAfter.value(), testMethod.getName())) {
                continue;
            }//  w  w  w  .  j a va  2s. c om

            // a Method annotated by @Test cannot also be annotated by
            // @CompareResourceAfter
            if (AnnotationUtils.findAnnotation(method, Test.class) != null) {
                log.warn("Method {} of Class {} cannot be annotated by {} and {}", method.getName(),
                        testClass.getName(), CompareResourceAfter.class, Test.class);
                continue;
            }

            // the @CompareResourceAfter annotated Method must have exactly 2
            // Parameters of Type org.springframework.core.io.Resource
            if (method.getParameterCount() != 1) {
                log.warn("Method {} of Class {} is annotated by {} but has {} Parameters instead of 1",
                        method.getName(), testClass.getName(), CompareResourceAfter.class.getName(),
                        method.getParameterCount());
                continue;
            }
            boolean correctParameterTypes = true;
            for (Class<?> parameterClass : method.getParameterTypes()) {
                correctParameterTypes = correctParameterTypes
                        && Resource.class.isAssignableFrom(parameterClass);
            }
            if (!correctParameterTypes) {
                log.warn("Method {} of Class {} (annotated by {}) must have 1 Parameter of Type {}",
                        method.getName(), testClass.getName(), CompareResourceAfter.class.getName(),
                        Resource.class.getName());
                continue;
            }

            log.debug("Call the Resource Comparsion Method {} on Class {}", method.getName(),
                    testClass.getName());
            hasSpecificResourceTest = true;
            try {
                method.invoke(testContext.getTestInstance(), target);
            } catch (InvocationTargetException e) {
                if (e.getCause() instanceof Exception) {
                    throw (Exception) e.getCause();
                }
                throw (Error) e.getCause();
            }
        }

        if (!hasSpecificResourceTest && ((OwncloudLocalFileTest) testContext.getTestInstance())
                .isCheckAllResourcesAgainstOriginal()) {
            compareResourcesWithOriginalSource(resourceLoader, target);
        }
    }
}