Author: dblevins
Date: Mon Sep  5 01:19:32 2011
New Revision: 1165140

URL: http://svn.apache.org/viewvc?rev=1165140&view=rev
Log:
Patch from Vishwanath, OPENEJB-1570: Validation: look for misplaced descriptors 
and warn
Thanks, Vishwanath! Very excellent functionality!

Added:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
   (with props)
    
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
   (with props)
    
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
   (with props)
    
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/Archives.java
   (with props)
Modified:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Exceptions.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/StringUtilities.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties
    
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java?rev=1165140&r1=1165139&r2=1165140&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
 Mon Sep  5 01:19:32 2011
@@ -41,11 +41,13 @@ import org.apache.openejb.jee.oejb3.Open
 import org.apache.openejb.loader.Options;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.ContainerSystem;
+import org.apache.openejb.util.Exceptions;
 import org.apache.openejb.util.Join;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.OptionsLog;
 import org.apache.openejb.util.ServiceManagerProxy;
+import org.apache.openejb.util.StringUtilities;
 import org.apache.xbean.naming.context.ContextFlyweight;
 
 import javax.ejb.EJBException;
@@ -406,8 +408,7 @@ public class OpenEjbContainer extends EJ
 
 
             if (moduleLocations.isEmpty()) {
-
-                throw new NoModulesFoundException("No modules to deploy 
found");
+               throw Exceptions.newNoModulesFoundException();
 
             }
 

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java?rev=1165140&r1=1165139&r2=1165140&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AppValidator.java
 Mon Sep  5 01:19:32 2011
@@ -97,7 +97,8 @@ public class AppValidator {
                 new CheckPersistenceRefs(),
                 new CheckDependsOn(),
                 new CheckUserTransactionRefs(),
-                new CheckAsynchronous()
+                new CheckAsynchronous(),
+                new CheckDescriptorLocation()
         };
         return rules;
     }

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java?rev=1165140&r1=1165139&r2=1165140&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 Mon Sep  5 01:19:32 2011
@@ -376,6 +376,11 @@ public class ConfigurationFactory implem
                 }
 
 
+                if (jarFiles.size() == 0) {
+                       logger.warning("config.noModulesFoundToDeploy");
+
+                }
+
             } catch (OpenEJBException alreadyHandled) {
             }
 

Added: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java?rev=1165140&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
 Mon Sep  5 01:19:32 2011
@@ -0,0 +1,100 @@
+/**
+ * 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.rules;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.openejb.config.AppModule;
+import org.apache.openejb.config.EjbModule;
+import org.apache.xbean.finder.ResourceFinder;
+
+public class CheckDescriptorLocation extends ValidationBase {
+
+
+       EjbModule currentModule;
+       
+       @Override
+       public void  validate(EjbModule ejbModule)
+       {
+               URL baseUrl=null;
+               this.currentModule=ejbModule;
+               File file=ejbModule.getFile();
+               if(file!=null)
+               {
+               validateDescriptorsAreNotPlacedInRoot(file);
+               }
+       
+       }
+       
+       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();
+               }
+               //ResourceFinder resourceFinder = new ResourceFinder(baseUrl);
+        Map<String, URL> descriptorsPlacedInWrongLocation = 
getDescriptorsPlacedInWrongLocation(resourceFinder);
+        
+        if(descriptorsPlacedInWrongLocation.size()>0)
+        {
+               
warnIncorrectLocationOfDescriptors(descriptorsPlacedInWrongLocation);
+               
+        }
+       }
+       
+       private static Map<String, URL> getDescriptorsPlacedInWrongLocation(
+                       ResourceFinder finder) {
+
+       Map<String,URL> descriptorsMap = null;
+        try {
+                       descriptorsMap= 
retrieveDescriptorsPlacedInWrongLocation(finder);
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+        return descriptorsMap;
+       }
+       
+
+
+       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;
+       }
+       
+    private void warnIncorrectLocationOfDescriptors(Map<String, URL> 
descriptorsPlacedInWrongLocation) {
+       for(Map.Entry<String, URL> 
map:descriptorsPlacedInWrongLocation.entrySet())
+       {
+
+       
warn(currentModule.toString(),"descriptor.incorrectLocation",map.getKey(),map.getValue());
+       }
+       }
+}
\ No newline at end of file

Propchange: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Exceptions.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Exceptions.java?rev=1165140&r1=1165139&r2=1165140&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Exceptions.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Exceptions.java
 Mon Sep  5 01:19:32 2011
@@ -16,13 +16,17 @@
  */
 package org.apache.openejb.util;
 
