All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.jpattern.ioc.ContextBeanFactory Maven / Gradle / Ivy

package com.googlecode.jpattern.ioc;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.googlecode.jpattern.ioc.exception.ConfigException;
import com.googlecode.jpattern.ioc.property.NullPropertyReader;
import com.googlecode.jpattern.ioc.reflection.ReflectionUtil;
import com.googlecode.jpattern.ioc.xml.Context;
import com.googlecode.jpattern.ioc.xstream.XStreamReader;

/**
 * @author Claudio Quaresima - [email protected] - 24/ott/08 11:31:30
 * @version $Id: $
 */
public final class ContextBeanFactory {

    private AbstractReader _reader = new XStreamReader();
    private Context _context = new Context();
    private BeanMapper _beanMapper = new BeanMapper();
    private ReflectionUtil _reflectionUtil = new ReflectionUtil();
    private IContextCreator _icontextCreator;

    public ContextBeanFactory(String aConfigFile) throws ConfigException {
        try {
			_context.init(_reader, new FileInputStream(new File(aConfigFile)));
	        _icontextCreator = new ContextCreator(_reflectionUtil, _beanMapper, _context);
	        createPropertyReader();
		} catch (IOException e) {
			throw new ConfigException(e);
		}
    }

    public ContextBeanFactory(InputStream aInputStream) throws ConfigException {
        try {
			_context.init(_reader, aInputStream);
	        _icontextCreator = new ContextCreator(_reflectionUtil, _beanMapper, _context);
	        createPropertyReader();
		} catch (IOException e) {
			throw new ConfigException(e);
		}
    }

    public Object get(String aBeanIdName) throws ConfigException {
        try {
            return _icontextCreator.create(aBeanIdName);
        }
        catch (ClassNotFoundException e) {
            throw new ConfigException(e);
        }
    }

    private void createPropertyReader() {
        IPropertyReader replacer;
        try {
            replacer = (IPropertyReader) _icontextCreator.create(IContextCreator.REPLACER_BEAN_ID);
            replacer.load();
        }
        catch (Exception e) {
            replacer = new NullPropertyReader();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy