001    // Copyright 2006, 2007, 2008, 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.services;
016    
017    import org.apache.tapestry5.ComponentResources;
018    import org.apache.tapestry5.internal.structure.Page;
019    import org.apache.tapestry5.ioc.annotations.Marker;
020    import org.apache.tapestry5.runtime.Component;
021    import org.apache.tapestry5.services.ComponentEventResultProcessor;
022    import org.apache.tapestry5.services.Traditional;
023    import org.slf4j.Logger;
024    
025    import java.io.IOException;
026    
027    @Marker({Traditional.class, ComponentInstanceProcessor.class})
028    public class ComponentInstanceResultProcessor implements ComponentEventResultProcessor<Component>
029    {
030        private final RequestPageCache requestPageCache;
031    
032        private final Logger logger;
033    
034        private final ActionRenderResponseGenerator generator;
035    
036        public ComponentInstanceResultProcessor(Logger logger, RequestPageCache requestPageCache,
037                                                ActionRenderResponseGenerator generator)
038        {
039            this.requestPageCache = requestPageCache;
040            this.logger = logger;
041            this.generator = generator;
042        }
043    
044        public void processResultValue(Component value) throws IOException
045        {
046            ComponentResources resources = value.getComponentResources();
047    
048            if (resources.getContainer() != null)
049                logger.warn(ServicesMessages.componentInstanceIsNotAPage(value));
050    
051            // We have all these layers and layers between us and the page instance, but its easy to
052            // extract the page class name and quickly re-resolve that to the page instance.
053    
054            Page page = requestPageCache.get(resources.getPageName());
055    
056            generator.generateResponse(page);
057        }
058    }