[jboss-cvs] JBossAS SVN: r93793 - in projects/interceptors/trunk: src and 14 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Sep 21 01:27:29 EDT 2009
Author: marius.bogoevici
Date: 2009-09-21 01:27:28 -0400 (Mon, 21 Sep 2009)
New Revision: 93793
Added:
projects/interceptors/trunk/copyright.txt
projects/interceptors/trunk/pom.xml
projects/interceptors/trunk/src/
projects/interceptors/trunk/src/main/
projects/interceptors/trunk/src/main/java/
projects/interceptors/trunk/src/main/java/org/
projects/interceptors/trunk/src/main/java/org/jboss/
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/Utils/
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/Utils/InterceptionUtils.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionType.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandler.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandlerFactory.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionProxyFactory.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorException.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreator.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandlerFactory.java
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/
projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java
projects/interceptors/trunk/src/test/
projects/interceptors/trunk/src/test/java/
projects/interceptors/trunk/src/test/java/org/
projects/interceptors/trunk/src/test/java/org/jboss/
projects/interceptors/trunk/src/test/java/org/jboss/interceptors/
projects/interceptors/trunk/src/test/java/org/jboss/interceptors/FootballTeam.java
projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java
projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptorTestLogger.java
Modified:
projects/interceptors/trunk/
Log:
Initial take on interceptors library - contains a basic interceptor-class to class mapping method and proxifycation support.
Property changes on: projects/interceptors/trunk
___________________________________________________________________
Name: svn:ignore
+ *.iws
interceptors.iml
interceptors.ipr
Added: projects/interceptors/trunk/copyright.txt
===================================================================
Added: projects/interceptors/trunk/pom.xml
===================================================================
--- projects/interceptors/trunk/pom.xml (rev 0)
+++ projects/interceptors/trunk/pom.xml 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,200 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.jboss</groupId>
+ <artifactId>jboss-parent</artifactId>
+ <version>4</version>
+ </parent>
+
+ <groupId>org.jboss.ejb.interceptors</groupId>
+ <artifactId>interceptors</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+
+ <name>JBoss Interceptors</name>
+ <description>JBoss Interceptors</description>
+
+ <scm>
+ <connection>scm:svn:http://anonsvn.jboss.org/repos/jbossas/projects/interceptors/trunk/</connection>
+ <developerConnection>scm:svn:https://svn.jboss.org/repos/jbossas/projects/interceptors/trunk/</developerConnection>
+ <url>http://viewvc.jboss.org/cgi-bin/viewvc.cgi/jbossas/projects/interceptors/trunk/</url>
+ </scm>
+
+ <properties>
+ <version.ejb3.api>3.1.0-Alpha1</version.ejb3.api>
+ <version.javassist>3.11.0.GA</version.javassist>
+ <version.junit>4.5</version.junit>
+ </properties>
+
+ <build>
+ <finalName>${artifactId}</finalName>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2.SP1</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ <showDeprecation>true</showDeprecation>
+ <showWarnings>true</showWarnings>
+ <optimize>true</optimize>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-release-plugin</artifactId>
+ <version>2.0-beta-9</version>
+ <configuration>
+ <generateReleasePoms>false</generateReleasePoms>
+ <tagBase>https://svn.jboss.org/repos/jbossas/projects/interceptors/tags</tagBase>
+ </configuration>
+ </plugin>
+ </plugins>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-idea-plugin</artifactId>
+ <configuration>
+ <downloadSources>true</downloadSources>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.4.3</version>
+ <configuration>
+ <redirectTestOutputToFile>true</redirectTestOutputToFile>
+ <!--testFailureIgnore>true</testFailureIgnore-->
+ <includes>
+ <include>org/jboss/test/**/*TestCase.java</include>
+ </includes>
+ </configuration>
+ </plugin>
+ <plugin>
+ <!-- Configure javadoc plugin with APIviz -->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <configuration>
+ <doclet>net.gleamynode.apiviz.APIviz</doclet>
+ <docletArtifact>
+ <groupId>net.gleamynode.apiviz</groupId>
+ <artifactId>apiviz</artifactId>
+ <version>1.1.0</version>
+ </docletArtifact>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+
+ <repositories>
+ <repository>
+ <id>repository.jboss.org</id>
+ <name>JBoss Repository</name>
+ <layout>default</layout>
+ <url>http://repository.jboss.org/maven2/</url>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ <repository>
+ <id>snapshots.jboss.org</id>
+ <name>JBoss Snapshots Repository</name>
+ <layout>default</layout>
+ <url>http://snapshots.jboss.org/maven2/</url>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ <releases>
+ <enabled>false</enabled>
+ </releases>
+ </repository>
+ <!-- APIviz repository -->
+ <repository>
+ <id>apiviz.release</id>
+ <name>APIviz releases</name>
+ <url>http://apiviz.googlecode.com/svn/site/repo/mvn/release</url>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-project-info-reports-plugin</artifactId>
+ <reportSets>
+ <reportSet>
+ <reports>
+ <report>dependencies</report>
+ <report>issue-tracking</report>
+ <report>license</report>
+ <report>scm</report>
+ </reports>
+ </reportSet>
+ </reportSets>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>findbugs-maven-plugin</artifactId>
+ <version>2.0.1</version>
+ <configuration>
+ <findbugsXmlOutput>true</findbugsXmlOutput>
+ <!-- Optional directory to put findbugs xml report -->
+ <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
+ </configuration>
+ </plugin>
+ </plugins>
+ </reporting>
+
+ <dependencyManagement>
+ <!-- The parent pom manages the inter-dependencies of the modules. -->
+ <dependencies>
+
+ <dependency>
+ <groupId>org.jboss.ejb3</groupId>
+ <artifactId>jboss-ejb3-api</artifactId>
+ <version>${version.ejb3.api}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${version.junit}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>javassist</groupId>
+ <artifactId>javassist</artifactId>
+ <version>${version.javassist}</version>
+ </dependency>
+
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.ejb3</groupId>
+ <artifactId>jboss-ejb3-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javassist</groupId>
+ <artifactId>javassist</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/Utils/InterceptionUtils.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/Utils/InterceptionUtils.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/Utils/InterceptionUtils.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.utils;
+
+import org.jboss.interceptor.proxy.InterceptorProxyCreatorImpl;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptionUtils
+{
+ public static void executePostConstruct(Object proxy) {
+ if (proxy instanceof InterceptorProxyCreatorImpl.LifecycleMixin) {
+ InterceptorProxyCreatorImpl.LifecycleMixin lifecycleMixin = (InterceptorProxyCreatorImpl.LifecycleMixin) proxy;
+ lifecycleMixin.lifecycle_mixin_$$_postConstruct();
+ }
+ }
+
+ public static void executePredestroy(Object proxy) {
+ if (proxy instanceof InterceptorProxyCreatorImpl.LifecycleMixin) {
+ InterceptorProxyCreatorImpl.LifecycleMixin lifecycleMixin = (InterceptorProxyCreatorImpl.LifecycleMixin) proxy;
+ lifecycleMixin.lifecycle_mixin_$$_preDestroy();
+ }
+ }
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,47 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.model;
+
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public interface InterceptionModel
+{
+
+ /**
+ * Returns the interceptors applicable for the given interception type and method
+ *
+ * @param interceptionType
+ * @param method
+ * @return list of interceptors
+ * @throws IllegalArgumentException if interceptionType is business method or around timeout
+ * but method is null, as well as if interceptionType is callback and method is not null
+ */
+ public List<Class<?>> getInterceptors(InterceptionType interceptionType, Method method);
+
+ /**
+ * Returns all interceptor classes that are applicable to the given intercepted entity
+ * @return
+ */
+ public Set<Class<?>> getAllInterceptors();
+
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,77 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.model;
+
+import java.lang.reflect.Method;
+import java.util.*;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptionModelImpl implements InterceptionModel
+{
+
+ private Map<InterceptionType, List<Class<?>>> lifecycleInterceptors = new HashMap<InterceptionType, List<Class<?>>>();
+
+ private Map<InterceptionType, Map<Method, List<Class<?>>>> methodBoundInterceptors = new HashMap<InterceptionType, Map<Method, List<Class<?>>>>();
+
+ private Set<Class<?>> allInterceptors = new LinkedHashSet<Class<?>>();
+
+
+ public List<Class<?>> getInterceptors(InterceptionType interceptionType, Method method)
+ {
+ if (interceptionType.isLifecycleCallback() && method != null)
+ throw new IllegalArgumentException("On a lifecycle callback, associated metod must be null");
+
+ if (!interceptionType.isLifecycleCallback() && method == null)
+ throw new IllegalArgumentException("Around-invoke and around-timeout interceptors are defined for a given method");
+
+ if (interceptionType.isLifecycleCallback())
+ {
+ return lifecycleInterceptors.get(interceptionType);
+ } else
+ {
+ if (methodBoundInterceptors.containsKey(interceptionType))
+ return methodBoundInterceptors.get(interceptionType).get(method);
+ else
+ return null;
+ }
+ }
+
+ public Set<Class<?>> getAllInterceptors()
+ {
+ return Collections.unmodifiableSet(allInterceptors);
+ }
+
+ public void setInterceptors(InterceptionType interceptionType, Method method, List<Class<?>> interceptors)
+ {
+ if (interceptionType.isLifecycleCallback())
+ {
+ lifecycleInterceptors.put(interceptionType, interceptors);
+ } else
+ {
+ if (null == methodBoundInterceptors.get(interceptionType))
+ {
+ methodBoundInterceptors.put(interceptionType, new HashMap<Method, List<Class<?>>>());
+ }
+ methodBoundInterceptors.get(interceptionType).put(method, interceptors);
+ }
+ allInterceptors.addAll(interceptors);
+ }
+
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionType.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionType.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionType.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.model;
+
+import javax.interceptor.AroundInvoke;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.ejb.PostActivate;
+import javax.ejb.PrePassivate;
+import java.lang.annotation.Annotation;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public enum InterceptionType
+{
+ AROUND_INVOKE(false, AroundInvoke.class),
+ //AROUND_TIMEOUT(false, AroundTimeout.class),
+ POST_CONSTRUCT(true, PostConstruct.class),
+ PRE_DESTROY(true, PreDestroy.class),
+ POST_ACTIVATE(true, PostActivate.class),
+ PRE_PASSIVATE(true, PrePassivate.class);
+
+ private boolean lifecycleCallback;
+ private Class<? extends Annotation> associatedAnnotation;
+
+ InterceptionType(boolean lifecycleCallback, Class<? extends Annotation> associatedAnnotation)
+ {
+ this.lifecycleCallback = lifecycleCallback;
+ this.associatedAnnotation = associatedAnnotation;
+ }
+
+ public boolean isLifecycleCallback()
+ {
+ return lifecycleCallback;
+ }
+
+ public Class<? extends Annotation> getAssociatedAnnotation()
+ {
+ return associatedAnnotation;
+ }
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+import org.jboss.interceptor.model.InterceptionType;
+
+import javax.interceptor.InvocationContext;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptionChain
+{
+
+ private Object target;
+
+ private Object[] parameters;
+
+ private Method targetMethod;
+ private Map<Class<?>, InterceptionHandler> interceptionHandlerMap;
+
+ private int currentPosition;
+ private List<Class<?>> interceptorClasses;
+
+ private final InterceptionType interceptionType;
+
+ public InterceptionChain(List<Class<?>> interceptorClasses, InterceptionType interceptionType, Object target, Method targetMethod, Object[] parameters, Map<Class<?>, InterceptionHandler> interceptionHandlerMap)
+ {
+ this.interceptorClasses = interceptorClasses;
+ this.interceptionType = interceptionType;
+ this.parameters = parameters;
+ this.target = target;
+ this.targetMethod = targetMethod;
+ this.interceptionHandlerMap = interceptionHandlerMap;
+ this.currentPosition = 0;
+ }
+
+ public Object invokeNext(InvocationContext invocationContext) {
+
+ if (currentPosition < interceptorClasses.size())
+ {
+ return interceptionHandlerMap.get(interceptorClasses.get(currentPosition++)).invoke(target, interceptionType, invocationContext);
+ }
+ else
+ {
+ if (targetMethod != null)
+ try
+ {
+ return targetMethod.invoke(target, parameters);
+ } catch (IllegalAccessException e)
+ {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e)
+ {
+ throw new RuntimeException(e);
+ }
+ else
+ return null;
+ }
+ }
+
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandler.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandler.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandler.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+import org.jboss.interceptor.model.InterceptionType;
+
+import javax.interceptor.InvocationContext;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public interface InterceptionHandler
+{
+ public Object invoke(Object target, InterceptionType interceptionType, InvocationContext invocationContext);
+
+ boolean handles(Class<?> clazz);
+}
+
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandlerFactory.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandlerFactory.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionHandlerFactory.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,26 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public interface InterceptionHandlerFactory {
+
+ InterceptionHandler createForClass(Class<?> clazz);
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionProxyFactory.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionProxyFactory.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionProxyFactory.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+import org.jboss.interceptor.registry.InterceptorRegistry;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public interface InterceptionProxyFactory<C> {
+
+ <T> T createInterceptedProxy(T instance, Class<T> expectedProxyClass, InterceptorRegistry<C> interceptorRegistry);
+
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorException.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorException.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorException.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptorException extends RuntimeException
+{
+ public InterceptorException()
+ {
+ }
+
+ public InterceptorException(String s)
+ {
+ super(s);
+ }
+
+ public InterceptorException(String s, Throwable throwable)
+ {
+ super(s, throwable);
+ }
+
+ public InterceptorException(Throwable throwable)
+ {
+ super(throwable);
+ }
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+import javax.interceptor.InvocationContext;
+import java.util.Map;
+import java.util.HashMap;
+import java.lang.reflect.Method;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptorInvocationContext implements InvocationContext
+{
+
+ private Map<String, Object> contextData = new HashMap<String, Object>();
+
+ private Method method;
+
+ private Object[] parameters;
+
+ private Object target;
+
+ private InterceptionChain interceptionChain;
+
+ public InterceptorInvocationContext(InterceptionChain interceptionChain, Object target, Method targetMethod, Object[] parameters)
+ {
+ this.interceptionChain = interceptionChain;
+ this.method = targetMethod;
+ this.parameters = parameters;
+ this.target = target;
+ }
+
+ public Map<String, Object> getContextData()
+ {
+ return contextData;
+ }
+
+ public Method getMethod()
+ {
+ return method;
+ }
+
+ public Object[] getParameters()
+ {
+ return parameters;
+ }
+
+ public Object getTarget()
+ {
+ return target;
+ }
+
+ public Object proceed() throws Exception
+ {
+ return interceptionChain.invokeNext(this);
+ }
+
+ public void setParameters(Object[] params)
+ {
+ if (method != null)
+ this.parameters = params;
+ }
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreator.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreator.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreator.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public interface InterceptorProxyCreator {
+ <T> T createProxyFromInstance(Object target, Class<T> proxyClass, Class<?>[] constructorTypes, Object[] constructorArguments) throws IllegalAccessException, InstantiationException;
+
+ <T> T createInstrumentedInstance(Class<T> proxyClass, Class<?>[] constructorTypes, Object[] constructorArguments) throws IllegalAccessException, InstantiationException;
+
+ <T> T createProxyFromInstance(Object target, Class<T> proxyClass) throws IllegalAccessException, InstantiationException;
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,232 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+import org.jboss.interceptor.registry.InterceptorRegistry;
+import org.jboss.interceptor.model.InterceptionType;
+import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.MethodHandler;
+
+import java.lang.reflect.Method;
+import java.util.*;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptorProxyCreatorImpl implements InterceptorProxyCreator
+{
+ public static final String POST_CONSTRUCT = "lifecycle_mixin_$$_postConstruct";
+ public static final String PRE_DESTROY = "lifecycle_mixin_$$_preDestroy";
+
+ private InterceptorRegistry<Class> interceptorRegistry;
+
+ private InterceptionHandlerFactory interceptionHandlerFactory;
+
+ public InterceptorProxyCreatorImpl(InterceptorRegistry<Class> interceptorRegistry, InterceptionHandlerFactory interceptionHandlerFactory)
+ {
+ this.interceptorRegistry = interceptorRegistry;
+ this.interceptionHandlerFactory = interceptionHandlerFactory;
+ }
+
+
+ public <T> T createProxyFromInstance(final Object target, Class<T> proxyClass, Class<?>[] constructorTypes, Object[] constructorArguments) throws IllegalAccessException, InstantiationException
+ {
+ ProxyFactory proxyFactory = new ProxyFactory();
+ if (proxyClass != null)
+ proxyFactory.setSuperclass(proxyClass);
+
+ proxyFactory.setInterfaces(new Class<?>[]{LifecycleMixin.class});
+
+ proxyFactory.setHandler(new InstanceProxifyingMethodHandler(target, proxyClass, interceptorRegistry));
+
+ try
+ {
+ return (T) proxyFactory.create(constructorTypes, constructorArguments);
+ } catch (Exception e)
+ {
+ throw new InterceptorException(e);
+ }
+ }
+
+ public <T> T createInstrumentedInstance(Class<T> proxyClass, Class<?>[] constructorTypes, Object[] constructorArguments) throws IllegalAccessException, InstantiationException
+ {
+ ProxyFactory proxyFactory = new ProxyFactory();
+ if (proxyClass != null)
+ proxyFactory.setSuperclass(proxyClass);
+
+
+ proxyFactory.setInterfaces(new Class<?>[]{LifecycleMixin.class});
+
+ proxyFactory.setHandler(new AutoProxifiedMethodHandler(proxyClass, interceptorRegistry));
+
+ try
+ {
+ return (T) proxyFactory.create(constructorTypes, constructorArguments);
+ } catch (Exception e)
+ {
+ throw new InterceptorException(e);
+ }
+ }
+
+ public <T> T constructInstrumentedInstance(final Object target, Class<T> proxyClass, Class<?>[] constructorTypes, Object[] constructorArguments) throws IllegalAccessException, InstantiationException
+ {
+ ProxyFactory proxyFactory = new ProxyFactory();
+ if (proxyClass != null)
+ proxyFactory.setSuperclass(target.getClass());
+
+ proxyFactory.setHandler(new InstanceProxifyingMethodHandler(target, proxyClass, interceptorRegistry));
+
+ try
+ {
+ return (T) proxyFactory.create(constructorTypes, constructorArguments);
+ } catch (Exception e)
+ {
+ throw new InterceptorException(e);
+ }
+ }
+
+ public <T> T createProxyFromInstance(final Object target, Class<T> proxyClass) throws IllegalAccessException, InstantiationException
+ {
+ return createProxyFromInstance(target, proxyClass, new Class[0], new Object[0]);
+ }
+
+
+ private class InstanceProxifyingMethodHandler implements MethodHandler
+ {
+ private final Object target;
+
+ private InterceptorRegistry registry;
+ private Map<Class<?>, InterceptionHandler> interceptorHandlerInstances = new HashMap<Class<?>, InterceptionHandler>();
+ private Class<?> targetClazz;
+
+ public InstanceProxifyingMethodHandler(Object target, Class<?> targetClass, InterceptorRegistry registry)
+ {
+ if (target == null)
+ this.target = this;
+ else
+ this.target = target;
+
+ if (targetClass != null)
+ this.targetClazz = targetClass;
+ else
+ this.targetClazz = target.getClass();
+
+ this.registry = registry;
+
+ for (Class<?> interceptorClazz : registry.getInterceptionModel(this.targetClazz).getAllInterceptors())
+ {
+ if (!interceptorClazz.equals(targetClazz))
+ interceptorHandlerInstances.put(interceptorClazz, interceptionHandlerFactory.createForClass(interceptorClazz));
+ }
+
+ interceptorHandlerInstances.put(targetClazz, new SimpleInterceptionHandler(target, targetClazz));
+ }
+
+// public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
+// {
+// List<Class<?>> interceptorClasses = registry.getInterceptionModel(targetClazz).getInterceptors(InterceptionType.AROUND_INVOKE, thisMethod);
+// List<Class<?>> interceptorClassesForMethod = interceptorClasses == null ? new ArrayList<Class<?>>() : interceptorClasses;
+// InterceptionChain chain = new InterceptionChain(interceptorClassesForMethod, InterceptionType.AROUND_INVOKE, target, thisMethod, args, interceptorHandlerInstances);
+// return chain.invokeNext(new InterceptorInvocationContext(chain, target, thisMethod, args));
+// }
+
+ public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
+ {
+ if (!thisMethod.getDeclaringClass().equals(LifecycleMixin.class))
+ {
+ return executeInterception(thisMethod, args, InterceptionType.AROUND_INVOKE);
+ } else
+ {
+ if (thisMethod.getName().equals(POST_CONSTRUCT))
+ {
+ return executeInterception(null, null, InterceptionType.POST_CONSTRUCT);
+ } else if (thisMethod.getName().equals(PRE_DESTROY))
+ {
+ return executeInterception(null, null, InterceptionType.PRE_DESTROY);
+ }
+ }
+
+ return null;
+ }
+
+ private Object executeInterception(Method thisMethod, Object[] args, InterceptionType interceptionType)
+ {
+ List<Class<?>> interceptorClasses = registry.getInterceptionModel(targetClazz).getInterceptors(interceptionType, thisMethod);
+ List<Class<?>> interceptorClassesForMethod = interceptorClasses == null ? new ArrayList<Class<?>>() : interceptorClasses;
+ InterceptionChain chain = new InterceptionChain(interceptorClassesForMethod, interceptionType, target, thisMethod, args, interceptorHandlerInstances);
+ return chain.invokeNext(new InterceptorInvocationContext(chain, target, thisMethod, args));
+ }
+ }
+
+
+ private static class AutoProxifiedMethodHandler implements MethodHandler
+ {
+ private InterceptorRegistry registry;
+ private Map<Class<?>, InterceptionHandler> interceptorHandlerInstances = new HashMap<Class<?>, InterceptionHandler>();
+ private Class<?> targetClazz;
+
+ public AutoProxifiedMethodHandler(Class<?> targetClazz, InterceptorRegistry registry)
+ {
+
+ if (targetClazz == null)
+ throw new IllegalArgumentException("Target class must not be null");
+
+ this.targetClazz = targetClazz;
+ this.registry = registry;
+
+ for (Class<?> interceptorClazz : registry.getInterceptionModel(this.targetClazz).getAllInterceptors())
+ {
+ interceptorHandlerInstances.put(interceptorClazz, new SimpleInterceptionHandler(interceptorClazz));
+ }
+ }
+
+ public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
+ {
+ if (!thisMethod.getDeclaringClass().equals(LifecycleMixin.class))
+ {
+ return executeInterception(self, thisMethod, proceed, args, InterceptionType.AROUND_INVOKE);
+ } else
+ {
+ if (thisMethod.getName().equals(POST_CONSTRUCT))
+ {
+ return executeInterception(self, null, null, null, InterceptionType.POST_CONSTRUCT);
+ } else if (thisMethod.getName().equals(PRE_DESTROY))
+ {
+ return executeInterception(self, null, null, null, InterceptionType.PRE_DESTROY);
+ }
+ }
+
+ return null;
+ }
+
+ private Object executeInterception(Object self, Method thisMethod, Method proceed, Object[] args, InterceptionType interceptionType)
+ {
+ List<Class<?>> interceptorClasses = registry.getInterceptionModel(targetClazz).getInterceptors(interceptionType, thisMethod);
+ List<Class<?>> interceptorClassesForMethod = interceptorClasses == null ? new ArrayList<Class<?>>() : interceptorClasses;
+ InterceptionChain chain = new InterceptionChain(interceptorClassesForMethod, interceptionType, self, proceed, args, interceptorHandlerInstances);
+ return chain.invokeNext(new InterceptorInvocationContext(chain, self, proceed, args));
+ }
+ }
+
+ public interface LifecycleMixin
+ {
+ public void lifecycle_mixin_$$_postConstruct();
+
+ public void lifecycle_mixin_$$_preDestroy();
+ }
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,106 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+import org.jboss.interceptor.model.InterceptionType;
+
+import javax.interceptor.InvocationContext;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class SimpleInterceptionHandler implements InterceptionHandler
+{
+
+ private final Object interceptorInstance;
+
+ private final Map<InterceptionType, Method> interceptorMethods = new HashMap<InterceptionType, Method>();
+
+ private Class<?> clazz;
+
+ public SimpleInterceptionHandler(Object interceptorInstance, Class<?> clazz)
+ {
+ if (interceptorInstance == null)
+ throw new IllegalArgumentException("Interceptor instance cannot be null");
+
+ this.clazz = (clazz == null) ?interceptorInstance.getClass():clazz;
+ this.interceptorInstance = interceptorInstance;
+ for (InterceptionType interceptionType : InterceptionType.values())
+ {
+ for (Method method : clazz.getDeclaredMethods())
+ {
+ if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(InvocationContext.class) && method.getAnnotation(interceptionType.getAssociatedAnnotation()) != null)
+ {
+ interceptorMethods.put(interceptionType, method);
+ }
+ }
+ }
+
+ }
+
+ public SimpleInterceptionHandler(Class<?> simpleInterceptorClass)
+ {
+
+ if (simpleInterceptorClass == null)
+ throw new IllegalArgumentException("Class must not be null");
+
+ this.clazz = simpleInterceptorClass;
+ try
+ {
+ this.interceptorInstance = simpleInterceptorClass.newInstance();
+ } catch (Exception e)
+ {
+ throw new InterceptorException("Cannot create interceptor instance:", e);
+ }
+ for (InterceptionType interceptionType : InterceptionType.values())
+ {
+ for (Method method : simpleInterceptorClass.getDeclaredMethods())
+ {
+ if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(InvocationContext.class) && method.getAnnotation(interceptionType.getAssociatedAnnotation()) != null)
+ {
+ interceptorMethods.put(interceptionType, method);
+ }
+ }
+ }
+
+ }
+
+ public Object invoke(Object target, InterceptionType interceptionType, InvocationContext invocationContext)
+ {
+ try
+ {
+ return interceptorMethods.get(interceptionType).invoke(interceptorInstance, new Object[]{invocationContext});
+ } catch (IllegalAccessException e)
+ {
+ throw new RuntimeException((e));
+ } catch (InvocationTargetException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ public boolean handles(Class<?> clazz)
+ {
+ return this.clazz.equals(clazz);
+ }
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandlerFactory.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandlerFactory.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandlerFactory.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,28 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.proxy;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class SimpleInterceptionHandlerFactory implements InterceptionHandlerFactory {
+
+ public InterceptionHandler createForClass(Class<?> clazz) {
+ return new SimpleInterceptionHandler(clazz);
+ }
+}
Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptor.registry;
+
+import org.jboss.interceptor.model.InterceptionModel;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Metadata store for information on how the an entity of a given type needs to be intercepted.
+ *
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptorRegistry<T>
+{
+ private Map<T, InterceptionModel> interceptionModelMap = new HashMap<T, InterceptionModel>();
+
+ public void registerInterceptionModel(T interceptedEntity, InterceptionModel interceptionModel)
+ {
+ this.interceptionModelMap.put(interceptedEntity, interceptionModel);
+ }
+
+ public InterceptionModel getInterceptionModel(T interceptedEntity)
+ {
+ return this.interceptionModelMap.get(interceptedEntity);
+ }
+}
Added: projects/interceptors/trunk/src/test/java/org/jboss/interceptors/FootballTeam.java
===================================================================
--- projects/interceptors/trunk/src/test/java/org/jboss/interceptors/FootballTeam.java (rev 0)
+++ projects/interceptors/trunk/src/test/java/org/jboss/interceptors/FootballTeam.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptors;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class FootballTeam {
+ private String teamName;
+
+ // an empty-argument constructor is required for proxifycation
+ public FootballTeam() {
+
+ }
+
+ public FootballTeam(String s) {
+ this.teamName = s;
+ }
+
+ public String getName() {
+ InterceptorTestLogger.add(FootballTeam.class, "getName");
+ return teamName;
+ }
+
+ @AroundInvoke
+ public Object itsMe(InvocationContext invocationContext) throws Exception {
+ InterceptorTestLogger.add(FootballTeam.class, "aroundInvokeBefore");
+ Object result = invocationContext.proceed();
+ InterceptorTestLogger.add(FootballTeam.class, "aroundInvokeAfter");
+ return result;
+ }
+}
Added: projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java
===================================================================
--- projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java (rev 0)
+++ projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,115 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptors;
+
+import org.jboss.interceptor.proxy.InterceptorProxyCreatorImpl;
+import org.jboss.interceptor.proxy.InterceptorProxyCreator;
+import org.jboss.interceptor.proxy.SimpleInterceptionHandlerFactory;
+import org.jboss.interceptor.registry.InterceptorRegistry;
+import org.jboss.interceptor.model.InterceptionModelImpl;
+import org.jboss.interceptor.model.InterceptionType;
+import org.jboss.interceptor.utils.InterceptionUtils;
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.Assert;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.util.Arrays;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptionTest {
+ private static final String TEAM_NAME = "Ajax Amsterdam";
+
+ private String[] expectedLoggedValues = {
+ "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_postConstruct",
+ "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_aroundInvokeBefore",
+ "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_aroundInvokeBefore",
+ "org.jboss.interceptors.FootballTeam_aroundInvokeBefore",
+ "org.jboss.interceptors.FootballTeam_getName",
+ "org.jboss.interceptors.FootballTeam_aroundInvokeAfter",
+ "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_aroundInvokeAfter",
+ "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_aroundInvokeAfter",
+ "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_preDestroy"
+
+ };
+
+ @Before
+ public void resetLog() {
+ InterceptorTestLogger.reset();
+ }
+
+ @Test
+ public void testInterception() throws Exception {
+
+ InterceptorRegistry<Class> interceptorRegistry = new InterceptorRegistry<Class>();
+ InterceptionModelImpl interceptionModel = new InterceptionModelImpl();
+ interceptionModel.setInterceptors(InterceptionType.AROUND_INVOKE, FootballTeam.class.getMethod("getName"), Arrays.asList(new Class<?>[]{MyFirstInterceptor.class, MySecondInterceptor.class, FootballTeam.class}));
+ interceptionModel.setInterceptors(InterceptionType.POST_CONSTRUCT, null, Arrays.asList(new Class<?>[]{MyFirstInterceptor.class}));
+ interceptionModel.setInterceptors(InterceptionType.PRE_DESTROY, null, Arrays.asList(new Class<?>[]{MySecondInterceptor.class}));
+ interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ InterceptorProxyCreator interceptorProxyCreator = new InterceptorProxyCreatorImpl(interceptorRegistry, new SimpleInterceptionHandlerFactory());
+ //SoccerTeam proxy = interceptorProxyCreator.createInstrumentedInstance(FootballTeam.class, new Class<?>[]{String.class}, new Object[]{"Poli Timisoara"});
+ FootballTeam proxy = interceptorProxyCreator.createProxyFromInstance(new FootballTeam(TEAM_NAME), FootballTeam.class);
+ InterceptionUtils.executePostConstruct(proxy);
+ Assert.assertEquals(TEAM_NAME, proxy.getName());
+ InterceptionUtils.executePredestroy(proxy);
+ Assert.assertArrayEquals(expectedLoggedValues, InterceptorTestLogger.getLog().toArray());
+
+ }
+
+
+ public static class MyFirstInterceptor {
+
+ @AroundInvoke
+ public Object doAround2(InvocationContext invocationContext) throws Exception {
+ InterceptorTestLogger.add(MyFirstInterceptor.class,"aroundInvokeBefore");
+ Object result = invocationContext.proceed();
+ InterceptorTestLogger.add(MyFirstInterceptor.class, "aroundInvokeAfter");
+ return result;
+ }
+
+ @PostConstruct
+ public Object doAfterConstruction(InvocationContext invocationContext) throws Exception {
+ InterceptorTestLogger.add(MyFirstInterceptor.class, "postConstruct");
+ return invocationContext.proceed();
+ }
+ }
+
+ public static class MySecondInterceptor extends MyFirstInterceptor {
+ @AroundInvoke
+ public Object doAround(InvocationContext invocationContext) throws Exception {
+ InterceptorTestLogger.add(MySecondInterceptor.class, "aroundInvokeBefore");
+ Object result = invocationContext.proceed();
+ InterceptorTestLogger.add(MySecondInterceptor.class, "aroundInvokeAfter");
+ return result;
+ }
+
+ @PreDestroy
+ public Object doneHere(InvocationContext invocationContext) throws Exception {
+ InterceptorTestLogger.add(MySecondInterceptor.class, "preDestroy");
+ return invocationContext.proceed();
+ }
+ }
+}
+
Added: projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptorTestLogger.java
===================================================================
--- projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptorTestLogger.java (rev 0)
+++ projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptorTestLogger.java 2009-09-21 05:27:28 UTC (rev 93793)
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.interceptors;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptorTestLogger {
+
+ private static final List<String> log = new ArrayList<String>();
+
+ public static void add(Class<?> clazz, String logValue) {
+ log.add(clazz.getName() + "_" + logValue);
+ }
+
+ public static List<String> getLog() {
+ return Collections.unmodifiableList(log);
+ }
+
+ public static void reset() {
+ log.clear();
+ }
+}
More information about the jboss-cvs-commits
mailing list