+import static org.apache.openejb.util.StringUtilities.join;
+
+import java.io.IOException;
+import java.io.NotSerializableException;
+
 import javax.ejb.EJBException;
 import javax.naming.AuthenticationException;
 import javax.naming.NamingException;
 import javax.transaction.RollbackException;
-import java.io.IOException;
-import java.io.NotSerializableException;
-import java.rmi.RemoteException;
+
+import org.apache.openejb.OpenEjbContainer.NoModulesFoundException;
 
 /**
  * @version $Rev$ $Date$
@@ -91,6 +95,18 @@ public class Exceptions {
     public static NotSerializableException 
newNotSerializableException(Throwable cause){
         return initCause(new NotSerializableException(), cause);
     }
+    
+    public static NoModulesFoundException newNoModulesFoundException()
+    {
+       return new NoModulesFoundException(join("No modules found to deploy.",
+                       "1)Maybe descriptors are placed in incorrect location.",
+                       "Descriptors could go under: ", 
+                "<base-dir>/META-INF or <base-dir>/WEB-INF", "but not directly 
under <base-dir>",
+                "Check 'Application Discovery via the Classpath' docs page for 
more info",
+                "2)Maybe no modules are present in the classpath.",
+                "Is 'openejb.base' system property pointing to the intended 
location?")
+                );
+    }
 
 
 }

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/StringUtilities.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/StringUtilities.java?rev=1165140&r1=1165139&r2=1165140&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/StringUtilities.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/StringUtilities.java
 Mon Sep  5 01:19:32 2011
@@ -102,5 +102,15 @@ public class StringUtilities {
 
         return stringList.toString();
     }
+    
+   public static String join(String... linesToBreak )
+   {
+           StringBuilder out = new StringBuilder("\n");
+           for (Object line : linesToBreak) {
+                out.append(line.toString()).append("\n");
+               }
+          int lastBreak = out.lastIndexOf("\n");
+          return out.replace(lastBreak, lastBreak+1, "").toString();
+   }
 
 }
\ No newline at end of file

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties?rev=1165140&r1=1165139&r2=1165140&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties
 Mon Sep  5 01:19:32 2011
@@ -154,4 +154,7 @@ unrecognizedContainerType = Unrecognized
 configureService.cannotInstantiateClass = Cannot instantiate class {0}
 
 # throw new 
OpenEJBException(messages.format("configureService.cannotInstantiateClass", 
serviceClass.getName()), e);
-configureService.cannotInstantiateServiceClass = Cannot instantiate service 
class {0}
\ No newline at end of file
+configureService.cannotInstantiateServiceClass = Cannot instantiate service 
class {0}
+
+# warning no modules found to deploy
+config.noModulesFoundToDeploy = No module found for deployment. Maybe the 
descriptors are placed in incorrect location.
\ No newline at end of file

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties?rev=1165140&r1=1165139&r2=1165140&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
 Mon Sep  5 01:19:32 2011
@@ -867,3 +867,9 @@ public interface {0} extends {2}'{}'
 1.specializes.extendsSimpleBean = @Specializes EJB extends non-EJB
 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 
+
+

Added: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java?rev=1165140&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
 Mon Sep  5 01:19:32 2011
@@ -0,0 +1,63 @@
+/**
+ * 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.config.rules.Key;
+import org.apache.openejb.config.rules.KeyType;
+import org.apache.openejb.config.rules.Keys;
+import org.apache.openejb.config.rules.ValidationRunner;
+import org.apache.openejb.util.Archives;
+import org.junit.runner.RunWith;
+
+import javax.ejb.Stateless;
+import javax.interceptor.AroundInvoke;
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+@RunWith(ValidationRunner.class)
+public class CheckDescriptorLocationTest {
+
+    public File jarFile;
+    public static final String JAR_FILENAME_PREFIX = "ValTest";
+
+    @Keys({@Key(value = "descriptor.incorrectLocation", type = 
KeyType.WARNING)})
+    public AppModule testWebinfJar() 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);
+
+        DeploymentLoader loader = new DeploymentLoader();
+        AppModule appModule = loader.load(jarFile);
+
+        return appModule;
+    }
+
+    @Stateless
+    class FooBean {
+        // need to add this @AroundInvoke to cause validation to fail. 
Validation does not
+        // fail on warnings, which causes this framework to not work properly
+        @AroundInvoke
+        public void sayCheese() {
+        }
+    }
+
+}
\ No newline at end of file

Propchange: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java?rev=1165140&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
 Mon Sep  5 01:19:32 2011
@@ -0,0 +1,56 @@
+/**
+ * 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 static org.junit.Assert.*;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+
+public class CheckDescriptorLocationTestFileDeletionHelper {
+
+       @Test
+       public void deleteFile() {
+               File fileLocation = new 
File(System.getProperty("java.io.tmpdir"));
+               assertTrue(fileLocation.isDirectory());
+               List<File> asList = Arrays.asList(fileLocation.listFiles());
+               deleteTestCreatedFiles(asList);
+
+       }
+
+       private void deleteTestCreatedFiles(List<File> asList) {
+               for (File file : asList) {
+                       deleteOrMarkForDelete(file);
+               }
+       }
+
+       private void deleteOrMarkForDelete(File file) {
+               if (file.getName().contains(
+                               
CheckDescriptorLocationTest.JAR_FILENAME_PREFIX)) {
+
+                       boolean deleted = file.delete();
+                       if (!deleted) {
+                               file.deleteOnExit();
+                       }
+
+               }
+       }
+
+}

Propchange: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/CheckDescriptorLocationTestFileDeletionHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/Archives.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/Archives.java?rev=1165140&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/Archives.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/Archives.java
 Mon Sep  5 01:19:32 2011
@@ -0,0 +1,142 @@
+/**
+ * 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.util;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+
+/**
+* @version $Rev$ $Date$
+*/
+public class Archives {
+
+    public static File fileArchive(Class[] classes) throws IOException {
+        return fileArchive(new HashMap<String, String>(), classes);
+    }
+
+    public static File fileArchive(Map<String, String> entries, Class... 
classes) throws IOException {
+
+        ClassLoader loader = Archives.class.getClassLoader();
+
+        File classpath = File.createTempFile("test", "archive");
+        assertTrue(classpath.delete());
+        assertTrue(classpath.mkdirs());
+        System.out.println("Archive file path:"+classpath.getCanonicalPath());
+
+        for (Class clazz : classes) {
+            String name = clazz.getName().replace('.', File.separatorChar) + 
".class";
+            File file = new File(classpath, name);
+
+            File d = file.getParentFile();
+
+            if (!d.exists()) assertTrue(d.getAbsolutePath(), d.mkdirs());
+
+            URL resource = loader.getResource(name);
+            assertNotNull(resource);
+
+            InputStream in = new BufferedInputStream(resource.openStream());
+            BufferedOutputStream out = new BufferedOutputStream(new 
FileOutputStream(file));
+
+            int i = -1;
+            while ((i = in.read()) != -1) {
+                out.write(i);
+            }
+
+            out.close();
+            in.close();
+        }
+
+        for (Map.Entry<String, String> entry : entries.entrySet()) {
+
+            final String key = entry.getKey().replace('/', File.separatorChar);
+
+            final File file = new File(classpath, key);
+
+            File d = file.getParentFile();
+
+            if (!d.exists()) assertTrue(d.getAbsolutePath(), d.mkdirs());
+
+            BufferedOutputStream out = new BufferedOutputStream(new 
FileOutputStream(file));
+
+            out.write(entry.getValue().getBytes());
+
+            out.close();
+        }
+
+        return classpath;
+    }
+
+    public static File jarArchive(Class[] classes) throws IOException {
+        return jarArchive(new HashMap<String, String>(), "temp", classes);
+    }
+
+    
+    public static File jarArchive(Map<String, String> entries, String 
archiveNamePrefix,Class... classes) throws IOException {
+
+        ClassLoader loader = Archives.class.getClassLoader();
+
+        File classpath = File.createTempFile(archiveNamePrefix, ".jar");
+
+        // Create the ZIP file
+        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new 
FileOutputStream(classpath)));
+
+        for (Class clazz : classes) {
+            String name = clazz.getName().replace('.', File.separatorChar) + 
".class";
+
+            URL resource = loader.getResource(name);
+            assertNotNull(resource);
+
+            // Add ZIP entry to output stream.
+            out.putNextEntry(new ZipEntry(name));
+
+            InputStream in = new BufferedInputStream(resource.openStream());
+
+            int i = -1;
+            while ((i = in.read()) != -1) {
+                out.write(i);
+            }
+
+            // Complete the entry
+            in.close();
+            out.closeEntry();
+        }
+
+        for (Map.Entry<String, String> entry : entries.entrySet()) {
+
+            out.putNextEntry(new ZipEntry(entry.getKey()));
+
+            out.write(entry.getValue().getBytes());
+        }
+
+        // Complete the ZIP file
+        out.close();
+        return classpath;
+    }
+}
\ No newline at end of file

Propchange: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/Archives.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to