[jboss-cvs] JBossAS SVN: r81883 - in projects/microcontainer/trunk: aop-mc-int/src/test/java/org/jboss/test/microcontainer/test and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Dec 1 05:00:11 EST 2008


Author: alesj
Date: 2008-12-01 05:00:11 -0500 (Mon, 01 Dec 2008)
New Revision: 81883

Added:
   projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java
   projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java
   projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.properties
   projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml
Modified:
   projects/microcontainer/trunk/kernel/src/main/java/org/jboss/beans/metadata/plugins/factory/GenericBeanFactory.java
Log:
[JBMICROCONT-397]; expose createBean method.
Port Kabir's tests.

Copied: projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java (from rev 81882, projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java)
===================================================================
--- projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java	                        (rev 0)
+++ projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java	2008-12-01 10:00:11 UTC (rev 81883)
@@ -0,0 +1,121 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.test.microcontainer.support;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.URLConnection;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ParentLastURLClassLoader extends URLClassLoader
+{
+   ClassLoader parent;
+   URLClassLoader delegate;
+
+   public ParentLastURLClassLoader(URL[] urls, ClassLoader parent)
+   {
+      super(urls, parent);
+      delegate = new URLClassLoader(urls);
+      this.parent = parent;
+   }
+
+   @Override
+   public URL findResource(String name)
+   {
+      URL url = delegate.findResource(name);
+      if (url == null && parent instanceof URLClassLoader)
+      {
+         url = ((URLClassLoader)parent).findResource(name);
+      }
+      return url;
+   }
+
+   @Override
+   public URL getResource(String name)
+   {
+      URL url = delegate.getResource(name);
+      if (url == null)
+      {
+         url = parent.getResource(name);
+      }
+      return url;
+   }
+
+   @Override
+   public Class<?> loadClass(String name) throws ClassNotFoundException
+   {
+      Class<?> clazz = findLoadedClass(name);
+      if (clazz != null)
+      {
+         return clazz;
+      }
+      String resourceName = name.replace('.', '/') + ".class";
+      URL mine = delegate.findResource(resourceName);
+      URL parents = parent.getResource(resourceName);
+      if (mine == null || mine.equals(parent))
+      {
+         return delegate.loadClass(name);
+      }
+      else
+      {
+         byte[] bytes = loadBytes(name, resourceName);
+         return defineClass(name, bytes, 0, bytes.length);
+      }
+   }
+
+   private byte[] loadBytes(String name, String resourceName)
+   {
+      InputStream in = delegate.getResourceAsStream(resourceName);
+      byte[] bytes = null;
+      try
+      {
+         ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         byte[] tmp = new byte[1024];
+         int read = 0;
+         while ( (read = in.read(tmp)) >= 0 )
+            baos.write(tmp, 0, read);
+         return baos.toByteArray();
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException("Unable to load class byte code " + name, e);
+      }
+      finally
+      {
+         try
+         {
+            in.close();
+         }
+         catch (IOException e)
+         {
+         }
+      }
+   }
+   
+}
\ No newline at end of file

