/**
 * 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.geronimo.jaxws.builder;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.jws.WebService;
import javax.xml.ws.WebServiceProvider;

import org.apache.geronimo.common.DeploymentException;
import org.apache.geronimo.deployment.util.DeploymentUtil;
import org.apache.geronimo.j2ee.deployment.Module;
import org.apache.geronimo.jaxws.PortInfo;
import org.apache.geronimo.kernel.classloader.JarFileClassLoader;
import org.apache.xbean.finder.ClassFinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WARWebServiceFinder implements WebServiceFinder {

    private static final Logger LOG = LoggerFactory.getLogger(WARWebServiceFinder.class);
    
    private static final WebServiceFinder webServiceFinder = getWebServiceFinder();
                
    private static WebServiceFinder getWebServiceFinder() {
        boolean useSimpleFinder = 
            Boolean.getBoolean("org.apache.geronimo.jaxws.builder.useSimpleFinder");
        
        WebServiceFinder webServiceFinder = null;
        
        if (useSimpleFinder) {
            webServiceFinder = new SimpleWARWebServiceFinder();
        } else {
            webServiceFinder = new AdvancedWARWebServiceFinder();
        }
        
        return webServiceFinder;
    }
    
    public Map<String, PortInfo> discoverWebServices(Module module, 
                                                     boolean isEJB,
                                                     Map correctedPortLocations)
            throws DeploymentException {                
        return webServiceFinder.discoverWebServices(module, isEJB, correctedPortLocations);
    }

    /**
     * Returns a list of any classes annotated with @WebService or
     * @WebServiceProvider annotation.
     */
    static List<Class> discoverWebServices(JarFile moduleFile,
                                           boolean isEJB,
                                           ClassLoader parentClassLoader)                                                      
            throws DeploymentException {
        LOG.debug("Discovering web service classes");

        File tmpDir = null;
        List<URL> urlList = new ArrayList<URL>();
        if (isEJB) {
            File jarFile = new File(moduleFile.getName());
            try {
                urlList.add(jarFile.toURL());
            } catch (MalformedURLException e) {
                // this should not happen
                throw new DeploymentException(e);
            }
        } else {
            /*
             * Can't get ClassLoader to load nested Jar files, so
             * unpack the module Jar file and discover all nested Jar files
             * within it and the classes/ directory.
             */
            try {
                tmpDir = DeploymentUtil.createTempDir();
                /*
                 * This is needed becuase DeploymentUtil.unzipToDirectory()
                 * always closes the passed JarFile.
                 */
                JarFile module = new JarFile(moduleFile.getName());
                DeploymentUtil.unzipToDirectory(module, tmpDir);
            } catch (IOException e) {
                if (tmpDir != null) {
                    DeploymentUtil.recursiveDelete(tmpDir);
                }
                throw new DeploymentException("Failed to expand the module archive", e);
            }

            // create URL list
            Enumeration<JarEntry> jarEnum = moduleFile.entries();
            while (jarEnum.hasMoreElements()) {
                JarEntry entry = jarEnum.nextElement();
                String name = entry.getName();
                if (name.equals("WEB-INF/classes/")) {
                    // ensure it is first
                    File classesDir = new File(tmpDir, "WEB-INF/classes/");
                    try {
                        urlList.add(0, classesDir.toURL());
                    } catch (MalformedURLException e) {
                        // this should not happen, ignore
                    }
                } else if (name.startsWith("WEB-INF/lib/")
                        && name.endsWith(".jar")) {
                    File jarFile = new File(tmpDir, name);
                    try {
                        urlList.add(jarFile.toURL());
                    } catch (MalformedURLException e) {
                        // this should not happen, ignore
                    }
                }
            }
        }
        
        URL[] urls = urlList.toArray(new URL[] {});
        JarFileClassLoader tempClassLoader = new JarFileClassLoader(null, urls, parentClassLoader);
        ClassFinder classFinder = new ClassFinder(tempClassLoader, urlList);

        List<Class> classes = new ArrayList<Class>();

        classes.addAll(classFinder.findAnnotatedClasses(WebService.class));
        classes.addAll(classFinder.findAnnotatedClasses(WebServiceProvider.class));       

        tempClassLoader.destroy();

        if (tmpDir != null) {
            DeploymentUtil.recursiveDelete(tmpDir);
        }

        return classes;
    }
}

@@ -1299,16 +1299,76 @@
 ntUtil;%0A
+import org.apache.geronimo.deployment.util.UnpackedJarFile;%0A
 import o
@@ -3596,32 +3596,230 @@
        %7D else %7B%0A
+            File baseDir = null;%0A            %0A            if (moduleFile instanceof UnpackedJarFile) %7B%0A                baseDir = ((UnpackedJarFile)moduleFile).getBaseDir();%0A            %7D else %7B%0A    
             /*%0A 
@@ -3830,16 +3830,20 @@
         
+  
+  
 * Can't 
@@ -3887,16 +3887,20 @@
 les, so%0A
+    
         
@@ -3967,16 +3967,20 @@
 r files%0A
+    
         
@@ -4029,35 +4029,43 @@
 y.%0A             
-*/%0A
+    */%0A    
             try 
@@ -4082,16 +4082,20 @@
         
+   
+ 
 tmpDir =
@@ -4135,35 +4135,43 @@
                 
-/*%0A
+    /*%0A    
                 
@@ -4247,16 +4247,20 @@
         
+ 
+   
 * always
@@ -4296,35 +4296,43 @@
                 
-*/%0A
+    */%0A    
                 
@@ -4391,32 +4391,36 @@
                 
+    
 DeploymentUtil.u
@@ -4456,32 +4456,36 @@
 r);%0A            
+    
 %7D catch (IOExcep
@@ -4502,32 +4502,36 @@
                 
+  
+  
 if (tmpDir != nu
@@ -4528,32 +4528,36 @@
 pDir != null) %7B%0A
+    
                 
@@ -4608,32 +4608,36 @@
                 
+    
 %7D%0A              
@@ -4630,32 +4630,36 @@
                 
+ 
+   
 throw new Deploy
@@ -4711,24 +4711,93 @@
 chive%22, e);%0A
+                %7D%0A                %0A                baseDir = tmpDir;%0A
             
@@ -5187,27 +5187,28 @@
  = new File(
-tmp
+base
 Dir, %22WEB-IN
@@ -5598,19 +5598,20 @@
 ew File(
-tmp
+base
 Dir, nam

handle in-place deployment