[jboss-cvs] JBossAS SVN: r79411 - in projects/aop/trunk/aop/docs/examples: arrayinterception and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 13 12:39:25 EDT 2008
Author: kabir.khan at jboss.com
Date: 2008-10-13 12:39:25 -0400 (Mon, 13 Oct 2008)
New Revision: 79411
Added:
projects/aop/trunk/aop/docs/examples/arrayinterception/
projects/aop/trunk/aop/docs/examples/arrayinterception/ArrayInterceptor.java
projects/aop/trunk/aop/docs/examples/arrayinterception/Driver.java
projects/aop/trunk/aop/docs/examples/arrayinterception/NotWoven.java
projects/aop/trunk/aop/docs/examples/arrayinterception/Woven.java
projects/aop/trunk/aop/docs/examples/arrayinterception/all.html
projects/aop/trunk/aop/docs/examples/arrayinterception/build.xml
projects/aop/trunk/aop/docs/examples/arrayinterception/jboss-aop.xml
projects/aop/trunk/aop/docs/examples/arrayinterception/pom.xml
Log:
[JBAOP-643] Tutorial for array interception
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/ArrayInterceptor.java
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/ArrayInterceptor.java (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/ArrayInterceptor.java 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ */
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.array.ArrayElementReadInvocation;
+import org.jboss.aop.array.ArrayElementWriteInvocation;
+import org.jboss.aop.array.IntArrayElementWriteInvocation;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class ArrayInterceptor implements Interceptor
+{
+ public String getName() { return "ArrayInterceptor"; }
+
+ public Object invoke(Invocation invocation) throws Throwable
+ {
+ try
+ {
+ System.out.println("<<< Entering ArrayInterceptor type: " + invocation.getClass().getName());
+ if (invocation instanceof ArrayElementReadInvocation)
+ {
+ return invoke((ArrayElementReadInvocation)invocation);
+ }
+ else if (invocation instanceof ArrayElementWriteInvocation)
+ {
+ return invoke((ArrayElementWriteInvocation)invocation);
+ }
+ throw new RuntimeException("This interceptor is for arrays");
+ }
+ finally
+ {
+ System.out.println(">>> Leaving ArrayInterceptor");
+ }
+ }
+
+ private Object invoke(ArrayElementReadInvocation inv) throws Throwable
+ {
+ System.out.println("<<< We have an array element read invocation of type: " + inv.getClass().getName());
+ Object o = inv.invokeNext();
+ System.out.println(">>> Returned value was " + o);
+ return o;
+ }
+
+ private Object invoke(ArrayElementWriteInvocation inv) throws Throwable
+ {
+ System.out.println("<<< We have an array element write invocation of type: " + inv.getClass().getName());
+ System.out.println("New value for index " + inv.getIndex() + " of " + inv.getTargetObject() + " is " + inv.getValue());
+ if (inv instanceof IntArrayElementWriteInvocation)
+ {
+ System.out.println("Typed value is " + ((IntArrayElementWriteInvocation)inv).getIntValue());
+ }
+ return inv.invokeNext();
+ }
+}
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/Driver.java
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/Driver.java (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/Driver.java 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class Driver
+{
+ public static void main(String[] args)
+ {
+ System.out.println("--- new Woven(); ---");
+ Woven woven = new Woven();
+ System.out.println("--- woven.setInt(0, 100); ---");
+ woven.setInt(0, 100);
+ System.out.println("--- woven.getInt(0); ---");
+ int i = woven.getInt(0);
+ System.out.println("--- woven.getInt(0) was " + i + " ---");
+
+ System.out.println("--- new NotWoven(); ---");
+ NotWoven notWoven = new NotWoven();
+ System.out.println("--- notWoven.setInt(0, 100); ---");
+ notWoven.setInt(0, 100);
+ System.out.println("--- notWoven.getInt(0); ---");
+ int i2 = notWoven.getInt(0);
+ System.out.println("--- notWoven.getInt(0) was " + i2 + " ---");
+ }
+}
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/NotWoven.java
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/NotWoven.java (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/NotWoven.java 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+/**
+ * Comment
+ *
+ * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class NotWoven
+{
+ int[] ints;
+
+ public NotWoven()
+ {
+ System.out.println("Initialising array");
+ ints = new int[] {10, 20};
+ }
+
+ public void setInt(int index, int value)
+ {
+ ints[index] = value;
+ }
+
+ public int getInt(int index)
+ {
+ return ints[index];
+ }
+}
+
+
+
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/Woven.java
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/Woven.java (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/Woven.java 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class Woven
+{
+ int[] ints;
+
+ public Woven()
+ {
+ System.out.println("Initialising array");
+ ints = new int[] {10, 20};
+ }
+
+ public void setInt(int index, int value)
+ {
+ ints[index] = value;
+ }
+
+ public int getInt(int index)
+ {
+ return ints[index];
+ }
+}
+
+
+
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/all.html
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/all.html (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/all.html 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,62 @@
+<html>
+<body>
+<p>
+<h2>Array Element Interception</h2>
+
+</p><p>
+<h4>Array Element Interception</h4>
+
+<p>To intercept when elements of an array is matched we add the following to our jboss-aop.xml:</p>
+<pre>
+ <arrayreplacement class="Woven"/>
+ <prepare expr="field(* Woven->ints)"/>
+
+ <arraybind type="READ_WRITE">
+ <interceptor-ref name="ArrayInterceptor"/>
+ </arraybind>
+
+</pre>
+This first <code>arrayreplacement</code> says that whenever an array is being acessed in the class
+<code>Woven</code> we should delegate those accesses on to JBoss AOP. Next we have a
+<code>prepare</prepare> statement that picks out an array field. Since that field belongs to a
+class picked out by <code>arrayreplacement</code>, that field gets registered for array element
+interception. Finally, we have a <code>arraybind</code> that says that whenever an array whose
+access has been woven and intercepted intercepted
+we should apply <code>ArrayInterceptor</code>.
+
+</p><p>
+<h4>Running</h4>
+
+<p>
+<b>THIS EXAMPLE REQUIRES JDK 5!! For other options, please look at the
+<a href="../valid_targets_not_annotated.html"/>non-annotated examples guide</a></b> To compile and run:</p>
+<pre>
+ $ run.aopc.50
+</pre>
+<p>It will javac the files and then run the AOPC precompiler to manipulate the bytecode, then finally run the example. The output should be similar to this:</p>
+<pre>
+_run.aopc.50:
+ [java] --- new Woven(); ---
+ [java] Initialising array
+ [java] --- woven.setInt(0, 100); ---
+ [java] <<< Entering ArrayInterceptor type: org.jboss.aop.array.IntArrayElementWriteInvocation
+ [java] <<< We have an array element write invocation of type: org.jboss.aop.array.IntArrayElementWriteInvocation
+ [java] New value for index 0 of [I at d9e5ad is 100
+ [java] Typed value is 100
+ [java] >>> Leaving ArrayInterceptor
+ [java] --- woven.getInt(0); ---
+ [java] <<< Entering ArrayInterceptor type: org.jboss.aop.array.IntArrayElementReadInvocation
+ [java] <<< We have an array element read invocation of type: org.jboss.aop.array.IntArrayElementReadInvocation
+ [java] >>> Returned value was 100
+ [java] >>> Leaving ArrayInterceptor
+ [java] --- woven.getInt(0) was 100 ---
+ [java] --- new NotWoven(); ---
+ [java] Initialising array
+ [java] --- notWoven.setInt(0, 100); ---
+ [java] --- notWoven.getInt(0); ---
+ [java] --- notWoven.getInt(0) was 100 ---
+</pre>
+</p><p>
+</p>
+</body>
+</html>
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/build.xml
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/build.xml (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/build.xml 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project default="usage" name="JBoss/AOP">
+ <import file="../examples-build.xml"/>
+
+ <target name="run.aopc.50" depends="_run.aopc.50"/>
+
+ <target name="run.loadtime.50" depends="_run.loadtime.50"/>
+</project>
+
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/jboss-aop.xml
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/jboss-aop.xml (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/jboss-aop.xml 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<aop>
+ <interceptor class="ArrayInterceptor"/>
+
+ <arrayreplacement class="Woven"/>
+ <prepare expr="field(* Woven->ints)"/>
+ <!-- Enabling this still will not cause interception for NotWoven since it does not match an arrayreplacement
+ <prepare expr="field(* NotWoven->ints)"/>
+ -->
+ <arraybind type="READ_WRITE">
+ <interceptor-ref name="ArrayInterceptor"/>
+ </arraybind>
+</aop>
Added: projects/aop/trunk/aop/docs/examples/arrayinterception/pom.xml
===================================================================
--- projects/aop/trunk/aop/docs/examples/arrayinterception/pom.xml (rev 0)
+++ projects/aop/trunk/aop/docs/examples/arrayinterception/pom.xml 2008-10-13 16:39:25 UTC (rev 79411)
@@ -0,0 +1,76 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.jboss.aop</groupId>
+ <artifactId>examples</artifactId>
+ <version>1.0.0</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.jboss.aop</groupId>
+ <artifactId>all</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0.0</version>
+ <description>JBoss AOP Examples</description>
+ <name>JBoss AOP Examples</name>
+ <url>http://labs.jboss.org/jbossaop/</url>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.maven.plugins</groupId>
+ <artifactId>maven-jbossaop-plugin</artifactId>
+ <version>${jboss.aop.plugin.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <sourceDirectory>.</sourceDirectory>
+ <outputDirectory>target</outputDirectory>
+ <defaultGoal>jbossaop:run</defaultGoal>
+
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>${jdkVersion}</source>
+ <target>${jdkVersion}</target>
+ <excludes>
+ <exclude>target/*</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.jboss.maven.plugins</groupId>
+ <artifactId>maven-jbossaop-plugin</artifactId>
+ <version>${jboss.aop.plugin.version}</version>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <configuration>
+ <aoppaths>
+ <aoppath>jboss-aop.xml</aoppath>
+ </aoppaths>
+ </configuration>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>run</id>
+ <configuration>
+ <aoppaths>
+ <aoppath>jboss-aop.xml</aoppath>
+ </aoppaths>
+ <executable>Driver</executable>
+ </configuration>
+ <goals>
+ <goal>run</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
More information about the jboss-cvs-commits
mailing list