Author: vishwanathk
Date: Mon Aug 20 21:44:21 2012
New Revision: 1375245

URL: http://svn.apache.org/viewvc?rev=1375245&view=rev
Log:
TOMEE-381 Warn for incorrect location of descriptors

Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
    
openejb/trunk/openejb/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java?rev=1375245&r1=1375244&r2=1375245&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
 Mon Aug 20 21:44:21 2012
@@ -16,78 +16,105 @@
  */
 package org.apache.openejb.config.rules;
 
+import org.apache.openejb.config.AppModule;
+import org.apache.openejb.config.DeploymentModule;
 import org.apache.openejb.config.EjbModule;
+import org.apache.openejb.config.WebModule;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
 import org.apache.xbean.finder.ResourceFinder;
 
 import java.io.File;
-import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
+
 
 public class CheckDescriptorLocation extends ValidationBase {
 
 
-    EjbModule currentModule;
+    private static final Logger logger = 
Logger.getInstance(LogCategory.OPENEJB_STARTUP_VALIDATION, 
"org.apache.openejb.config.rules");
 
     @Override
-    public void validate(EjbModule ejbModule) {
-        URL baseUrl = null;
-        this.currentModule = ejbModule;
-        File file = ejbModule.getFile();
-        if (file != null) {
-            validateDescriptorsAreNotPlacedInRoot(file);
-        }
+    public void validate(AppModule appModule){
 
-    }
+        List<String> validated = new ArrayList<String>();
 
-    private void validateDescriptorsAreNotPlacedInRoot(File file) {
-        ResourceFinder resourceFinder = null;
-        try {
-            resourceFinder = new ResourceFinder(file.toURI().toURL());
-        } catch (MalformedURLException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+        for(WebModule webModule:appModule.getWebModules())
+        {
+            validated.add(webModule.getModuleId());
+            validateWebModule(webModule);
+        }
+
+        for(EjbModule ejbModule:appModule.getEjbModules())
+        {
+            //without this check, 
CheckDescriptorLocationTest#testWarWithDescriptorInRoot() would fail
+            if(!validated.contains(ejbModule.getModuleId()))
+            {
+                validateEjbModule(ejbModule);
+            }
         }
-        //ResourceFinder resourceFinder = new ResourceFinder(baseUrl);
-        Map<String, URL> descriptorsPlacedInWrongLocation = 
getDescriptorsPlacedInWrongLocation(resourceFinder);
 
-        if (descriptorsPlacedInWrongLocation.size() > 0) {
-            
warnIncorrectLocationOfDescriptors(descriptorsPlacedInWrongLocation);
+    }
 
+    private void validateWebModule(DeploymentModule webModule) {
+        URL baseUrl = null;
+        this.module= webModule;
+        List<String> descriptorsToSearch = 
Arrays.asList("beans.xml","ejb-jar.xml","faces-config.xml");
+        File file = webModule.getFile();
+        if (file != null) {
+            try {
+                URL rootOfArchive=file.toURI().toURL();
+                URL metaInf=new 
URL(rootOfArchive.toExternalForm()+"META-INF/");
+                Map<String, URL> incorrectlyLocatedDescriptors
+                        = retrieveDescriptors(file, descriptorsToSearch, 
rootOfArchive, metaInf);
+                
warnIncorrectLocationOfDescriptors(incorrectlyLocatedDescriptors,"WEB-INF");
+            } catch (MalformedURLException ignored) {
+                    //ignored
+            }
         }
-    }
 
-    private static Map<String, URL> getDescriptorsPlacedInWrongLocation(
-            ResourceFinder finder) {
+    }
 
-        Map<String, URL> descriptorsMap = null;
-        try {
-            descriptorsMap = retrieveDescriptorsPlacedInWrongLocation(finder);
-        } catch (IOException e) {
-            e.printStackTrace();
+    public void validateEjbModule(DeploymentModule deploymentModule) {
+        URL baseUrl = null;
+        this.module= deploymentModule;
+        List<String> descriptorsToSearch = 
Arrays.asList("beans.xml","ejb-jar.xml","openejb-jar.xml","env-entries.properties");
+        File file = deploymentModule.getFile();
+        if (file != null) {
+            try {
+                URL rootOfArchive=file.toURI().toURL();
+                Map<String, URL> incorrectlyLocatedDescriptors
+                        = retrieveDescriptors(file, descriptorsToSearch, 
rootOfArchive);
+                
warnIncorrectLocationOfDescriptors(incorrectlyLocatedDescriptors,"META-INF");
+            } catch (MalformedURLException ignored) {
+                  //ignored
+            }
         }
-        return descriptorsMap;
+
     }
 
+    private static Map<String,URL> retrieveDescriptors(File file, List<String> 
descriptorsToSearch, URL... locationsToSearch ){
+      final Map<String,URL>  descriptorAndWrongLocation = new 
HashMap<String,URL>();
 
-    private static Map<String, URL> retrieveDescriptorsPlacedInWrongLocation(
-            ResourceFinder finder) throws IOException {
-        final Map<String, URL> map = new HashMap<String, URL>();
-        String[] known = {"web.xml", "ejb-jar.xml", "openejb-jar.xml", 
"env-entries.properties", "beans.xml", "ra.xml", "application.xml", 
"application-client.xml", "persistence.xml"};
-        for (String descriptor : known) {
-            final URL url = finder.getResource(descriptor);
-            if (url != null) map.put(descriptor, url);
-        }
 
-        return map;
+            ResourceFinder finder = new ResourceFinder(locationsToSearch);
+            for(String descriptor:descriptorsToSearch)
+            {
+                URL resource = finder.getResource(descriptor);
+                if(resource!=null)
+                {
+                   descriptorAndWrongLocation.put(descriptor,resource);
+                }
+            }
+
+        return descriptorAndWrongLocation;
     }
 
-    private void warnIncorrectLocationOfDescriptors(Map<String, URL> 
descriptorsPlacedInWrongLocation) {
+    private void warnIncorrectLocationOfDescriptors(Map<String, URL> 
descriptorsPlacedInWrongLocation, String expectedLocation) {
         for (Map.Entry<String, URL> map : 
descriptorsPlacedInWrongLocation.entrySet()) {
 
-            warn(currentModule.toString(), "descriptor.incorrectLocation", 
map.getKey(), map.getValue());
+            warn(this.module.toString(), "descriptor.incorrectLocation", 
map.getValue().toExternalForm(), expectedLocation);
         }
     }
 }
\ No newline at end of file

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=1375245&r1=1375244&r2=1375245&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
 Mon Aug 20 21:44:21 2012
@@ -873,9 +873,9 @@ public interface {0} extends {2}'{}'
 2.specializes.extendsSimpleBean = @Specializes EJB extends non-EJB {0}
 3.specializes.extendsSimpleBean = EJB class annotated with @Specializes must 
extend another EJB.  The concept of specialization is a step beyond Java 
inheritance in that an @Specializes bean fully replaces another beans role in 
the system, inheriting all its meta-data in the process.  The @Specializes EJB 
must directly subclass the bean it replaces and that bean must also be an EJB.  
Please fix class {0}.
 
-1.descriptor.incorrectLocation = Descriptor placed in incorrect location
-2.descriptor.incorrectLocation = Descriptor {0} placed in incorrect location 
{1}
-3.descriptor.incorrectLocation = Found descriptor file {0} directly under 
base-directory {1}. Descriptors are to be placed at base-directory/META-INF or 
base-directory/WEB-INF 
+1.descriptor.incorrectLocation = Incorrect location of file {0} The file 
should be in {1} directory
+2.descriptor.incorrectLocation = Incorrect location of file {0} The file 
should be in {1} directory
+3.descriptor.incorrectLocation = Incorrect location of file {0} The file 
should be in {1} directory
 
 # TODO improve this message.  It's a little short and not helpful
 1.cdi.injectionPointOnNonBean = Invalid @Inject InjectionPoint (see CDI 1.0 
section 5.5.7)

Modified: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java?rev=1375245&r1=1375244&r2=1375245&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
 Mon Aug 20 21:44:21 2012
@@ -21,6 +21,7 @@ import org.apache.openejb.config.rules.K
 import org.apache.openejb.config.rules.Keys;
 import org.apache.openejb.config.rules.ValidationRunner;
 import org.apache.openejb.util.Archives;
+import org.apache.openejb.util.WebArchives;
 import org.junit.runner.RunWith;
 
 import javax.ejb.Stateless;
@@ -32,24 +33,60 @@ import java.util.Map;
 @RunWith(ValidationRunner.class)
 public class CheckDescriptorLocationTest {
 
-    public File jarFile;
-    public static final String JAR_FILENAME_PREFIX = "ValTest";
+ public static final String FILENAME_PREFIX = "ValTest";
 
     @Keys({@Key(value = "descriptor.incorrectLocation", type = 
KeyType.WARNING)})
-    public AppModule testWebinfJar() throws Exception {
+    public AppModule testJar() throws Exception {
         Map<String, String> map = new HashMap<String, String>();
         map.put("ejb-jar.xml", "<ejb-jar/>"); // Place the descriptor in
         // incorrect location (directly
         // under root)
 
-        jarFile = Archives.jarArchive(map, JAR_FILENAME_PREFIX, FooBean.class);
-
+        File jarFile = Archives.jarArchive(map,FILENAME_PREFIX, FooBean.class);
         DeploymentLoader loader = new DeploymentLoader();
         AppModule appModule = loader.load(jarFile);
 
         return appModule;
     }
 
+
+    @Keys({@Key(value = "descriptor.incorrectLocation", type = 
KeyType.WARNING, count=1)})
+    public AppModule testWarWithDescriptorInMetaInf() throws Exception {
+        Map<String, String> map = new HashMap<String, String>();
+        map.put("META-INF/ejb-jar.xml", "<ejb-jar/>");
+        map.put("WEB-INF/web.xml","<web-app/>");
+
+        // Place the descriptor in
+        // incorrect location (directly
+        // under root)
+        File warFile = WebArchives.warArchive(map, FILENAME_PREFIX, 
FooBean.class);
+
+        DeploymentLoader loader = new DeploymentLoader();
+        AppModule appModule = loader.load(warFile);
+
+        return appModule;
+    }
+
+
+    @Keys({@Key(value = "descriptor.incorrectLocation", type = 
KeyType.WARNING, count=1)})
+    public AppModule testWarWithDescriptorInRoot() throws Exception {
+        Map<String, String> map = new HashMap<String, String>();
+        map.put("ejb-jar.xml", "<ejb-jar/>");
+        map.put("WEB-INF/web.xml","<web-app/>");
+
+        // Place the descriptor in
+        // incorrect location (directly
+        // under root)
+        File warFile = WebArchives.warArchive(map, FILENAME_PREFIX, 
FooBean.class);
+
+        DeploymentLoader loader = new DeploymentLoader();
+        AppModule appModule = loader.load(warFile);
+
+        return appModule;
+    }
+
+
+
     @Stateless
     class FooBean {
         // need to add this @AroundInvoke to cause validation to fail. 
Validation does not

Modified: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java?rev=1375245&r1=1375244&r2=1375245&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
 Mon Aug 20 21:44:21 2012
@@ -50,7 +50,7 @@ public class CheckDescriptorLocationTest
 
     private void deleteOrMarkForDelete(File file) {
         if (file.getName().contains(
-                CheckDescriptorLocationTest.JAR_FILENAME_PREFIX)) {
+                CheckDescriptorLocationTest.FILENAME_PREFIX)) {
 
             boolean deleted = file.delete();
             if (!deleted) {


Reply via email to