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

org.milyn.javabean.dynamic.ext.BeanWriterFactory Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
 * Milyn - Copyright (C) 2006 - 2010
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License (version 2.1) as published by the Free Software
 *  Foundation.
 *
 *  This library 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 Lesser General Public License for more details:
 *  http://www.gnu.org/licenses/lgpl.txt
 */

package org.milyn.javabean.dynamic.ext;

import org.milyn.cdr.SmooksConfigurationException;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.cdr.annotation.AppContext;
import org.milyn.cdr.annotation.Config;
import org.milyn.cdr.annotation.ConfigParam;
import org.milyn.cdr.annotation.Configurator;
import org.milyn.container.ApplicationContext;
import org.milyn.delivery.ContentHandler;
import org.milyn.delivery.annotation.Initialize;
import org.milyn.javabean.dynamic.serialize.BeanWriter;
import org.milyn.javabean.ext.BeanConfigUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * {@link BeanWriter} Factory.
 *
 * [email protected]
 */
public class BeanWriterFactory implements ContentHandler {

    @ConfigParam
    private String beanId;
    @ConfigParam(name = "class")
    private Class beanWriterClass;
    @ConfigParam(name = BeanConfigUtil.BEAN_CLASS_CONFIG)
    private Class beanClass;
    @Config
    private SmooksResourceConfiguration config;
    @AppContext
    private ApplicationContext appContext;

    @Initialize
    public void createBeanWriter() {
        try {
            BeanWriter beanWriter = beanWriterClass.newInstance();

            Configurator.configure(beanWriter, config, appContext);
            getBeanWriters(beanClass, appContext).put(config.getSelectorNamespaceURI(), beanWriter);            
        } catch (InstantiationException e) {
            throw new SmooksConfigurationException("Unable to create BeanWriter instance.", e);
        } catch (IllegalAccessException e) {
            throw new SmooksConfigurationException("Unable to create BeanWriter instance.", e);
        }
    }

    public static Map getBeanWriters(Class beanClass, ApplicationContext appContext) {
        Map, Map> beanWriterMap = getBeanWriters(appContext);
        Map beanWriters = beanWriterMap.get(beanClass);

        if(beanWriters == null) {
            beanWriters = new LinkedHashMap();
            beanWriterMap.put(beanClass, beanWriters);
        }

        return beanWriters;
    }

    public static Map, Map> getBeanWriters(ApplicationContext appContext) {
        Map, Map> beanWriters = (Map, Map>) appContext.getAttribute(BeanWriter.class);

        if(beanWriters == null) {
            beanWriters = new HashMap, Map>();
            appContext.setAttribute(BeanWriter.class, beanWriters);
        }

        return beanWriters;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy