Java Code Examples for org.springframework.beans.factory.DisposableBean

The following examples show how to use org.springframework.beans.factory.DisposableBean. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractPrototypeBasedTargetSource.java    License: MIT License 6 votes vote down vote up
/**
 * Subclasses should call this method to destroy an obsolete prototype instance.
 * @param target the bean instance to destroy
 */
protected void destroyPrototypeInstance(Object target) {
	if (logger.isDebugEnabled()) {
		logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'");
	}
	if (getBeanFactory() instanceof ConfigurableBeanFactory) {
		((ConfigurableBeanFactory) getBeanFactory()).destroyBean(getTargetBeanName(), target);
	}
	else if (target instanceof DisposableBean) {
		try {
			((DisposableBean) target).destroy();
		}
		catch (Throwable ex) {
			logger.warn("Destroy method on bean with name '" + getTargetBeanName() + "' threw an exception", ex);
		}
	}
}
 
Example #2
Source Project: spring-analysis-note   Author: Vip-Augus   File: ContextCleanupListener.java    License: MIT License 6 votes vote down vote up
/**
 * Find all ServletContext attributes which implement {@link DisposableBean}
 * and destroy them, removing all affected ServletContext attributes eventually.
 * @param sc the ServletContext to check
 */
static void cleanupAttributes(ServletContext sc) {
	Enumeration<String> attrNames = sc.getAttributeNames();
	while (attrNames.hasMoreElements()) {
		String attrName = attrNames.nextElement();
		if (attrName.startsWith("org.springframework.")) {
			Object attrValue = sc.getAttribute(attrName);
			if (attrValue instanceof DisposableBean) {
				try {
					((DisposableBean) attrValue).destroy();
				}
				catch (Throwable ex) {
					logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
				}
			}
		}
	}
}
 
Example #3
Source Project: java-technology-stack   Author: codeEngraver   File: ContextCleanupListener.java    License: MIT License 6 votes vote down vote up
/**
 * Find all ServletContext attributes which implement {@link DisposableBean}
 * and destroy them, removing all affected ServletContext attributes eventually.
 * @param sc the ServletContext to check
 */
static void cleanupAttributes(ServletContext sc) {
	Enumeration<String> attrNames = sc.getAttributeNames();
	while (attrNames.hasMoreElements()) {
		String attrName = attrNames.nextElement();
		if (attrName.startsWith("org.springframework.")) {
			Object attrValue = sc.getAttribute(attrName);
			if (attrValue instanceof DisposableBean) {
				try {
					((DisposableBean) attrValue).destroy();
				}
				catch (Throwable ex) {
					logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
				}
			}
		}
	}
}
 
Example #4
Source Project: java-technology-stack   Author: codeEngraver   File: DisposableBeanAdapter.java    License: MIT License 6 votes vote down vote up
/**
 * If the current value of the given beanDefinition's "destroyMethodName" property is
 * {@link AbstractBeanDefinition#INFER_METHOD}, then attempt to infer a destroy method.
 * Candidate methods are currently limited to public, no-arg methods named "close" or
 * "shutdown" (whether declared locally or inherited). The given BeanDefinition's
 * "destroyMethodName" is updated to be null if no such method is found, otherwise set
 * to the name of the inferred method. This constant serves as the default for the
 * {@code @Bean#destroyMethod} attribute and the value of the constant may also be
 * used in XML within the {@code <bean destroy-method="">} or {@code
 * <beans default-destroy-method="">} attributes.
 * <p>Also processes the {@link java.io.Closeable} and {@link java.lang.AutoCloseable}
 * interfaces, reflectively calling the "close" method on implementing beans as well.
 */
@Nullable
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
	String destroyMethodName = beanDefinition.getDestroyMethodName();
	if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||
			(destroyMethodName == null && bean instanceof AutoCloseable)) {
		// Only perform destroy method inference or Closeable detection
		// in case of the bean not explicitly implementing DisposableBean
		if (!(bean instanceof DisposableBean)) {
			try {
				return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
			}
			catch (NoSuchMethodException ex) {
				try {
					return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
				}
				catch (NoSuchMethodException ex2) {
					// no candidate destroy method found
				}
			}
		}
		return null;
	}
	return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
}
 
