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    
015    package org.apache.tapestry5.internal.spring;
016    
017    import org.apache.tapestry5.ioc.ObjectCreator;
018    import org.apache.tapestry5.ioc.ScopeConstants;
019    import org.apache.tapestry5.ioc.ServiceBuilderResources;
020    import org.apache.tapestry5.ioc.def.ServiceDef2;
021    import org.springframework.context.ApplicationContext;
022    
023    import java.util.Collections;
024    import java.util.Set;
025    
026    public 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        public boolean isPreventDecoration()
039        {
040            return true;
041        }
042    
043        public ObjectCreator createServiceCreator(ServiceBuilderResources resources)
044        {
045            return new ObjectCreator()
046            {
047                public Object createObject()
048                {
049                    return context.getBean(beanName);
050                }
051    
052                @Override
053                public String toString()
054                {
055                    return String.format("ObjectCreator<Spring Bean '%s'>", beanName);
056                }
057            };
058        }
059    
060        public String getServiceId()
061        {
062            return beanName;
063        }
064    
065        public Set<Class> getMarkers()
066        {
067            return Collections.emptySet();
068        }
069    
070        public Class getServiceInterface()
071        {
072            return context.getType(beanName);
073        }
074    
075        public String getServiceScope()
076        {
077            return ScopeConstants.DEFAULT;
078        }
079    
080        public boolean isEagerLoad()
081        {
082            return false;
083        }
084    }