/*
 * JBoss, Home of Professional Open Source
 * Copyright 2009, Red Hat Middleware LLC, 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.as.arquillian.container;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.jboss.arquillian.container.test.spi.client.deployment.ApplicationArchiveProcessor;
import org.jboss.arquillian.test.spi.TestClass;
import org.jboss.logging.Logger;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.container.ManifestContainer;

/**
 * An {@link ApplicationArchiveProcessor} for module test deployments.
 *
 * @author Thomas.Diesler@jboss.com
 * @author Kabir Khan
 * @since 17-Nov-2010
 */
public class ModuleApplicationArchiveProcessor implements ApplicationArchiveProcessor {

    private static final Logger log = Logger.getLogger(ModuleApplicationArchiveProcessor.class);

    static final List<String> defaultDependencies = new ArrayList<String>();
    static {
        defaultDependencies.add("deployment.arquillian-service");
        defaultDependencies.add("org.jboss.modules");
        defaultDependencies.add("org.jboss.msc");
    }
    /*
    static final List<String> jsfDependencies = new ArrayList<String>();
    static {
        jsfDependencies.add("org.jboss.jsfunit.arquillian");
        jsfDependencies.add("org.jboss.jsfunit.core");
        jsfDependencies.add("org.jboss.jsfunit.arquillian");
        jsfDependencies.add("net.sourceforge.htmlunit");
        jsfDependencies.add("org.apache.james.mime4j");
        jsfDependencies.add("org.apache.commons.codec");
        jsfDependencies.add("org.apache.commons.collections");
        jsfDependencies.add("org.apache.commons.io");
        jsfDependencies.add("org.apache.commons.lang");
        jsfDependencies.add("org.apache.commons.logging");
        jsfDependencies.add("org.apache.httpcomponents");
        jsfDependencies.add("org.apache.xalan");
        jsfDependencies.add("org.apache.xerces");
        jsfDependencies.add("org.w3c.css.sac");
        jsfDependencies.add("net.sourceforge.cssparser");
        jsfDependencies.add("net.sourceforge.nekohtml");
    }
    */

    @Override
    public void process(Archive<?> appArchive, TestClass testClass) {
        if (appArchive instanceof ManifestContainer<?> == false)
            throw new IllegalArgumentException("ManifestContainer expected " + appArchive);

        final Manifest manifest = ManifestUtils.getOrCreateManifest(appArchive);
        Attributes attributes = manifest.getMainAttributes();
        String value = attributes.getValue("Dependencies");
        StringBuffer moduleDeps = new StringBuffer(value != null && value.trim().length() > 0 ? value : "org.jboss.modules");
        for (String dep : defaultDependencies) {
            if (moduleDeps.indexOf(dep) < 0)
                moduleDeps.append("," + dep);
        }
        /*
        if (Boolean.valueOf(System.getProperty("jboss.arquillian.jsfunit", "false"))) {
            for (String dep : jsfDependencies) {
                if (moduleDeps.indexOf(dep) < 0) {
                    moduleDeps.append("," + dep);
                }
            }
        }
        */
        log.debugf("Add dependencies: %s", moduleDeps);
        attributes.putValue("Dependencies", moduleDeps.toString());

        // Add the manifest to the archive
        ArchivePath manifestPath = ArchivePaths.create(JarFile.MANIFEST_NAME);
        appArchive.delete(manifestPath);
        appArchive.add(new Asset() {
                public InputStream openStream() {
                    try {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        manifest.write(baos);
                        return new ByteArrayInputStream(baos.toByteArray());
                    } catch (IOException ex) {
                        throw new IllegalStateException("Cannot write manifest", ex);
                    }
                }
            }, manifestPath);
    }
}

@@ -826,285 +826,8 @@
 r;%0A%0A
-import java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.jar.Attributes;%0Aimport java.util.jar.JarFile;%0Aimport java.util.jar.Manifest;%0A%0A
 impo
@@ -1236,16 +1236,293 @@
 ainer;%0A%0A
+import java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.jar.Attributes;%0Aimport java.util.jar.JarFile;%0Aimport java.util.jar.Manifest;%0A%0A
 /**%0A * A
@@ -1931,32 +1931,33 @@
 List%3CString%3E();%0A
+%0A
     static %7B%0A   
@@ -3516,24 +3516,207 @@
 tributes();%0A
+        if (attributes.getValue(Attributes.Name.MANIFEST_VERSION.toString()) == null) %7B%0A            attributes.putValue(Attributes.Name.MANIFEST_VERSION.toString(), %221.0%22);%0A        %7D%0A
         Stri
@@ -4678,24 +4678,28 @@
             
+    
 public Input
@@ -4744,14 +4744,22 @@
     
+    
 try %7B%0A
+    
     
@@ -4860,16 +4860,20 @@
         
+  
+  
 manifest
@@ -4882,24 +4882,28 @@
 rite(baos);%0A
+    
             
@@ -4987,16 +4987,20 @@
         
+    
 %7D catch 
@@ -5034,32 +5034,36 @@
                 
+   
+ 
 throw new Illega
@@ -5108,16 +5108,42 @@
 %22, ex);%0A
+                        %7D%0A
         
@@ -5156,38 +5156,24 @@
       %7D%0A    
-            %7D%0A
             

Fix problem with arquillian packager when manifest has no version