Example #5
Source Project: spring-context-support   Author: alibaba   File: AnnotationInjectedBeanPostProcessor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void destroy() throws Exception {

    for (Object object : injectedObjectsCache.values()) {
        if (logger.isInfoEnabled()) {
            logger.info(object + " was destroying!");
        }

        if (object instanceof DisposableBean) {
            ((DisposableBean) object).destroy();
        }
    }

    injectionMetadataCache.clear();
    injectedObjectsCache.clear();

    if (logger.isInfoEnabled()) {
        logger.info(getClass() + " was destroying!");
    }

}
 
Example #6
Source Project: spring-context-support   Author: alibaba   File: AbstractAnnotationBeanPostProcessor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void destroy() throws Exception {

    for (Object object : injectedObjectsCache.values()) {
        if (logger.isInfoEnabled()) {
            logger.info(object + " was destroying!");
        }

        if (object instanceof DisposableBean) {
            ((DisposableBean) object).destroy();
        }
    }

    injectionMetadataCache.clear();
    injectedObjectsCache.clear();

    if (logger.isInfoEnabled()) {
        logger.info(getClass() + " was destroying!");
    }

}
 
Example #7
Source Project: lams   Author: lamsfoundation   File: ContextCleanupListener.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find all ServletContext attributes which implement {@link DisposableBean}
 * and destroy them, removing all affected ServletContext attributes eventually.
 * @param sc the ServletContext to check
 */
static void cleanupAttributes(ServletContext sc) {
	Enumeration<String> attrNames = sc.getAttributeNames();
	while (attrNames.hasMoreElements()) {
		String attrName = attrNames.nextElement();
		if (attrName.startsWith("org.springframework.")) {
			Object attrValue = sc.getAttribute(attrName);
			if (attrValue instanceof DisposableBean) {
				try {
					((DisposableBean) attrValue).destroy();
				}
				catch (Throwable ex) {
					logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
				}
			}
		}
	}
}
 
Example #8
Source Project: lams   Author: lamsfoundation   File: DisposableBeanAdapter.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * If the current value of the given beanDefinition's "destroyMethodName" property is
 * {@link AbstractBeanDefinition#INFER_METHOD}, then attempt to infer a destroy method.
 * Candidate methods are currently limited to public, no-arg methods named "close" or
 * "shutdown" (whether declared locally or inherited). The given BeanDefinition's
 * "destroyMethodName" is updated to be null if no such method is found, otherwise set
 * to the name of the inferred method. This constant serves as the default for the
 * {@code @Bean#destroyMethod} attribute and the value of the constant may also be
 * used in XML within the {@code <bean destroy-method="">} or {@code
 * <beans default-destroy-method="">} attributes.
 * <p>Also processes the {@link java.io.Closeable} and {@link java.lang.AutoCloseable}
 * interfaces, reflectively calling the "close" method on implementing beans as well.
 */
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
	String destroyMethodName = beanDefinition.getDestroyMethodName();
	if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||
			(destroyMethodName == null && closeableInterface.isInstance(bean))) {
		// Only perform destroy method inference or Closeable detection
		// in case of the bean not explicitly implementing DisposableBean
		if (!(bean instanceof DisposableBean)) {
			try {
				return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
			}
			catch (NoSuchMethodException ex) {
				try {
					return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
				}
				catch (NoSuchMethodException ex2) {
					// no candidate destroy method found
				}
			}
		}
		return null;
	}
	return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
}
 
Example #9
Source Project: lams   Author: lamsfoundation   File: AbstractPrototypeBasedTargetSource.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Subclasses should call this method to destroy an obsolete prototype instance.
 * @param target the bean instance to destroy
 */
