// Copyright 2007 The Apache Software Foundation
//
// 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.apache.tapestry.internal.spring;

import org.apache.tapestry.ioc.IOCConstants;
import org.apache.tapestry.ioc.ObjectCreator;
import org.apache.tapestry.ioc.ServiceBuilderResources;
import org.apache.tapestry.ioc.def.ContributionDef;
import org.apache.tapestry.ioc.def.DecoratorDef;
import org.apache.tapestry.ioc.def.ModuleDef;
import org.apache.tapestry.ioc.def.ServiceDef;
import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
 * A wrapper that converts a Spring {@link ApplicationContext} into a set of service definitions, compatible with
 * Tapestry 5 IoC, for the beans defined in the context, as well as the context itself.
 */
public class SpringModuleDef implements ModuleDef
{
    private static final String CONTEXT_SERVICE_ID = WebApplicationContext.class.getSimpleName();

    private final ApplicationContext context;

    private final Map<String, ServiceDef> serviceDefs = newCaseInsensitiveMap();

    public SpringModuleDef(final ApplicationContext context)
    {
        this.context = context;

        for (final String beanName : BeanFactoryUtils.beanNamesIncludingAncestors(this.context))
        {
            ServiceDef serviceDef = new ServiceDef()
            {
                private Object getBean()
                {
                    return this.context.getBean(beanName);
                }

                private Class getBeanType()
                {
                    return this.context.getType(beanName);
                }

                public ObjectCreator createServiceCreator(ServiceBuilderResources resources)
                {
                    return new ObjectCreator()
                    {
                        public Object createObject()
                        {
                            return getBean();
                        }
                    };
                }

                public String getServiceId()
                {
                    return beanName;
                }

                public Class getServiceInterface()
                {
                    return getBeanType();
                }

                public String getServiceScope()
                {
                    return IOCConstants.DEFAULT_SCOPE;
                }

                public boolean isEagerLoad()
                {
                    return false;
                }

                /** Returns an empty set, Spring has no concept of a marker annotation. */
                public Set<Class> getMarkers()
                {
                    return Collections.emptySet();
                }

            };

            serviceDefs.put(beanName, serviceDef);
        }

        // And add one service that is the Spring WebApplicationContext.

        ServiceDef serviceDef = new ServiceDef()
        {

            public ObjectCreator createServiceCreator(ServiceBuilderResources resources)
            {
                return new ObjectCreator()
                {
                    public Object createObject()
                    {
                        return this.context;
                    }
                };
            }

            public String getServiceId()
            {
                return CONTEXT_SERVICE_ID;
            }

            public Class getServiceInterface()
            {
                return WebApplicationContext.class;
            }

            public String getServiceScope()
            {
                return IOCConstants.DEFAULT_SCOPE;
            }

            public boolean isEagerLoad()
            {
                return false;
            }

            /** Returns null. */
            public Set<Class> getMarkers()
            {
                return Collections.emptySet();
            }

        };

        serviceDefs.put(CONTEXT_SERVICE_ID, serviceDef);
    }

    public Class getBuilderClass()
    {
        return null;
    }

    /**
     * Returns an empty set.
     */
    public Set<ContributionDef> getContributionDefs()
    {
        return Collections.emptySet();
    }

    /**
     * Returns an empty set.
     */
    public Set<DecoratorDef> getDecoratorDefs()
    {
        return Collections.emptySet();
    }

    public String getLoggerName()
    {
        return "Spring";
    }

    public ServiceDef getServiceDef(String serviceId)
    {
        return serviceDefs.get(serviceId);
    }

    public Set<String> getServiceIds()
    {
        return serviceDefs.keySet();
    }

}

@@ -10,16 +10,22 @@
 ght 2007
+, 2008
  The Apa
@@ -48,16 +48,16 @@
 ndation%0A
-
 //%0A// Li
@@ -1704,55 +1704,8 @@
 );%0A%0A
-    private final ApplicationContext context;%0A%0A
     
@@ -1853,41 +1853,8 @@
   %7B%0A
-        this.context = context;%0A%0A
     
@@ -1931,21 +1931,16 @@
 cestors(
-this.
 context)
@@ -2096,37 +2096,32 @@
          return 
-this.
 context.getBean(
@@ -2235,29 +2235,24 @@
      return 
-this.
 context.getT
@@ -3600,17 +3600,16 @@
       %7B%0A
-%0A
         
@@ -3827,32 +3827,32 @@
               %7B%0A
+
                 
@@ -3870,13 +3870,8 @@
 urn 
-this.
 cont

Remove leading underscores from field names