Author: dain
Date: Sat Sep 29 20:09:19 2007
New Revision: 580672

URL: http://svn.apache.org/viewvc?rev=580672&view=rev
Log:
Read persistence context annotation using ASM when embedded in tomcat with 
invalid persistence context annotation class

Added:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnn.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnnFactory.java
    
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/PersistenceContextAnnFactoryTest.java
Modified:
    
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentsResolver.java

Modified: 
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java?rev=580672&r1=580671&r2=580672&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
 (original)
+++ 
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
 Sat Sep 29 20:09:19 2007
@@ -190,13 +190,7 @@
 
         // add our magic bits to the catalina sh file
         String openejbJavaagentPath = 
paths.getCatalinaBaseDir().toURI().relativize(paths.getOpenEJBJavaagentJar().toURI()).getPath();
-        String updatedAnnotationApiPath = 
paths.getCatalinaBaseDir().toURI().relativize(paths.getUpdatedAnnotationApiJar().toURI()).getPath();
         String newCatalinaSh = catalinaShOriginal.replace("# ----- Execute The 
Requested Command",
-                "# Update non-compliant Tomcat annotation-api.jar\n" +
-                "if [ -r \"$CATALINA_BASE\"/" + updatedAnnotationApiPath + " 
]; then\n" +
-                "  cp \"$CATALINA_BASE\"/" + updatedAnnotationApiPath + "  
\"$CATALINA_HOME\"/lib/annotations-api.jar \n" +
-                "fi\n" +
-                "\n" +
                 "# Add OpenEJB javaagent\n" +
                 "if [ -r \"$CATALINA_BASE\"/" + openejbJavaagentPath + " ]; 
then\n" +
                 "  JAVA_OPTS=\"\"-javaagent:$CATALINA_BASE/" + 
openejbJavaagentPath + "\" $JAVA_OPTS\"\n" +
@@ -234,13 +228,7 @@
 
         // add our magic bits to the catalina bat file
         openejbJavaagentPath = openejbJavaagentPath.replace('/', '\\');
-        updatedAnnotationApiPath = updatedAnnotationApiPath.replace('/', '\\');
         String newCatalinaBat = catalinaBatOriginal.replace("rem ----- Execute 
The Requested Command",
-                "rem Update non-compliant Tomcat annotation-api.jar\r\n" +
-                "if not exist \"%CATALINA_BASE%\\" + updatedAnnotationApiPath 
+ "\" goto noUpdatedAnnotationJar\r\n" +
-                "copy /Y \"%CATALINA_BASE%\\" + updatedAnnotationApiPath + "\" 
\"%CATALINA_HOME%\\lib\\annotations-api.jar\"\r\n" +
-                ":noUpdatedAnnotationJar\r\n" +
-                "\r\n" +
                 "rem Add OpenEJB javaagent\r\n" +
                 "if not exist \"%CATALINA_BASE%\\" + openejbJavaagentPath + 
"\" goto noOpenEJBJavaagent\r\n" +
                 "set JAVA_OPTS=\"-javaagent:%CATALINA_BASE%\\" + 
openejbJavaagentPath + "\" %JAVA_OPTS%\r\n" +
@@ -251,7 +239,6 @@
         // overwrite the catalina.bat file
         if (writeAll(paths.getCatalinaBatFile(), newCatalinaBat)) {
             addInfo("Add OpenEJB JavaAgent to catalina.bat");
-//            addInfo("Added OpenEJB javaagent to Tomcat catalina.bat file.");
         }
     }
 

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java?rev=580672&r1=580671&r2=580672&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
 Sat Sep 29 20:09:19 2007
@@ -299,7 +299,7 @@
 
                 EntityManagerFactory factory = 
