[jboss-cvs] JBossAS SVN: r65649 - projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Sep 27 05:52:38 EDT 2007


Author: kabir.khan at jboss.com
Date: 2007-09-27 05:52:38 -0400 (Thu, 27 Sep 2007)
New Revision: 65649

Added:
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/OutOfProcessProxySerializer.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializablePOJO.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializeContainerProxyTestCase.java
Log:
Add failing test showing that container proxies do not work out of vm

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/OutOfProcessProxySerializer.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/OutOfProcessProxySerializer.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/OutOfProcessProxySerializer.java	2007-09-27 09:52:38 UTC (rev 65649)
@@ -0,0 +1,191 @@
+/*
+* 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.aop.proxy;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.PrintStream;
+
+import junit.framework.Assert;
+
+import org.jboss.aop.AspectManager;
+import org.jboss.aop.advice.AdviceBinding;
+import org.jboss.aop.advice.AdviceFactory;
+import org.jboss.aop.advice.AspectDefinition;
+import org.jboss.aop.advice.GenericAspectFactory;
+import org.jboss.aop.advice.InterceptorFactory;
+import org.jboss.aop.advice.Scope;
+import org.jboss.aop.pointcut.PointcutExpression;
+import org.jboss.aop.proxy.container.AOPProxyFactory;
+import org.jboss.aop.proxy.container.AOPProxyFactoryParameters;
+import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class OutOfProcessProxySerializer extends Assert
+{
+   final static int WRONG_ARGS = 1;
+   final static int NO_SUCH_FILE = 2;
+   final static int GENERAL_ERROR = 3;
+   
+   public static void main (String[] args)
+   {
+      if (args.length != 1)
+      {
+         System.exit(WRONG_ARGS);
+      }
+      
+      File file = new File(args[0]);
+      if (!file.exists())
+      {
+         System.exit(NO_SUCH_FILE);
+      }
+      
+      try
+      {
+         
+//         InstanceDomain domain = new InstanceDomain(AspectManager.instance(), "blah", false);
+//
+//         
+//         InterfaceIntroduction intro = new InterfaceIntroduction("intro", "*", null);
+//         String[] intfs = {MixinInterface.class.getName()};
+//         InterfaceIntroduction.Mixin mixin = new InterfaceIntroduction.Mixin(Mixin.class.getName(), intfs, null, false);
+//         intro.getMixins().add(mixin);
+//         domain.addInterfaceIntroduction(intro);
+//
+//         
+//         AspectDefinition def = new AspectDefinition("aspect", Scope.PER_VM, new GenericAspectFactory(EchoInterceptor.class.getName(), null));
+//         domain.addAspectDefinition(def);
+//         AdviceFactory advice = new AdviceFactory(def, "invoke");
+//         domain.addInterceptorFactory(advice.getName(), advice);
+//         {
+//         PointcutExpression pointcut = new PointcutExpression("pointcut", "execution(java.lang.String " + POJO.class.getName() + "->helloWorld(..))");
+//         domain.addPointcut(pointcut);
+//         InterceptorFactory[] interceptors = {advice};
+//         AdviceBinding binding = new AdviceBinding("pojo-binding", pointcut, null, null, interceptors);
+//         domain.addBinding(binding);
+//         }
+//
+//         {
+//         PointcutExpression pointcut = new PointcutExpression("mixin-pointcut", "execution(java.lang.String $instanceof{" + MixinInterface.class.getName() + "}->intercepted(..))");
+//         domain.addPointcut(pointcut);
+//         InterceptorFactory[] interceptors = {advice};
+//         AdviceBinding binding = new AdviceBinding("mixin-binding", pointcut, null, null, interceptors);
+//         domain.addBinding(binding);
+//         }
+//
+//         
+//         AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
+//         AOPProxyFactory factory = new GeneratedAOPProxyFactory();
+//         params.set
+//         factory.createAdvisedProxy(params);
+//         
+//         Class proxyClass = ContainerProxyFactory.getProxyClass(POJO.class, domain);
+//         ClassProxyContainer container = new ClassProxyContainer("test", domain);
+//         domain.setAdvisor(container);
+//         container.setClass(proxyClass);
+//         container.initializeClassContainer();
+//         POJO proxy = (POJO) proxyClass.newInstance();
+//         AspectManaged cp = (AspectManaged)proxy;
+//         cp.setAdvisor(container);
+//         Delegate delegate = (Delegate)cp;
+//         delegate.setDelegate(new POJO());
+//
+//         MixinInterface mi = (MixinInterface) proxy;
+//         System.out.println("--- mixin");
+//         assertEquals(mi.hello("mixin"), "mixin");
+//         System.out.println("--- hw");
+//         assertEquals("echoed", proxy.helloWorld());
+//         System.out.println("--- icptd");
+//         assertEquals("echoed", mi.intercepted("error"));
+//         
+         
+         AspectManager manager = AspectManager.instance();
+//         AspectDefinition def = new AspectDefinition("perinstanceaspect", Scope.PER_INSTANCE, new GenericAspectFactory(TestInterceptor.class.getName(), null));
+         AspectDefinition def = new AspectDefinition("perinstanceaspect", Scope.PER_VM, new GenericAspectFactory(TestInterceptor.class.getName(), null));
+         AdviceFactory advice = new AdviceFactory(def, "invoke");
+         PointcutExpression pointcut = new PointcutExpression("perinstancepointcut", "execution(* $instanceof{" + SomeInterface.class.getName() + "}->*(..))");
+         InterceptorFactory[] interceptors = {advice};
+         AdviceBinding binding = new AdviceBinding("perinstancebinding", pointcut, null, null, interceptors);
+
+         manager.addAspectDefinition(def);
+         manager.addInterceptorFactory(advice.getName(), advice);
+         manager.addPointcut(pointcut);
+         manager.addBinding(binding);
+            
+         AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
+         params.setInterfaces(new Class[] {SomeInterface.class});
+         params.setTarget(new SerializablePOJO());
+         
+         AOPProxyFactory factory = new GeneratedAOPProxyFactory();
+         SomeInterface si = (SomeInterface)factory.createAdvisedProxy(params);
+         
+         si.helloWorld();
+         
+         assertTrue(TestInterceptor.invoked);
+         
+         ObjectOutputStream out = null;
+         try
+         {
+            out = new ObjectOutputStream(new FileOutputStream(file));
+            out.writeObject(si);
+         }
+         finally
+         {
+            try
+            {
+               out.close();
+            }
+            catch(Exception e)
+            {
+            }
+         }
+      }
+      catch (Throwable t)
+      {
+         PrintStream out = null;
+         try
+         {
+            out = new PrintStream(new FileOutputStream(file));
+            t.printStackTrace(out);
+         }
+         catch(Exception e)
+         {
+         }
+         finally
+         {
+            try
+            {
+               out.close();
+            }
+            catch(Exception e)
+            {
+            }
+         }
+         System.exit(GENERAL_ERROR);
+      }
+   }
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializablePOJO.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializablePOJO.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializablePOJO.java	2007-09-27 09:52:38 UTC (rev 65649)
@@ -0,0 +1,37 @@
+/*
+* 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.aop.proxy;
+
+import java.io.Serializable;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class SerializablePOJO implements Serializable 
+{
+   public String helloWorld()
+   {
+      return "error";
+   }
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializeContainerProxyTestCase.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializeContainerProxyTestCase.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/SerializeContainerProxyTestCase.java	2007-09-27 09:52:38 UTC (rev 65649)
@@ -0,0 +1,226 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, 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.aop.proxy;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
+ * @version $Revision: 64431 $
+ */
+public class SerializeContainerProxyTestCase extends TestCase
+{
+
+   public static void main(String[] args)
+   {
+      TestRunner.run(suite());
+   }
+
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite("SerializeContainerProxyTestCase");
+      suite.addTestSuite(SerializeContainerProxyTestCase.class);
+      return suite;
+   }
+
+   public SerializeContainerProxyTestCase(String name)
+   {
+      super(name);
+   }
+
+   public void testContainerProxy() throws Exception
+   {
+      try
+      {
+         File proxyFile = runExternalProcess();
+         
+         ObjectInputStream in = new ObjectInputStream(new FileInputStream(proxyFile));
+         SomeInterface si = (SomeInterface)in.readObject();
+
+         si.helloWorld();
+         assertTrue(TestInterceptor.invoked);
+      }
+      catch(Exception e)
+      {
+         e.printStackTrace();
+         throw e;
+      }
+   }
+
+   private File runExternalProcess() throws Exception
+   {
+      Properties props = System.getProperties();
+      
+      String classPath = props.getProperty("java.class.path");      
+      String libraryPath = props.getProperty("sun.boot.library.path") + File.separator + "java";
+      String java = findJava(libraryPath);
+   
+      File proxyFile = File.createTempFile("proxy", "err");
+      proxyFile.deleteOnExit();
+      
+//      System.out.println(classPath);
+      boolean debugFlag = System.getProperty("jboss.aop.debug.classes", "false").equals("true"); 
+      String debug = debugFlag ? "-Djboss.aop.debug.classes=true " : "";
+      
+      Process proc = Runtime.getRuntime().exec(
+            java + 
+            " -classpath " + classPath + " " + 
+            debug + 
+            OutOfProcessProxySerializer.class.getName() + " " + 
+            proxyFile.getAbsolutePath());
+      int result = proc.waitFor();
+      
+      switch (result)
+      {
+         case OutOfProcessProxySerializer.WRONG_ARGS:
+            throw new RuntimeException("Wrong number of args passed in");
+         case OutOfProcessProxySerializer.NO_SUCH_FILE:
+            throw new RuntimeException("No file found " + proxyFile);
+         case OutOfProcessProxySerializer.GENERAL_ERROR:
+            String externalException = getExternalException(proxyFile);
+            throw new RuntimeException(externalException);
+      }
+      
+      return proxyFile;
+   }
+   
+   private String getExternalException(File proxyFile)
+   {
+      Reader reader = null;
+      StringBuffer sb = new StringBuffer();
+      try
+      {
+         reader = new FileReader(proxyFile);
+         int r = reader.read();
+         while (r != -1)
+         {
+            sb.append((char)r);
+            r = reader.read();
+         }
+      }
+      catch(Exception e)
+      {
+      }
+      finally
+      {
+         try
+         {
+            reader.close();
+         }
+         catch(Exception e)
+         {
+         }
+      }
+      return sb.toString();
+   }
+   
+   private String findJava(String classPath)
+   {
+      String java = null;
+      StringTokenizer tok = new StringTokenizer(classPath, File.pathSeparator);
+      while (tok.hasMoreTokens())
+      {
+         String path = tok.nextToken();
+         if (path.endsWith(".jar"))
+         {
+            continue;
+         }
+         java = getJavaPath(path);
+         if (java != null)
+         {
+            return java;
+         }
+      }
+      return null;
+   }
+   
+   private String getJavaPath(String dirName)
+   {
+      if (File.separatorChar == '/')
+      {
+         //Probably on Linuxx
+         //AFAIK on linux we have no extension after java in the bin directory?
+         String file = getFile(dirName + File.separator + "java");
+         if (file != null)
+         {
+            return file;
+         }
+         file = getFile(dirName);
+         if (file != null)
+         {
+            return file;
+         }
+      }
+      else if (File.separatorChar == '\\')
+      {
+         //We're probably on windows
+         String file = getFile(dirName + File.separator + "java.exe");
+         if (file != null)
+         {
+            return file;
+         }
+         file = getFile(dirName);
+         if (file != null)
+         {
+            return file;
+         }
+         file = getFile(dirName + ".exe");
+         if (file != null)
+         {
+            return file;
+         }
+      }
+      else
+      {
+         throw new RuntimeException("Cannot figure out OS");
+      }
+      return null;
+   }
+   
+   private String getFile(String java)
+   {
+      if (java != null)
+      {
+         File file = new File(java);
+         
+         if (file.exists() && !file.isDirectory())
+         {
+            System.out.println("Using Java executable: " + java);
+            return file.getAbsolutePath();
+         }
+      }
+      return null;
+   }
+}




More information about the jboss-cvs-commits mailing list