protected void destroyPrototypeInstance(Object target) {
	if (this.logger.isDebugEnabled()) {
		this.logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'");
	}
	if (getBeanFactory() instanceof ConfigurableBeanFactory) {
		((ConfigurableBeanFactory) getBeanFactory()).destroyBean(getTargetBeanName(), target);
	}
	else if (target instanceof DisposableBean) {
		try {
			((DisposableBean) target).destroy();
		}
		catch (Throwable ex) {
			logger.error("Couldn't invoke destroy method of bean with name '" + getTargetBeanName() + "'", ex);
		}
	}
}
 
Example #10
Source Project: blog_demos   Author: zq2599   File: DisposableBeanAdapter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * If the current value of the given beanDefinition's "destroyMethodName" property is
 * {@link AbstractBeanDefinition#INFER_METHOD}, then attempt to infer a destroy method.
 * Candidate methods are currently limited to public, no-arg methods named "close"
 * (whether declared locally or inherited). The given BeanDefinition's
 * "destroyMethodName" is updated to be null if no such method is found, otherwise set
 * to the name of the inferred method. This constant serves as the default for the
 * {@code @Bean#destroyMethod} attribute and the value of the constant may also be
 * used in XML within the {@code <bean destroy-method="">} or {@code
 * <beans default-destroy-method="">} attributes.
 * <p>Also processes the {@link Closeable} and {@link AutoCloseable}
 * interfaces, reflectively calling the "close" method on implementing beans as well.
 */
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
	if (AbstractBeanDefinition.INFER_METHOD.equals(beanDefinition.getDestroyMethodName()) ||
			(beanDefinition.getDestroyMethodName() == null && closeableInterface.isInstance(bean))) {
		// Only perform destroy method inference or Closeable detection
		// in case of the bean not explicitly implementing DisposableBean
		if (!(bean instanceof DisposableBean)) {
			try {
				return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
			}
			catch (NoSuchMethodException ex) {
				try {
					return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
				}
				catch (NoSuchMethodException ex2) {
					// no candidate destroy method found
				}
			}
		}
		return null;
	}
	return beanDefinition.getDestroyMethodName();
}
 
Example #11
Source Project: sakai   Author: sakaiproject   File: SakaiContextLoaderListener.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Find all ServletContext attributes which implement {@link DisposableBean}
 * and destroy them, removing all affected ServletContext attributes eventually.
 * @param sc the ServletContext to check
 */
private static void cleanupAttributes(ServletContext sc) {
	Enumeration<String> attrNames = sc.getAttributeNames();
	while (attrNames.hasMoreElements()) {
		String attrName = attrNames.nextElement();
		if (attrName.startsWith("org.springframework.")) {
			Object attrValue = sc.getAttribute(attrName);
			if (attrValue instanceof DisposableBean) {
				try {
					((DisposableBean) attrValue).destroy();
				}
				catch (Throwable ex) {
					log.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
				}
			}
		}
	}
}
 
Example #12
Source Project: spring4-understanding   Author: langtianya   File: DisposableBeanAdapter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * If the current value of the given beanDefinition's "destroyMethodName" property is
 * {@link AbstractBeanDefinition#INFER_METHOD}, then attempt to infer a destroy method.
 * Candidate methods are currently limited to public, no-arg methods named "close" or
 * "shutdown" (whether declared locally or inherited). The given BeanDefinition's
 * "destroyMethodName" is updated to be null if no such method is found, otherwise set
 * to the name of the inferred method. This constant serves as the default for the
 * {@code @Bean#destroyMethod} attribute and the value of the constant may also be
 * used in XML within the {@code <bean destroy-method="">} or {@code
 * <beans default-destroy-method="">} attributes.
 * <p>Also processes the {@link java.io.Closeable} and {@link java.lang.AutoCloseable}
 * interfaces, reflectively calling the "close" method on implementing beans as well.
 */
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
	String destroyMethodName = beanDefinition.getDestroyMethodName();
	if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||
			(destroyMethodName == null && closeableInterface.isInstance(bean))) {
		// Only perform destroy method inference or Closeable detection
		// in case of the bean not explicitly implementing DisposableBean
		if (!(bean instanceof DisposableBean)) {
			try {
				return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
			}
			catch (NoSuchMethodException ex) {
				try {
					return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
				}
				catch (NoSuchMethodException ex2) {
					// no candidate destroy method found
				}
			}
		}
		return null;
	}
	return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
}
 
Example #13
Source Project: spring-vault   Author: spring-projects   File: ClientHttpRequestFactoryFactoryIntegrationTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void httpComponentsClientUsingPemShouldWork() throws Exception {

	File caCertificate = new File(Settings.findWorkDir(), "ca/certs/ca.cert.pem");
	SslConfiguration sslConfiguration = SslConfiguration.forTrustStore(SslConfiguration.KeyStoreConfiguration
			.of(new FileSystemResource(caCertificate)).withStoreType(SslConfiguration.PEM_KEYSTORE_TYPE));

	ClientHttpRequestFactory factory = HttpComponents.usingHttpComponents(new ClientOptions(), sslConfiguration);
	RestTemplate template = new RestTemplate(factory);

	String response = request(template);

	assertThat(factory).isInstanceOf(HttpComponentsClientHttpRequestFactory.class);
	assertThat(response).isNotNull().contains("initialized");

	((DisposableBean) factory).destroy();
}
 
Example #14
Source Project: spring-analysis-note   Author: Vip-Augus   File: JmsListenerEndpointRegistry.java    License: MIT License 5 votes vote down vote up
@Override
public void destroy() {
	for (MessageListenerContainer listenerContainer : getListenerContainers()) {
		if (listenerContainer instanceof DisposableBean) {
			try {
				((DisposableBean) listenerContainer).destroy();
			}
			catch (Throwable ex) {
				logger.warn("Failed to destroy message listener container", ex);
			}
		}
	}
}
 
Example #15
Source Project: spring-analysis-note   Author: Vip-Augus   File: DisposableBeanAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 * @param bean the bean instance (never {@code null})
 * @param beanName the name of the bean
 * @param beanDefinition the merged bean definition
 * @param postProcessors the List of BeanPostProcessors
 * (potentially DestructionAwareBeanPostProcessor), if any
 */
public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition beanDefinition,
		List<BeanPostProcessor> postProcessors, @Nullable AccessControlContext acc) {

	Assert.notNull(bean, "Disposable bean must not be null");
	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean =
			(this.bean instanceof DisposableBean && !beanDefinition.isExternallyManagedDestroyMethod("destroy"));
	this.nonPublicAccessAllowed = beanDefinition.isNonPublicAccessAllowed();
	this.acc = acc;
	String destroyMethodName = inferDestroyMethodIfNecessary(bean, beanDefinition);
	if (destroyMethodName != null && !(this.invokeDisposableBean && "destroy".equals(destroyMethodName)) &&
			!beanDefinition.isExternallyManagedDestroyMethod(destroyMethodName)) {
		this.destroyMethodName = destroyMethodName;
		this.destroyMethod = determineDestroyMethod(destroyMethodName);
		if (this.destroyMethod == null) {
			if (beanDefinition.isEnforceDestroyMethod()) {
				throw new BeanDefinitionValidationException("Could not find a destroy method named '" +
						destroyMethodName + "' on bean with name '" + beanName + "'");
			}
		}
		else {
			Class<?>[] paramTypes = this.destroyMethod.getParameterTypes();
			if (paramTypes.length > 1) {
				throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
						beanName + "' has more than one parameter - not supported as destroy method");
			}
			else if (paramTypes.length == 1 && boolean.class != paramTypes[0]) {
				throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
						beanName + "' has a non-boolean parameter - not supported as destroy method");
			}
		}
	}
	this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
 