emfLinkResolver.resolveLink(contextInfo.persistenceUnitName, moduleUri);
                 if (factory == null) {
-                    throw new IllegalArgumentException("Persistence unit " + 
contextInfo.persistenceUnitName + " for persistence-context-ref " +
+                    throw new IllegalArgumentException("Persistence unit \"" + 
contextInfo.persistenceUnitName + "\" for persistence-context-ref " +
                             contextInfo.referenceName + " not found");
                 }
 

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=580672&r1=580671&r2=580672&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
 Sat Sep 29 20:09:19 2007
@@ -16,9 +16,8 @@
  */
 package org.apache.openejb.config;
 
-import static org.apache.openejb.util.Join.join;
-import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.DeploymentInfo;
+import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.jee.ActivationConfig;
 import org.apache.openejb.jee.ApplicationClient;
 import org.apache.openejb.jee.AroundInvoke;
@@ -29,6 +28,7 @@
 import org.apache.openejb.jee.EjbRef;
 import org.apache.openejb.jee.EnterpriseBean;
 import org.apache.openejb.jee.ExcludeList;
+import org.apache.openejb.jee.Filter;
 import org.apache.openejb.jee.InitMethod;
 import org.apache.openejb.jee.InjectionTarget;
 import org.apache.openejb.jee.Interceptor;
@@ -37,6 +37,7 @@
 import org.apache.openejb.jee.JndiReference;
 import org.apache.openejb.jee.Lifecycle;
 import org.apache.openejb.jee.LifecycleCallback;
+import org.apache.openejb.jee.Listener;
 import org.apache.openejb.jee.MessageDrivenBean;
 import org.apache.openejb.jee.MethodParams;
 import org.apache.openejb.jee.MethodPermission;
@@ -53,19 +54,18 @@
 import org.apache.openejb.jee.ResourceEnvRef;
 import org.apache.openejb.jee.ResourceRef;
 import org.apache.openejb.jee.SecurityIdentity;
+import org.apache.openejb.jee.SecurityRoleRef;
 import org.apache.openejb.jee.ServiceRef;
+import org.apache.openejb.jee.Servlet;
 import org.apache.openejb.jee.SessionBean;
+import org.apache.openejb.jee.SessionType;
 import org.apache.openejb.jee.StatefulBean;
 import org.apache.openejb.jee.StatelessBean;
+import org.apache.openejb.jee.TimerConsumer;
 import org.apache.openejb.jee.TransAttribute;
 import org.apache.openejb.jee.TransactionType;
-import org.apache.openejb.jee.SecurityRoleRef;
-import org.apache.openejb.jee.TimerConsumer;
-import org.apache.openejb.jee.SessionType;
 import org.apache.openejb.jee.WebApp;
-import org.apache.openejb.jee.Servlet;
-import org.apache.openejb.jee.Filter;
-import org.apache.openejb.jee.Listener;
+import static org.apache.openejb.util.Join.join;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 import org.apache.xbean.finder.ClassFinder;
@@ -74,11 +74,12 @@
 import javax.annotation.PreDestroy;
 import javax.annotation.Resource;
 import javax.annotation.Resources;
+import javax.annotation.security.DeclareRoles;
 import javax.annotation.security.DenyAll;
 import javax.annotation.security.PermitAll;
 import javax.annotation.security.RolesAllowed;
 import javax.annotation.security.RunAs;
-import javax.annotation.security.DeclareRoles;
+import javax.ejb.ApplicationException;
 import javax.ejb.EJB;
 import javax.ejb.EJBHome;
 import javax.ejb.EJBLocalHome;
@@ -100,19 +101,17 @@
 import javax.ejb.TransactionAttributeType;
 import javax.ejb.TransactionManagement;
 import javax.ejb.TransactionManagementType;
-import javax.ejb.ApplicationException;
 import javax.interceptor.ExcludeClassInterceptors;
 import javax.interceptor.ExcludeDefaultInterceptors;
 import javax.interceptor.Interceptors;
+import javax.jws.WebService;
 import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceContexts;
-import javax.persistence.PersistenceProperty;
 import javax.persistence.PersistenceUnit;
 import javax.persistence.PersistenceUnits;
+import javax.xml.ws.WebServiceProvider;
 import javax.xml.ws.WebServiceRef;
 import javax.xml.ws.WebServiceRefs;
-import javax.xml.ws.WebServiceProvider;
-import javax.jws.WebService;
 import java.io.File;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -120,13 +119,13 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
-import java.util.Properties;
-import java.util.HashMap;
-import java.util.HashSet;
 
 /**
  * @version $Rev$ $Date$
@@ -1092,27 +1091,30 @@
             // @PersistenceContext
             //
 
+            PersistenceContextAnnFactory pcFactory = new 
PersistenceContextAnnFactory();
             List<PersistenceContext> persistenceContextList = new 
ArrayList<PersistenceContext>();
             for (Class<?> clazz : 
classFinder.findAnnotatedClasses(PersistenceContexts.class)) {
                 PersistenceContexts persistenceContexts = 
clazz.getAnnotation(PersistenceContexts.class);
                 
persistenceContextList.addAll(Arrays.asList(persistenceContexts.value()));
+                pcFactory.addAnnotations(clazz);
             }
             for (Class<?> clazz : 
classFinder.findAnnotatedClasses(PersistenceContext.class)) {
                 PersistenceContext persistenceContext = 
clazz.getAnnotation(PersistenceContext.class);
                 persistenceContextList.add(persistenceContext);
+                pcFactory.addAnnotations(clazz);
             }
             for (PersistenceContext pCtx : persistenceContextList) {
-                buildPersistenceContext(consumer, pCtx, null);
+                buildPersistenceContext(consumer, pcFactory.create(pCtx, 
null), null);
             }
             for (Field field : 
classFinder.findAnnotatedFields(PersistenceContext.class)) {
                 PersistenceContext pCtx = 
field.getAnnotation(PersistenceContext.class);
                 Member member = new FieldMember(field);
-                buildPersistenceContext(consumer, pCtx, member);
+                buildPersistenceContext(consumer, pcFactory.create(pCtx, 
member), member);
             }
             for (Method method : 
classFinder.findAnnotatedMethods(PersistenceContext.class)) {
                 PersistenceContext pCtx = 
method.getAnnotation(PersistenceContext.class);
                 Member member = new MethodMember(method);
-                buildPersistenceContext(consumer, pCtx, member);
+                buildPersistenceContext(consumer, pcFactory.create(pCtx, 
member), member);
             }
 
         }
@@ -1299,7 +1301,7 @@
          * @param member
          * @throws OpenEJBException
          */
-        private void buildPersistenceContext(JndiConsumer consumer, 
PersistenceContext persistenceContext, Member member) throws OpenEJBException {
+        private void buildPersistenceContext(JndiConsumer consumer, 
PersistenceContextAnn persistenceContext, Member member) throws 
OpenEJBException {
             String refName = persistenceContext.name();
 
             if (refName.equals("")) {
@@ -1313,22 +1315,20 @@
             PersistenceContextRef persistenceContextRef = 
consumer.getPersistenceContextRefMap().get(refName);
             if (persistenceContextRef == null) {
                 persistenceContextRef = new PersistenceContextRef();
-                    
persistenceContextRef.setPersistenceUnitName(persistenceContext.unitName());
+                
persistenceContextRef.setPersistenceUnitName(persistenceContext.unitName());
                 persistenceContextRef.setPersistenceContextRefName(refName);
-                if (persistenceContext.type() == 
javax.persistence.PersistenceContextType.EXTENDED) {
+                if ("EXTENDED".equalsIgnoreCase(persistenceContext.type())) {
                     
persistenceContextRef.setPersistenceContextType(PersistenceContextType.EXTENDED);
                 } else {
                     
persistenceContextRef.setPersistenceContextType(PersistenceContextType.TRANSACTION);
                 }
                 consumer.getPersistenceContextRef().add(persistenceContextRef);
             } else {
-                if (persistenceContextRef.getPersistenceUnitName() == null || 
("").equals(persistenceContextRef.getPersistenceUnitName()))
-                {
+                if (persistenceContextRef.getPersistenceUnitName() == null || 
("").equals(persistenceContextRef.getPersistenceUnitName())) {
                     
persistenceContextRef.setPersistenceUnitName(persistenceContext.unitName());
                 }
-                if (persistenceContextRef.getPersistenceContextType() == null 
|| ("").equals(persistenceContextRef.getPersistenceContextType()))
-                {
-                    if (persistenceContext.type() == 
javax.persistence.PersistenceContextType.EXTENDED) {
+                if (persistenceContextRef.getPersistenceContextType() == null 
|| ("").equals(persistenceContextRef.getPersistenceContextType())) {
+                    if 
("EXTENDED".equalsIgnoreCase(persistenceContext.type())) {
                         
persistenceContextRef.setPersistenceContextType(PersistenceContextType.EXTENDED);
                     } else {
                         
persistenceContextRef.setPersistenceContextType(PersistenceContextType.TRANSACTION);
@@ -1342,19 +1342,18 @@
                 
persistenceContextRef.setPersistenceProperty(persistenceProperties);
             }
 
-            Property property = null;
-            for (PersistenceProperty persistenceProperty : 
persistenceContext.properties()) {
+            for (Map.Entry<String, String> persistenceProperty : 
persistenceContext.properties().entrySet()) {
                 boolean flag = true;
                 for (Property prpty : persistenceProperties) {
-                    if (prpty.getName().equals(persistenceProperty.name())) {
+                    if (prpty.getName().equals(persistenceProperty.getKey())) {
                         flag = false;
                         break;
                     }
                 }
                 if (flag) {
-                    property = new Property();
-                    property.setName(persistenceProperty.name());
-                    property.setValue(persistenceProperty.value());
+                    Property property = new Property();
+                    property.setName(persistenceProperty.getKey());
+                    property.setValue(persistenceProperty.getValue());
                     persistenceProperties.add(property);
                 }
             }

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentsResolver.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentsResolver.java?rev=580672&r1=580671&r2=580672&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentsResolver.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentsResolver.java
 Sat Sep 29 20:09:19 2007
@@ -71,7 +71,7 @@
 
         ////////////////////////////////
         //
-        //  Unpacked "Jar" directory
+        //  Unpacked "Jar" directory with descriptor
         //
         ////////////////////////////////
         File ejbJarXml = new File(dir, "META-INF" + File.separator + 
"ejb-jar.xml");
@@ -90,28 +90,35 @@
             return;
         }
 
-        HashMap<String, URL> files = new HashMap<String, URL>();
-        DeploymentLoader.scanDir(dir, files, "");
-        for (String fileName : files.keySet()) {
-            if (fileName.endsWith(".class")) {
-                if (!jarList.contains(dir.getAbsolutePath())) {
-                    jarList.add(dir.getAbsolutePath());
-                }
-                return;
+        ////////////////////////////////
+        //
+        //  Directory contains Jar files
+        //
+        ////////////////////////////////
+        boolean hasNestedArchives = false;
+        for (File file : dir.listFiles()) {
+            if (file.getName().endsWith(".jar") || 
file.getName().endsWith(".ear")) {
+                if (jarList.contains(file.getAbsolutePath())) continue;
+                jarList.add(file.getAbsolutePath());
+                hasNestedArchives = true;
             }
         }
 
         ////////////////////////////////
         //
-        //  Directory contains Jar files
+        //  Unpacked "Jar" directory w/o descriptor
         //
         ////////////////////////////////
-        for (String fileName : files.keySet()) {
-            if (fileName.endsWith(".jar") || fileName.endsWith(".ear")) {
-                File jar = new File(dir, fileName);
-
-                if (jarList.contains(jar.getAbsolutePath())) continue;
-                jarList.add(jar.getAbsolutePath());
+        if (!hasNestedArchives) {
+            HashMap<String, URL> files = new HashMap<String, URL>();
+            DeploymentLoader.scanDir(dir, files, "");
+            for (String fileName : files.keySet()) {
+                if (fileName.endsWith(".class")) {
+                    if (!jarList.contains(dir.getAbsolutePath())) {
+                        jarList.add(dir.getAbsolutePath());
+                    }
+                    return;
+                }
             }
         }
     }

Added: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnn.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnn.java?rev=580672&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnn.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnn.java
 Sat Sep 29 20:09:19 2007
@@ -0,0 +1,27 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.openejb.config;
+
+import java.util.Map;
+
+public interface PersistenceContextAnn {
+    public abstract String name();
+    public abstract String unitName();
+    public abstract String type();
+    public abstract Map<String,String> properties();
+}

Added: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnnFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnnFactory.java?rev=580672&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnnFactory.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceContextAnnFactory.java
 Sat Sep 29 20:09:19 2007
@@ -0,0 +1,260 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.openejb.config;
+
+import org.apache.openejb.OpenEJBException;
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.commons.EmptyVisitor;
+
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceProperty;
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class PersistenceContextAnnFactory {
+    private static boolean useAsm;
+    static {
+        boolean isPersistenceContextAnnotationValid = false;
+        try {
+            // Tomcat persistence context class is missing the properties 
method
+            Class<?> persistenceContextClass = 
Class.forName("javax.persistence.PersistenceContext");
+            persistenceContextClass.getMethod("properties", (Class[]) null);
+            isPersistenceContextAnnotationValid = true;
+        } catch (Exception e) {
+        }
+        useAsm = !isPersistenceContextAnnotationValid;
+    }
+
+    public Map<String, AsmPersistenceContext> contexts = new HashMap<String, 
AsmPersistenceContext>();
+    private final Set<String> processed = new HashSet<String>();
+
+    public void addAnnotations(Class c) throws OpenEJBException {
+        if (!useAsm) return;
+        if (processed.contains(c.getName())) return;
+
+        try {
+            URL u = c.getResource("/" + c.getName().replace('.', '/') + 
".class");
+            ClassReader r = new ClassReader(u.openStream());
+            r.accept(new PersistenceContextReader(), true);
+        } catch (IOException e) {
+            throw new OpenEJBException("Unable to read class " + c.getName());
+        }
+
+        processed.add(c.getName());
+    }
+
+    public PersistenceContextAnn create(PersistenceContext persistenceContext, 
AnnotationDeployer.Member member) throws OpenEJBException {
+        if (useAsm) {
+            if (member != null) {
+                addAnnotations(member.getDeclaringClass());
+            }
+
+            String name = persistenceContext.name();
+            if (name == null || name.equals("")) {
+                name = (member == null) ? null : 
member.getDeclaringClass().getName() + "/" + member.getName();
+            }
+
+            AsmPersistenceContext asmPersistenceContext = contexts.get(name);
+            if (asmPersistenceContext == null) {
+                throw new NullPointerException("PersistenceContext " + name + 
" not found");
+            }
+            return asmPersistenceContext;
+        } else {
+            return new DirectPersistenceContext(persistenceContext);
+        }
+    }
+
+    private static class DirectPersistenceContext implements 
PersistenceContextAnn {
+        private final PersistenceContext persistenceContext;
+
+
+        public DirectPersistenceContext(PersistenceContext persistenceContext) 
{
+            if (persistenceContext == null) throw new 
NullPointerException("persistenceContext is null");
+            this.persistenceContext = persistenceContext;
+        }
+
+        public String name() {
+            return persistenceContext.name();
+        }
+
+        public String unitName() {
+            return persistenceContext.unitName();
+        }
+
+        public String type() {
+            if (persistenceContext.type() == null) return null;
+            return persistenceContext.type().toString();
+        }
+
+        public Map<String, String> properties() {
+            Map<String, String> properties = new LinkedHashMap<String, 
String>();
+            for (PersistenceProperty property : 
persistenceContext.properties()) {
+                properties.put(property.name(), property.value());
+            }
+            return properties;
+        }
+
+        public String toString() {
+            return persistenceContext.toString();
+        }
+    }
+
+    private static class AsmPersistenceContext implements 
PersistenceContextAnn {
+        public String name;
+        public String unitName;
+        public String type;
+        public final Map<String, String> properties = new 
LinkedHashMap<String, String>();
+
+        public String name() {
+            return name;
+        }
+
+        public String unitName() {
+            return unitName;
+        }
+
+        public String type() {
+            return type;
+        }
+
+        public Map<String, String> properties() {
+            return properties;
+        }
+
+        public String toString() {
+            return "@PersistenceContext(name = \"" + name + "\", unitName = 
\"" + unitName + "\", type = PersistenceContextType." + type + ", " + 
properties + ")";
+        }
+    }
+
+    private class PersistenceContextReader extends EmptyVisitor {
+        private String className;
+        private String currentName;
+
+        public void visit(int version, int access, String name, String 
signature, String superName, String[] interfaces) {
+            className = name.replace("/", ".");
+            super.visit(version, access, name, signature, superName, 
interfaces);
+        }
+
+        public FieldVisitor visitField(int access, String name, String desc, 
String signature, Object value) {
+            currentName = name;
+            return super.visitField(access, name, desc, signature, value);
+        }
+
+        public MethodVisitor visitMethod(int access, String name, String desc, 
String signature, String[] exceptions) {
+            currentName = name;
+
+            // setFoo -> foo
+            if (currentName.startsWith("set")) {
+                currentName = currentName.substring(3);
+            }
+            if (currentName.length() > 0) {
+                currentName = Character.toLowerCase(currentName.charAt(0)) + 
currentName.substring(1);
+            }
+
+            return super.visitMethod(access, name, desc, signature, 
exceptions);
+        }
+
+        public AnnotationVisitor visitAnnotation(String desc, boolean visible) 
{
+            return visitAnnotation(null, desc);
+        }
+
+        public AnnotationVisitor visitAnnotation(String name, String desc) {
+            if ("Ljavax/persistence/PersistenceContext;".equals(desc)) {
+                PersistenceContextVisitor visitor = new 
PersistenceContextVisitor(className, currentName);
+                return visitor;
+            } else if ("Ljavax/persistence/PersistenceContexts;".equals(desc)) 
{
+                return this;
+            }
+            return new EmptyVisitor();
+        }
+
+        public AnnotationVisitor visitParameterAnnotation(int i, String 
string, boolean b) {
+            return new EmptyVisitor();
+        }
+
+        public AnnotationVisitor visitAnnotationDefault() {
+            return new EmptyVisitor();
+        }
+
+        public AnnotationVisitor visitArray(String string) {
+            return this;
+        }
+    }
+
+    private class PersistenceContextVisitor implements AnnotationVisitor {
+        private AsmPersistenceContext persistenceContext = new 
AsmPersistenceContext();
+
+        public PersistenceContextVisitor(String className, String memberName) {
+            persistenceContext.name = className + "/" + memberName;
+        }
+
+        public void visit(String name, Object value) {
+            setValue(name, value == null ? null : value.toString());
+        }
+
+        public void visitEnum(String name, String type, String value) {
+            setValue(name, value == null ? null : value.toString());
+        }
+
+        public AnnotationVisitor visitAnnotation(String name, String desc) {
+            setValue(name, desc);
+            return null;
+        }
+
+        private void setValue(String name, String value) {
+            if ("name".equals(name)) {
+                persistenceContext.name = value;
+            } else if ("unitName".equals(name)) {
+                persistenceContext.unitName = value;
+            } else if ("type".equals(name)) {
+                persistenceContext.type = value;
+            }
+        }
+
+        public AnnotationVisitor visitArray(String string) {
+            return new EmptyVisitor() {
+                private String name;
+                private String value;
+
+                public void visit(String n, Object v) {
+                    if ("name".equals(n)) {
+                        name = (v == null ? null : v.toString());
+                    } else if ("value".equals(n)) {
+                        value = (v == null ? null : v.toString());
+                    }
+                }
+
+                public void visitEnd() {
+                    persistenceContext.properties.put(name, value);
+                }
+            };
+        }
+
+        public void visitEnd() {
+            contexts.put(persistenceContext.name, persistenceContext);
+        }
+    }
+}

Added: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/PersistenceContextAnnFactoryTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/PersistenceContextAnnFactoryTest.java?rev=580672&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/PersistenceContextAnnFactoryTest.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/PersistenceContextAnnFactoryTest.java
 Sat Sep 29 20:09:19 2007
@@ -0,0 +1,113 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.openejb.config;
+
+import junit.framework.TestCase;
+
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.PersistenceContexts;
+import javax.persistence.PersistenceProperty;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.HashMap;
+
+public class PersistenceContextAnnFactoryTest extends TestCase {
+    public void test() throws Exception {
+        Field useAsm = 
PersistenceContextAnnFactory.class.getDeclaredField("useAsm");
+        useAsm.setAccessible(true);
+        useAsm.set(null, true);
+        
+        PersistenceContextAnnFactory factory = new 
PersistenceContextAnnFactory();
+        factory.addAnnotations(Foo.class);
+
+        for (PersistenceContext annotation : 
Foo.class.getAnnotation(PersistenceContexts.class).value()) {
+            assertEq(annotation, factory.create(annotation, null));
+        }
+        
+        PersistenceContext annotation = 
Foo.class.getAnnotation(PersistenceContext.class);
+        assertEq(annotation, factory.create(annotation, null));
+
+        for (Field field : Foo.class.getFields()) {
+            annotation = field.getAnnotation(PersistenceContext.class);
+            assertEq(annotation, factory.create(annotation, new 
AnnotationDeployer.FieldMember(field)));
+        }
+
+        for (Method method : Foo.class.getMethods()) {
+            annotation = method.getAnnotation(PersistenceContext.class);
+            if (annotation != null) {
+                assertEq(annotation, factory.create(annotation, new 
AnnotationDeployer.MethodMember(method)));
+            }
+        }
+    }
+
+    private static void assertEq(PersistenceContext annotation, 
PersistenceContextAnn wrapper) {
+        if (annotation.name().length() > 0) {
+            assertEquals(annotation.name(), wrapper.name());
+        }
+        assertEquals(annotation.unitName(), wrapper.unitName());
+        assertEquals(annotation.type().toString(), wrapper.type());
+
+        Map<String,String> properties = new HashMap<String,String>();
+        for (PersistenceProperty property : annotation.properties()) {
+            properties.put(property.name(), property.value());
+        }
+        assertEquals(properties, wrapper.properties());
+    }
+
+    @PersistenceContexts({
+            @PersistenceContext(name = "classPCs1", unitName = "CPCs1u", type 
= PersistenceContextType.EXTENDED, properties = {
+                    @PersistenceProperty(name = "classPCs1-1", value = 
"CPCs1"),
+                    @PersistenceProperty(name = "classPCs1-2", value = "CPCs2")
+            }),
+            @PersistenceContext(name = "classPCs2", unitName = "CPCs2u", type 
= PersistenceContextType.EXTENDED, properties = {
+                    @PersistenceProperty(name = "classPCs2-1", value = 
"CPCs1"),
+                    @PersistenceProperty(name = "classPCs2-2", value = "CPCs2")
+            })
+    })
+    @PersistenceContext(name = "class", unitName = "cu", type = 
PersistenceContextType.EXTENDED, properties = {
+            @PersistenceProperty(name = "class1", value = "c1"),
+            @PersistenceProperty(name = "class2", value = "c2")
+    })
+    public static class Foo {
+        @PersistenceContext(name = "field", unitName = "fu", type = 
PersistenceContextType.EXTENDED, properties = {
+                @PersistenceProperty(name = "field1", value = "f1"),
+                @PersistenceProperty(name = "field2", value = "f1")
+        })
+        public Object field;
+
+        @PersistenceContext(name = "method", unitName = "mu", type = 
PersistenceContextType.EXTENDED, properties = {
+                @PersistenceProperty(name = "method1", value = "m1"),
+                @PersistenceProperty(name = "method2", value = "m2")
+        })
+        public void method() {}
+
+        @PersistenceContext(unitName = "myfu", type = 
PersistenceContextType.EXTENDED, properties = {
+                @PersistenceProperty(name = "myField1", value = "myf1"),
+                @PersistenceProperty(name = "myField2", value = "myf1")
+        })
+        public Object myField;
+
+        @PersistenceContext(unitName = "mymu", type = 
PersistenceContextType.EXTENDED, properties = {
+                @PersistenceProperty(name = "myMethod1", value = "mym1"),
+                @PersistenceProperty(name = "myMethod2", value = "mym2")
+        })
+        public void setMyMethod() {}
+    }
+}


Reply via email to