/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.framework;

import org.jfox.framework.extra.DynamicProxyInstantiatingComponentFactory;
import org.jfox.framework.extra.MethodInterceptable;
import org.jfox.framework.extra.NamedConstrComponentFactory;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ComponentMetaFactory {

    public static ComponentMeta getComponentMeta(Class implementation,
                                                 Parameter[] constrctCompParamPack,
                                                 ParameterPack[] methodComponentParamPack)
            throws ComponentException {
        ComponentFactory componentFactory = null;
//    new ConstructorComponentFactory(implementation,constrctCompParamPack);
        if(MethodInterceptable.class.isAssignableFrom(implementation)) {
            componentFactory = new DynamicProxyInstantiatingComponentFactory(implementation, constrctCompParamPack);
        }
        else {
            componentFactory = new ConstrComponentFactory(implementation, constrctCompParamPack);
        }
        if(methodComponentParamPack != null) {
            componentFactory = new PropertyComponentFactory((InstantiatingComponentFactory) componentFactory, methodComponentParamPack);
        }
        return new ComponentMeta(componentFactory);
    }

    public static ComponentMeta getComponentMeta(Class implementation,
                                                 Parameter[] constrctCompParamPack,
                                                 ParameterPack[] methodComponentParamPack,
                                                 boolean typeSingleton,
                                                 boolean singleton)
            throws ComponentException {
        ComponentMeta meta = getComponentMeta(implementation, constrctCompParamPack, methodComponentParamPack);
        meta.setTypeSingleton(typeSingleton);
        meta.setSingleton(singleton);
        return meta;
    }

    public static ComponentMeta getComponentMeta(Component component) {
        return new ComponentMeta(new InstanceComponentFactory(component));
    }

    public static ComponentMeta getComponentMeta(Component component, boolean typeSingleton) {
        ComponentMeta meta = getComponentMeta(component);
        meta.setTypeSingleton(typeSingleton);
        return meta;
    }

    public static ComponentMeta getComponentMeta(ComponentName name,
                                                 Parameter[] constrctCompParamPack,
                                                 ParameterPack[] methodComponentParamPack)
            throws ComponentException {
        ComponentFactory componentFactory = null;
        if(MethodInterceptable.class.isAssignableFrom(name.getImplementation())) {
            componentFactory = new DynamicProxyInstantiatingComponentFactory(name, constrctCompParamPack);
        }
        else {
            componentFactory = new NamedConstrComponentFactory(name, constrctCompParamPack);
        }
        if(methodComponentParamPack != null) {
            componentFactory = new PropertyComponentFactory((InstantiatingComponentFactory) componentFactory, methodComponentParamPack);
        }
        return new ComponentMeta(componentFactory);
    }

    public static ComponentMeta getComponentMeta(ComponentName name,
                                                 Parameter[] constrctCompParamPack,
                                                 ParameterPack[] methodComponentParamPack,
                                                 boolean typeSingleton,
                                                 boolean singleton)

            throws ComponentException {
        ComponentMeta meta = getComponentMeta(name, constrctCompParamPack, methodComponentParamPack);
        meta.setTypeSingleton(typeSingleton);
        meta.setSingleton(singleton);
        return meta;
    }

    public static void main(String[] args) {

    }
}