Example #16
Source Project: spring-analysis-note   Author: Vip-Augus   File: DisposableBeanAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 * @param bean the bean instance (never {@code null})
 * @param postProcessors the List of BeanPostProcessors
 * (potentially DestructionAwareBeanPostProcessor), if any
 */
public DisposableBeanAdapter(Object bean, List<BeanPostProcessor> postProcessors, AccessControlContext acc) {
	Assert.notNull(bean, "Disposable bean must not be null");
	this.bean = bean;
	this.beanName = bean.getClass().getName();
	this.invokeDisposableBean = (this.bean instanceof DisposableBean);
	this.nonPublicAccessAllowed = true;
	this.acc = acc;
	this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
 
Example #17
Source Project: flowable-engine   Author: flowable   File: JmsChannelModelProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void unregisterChannelModel(ChannelModel channelModel, String tenantId, EventRepositoryService eventRepositoryService) {
    String endpointId = getEndpointId(channelModel,tenantId);
    // currently it is not possible to unregister a listener container
    // In order not to do a lot of the logic that Spring does we are manually accessing the containers to remove them
    // see https://github.com/spring-projects/spring-framework/issues/24228
    MessageListenerContainer listenerContainer = endpointRegistry.getListenerContainer(endpointId);
    if (listenerContainer != null) {
        listenerContainer.stop();
    }

    if (listenerContainer instanceof DisposableBean) {
        try {
            ((DisposableBean) listenerContainer).destroy();
        } catch (Exception e) {
            throw new RuntimeException("Failed to destroy listener container", e);
        }
    }

    Field listenerContainersField = ReflectionUtils.findField(endpointRegistry.getClass(), "listenerContainers");
    if (listenerContainersField != null) {
        listenerContainersField.setAccessible(true);
        @SuppressWarnings("unchecked")
        Map<String, MessageListenerContainer> listenerContainers = (Map<String, MessageListenerContainer>) ReflectionUtils.getField(listenerContainersField, endpointRegistry);
        if (listenerContainers != null) {
            listenerContainers.remove(endpointId);
        }
    } else {
        throw new IllegalStateException("Endpoint registry " + endpointRegistry + " does not have listenerContainers field");
    }
}
 
Example #18
Source Project: spring-analysis-note   Author: Vip-Augus   File: DisposableBeanAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Check whether the given bean has any kind of destroy method to call.
 * @param bean the bean instance
 * @param beanDefinition the corresponding bean definition
 */
public static boolean hasDestroyMethod(Object bean, RootBeanDefinition beanDefinition) {
	if (bean instanceof DisposableBean || bean instanceof AutoCloseable) {
		return true;
	}
	String destroyMethodName = beanDefinition.getDestroyMethodName();
	if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName)) {
		return (ClassUtils.hasMethod(bean.getClass(), CLOSE_METHOD_NAME) ||
				ClassUtils.hasMethod(bean.getClass(), SHUTDOWN_METHOD_NAME));
	}
	return StringUtils.hasLength(destroyMethodName);
}
 
