001    // Copyright 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 java.io.IOException;
018    
019    import org.apache.hivemind.util.Defense;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.engine.IEngineService;
022    import org.apache.tapestry.engine.ILink;
023    
024    /**
025     * Outer proxy for engine services. The inner proxy resolves the engine service name to a engine
026     * service implementation and installed it into the outer proxy as a delegate. Although HiveMind
027     * does provide a similar system of inner and outer delegates, Tapestry's engine-service:
028     * {@link org.apache.tapestry.services.impl.EngineServiceObjectProvider} object provider can
029     * cause exceptions (recurive service build) when attempting to link two services together. This
030     * extra layer of proxying resolves that issue.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    public class EngineServiceOuterProxy implements IEngineService
036    {
037        private final String _serviceName;
038    
039        private IEngineService _delegate;
040    
041        public EngineServiceOuterProxy(String serviceName)
042        {
043            Defense.notNull(serviceName, "serviceName");
044    
045            _serviceName = serviceName;
046        }
047    
048        void installDelegate(IEngineService delegate)
049        {
050            _delegate = delegate;
051        }
052    
053        IEngineService getDelegate()
054        {
055            return _delegate;
056        }
057    
058        public ILink getLink(IRequestCycle cycle, boolean post, Object parameter)
059        {
060            return _delegate.getLink(cycle, false, parameter);
061        }
062    
063        public void service(IRequestCycle cycle) throws IOException
064        {
065            _delegate.service(cycle);
066        }
067    
068        public String getName()
069        {
070            return _serviceName;
071        }
072    
073        public String toString()
074        {
075            return ImplMessages.engineServiceOuterProxyToString(_serviceName);
076        }
077    
078    }