/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2006 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Copyright (C) 2007 The OpenNMS Group, Inc.  All rights reserved.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 *      OpenNMS Licensing       <license@opennms.org>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 */
package org.opennms.core.utils;

import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.access.DefaultLocatorFactory;

/**
 * Helper methods for working with Spring beans.
 * 
 * @author <a href="mailto:dj@opennms.org">DJ Gregor</a>
 */
public class BeanUtils {
    /**
     * Don't allow this class to be instantiated.  All methods are static.
     */
    private BeanUtils() {
    }

    /**
     * Get a Spring BeanFactory by context ID.
     * 
     * @param contextId the context ID of the BeanFactory to fetch
     * @return the BeanFactory
     */
    public static BeanFactoryReference getFactory(String contextId) {
        BeanFactoryLocator beanFactoryLoader = DefaultLocatorFactory.getInstance();
        return beanFactoryLoader.useBeanFactory(contextId);
    }

    /**
     * Get a Spring bean by name.  Uses Java 5 generics to cast the returned
     * class to the appropriate type.
     * 
     * @param <T> class for the returned bean
     * @param beanFactory bean factory to use to fetch this bean
     * @param beanId ID of the bean to fetch
     * @param clazz class representing the type for the returned bean
     * @return bean for given bean ID casted to the appropriate class
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(BeanFactoryReference beanFactory, String beanId, Class<T> clazz) {
        return (T) beanFactory.getFactory().getBean(beanId);
    }
    
    /**
     * Helper method that combines getFactory and getBean.
     * 
     * @param <T> class for the returned bean
     * @param contextId the context ID of the BeanFactory from which to fetch
     *      this bean
     * @param beanId ID of the bean to fetch
     * @param clazz class representing the type for the returned bean
     * @return bean for given bean ID casted to the appropriate class
     */
    public static <T> T getBean(String contextId, String beanId, Class<T> clazz) {
        return getBean(getFactory(contextId), beanId, clazz);
    }
}

@@ -2075,24 +2075,28 @@
 eference get
+Bean
 Factory(Stri
@@ -2107,24 +2107,24 @@
 ontextId) %7B%0A
-
         Bean
@@ -2949,16 +2949,20 @@
 ines get
+Bean
 Factory 
@@ -3430,16 +3430,20 @@
 Bean(get
+Bean
 Factory(
@@ -3476,10 +3476,540 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * Helper method that calls getBeanFactory(contextId).getFactory()%0A     * and casts the result.%0A     * %0A     * @param %3CT%3E class for the returned factory%0A     * @param contextId the context ID of the BeanFactory to fetch%0A     * @param clazz class representing the type for the returned factory%0A     * @return the factory casted to %3CT%3E%0A     */%0A    @SuppressWarnings(%22unchecked%22)%0A    public static %3CT%3E T getFactory(String contextId, Class%3CT%3E clazz) %7B%0A        return (T) getBeanFactory(contextId).getFactory();%0A    %7D%0A
 %7D%0A

Rename getFactory to getBeanFactory and add a getFactory method that calls getBeanFactory(contextId).getFactory()