/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.webbeans.ejb.common.util;

import javax.enterprise.context.spi.CreationalContext;

import javassist.util.proxy.ProxyFactory;

import org.apache.webbeans.ejb.common.component.BaseEjbBean;
import org.apache.webbeans.ejb.common.component.EjbBeanCreatorImpl;
import org.apache.webbeans.ejb.common.proxy.EjbBeanProxyHandler;
import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.proxy.JavassistProxyFactory;
import org.apache.webbeans.util.ClassUtil;

/**
 * @version $Rev: 917060 $ $Date: 2010-02-28 00:14:47 +0200 (Sun, 28 Feb 2010) $
 */
public final class EjbDefinitionUtility
{
    private EjbDefinitionUtility()
    {
        
    }

    @SuppressWarnings("unchecked")
    public static void defineApiType(BaseEjbBean<?> ejbComponent)
    {        
        EjbBeanCreatorImpl<?> creator = new EjbBeanCreatorImpl(ejbComponent);
        creator.defineApiType();
    }
    
    @SuppressWarnings({"unchecked","deprecation"})
    public static <T> T defineEjbBeanProxy(BaseEjbBean<T> bean, Class<?> iface, CreationalContext<?> creationalContext)
    {
        try
        {
            bean.setIface(iface);
            
            Class<?> clazz = JavassistProxyFactory.getInstance().getEjbBeanProxyClass(bean);
            if(clazz != null)
            {
                return (T)ClassUtil.newInstance(clazz);
            }
            
            //Proxy factory instance
            ProxyFactory factory = new ProxyFactory();            
            
            EjbBeanProxyHandler handler = new EjbBeanProxyHandler(bean,creationalContext);            
            factory.setHandler(handler);
            factory.setInterfaces(new Class[]{iface});
         
            return (T)(JavassistProxyFactory.getInstance().defineEjbBeanProxyClass(bean, factory).newInstance());
            
        }
        catch(Exception e)
        {
            throw new WebBeansException(e);
        }
    }
}

@@ -938,16 +938,57 @@
 Factory;
+%0Aimport javassist.util.proxy.ProxyObject;
 %0A%0Aimport
@@ -1290,51 +1290,8 @@
 ory;
-%0Aimport org.apache.webbeans.util.ClassUtil;
 %0A%0A/*
@@ -1904,32 +1904,68 @@
   try%0A        %7B%0A
+            T proxyInstance = null;%0A
             bean
@@ -2113,9 +2113,9 @@
 azz 
-!
+=
 = nu
@@ -2152,45 +2152,48 @@
     
-return (T)ClassUtil.newInstance(clazz
+ProxyFactory factory = new ProxyFactory(
 );%0A 
@@ -2204,117 +2204,158 @@
     
-   %7D%0A
        
-     %0A            //Proxy factory instance%0A            ProxyFactory factory = new ProxyF
+factory.setInterfaces(new Class%5B%5D%7Biface%7D);%0A                clazz = JavassistProxyFactory.getInstance().defineEjbBeanProxyClass(bean, f
 actory
-(
 );
+%0A
     
@@ -2354,32 +2354,33 @@
 y);%0A            
+%7D
 %0A            %0A  
@@ -2369,32 +2369,85 @@
  %7D%0A            %0A
+            proxyInstance = (T) clazz.newInstance();%0A
             EjbB
@@ -2501,16 +2501,17 @@
 er(bean,
+ 
 creation
@@ -2521,28 +2521,16 @@
 ontext);
-            
 %0A       
@@ -2530,31 +2530,52 @@
             
-factory
+((ProxyObject)proxyInstance)
 .setHandler(
@@ -2600,170 +2600,41 @@
     
-factory.setInterfaces(new Class%5B%5D%7Biface%7D);%0A         %0A            return (T)(JavassistProxyFactory.getInstance().defineEjbBeanProxyClass(bean, factory).new
+%0A            return proxy
 Instance
 ());
@@ -2633,11 +2633,8 @@
 ance
-())
 ;%0A  

use new MethodHandler for each proxy instance for EJBs.