tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1355942 - in /openejb/trunk/openejb/container/openejb-core/src/main: java/org/apache/openejb/config/ java/org/apache/openejb/config/rules/ resources/org/apache/openejb/config/rules/
Date Sun, 01 Jul 2012 16:01:30 GMT
Author: rmannibucau
Date: Sun Jul  1 16:01:28 2012
New Revision: 1355942

URL: http://svn.apache.org/viewvc?rev=1355942&view=rev
Log:
TOMEE-260 basic validation on rest methods

Added:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckRestMethodArePublic.java
Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
    openejb/trunk/openejb/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java?rev=1355942&r1=1355941&r2=1355942&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
Sun Jul  1 16:01:28 2012
@@ -36,6 +36,7 @@ import org.apache.openejb.config.rules.C
 import org.apache.openejb.config.rules.CheckInjectionTargets;
 import org.apache.openejb.config.rules.CheckMethods;
 import org.apache.openejb.config.rules.CheckPersistenceRefs;
+import org.apache.openejb.config.rules.CheckRestMethodArePublic;
 import org.apache.openejb.config.rules.CheckUserTransactionRefs;
 import org.apache.openejb.config.rules.CheckAnnotations;
 import org.apache.openejb.config.rules.CheckIncorrectPropertyNames;
@@ -122,7 +123,8 @@ public class AppValidator {
                 new CheckAsynchronous(),
                 new CheckDescriptorLocation(),
                 new CheckAnnotations(),
-                new CheckIncorrectPropertyNames()
+                new CheckIncorrectPropertyNames(),
+                new CheckRestMethodArePublic()
         };
         if (additionalValidators == null || additionalValidators.length == 0) {
             return defaultRules;

Added: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckRestMethodArePublic.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckRestMethodArePublic.java?rev=1355942&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckRestMethodArePublic.java
(added)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckRestMethodArePublic.java
Sun Jul  1 16:01:28 2012
@@ -0,0 +1,99 @@
+package org.apache.openejb.config.rules;
+
+import org.apache.openejb.config.AppModule;
+import org.apache.openejb.config.EjbModule;
+import org.apache.openejb.config.ValidationContext;
+import org.apache.openejb.config.ValidationRule;
+import org.apache.openejb.config.WebModule;
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.SessionBean;
+
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+public class CheckRestMethodArePublic implements ValidationRule {
+    @Override
+    public void validate(final AppModule appModule) {
+        // valid standalone classes
+        final Collection<String> standAloneClasses = new ArrayList<String>();
+        for (EjbModule ejb : appModule.getEjbModules()) {
+            for (EnterpriseBean bean : ejb.getEjbJar().getEnterpriseBeans()) {
+                if (bean instanceof SessionBean && ((SessionBean) bean).isRestService())
{
+                    standAloneClasses.add(bean.getEjbClass());
+                    valid(ejb.getValidation(), ejb.getClassLoader(), bean.getEjbClass());
+                }
+            }
+        }
+
+        for (WebModule web : appModule.getWebModules()) {
+            // build the list of classes to validate
+            final Collection<String> classes = new ArrayList<String>();
+            classes.addAll(web.getRestClasses());
+            classes.addAll(web.getEjbRestServices());
+
+            for (String app : web.getRestApplications()) {
+                Class<?> clazz;
+                try {
+                    clazz = web.getClassLoader().loadClass(app);
+                } catch (ClassNotFoundException e) {
+                    continue; // managed elsewhere, here we just check methods
+                }
+
+                final Application appInstance;
+                try {
+                    appInstance = (Application) clazz.newInstance();
+                } catch (Exception e) {
+                    continue; // managed elsewhere
+                }
+
+                for (Class<?> rsClass : appInstance.getClasses()) {
+                    classes.add(rsClass.getName());
+                }
+                for (Object rsSingleton : appInstance.getSingletons()) {
+                    classes.add(rsSingleton.getClass().getName());
+                }
+            }
+
+            // try to avoid to valid twice the same classes
+            final Iterator<String> it = classes.iterator();
+            while (it.hasNext()) {
+                final String current = it.next();
+                if (standAloneClasses.contains(current)) {
+                    it.remove();
+                }
+            }
+
+            // valid
+            for (String classname : classes) {
+                valid(web.getValidation(), web.getClassLoader(), classname);
+            }
+
+            classes.clear();
+        }
+
+        standAloneClasses.clear();
+    }
+
+    private void valid(final ValidationContext validation, final ClassLoader classLoader,
final String classname) {
+        Class<?> clazz;
+        try {
+            clazz = classLoader.loadClass(classname);
+        } catch (ClassNotFoundException e) {
+            return; // managed elsewhere
+        }
+
+        while (!Object.class.equals(clazz) && clazz != null) {
+            for (Method mtd : clazz.getDeclaredMethods()) {
+                if (mtd.getAnnotation(Path.class) != null && !Modifier.isPublic(mtd.getModifiers()))
{
+                    validation.warn(mtd.toGenericString(), "rest.method.visibility", "JAX-RS
methods should be public");
+                }
+            }
+            clazz = clazz.getSuperclass();
+        }
+    }
+}

Modified: openejb/trunk/openejb/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties?rev=1355942&r1=1355941&r2=1355942&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
Sun Jul  1 16:01:28 2012
@@ -898,4 +898,8 @@ public interface {0} extends {2}'{}'
 
 1.incorrect.property.name= Property key [{0}] might be incorrect. Did you mean to add [{1}]
?
 2.incorrect.property.name= Property key [{0}] might be incorrect. Did you mean to add [{1}]
?
-3.incorrect.property.name= Property key [{0}] might be incorrect. Did you mean to add [{1}]
?
\ No newline at end of file
+3.incorrect.property.name= Property key [{0}] might be incorrect. Did you mean to add [{1}]
?
+
+1.rest.method.visibility = JAX-RS Method [{0}} should be public.
+2.rest.method.visibility = JAX-RS Method [{0}} should be public.
+3.rest.method.visibility = JAX-RS Method [{0}} should be public.
\ No newline at end of file



Mime
View raw message