Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 Eric Dalquist Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package com.googlecode.shutdownlistener.spring;
Utility that wraps an org.springframework.context.ApplicationContext to handle shutting down. If the provided context contains a ShutdownHandlerBean it is used, if not a child org.springframework.context.ApplicationContext is created and a ShutdownHandlerBean is registered there. To use create a new instance and then call waitForShutdown() to wait for a shutdown call to the ShutdownHandlerBean's socket.

Author(s):
Eric Dalquist
Version:
$Revision: 2 $
public final class ApplicationContextShutdownWrapper {
    private final ApplicationContext applicationContext;
    private final DisposableBean disposableBean;
    private final ShutdownHandler shutdownHandler;
    public ApplicationContextShutdownWrapper(ApplicationContext applicationContext) {
        Assert.notNull(applicationContext"ApplicationContext cannot be null");
        this. = applicationContext;
        if (!(applicationContext instanceof DisposableBean)) {
            throw new IllegalArgumentException("The provided BeanFactory must implement DisposableBean");
        }
        this. = (DisposableBean)this.;
        ShutdownHandler shutdownHandler = null;
        try {
            shutdownHandler = this..getBean(ShutdownHandlerBean.class);
        }
        catch (NoSuchBeanDefinitionException e) {
            //Ignore
        }
        
        if (shutdownHandler != null) {
            this. = shutdownHandler;
            this. = null;
        }
        else {
            this. = new GenericApplicationContext(this.);
            
            final GenericBeanDefinition shutdownBeanDefinition = new GenericBeanDefinition();
            shutdownBeanDefinition.setBeanClass(ShutdownHandlerBean.class);
            
            this..registerBeanDefinition(ShutdownHandler.class.getName(), shutdownBeanDefinition);
            this..refresh();
            this. = this..getBean(ShutdownHandler.class);
        }
    }

    
    public void waitForShutdown() throws Exception {
        this..waitForShutdown();
        this..destroy();
        if (this. != null) {
            this..destroy();
        }
    }
New to GrepCode? Check out our FAQ X