pt.esper.SpringManager.java Source code

Java tutorial

Introduction

Here is the source code for pt.esper.SpringManager.java

Source

/**
 * Copyright 2010 Google Inc.
    
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
    
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 **/
package pt.esper;

import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * This class is the integration point with the Spring framework. It manages and
 * bootstraps spring.
 */
public class SpringManager {

    private static SpringManager instance;
    private static FileSystemXmlApplicationContext applicationContext;
    private boolean initialized = false;

    private SpringManager() {
        System.out.println("Spring Manager Created");
    }

    public static SpringManager getInstance() {
        if (instance == null) {
            instance = new SpringManager();
        }
        return instance;
    }

    public void initialize() {
        System.out.println("Initializing SpringManager.");
        try {
            applicationContext = new FileSystemXmlApplicationContext(
                    new String[] { "src/main/resources/config/spring.xml" });
            applicationContext.registerShutdownHook();
        } catch (Exception e) {
            System.out.println("Exception while loading Application Context XML File: " + e.toString());
            System.exit(1);
        }
        initialized = true;
        System.out.println("Initialized successfully");
    }

    public void start() {
        if (!initialized || applicationContext == null) {
            throw new IllegalStateException("Please initialize the SpringManager before starting it.");
        }
        applicationContext.start();

        System.out.println("Spring Manager started");

    }

    public void shutdown() {
        System.out.println(" Service was shutdown");
    }

    public Object getBean(String name) {
        if (!initialized || applicationContext == null) {
            throw new IllegalStateException("Please initialized the Spring Manager first.");
        }
        return applicationContext.getBean(name);
    }

}