[jboss-cvs] jboss-seam/examples/spring/src/org/jboss/seam/ioc ...

Ales Justin ales.justin at genera-lynx.com
Fri Feb 9 12:37:18 EST 2007


  User: alesj   
  Date: 07/02/09 12:37:18

  Added:       examples/spring/src/org/jboss/seam/ioc   ProxyUtils.java
                        IoCComponent.java
  Log:
  Added youngm's Spring code + my initial MC code (need to write scopes first in MC in order to fully support integration with Seam).
  
  Revision  Changes    Path
  1.1      date: 2007/02/09 17:37:18;  author: alesj;  state: Exp;jboss-seam/examples/spring/src/org/jboss/seam/ioc/ProxyUtils.java
  
  Index: ProxyUtils.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2006, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This 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 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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 this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.seam.ioc;
  
  import java.lang.reflect.Proxy;
  import java.util.Set;
  
  import net.sf.cglib.proxy.Enhancer;
  import org.jboss.seam.intercept.JavaBeanInterceptor;
  import org.jboss.seam.log.LogProvider;
  import org.jboss.seam.log.Logging;
  
  /**
   * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
   */
  public class ProxyUtils
  {
     private static final LogProvider log = Logging.getLogProvider(ProxyUtils.class);
  
     public static boolean isProxy(Class clazz)
     {
        return Proxy.isProxyClass(clazz);
     }
  
     public static Object enhance(Object bean, Set<Class> interfaces, IoCComponent component) throws Exception
     {
        Class beanClass = bean.getClass();
        if (isProxy(beanClass))
        {
           throw new RuntimeException("Seam cannot wrap JDK proxied IoC beans. Please use CGLib or Javassist proxying instead");
        }
        //
        if (isCglibProxyClass(beanClass))
        {
           beanClass = beanClass.getSuperclass();
        }
        else if (isJavassistProxyClass(beanClass))
        {
           // todo
        }
        if (log.isDebugEnabled())
        {
           log.debug("Creating proxy for " + component.getIoCName() + " Seam component '"
                 + component.getName() + "' using class: " + beanClass.getName());
        }
        // create pojo proxy
        JavaBeanInterceptor interceptor = new JavaBeanInterceptor(bean, component);
        // Should probably create a Factory but required a lot of duplicated
        // code and there is potential for a spring bean to provide
        // different interfaces at different times in an application. If
        // need is great I can create a Factory and assume the same
        // interfaces all the time.
        bean = Enhancer.create(beanClass, interfaces.toArray(new Class[interfaces.size()]), interceptor);
        interceptor.postConstruct();
        return bean;
     }
  
     public static boolean isCglibProxyClass(Class clazz)
     {
        return clazz != null && clazz.getName().contains("$$");
     }
  
     public static boolean isJavassistProxyClass(Class clazz)
     {
        return clazz != null && clazz.getName().contains("$$");
     }
  
  }
  
  
  
  1.1      date: 2007/02/09 17:37:18;  author: alesj;  state: Exp;jboss-seam/examples/spring/src/org/jboss/seam/ioc/IoCComponent.java
  
  Index: IoCComponent.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2006, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This 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 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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 this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.seam.ioc;
  
  import java.util.Arrays;
  import java.util.HashSet;
  import java.util.Set;
  import javax.servlet.http.HttpSessionActivationListener;
  
  import org.jboss.seam.Component;
  import org.jboss.seam.InterceptionType;
  import org.jboss.seam.ScopeType;
  import org.jboss.seam.core.Mutable;
  import org.jboss.seam.log.LogProvider;
  import org.jboss.seam.log.Logging;
  
  /**
   * An extension of Component that allows external IoC
   * to provide the base instance for a seam component.
   *
   * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
   */
  public abstract class IoCComponent extends Component
  {
     protected final LogProvider log = Logging.getLogProvider(getClass());
  
     /**
      * Creates a Spring Seam Component given a beanFactory.
      *
      * @param clazz   class
      * @param name    component name
      * @param scope   component scope
      */
     public IoCComponent(Class clazz, String name, ScopeType scope)
     {
        super(clazz, name, scope, null);
     }
  
     protected abstract String getIoCName();
  
     protected abstract Object instantiateIoCBean() throws Exception;
  
     /**
      * Instantiates a IoC bean and provides it as a java bean to be wrapped
      * by seam.
      *
      * @see org.jboss.seam.Component#instantiateJavaBean()
      */
     protected Object instantiateJavaBean() throws Exception
     {
        Object bean = instantiateIoCBean();
        // initialize the bean following Component.instantiateJavaBean()'s
        // pattern.
        if (getInterceptionType() == InterceptionType.NEVER)
        {
           initialize(bean);
           callPostConstructMethod(bean);
        }
        else
        {
           // Add all of the interfaces of the bean instance into the Seam
           // proxy bean because spring's proxies add a bunch of interfaces too
           // that should be accessible.
           Set<Class> interfaces = new HashSet<Class>(Arrays.asList(bean.getClass().getInterfaces()));
           interfaces.add(HttpSessionActivationListener.class);
           interfaces.add(Mutable.class);
           // enhance bean
           bean = ProxyUtils.enhance(bean, interfaces, this);
        }
        return bean;
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list