/**
 * 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.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.geronimo.common.DeploymentException;
import org.apache.geronimo.j2ee.deployment.Module;
import org.apache.geronimo.j2ee.deployment.WebModule;
import org.apache.geronimo.jaxws.JAXWSUtils;
import org.apache.geronimo.jaxws.PortInfo;
import org.apache.openejb.jee.Servlet;
import org.apache.openejb.jee.ServletMapping;
import org.apache.openejb.jee.WebApp;
import org.osgi.framework.Bundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleWARWebServiceFinder implements WebServiceFinder {

    private static final Logger LOG = LoggerFactory.getLogger(SimpleWARWebServiceFinder.class);

    public Map<String, PortInfo> discoverWebServices(Module module,
                                                     boolean isEJB,
                                                     Map correctedPortLocations)
            throws DeploymentException {
        Map<String, PortInfo> map = new HashMap<String, PortInfo>();
        discoverPOJOWebServices(module, correctedPortLocations, map);
        return map;
    }

    private void discoverPOJOWebServices(Module module,
                                         Map correctedPortLocations,
                                         Map<String, PortInfo> map)
        throws DeploymentException {
        Bundle bundle = module.getEarContext().getDeploymentBundle();
        WebApp webApp = (WebApp) module.getSpecDD();

        // find web services
        List<Servlet> servletTypes = webApp.getServlet();

        if (webApp.getServlet().size() == 0) {
            // web.xml not present (empty really), discover annotated
            // classes and update DD
            List<Class> services = WARWebServiceFinder.discoverWebServices(module.getModuleFile(), false, this.getClass().getClassLoader());
            String contextRoot = ((WebModule) module).getContextRoot();
            for (Class service : services) {
                // skip interfaces and such
                if (!JAXWSUtils.isWebService(service)) {
                    continue;
                }

                LOG.debug("Discovered POJO Web Service: " + service.getName());

                // add new <servlet/> element
                Servlet servlet = new Servlet();
                servlet.setServletName(service.getName());
                servlet.setServletClass(service.getName());
                webApp.getServlet().add(servlet);
                // add new <servlet-mapping/> element
                String location = "/" + JAXWSUtils.getServiceName(service);
                ServletMapping servletMapping = new ServletMapping();
                servletMapping.setServletName(service.getName());
                servletMapping.getUrlPattern().add(location);

                // map service
                PortInfo portInfo = new PortInfo();
                portInfo.setLocation(contextRoot + location);
                map.put(service.getName(), portInfo);
            }
        } else {
            // web.xml present, examine servlet classes and check for web
            // services
            for (Servlet servletType : servletTypes) {
                String servletName = servletType.getServletName().trim();
                if (servletType.getServletClass() != null) {
                    String servletClassName = servletType.getServletClass().trim();
                    try {
                        Class servletClass = bundle.loadClass(servletClassName);
                        if (JAXWSUtils.isWebService(servletClass)) {
                            LOG.debug("Found POJO Web Service: " + servletName);
                            PortInfo portInfo = new PortInfo();
                            map.put(servletName, portInfo);
                        }
                    } catch (Exception e) {
                        throw new DeploymentException("Failed to load servlet class "
                                                      + servletClassName + " from bundle " +  bundle, e);
                    }
                }
            }

            // update web service locations
            for (Map.Entry entry : map.entrySet()) {
                String servletName = (String) entry.getKey();
                PortInfo portInfo = (PortInfo) entry.getValue();

                String location = (String) correctedPortLocations.get(servletName);
                if (location != null) {
                    portInfo.setLocation(location);
                }
            }
        }
    }

}

@@ -1793,32 +1793,41 @@
 mentException %7B%0A
+        %0A
         Map%3CStri
@@ -1891,326 +1891,8 @@
     
-discoverPOJOWebServices(module, correctedPortLocations, map);%0A        return map;%0A    %7D%0A%0A    private void discoverPOJOWebServices(Module module,%0A                                         Map correctedPortLocations,%0A                                         Map%3CString, PortInfo%3E map)%0A        throws DeploymentException %7B
 %0A   
@@ -5083,13 +5083,44 @@
   %7D%0A    
-%7D
+    %0A        return map;%0A    %7D%0A%0A
 %0A%0A%7D%0A

Simplify the code of the webService finder.