001: // Copyright 2007 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.tapestry.internal.spring;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
018:
019: import java.util.Collections;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import org.apache.tapestry.ioc.IOCConstants;
024: import org.apache.tapestry.ioc.ObjectCreator;
025: import org.apache.tapestry.ioc.ServiceBuilderResources;
026: import org.apache.tapestry.ioc.def.ContributionDef;
027: import org.apache.tapestry.ioc.def.DecoratorDef;
028: import org.apache.tapestry.ioc.def.ModuleDef;
029: import org.apache.tapestry.ioc.def.ServiceDef;
030: import org.springframework.beans.factory.BeanFactoryUtils;
031: import org.springframework.context.ApplicationContext;
032: import org.springframework.web.context.WebApplicationContext;
033:
034: /**
035: * A wrapper that converts a Spring {@link ApplicationContext} into a set of service definitions,
036: * compatible with Tapestry 5 IoC, for the beans defined in the context, as well as the context
037: * itself.
038: */
039: public class SpringModuleDef implements ModuleDef {
040: private static final String CONTEXT_SERVICE_ID = WebApplicationContext.class
041: .getSimpleName();
042:
043: private final ApplicationContext _context;
044:
045: private final Map<String, ServiceDef> _serviceDefs = newCaseInsensitiveMap();
046:
047: public SpringModuleDef(final ApplicationContext context) {
048: _context = context;
049:
050: for (final String beanName : BeanFactoryUtils
051: .beanNamesIncludingAncestors(_context)) {
052: ServiceDef serviceDef = new ServiceDef() {
053: private Object getBean() {
054: return _context.getBean(beanName);
055: }
056:
057: public ObjectCreator createServiceCreator(
058: ServiceBuilderResources resources) {
059: return new ObjectCreator() {
060: public Object createObject() {
061: return getBean();
062: }
063: };
064: }
065:
066: public String getServiceId() {
067: return beanName;
068: }
069:
070: public Class getServiceInterface() {
071: return getBean().getClass();
072: }
073:
074: public String getServiceScope() {
075: return IOCConstants.DEFAULT_SCOPE;
076: }
077:
078: public boolean isEagerLoad() {
079: return false;
080: }
081: };
082:
083: _serviceDefs.put(beanName, serviceDef);
084: }
085:
086: // And add one service that is the Spring WebApplicationContext.
087:
088: ServiceDef serviceDef = new ServiceDef() {
089:
090: public ObjectCreator createServiceCreator(
091: ServiceBuilderResources resources) {
092: return new ObjectCreator() {
093: public Object createObject() {
094: return _context;
095: }
096: };
097: }
098:
099: public String getServiceId() {
100: return CONTEXT_SERVICE_ID;
101: }
102:
103: public Class getServiceInterface() {
104: return WebApplicationContext.class;
105: }
106:
107: public String getServiceScope() {
108: return IOCConstants.DEFAULT_SCOPE;
109: }
110:
111: public boolean isEagerLoad() {
112: return false;
113: }
114: };
115:
116: _serviceDefs.put(CONTEXT_SERVICE_ID, serviceDef);
117: }
118:
119: public Class getBuilderClass() {
120: return null;
121: }
122:
123: /** Returns an empty set. */
124: public Set<ContributionDef> getContributionDefs() {
125: return Collections.emptySet();
126: }
127:
128: /** Returns an empty set. */
129: public Set<DecoratorDef> getDecoratorDefs() {
130: return Collections.emptySet();
131: }
132:
133: public String getLogName() {
134: return "Spring";
135: }
136:
137: public ServiceDef getServiceDef(String serviceId) {
138: return _serviceDefs.get(serviceId);
139: }
140:
141: public Set<String> getServiceIds() {
142: return _serviceDefs.keySet();
143: }
144:
145: }
|