Example #19
Source Project: alibaba-rsocket-broker   Author: alibaba   File: MainLayout.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void destroy() throws Exception {
    for (Component value : tab2Workspace.values()) {
        if (value instanceof DisposableBean) {
            ((DisposableBean) value).destroy();
        }
    }
}
 
Example #20
@Override
public void destroy() throws Exception {
    if (executor instanceof DisposableBean) {
        DisposableBean bean = (DisposableBean) executor;
        bean.destroy();
    }
}
 
Example #21
Source Project: java-technology-stack   Author: codeEngraver   File: JmsListenerEndpointRegistry.java    License: MIT License 5 votes vote down vote up
@Override
public void destroy() {
	for (MessageListenerContainer listenerContainer : getListenerContainers()) {
		if (listenerContainer instanceof DisposableBean) {
			try {
				((DisposableBean) listenerContainer).destroy();
			}
			catch (Throwable ex) {
				logger.warn("Failed to destroy message listener container", ex);
			}
		}
	}
}
 
Example #22
Source Project: java-technology-stack   Author: codeEngraver   File: DisposableBeanAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 * @param bean the bean instance (never {@code null})
 * @param beanName the name of the bean
 * @param beanDefinition the merged bean definition
 * @param postProcessors the List of BeanPostProcessors
 * (potentially DestructionAwareBeanPostProcessor), if any
 */
