001// Copyright 2009 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.spring;
016
017import org.apache.tapestry5.ioc.ObjectCreator;
018import org.apache.tapestry5.ioc.ScopeConstants;
019import org.apache.tapestry5.ioc.ServiceBuilderResources;
020import org.apache.tapestry5.ioc.def.ServiceDef2;
021import org.springframework.context.ApplicationContext;
022
023import java.util.Collections;
024import java.util.Set;
025
026public class SpringBeanServiceDef implements ServiceDef2
027{
028    private final String beanName;
029
030    private final ApplicationContext context;
031
032    public SpringBeanServiceDef(String beanName, ApplicationContext context)
033    {
034        this.beanName = beanName;
035        this.context = context;
036    }
037
038    @Override
039    public boolean isPreventDecoration()
040    {
041        return true;
042    }
043
044    @Override
045    public ObjectCreator createServiceCreator(ServiceBuilderResources resources)
046    {
047        return new ObjectCreator()
048        {
049            @Override
050            public Object createObject()
051            {
052                return context.getBean(beanName);
053            }
054
055            @Override
056            public String toString()
057            {
058                return String.format("ObjectCreator<Spring Bean '%s'>", beanName);
059            }
060        };
061    }
062
063    @Override
064    public String getServiceId()
065    {
066        return beanName;
067    }
068
069    @Override
070    public Set<Class> getMarkers()
071    {
072        return Collections.emptySet();
073    }
074
075    @Override
076    public Class getServiceInterface()
077    {
078        return context.getType(beanName);
079    }
080
081    @Override
082    public String getServiceScope()
083    {
084        return ScopeConstants.DEFAULT;
085    }
086
087    @Override
088    public boolean isEagerLoad()
089    {
090        return false;
091    }
092}