/*
 * ProxyExpander.java  $Revision: 1.3 $  $Date: 2007/10/03 04:39:40 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold 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.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.meldcraft.application.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.meldcraft.application.ManagedElementFactory;
import com.meldcraft.util.AbstractStringExpander;
import com.meldcraft.util.ReflectUtil;
import com.meldcraft.util.StringUtil;

/**
 * @version $Revision: 1.3 $ $Date: 2007/10/03 04:39:40 $
 * @author Rob Richards
 */
public class ProxyExpander extends AbstractStringExpander
{
    private BeanContext mBeanContext;

    public ProxyExpander(BeanContext context)
    {
        super("<proxy");
        mBeanContext = context;
    }

    public boolean expandProxy(String str)
    {
        return expand(str);
    }

    @Override
    protected Object expandPart(String elementId, String properties)
    {
        Object ret = null;

        // <proxy>
        Class type = null;
        Method[] methods = ReflectUtil.findMethods(mBeanContext.getTarget(),
                                                   mBeanContext.getTargetProperty());
        if (methods != null)
        {
            for (int i=0 ; i < methods.length ; i++)
            {
                Class[] types = methods[i].getParameterTypes();
                if ((types != null) && (types.length == 1))
                {
                    if (types[0].isInterface())
                    {
                        type = types[0];
                        break;
                    }
                }
            }
        }

        if (type != null)
        {
            Map defaults = new HashMap();
            defaults.put("className", DefaultApplicationContextProxy.class.getName());
            defaults.put("interfaces", type.getName());
            defaults.put("interfaceMethodProperties", properties);

            if (StringUtil.isEmpty(elementId))
            {
                elementId = mBeanContext.getTargetProperty() + "Proxy";
                if (mBeanContext.getTargetElementId() != null)
                {
                    elementId = mBeanContext.getTargetElementId() + "." + elementId;
                }
            }

            ret = ManagedElementFactory.getInstance(mBeanContext.getApplicationContext())
                                       .createManagedElement(elementId, defaults, null);
        }
        return ret;
    }

}