public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition beanDefinition,
		List<BeanPostProcessor> postProcessors, @Nullable AccessControlContext acc) {

	Assert.notNull(bean, "Disposable bean must not be null");
	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean =
			(this.bean instanceof DisposableBean && !beanDefinition.isExternallyManagedDestroyMethod("destroy"));
	this.nonPublicAccessAllowed = beanDefinition.isNonPublicAccessAllowed();
	this.acc = acc;
	String destroyMethodName = inferDestroyMethodIfNecessary(bean, beanDefinition);
	if (destroyMethodName != null && !(this.invokeDisposableBean && "destroy".equals(destroyMethodName)) &&
			!beanDefinition.isExternallyManagedDestroyMethod(destroyMethodName)) {
		this.destroyMethodName = destroyMethodName;
		this.destroyMethod = determineDestroyMethod(destroyMethodName);
		if (this.destroyMethod == null) {
			if (beanDefinition.isEnforceDestroyMethod()) {
				throw new BeanDefinitionValidationException("Could not find a destroy method named '" +
						destroyMethodName + "' on bean with name '" + beanName + "'");
			}
		}
		else {
			Class<?>[] paramTypes = this.destroyMethod.getParameterTypes();
			if (paramTypes.length > 1) {
				throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
						beanName + "' has more than one parameter - not supported as destroy method");
			}
			else if (paramTypes.length == 1 && boolean.class != paramTypes[0]) {
				throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
						beanName + "' has a non-boolean parameter - not supported as destroy method");
			}
		}
	}
	this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
 
Example #23
Source Project: java-technology-stack   Author: codeEngraver   File: DisposableBeanAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 * @param bean the bean instance (never {@code null})
 * @param postProcessors the List of BeanPostProcessors
 * (potentially DestructionAwareBeanPostProcessor), if any
 */
public DisposableBeanAdapter(Object bean, List<BeanPostProcessor> postProcessors, AccessControlContext acc) {
	Assert.notNull(bean, "Disposable bean must not be null");
	this.bean = bean;
	this.beanName = bean.getClass().getName();
	this.invokeDisposableBean = (this.bean instanceof DisposableBean);
	this.nonPublicAccessAllowed = true;
	this.acc = acc;
	this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
 
Example #24
Source Project: java-technology-stack   Author: codeEngraver   File: DisposableBeanAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Check whether the given bean has any kind of destroy method to call.
 * @param bean the bean instance
 * @param beanDefinition the corresponding bean definition
 */
public static boolean hasDestroyMethod(Object bean, RootBeanDefinition beanDefinition) {
	if (bean instanceof DisposableBean || bean instanceof AutoCloseable) {
		return true;
	}
	String destroyMethodName = beanDefinition.getDestroyMethodName();
	if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName)) {
		return (ClassUtils.hasMethod(bean.getClass(), CLOSE_METHOD_NAME) ||
				ClassUtils.hasMethod(bean.getClass(), SHUTDOWN_METHOD_NAME));
	}
	return StringUtils.hasLength(destroyMethodName);
}
 
Example #25
Source Project: cola-cloud   Author: leecho   File: AsyncExceptionHandlingAsyncTaskExecutor.java    License: MIT License 5 votes vote down vote up
@Override
public void destroy() throws Exception {
    if (executor instanceof DisposableBean) {
        DisposableBean bean = (DisposableBean) executor;
        bean.destroy();
    }
}
 
Example #26
Source Project: paascloud-master   Author: paascloud   File: ExceptionHandlingAsyncTaskExecutor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Destroy.
 *
 * @throws Exception the exception
 */
@Override
public void destroy() throws Exception {
	if (executor instanceof DisposableBean) {
		DisposableBean bean = (DisposableBean) executor;
		bean.destroy();
	}
}
 