Copied: projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java (from rev 81882, projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java)
===================================================================
--- projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java	                        (rev 0)
+++ projects/microcontainer/trunk/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java	2008-12-01 10:00:11 UTC (rev 81883)
@@ -0,0 +1,170 @@
+/*
+* 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.test.microcontainer.test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import junit.framework.Test;
+
+import org.jboss.aop.advice.AspectDefinition;
+import org.jboss.aop.advice.AspectFactory;
+import org.jboss.aop.advice.AspectFactoryWithClassLoader;
+import org.jboss.aop.microcontainer.beans.Aspect;
+import org.jboss.test.aop.junit.AOPMicrocontainerTest;
+import org.jboss.test.microcontainer.support.ParentLastURLClassLoader;
+import org.jboss.test.microcontainer.support.TestAspect;
+
+/**
+ * Checks that an aspect can have its classloader overridden
+ *
+ * @author <a href="mailto:kabir.khan at jboss.com">Kabir Khan</a>
+ */
+public class OverriddenClassLoaderTestCase extends AOPMicrocontainerTest
+{
+   public OverriddenClassLoaderTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(OverriddenClassLoaderTestCase.class);
+   }
+
+   public void testOverriddenLoader() throws Exception
+   {
+      ClassLoader loader = createParentLastURLClassLoader();
+      
+      Class<?> clazz = loader.loadClass(TestAspect.class.getName());
+      assertNotSame(TestAspect.class, clazz);
+      assertSame(loader, clazz.getClassLoader());
+      
+      Aspect aspect = assertInstanceOf(getBean("Aspect"), Aspect.class, false);     
+      AspectDefinition def = aspect.getDefinition();
+      AspectFactory factory = def.getFactory();
+      
+      Object global = factory.createPerVM();
+      assertSame(getClass().getClassLoader(), global.getClass().getClassLoader());
+
+      AspectFactoryWithClassLoader factoryCl = assertInstanceOf(factory, AspectFactoryWithClassLoader.class);
+      factoryCl.pushScopedClassLoader(loader);
+      try
+      {
+         Object scoped = factory.createPerVM();
+         ClassLoader scopedLoader = scoped.getClass().getClassLoader();
+         assertSame(loader, scopedLoader);
+      }
+      finally
+      {
+         factoryCl.popScopedClassLoader();
+      }
+   }
+   
+   private ClassLoader createParentLastURLClassLoader() throws Exception
+   {
+      File jarFile = createJar();
+      
+      URL jarUrl = jarFile.toURL();
+      return new ParentLastURLClassLoader(new URL[] {jarUrl}, getClass().getClassLoader());
+   }
+   
+   private File createJar() throws Exception
+   {
+      String resource = TestAspect.class.getName().replace('.', '/') + ".class";
+      URL url = getClass().getClassLoader().getResource(resource);
+      assertNotNull(resource + " not found.", url);
+      File classFile = new File(url.toURI());
+
+      //    File jarFile = new File("test-overridden-classloader.jar");
+      File jarFile = createTempFile("test-overridden-classloader", "jar");
+      jarFile.deleteOnExit();
+      
+      if (jarFile.exists())
+      {
+         assertTrue(jarFile.delete());
+      }
+      JarOutputStream out = null;
+      try
+      {
+         out = new JarOutputStream(new FileOutputStream(jarFile), new Manifest());
+         JarEntry entry = new JarEntry(resource);
+         out.putNextEntry(entry);
+         FileInputStream in = null;
+         try
+         {
+            in = new FileInputStream(classFile);
+            byte[] buf = new byte[200000];
+            while(true)
+            {
+               int i = in.read(buf, 0, buf.length);
+               if (i <=0)
+               {
+                  break;
+               }
+               out.write(buf, 0, i);
+            }
+         }
+         finally
+         {
+            in.close();
+         }
+      }
+      finally
+      {
+         out.close();
+      }
+      getLog().debug("===> Created jar " + jarFile.getAbsolutePath());
+      return jarFile;
+   }
+   
+   private File createTempFile(final String prefix, final String suffix) throws Exception
+   {
+      if (System.getSecurityManager() == null)
+         return File.createTempFile(prefix, suffix);
+      else
+      {
+         try
+         {
+            return AccessController.doPrivileged(new PrivilegedExceptionAction<File>()
+            {
+               public File run() throws Exception
+               {
+                  return File.createTempFile(prefix, suffix);
+               }
+            });
+         }
+         catch (PrivilegedActionException e)
+         {
+            throw e.getException();
+         }
+      }
+   }
+}

Copied: projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.properties (from rev 81882, projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.properties)
===================================================================
--- projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.properties	                        (rev 0)
+++ projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.properties	2008-12-01 10:00:11 UTC (rev 81883)
@@ -0,0 +1 @@
+test.Permission.1=java.io.FilePermission, <<ALL FILES>>, write,delete

Copied: projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml (from rev 81882, projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml)
===================================================================
--- projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml	                        (rev 0)
+++ projects/microcontainer/trunk/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml	2008-12-01 10:00:11 UTC (rev 81883)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+   <bean name="AspectManager" class="org.jboss.aop.AspectManager">
+      <constructor factoryClass="org.jboss.aop.AspectManager" factoryMethod="instance"/>
+   </bean>
+
+   <aspect  xmlns="urn:jboss:aop-beans:1.0" name="Aspect" class="org.jboss.test.microcontainer.support.TestAspect"/>   
+</deployment>

Modified: projects/microcontainer/trunk/kernel/src/main/java/org/jboss/beans/metadata/plugins/factory/GenericBeanFactory.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/java/org/jboss/beans/metadata/plugins/factory/GenericBeanFactory.java	2008-12-01 08:53:32 UTC (rev 81882)
+++ projects/microcontainer/trunk/kernel/src/main/java/org/jboss/beans/metadata/plugins/factory/GenericBeanFactory.java	2008-12-01 10:00:11 UTC (rev 81883)
@@ -199,7 +199,7 @@
     * @return the bean
     * @throws Throwable for any error
     */
-   private Object createBean(ClassLoader cl) throws Throwable
+   protected Object createBean(ClassLoader cl) throws Throwable
    {
       ClassLoader loader = cl;
       if (loader == null)




More information about the jboss-cvs-commits mailing list