001 // Copyright 2004, 2005 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.services.impl;
016
017 import org.apache.hivemind.ApplicationRuntimeException;
018 import org.apache.hivemind.ServiceImplementationFactory;
019 import org.apache.hivemind.ServiceImplementationFactoryParameters;
020 import org.apache.hivemind.lib.DefaultImplementationBuilder;
021 import org.apache.tapestry.spec.IApplicationSpecification;
022
023 /**
024 * An implementation of {@link org.apache.hivemind.ServiceImplementationFactory}that looks for a
025 * service implementation provided as an
026 * {@link org.apache.tapestry.spec.ILibrarySpecification#getExtension(String) application
027 * extension}. If no such extension exists, then a
028 * {@link org.apache.hivemind.lib.DefaultImplementationBuilder default implementation}is
029 * constructed and returned instead. This allows compatibility with Tapestry 3.0 and earlier
030 * application extensions (though those will be phased out in the future).
031 *
032 * @author Howard Lewis Ship
033 * @since 4.0
034 */
035 public class ExtensionLookupFactory implements ServiceImplementationFactory
036 {
037 private IApplicationSpecification _specification;
038
039 private DefaultImplementationBuilder _defaultBuilder;
040
041 public Object createCoreServiceImplementation(
042 ServiceImplementationFactoryParameters factorParameters)
043 {
044 ExtensionLookupParameter p = (ExtensionLookupParameter) factorParameters.getParameters()
045 .get(0);
046
047 String extensionName = p.getExtensionName();
048
049 Class serviceInterface = factorParameters.getServiceInterface();
050
051 try
052 {
053 if (_specification.checkExtension(extensionName))
054 return _specification.getExtension(extensionName, serviceInterface);
055
056 if (p.getDefault() != null)
057 return p.getDefault();
058
059 return _defaultBuilder.buildDefaultImplementation(serviceInterface);
060 }
061 catch (Exception ex)
062 {
063 throw new ApplicationRuntimeException(ex.getMessage(), p.getLocation(), ex);
064 }
065 }
066
067 public void setDefaultBuilder(DefaultImplementationBuilder builder)
068 {
069 _defaultBuilder = builder;
070 }
071
072 public void setSpecification(IApplicationSpecification specification)
073 {
074 _specification = specification;
075 }
076
077 }