Example #27
Source Project: spring-cloud-aws   Author: spring-cloud   File: RedisCacheFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void createCache_withMockedRedisConnectionFactory_createsAndDestroysConnectionFactory()
		throws Exception {
	// Arrange
	RedisConnectionFactory connectionFactory = Mockito.mock(
			RedisConnectionFactory.class,
			Mockito.withSettings().extraInterfaces(DisposableBean.class));
	RedisCacheFactory redisCacheFactory = new RedisCacheFactory() {

		@Override
		protected RedisConnectionFactory createConnectionClient(String hostName,
				int port) {
			assertThat(hostName).isEqualTo("someHost");
			assertThat(port).isEqualTo(4711);
			return connectionFactory;
		}
	};

	// Act
	Cache cache = redisCacheFactory.createCache("test", "someHost", 4711);
	redisCacheFactory.destroy();

	// Assert
	assertThat(cache).isNotNull();
	assertThat(cache.getName()).isEqualTo("test");
	assertThat(cache.getNativeCache()).isNotNull();

	DisposableBean disposableBean = (DisposableBean) connectionFactory;
	Mockito.verify(disposableBean, Mockito.times(1)).destroy();
}
 
Example #28
Source Project: lams   Author: lamsfoundation   File: DisposableBeanAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 * @param bean the bean instance (never {@code null})
 * @param beanName the name of the bean
 * @param beanDefinition the merged bean definition
 * @param postProcessors the List of BeanPostProcessors
 * (potentially DestructionAwareBeanPostProcessor), if any
 */
public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition beanDefinition,
		List<BeanPostProcessor> postProcessors, AccessControlContext acc) {

	Assert.notNull(bean, "Disposable bean must not be null");
	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean =
			(this.bean instanceof DisposableBean && !beanDefinition.isExternallyManagedDestroyMethod("destroy"));
	this.nonPublicAccessAllowed = beanDefinition.isNonPublicAccessAllowed();
	this.acc = acc;
	String destroyMethodName = inferDestroyMethodIfNecessary(bean, beanDefinition);
	if (destroyMethodName != null && !(this.invokeDisposableBean && "destroy".equals(destroyMethodName)) &&
			!beanDefinition.isExternallyManagedDestroyMethod(destroyMethodName)) {
		this.destroyMethodName = destroyMethodName;
		this.destroyMethod = determineDestroyMethod();
		if (this.destroyMethod == null) {
			if (beanDefinition.isEnforceDestroyMethod()) {
				throw new BeanDefinitionValidationException("Couldn't find a destroy method named '" +
						destroyMethodName + "' on bean with name '" + beanName + "'");
			}
		}
		else {
			Class<?>[] paramTypes = this.destroyMethod.getParameterTypes();
			if (paramTypes.length > 1) {
				throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
						beanName + "' has more than one parameter - not supported as destroy method");
			}
			else if (paramTypes.length == 1 && boolean.class != paramTypes[0]) {
				throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
						beanName + "' has a non-boolean parameter - not supported as destroy method");
			}
		}
	}
	this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
 
Example #29
Source Project: lams   Author: lamsfoundation   File: DisposableBeanAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 * @param bean the bean instance (never {@code null})
 * @param postProcessors the List of BeanPostProcessors
 * (potentially DestructionAwareBeanPostProcessor), if any
 */
public DisposableBeanAdapter(Object bean, List<BeanPostProcessor> postProcessors, AccessControlContext acc) {
	Assert.notNull(bean, "Disposable bean must not be null");
	this.bean = bean;
	this.beanName = null;
	this.invokeDisposableBean = (this.bean instanceof DisposableBean);
	this.nonPublicAccessAllowed = true;
	this.acc = acc;
	this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
 
Example #30
Source Project: lams   Author: lamsfoundation   File: DisposableBeanAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check whether the given bean has any kind of destroy method to call.
 * @param bean the bean instance
 * @param beanDefinition the corresponding bean definition
 */
public static boolean hasDestroyMethod(Object bean, RootBeanDefinition beanDefinition) {
	if (bean instanceof DisposableBean || closeableInterface.isInstance(bean)) {
		return true;
	}
	String destroyMethodName = beanDefinition.getDestroyMethodName();
	if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName)) {
		return (ClassUtils.hasMethod(bean.getClass(), CLOSE_METHOD_NAME) ||
				ClassUtils.hasMethod(bean.getClass(), SHUTDOWN_METHOD_NAME));
	}
	return StringUtils.hasLength(destroyMethodName);
}