package org.codehaus.xfire.spring;

import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.WebAnnotations;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.ServiceRegistry;
import org.codehaus.xfire.service.invoker.BeanInvoker;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * This BeanPostProcessor will create an XFire service from a service class if and only if
 * it is marked with the JSR 181 {@lit @}WebService attribute. This replaces the need for using the
 * ServiceComponent class.
 * 
 * @see org.codehaus.xfire.spring.ServiceBean
 * @author Jason Carreira <jcarreira@eplus.com>
 */
public class Jsr181BeanPostProcessor
    implements BeanPostProcessor
{
    private WebAnnotations annotations;

    private AnnotationServiceFactory serviceFactory;

    private ServiceRegistry registry;

    public Jsr181BeanPostProcessor(WebAnnotations annotations,
            AnnotationServiceFactory serviceFactory, ServiceRegistry registry)
    {
        this.annotations = annotations;
        this.serviceFactory = serviceFactory;
        this.registry = registry;
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName)
        throws BeansException
    {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName)
        throws BeansException
    {
        Class clazz = bean.getClass();
        if (annotations.hasWebServiceAnnotation(clazz))
        {
            Service service = serviceFactory.create(clazz);
            service.setInvoker(new BeanInvoker(bean));
            registry.register(service);
        }
        return bean;
    }
}

@@ -345,16 +345,69 @@
 eption;%0A
+import org.springframework.beans.FatalBeanException;%0A
 import o
@@ -1542,26 +1542,79 @@
     
-Class clazz = 
+try%0A        %7B%0A            Class clazz = SpringUtils.getUserTarget(
 bean
+)
 .get
@@ -1630,16 +1630,20 @@
         
+  
+  
 if (anno
@@ -1682,32 +1682,36 @@
 clazz))%0A        
+    
 %7B%0A            Se
@@ -1708,16 +1708,20 @@
         
+ 
+   
 Service 
@@ -1760,16 +1760,20 @@
 clazz);%0A
+    
         
@@ -1831,16 +1831,20 @@
         
+    
 registry
@@ -1871,18 +1871,26 @@
         
-%7D%0A
+    %7D%0A    
         
@@ -1898,20 +1898,157 @@
 eturn bean;%0A
+        %7D%0A        catch (Exception e)%0A        %7B%0A            throw new FatalBeanException(%22Could not get the target bean.%22, e);%0A        %7D%0A
     %7D%0A%7D%0A

get the underlying proxied object