001// Copyright 2010 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
015package org.apache.tapestry5.internal.services;
016
017import java.io.IOException;
018
019import org.apache.tapestry5.*;
020import org.apache.tapestry5.internal.EmptyEventContext;
021import org.apache.tapestry5.ioc.annotations.Symbol;
022import org.apache.tapestry5.services.ComponentEventResultProcessor;
023import org.apache.tapestry5.services.HttpError;
024import org.apache.tapestry5.services.MetaDataLocator;
025import org.slf4j.Logger;
026
027import javax.servlet.http.HttpServletResponse;
028
029public class PageActivatorImpl implements PageActivator
030{
031    private final Logger logger;
032
033    private final MetaDataLocator metaDataLocator;
034
035    private final UnknownActivationContextHandler unknownActivationContextHandler;
036
037    public PageActivatorImpl(Logger logger, MetaDataLocator metaDataLocator,
038                             UnknownActivationContextHandler unknownActivationContextHandler)
039    {
040        this.logger = logger;
041        this.metaDataLocator = metaDataLocator;
042        this.unknownActivationContextHandler = unknownActivationContextHandler;
043    }
044
045    @SuppressWarnings("unchecked")
046    public boolean activatePage(ComponentResources pageResources, EventContext activationContext,
047            ComponentEventResultProcessor resultProcessor) throws IOException
048    {
049        TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(resultProcessor);
050
051        boolean handled = pageResources.triggerContextEvent(EventConstants.ACTIVATE, activationContext, callback);
052
053        boolean acceptEmpty = !pageResources.getComponentModel().handlesEvent(EventConstants.ACTIVATE) &&
054                                activationContext.getCount() == 0;
055
056        boolean checkUnknown = metaDataLocator.findMeta(MetaDataConstants.UNKNOWN_ACTIVATION_CONTEXT_CHECK,
057                                                        pageResources, Boolean.class);
058
059        if ( !handled && !acceptEmpty && checkUnknown &&
060                !pageResources.getComponentModel().handleActivationEventContext())
061        {
062            logger.info("Page {} required an exact activation context, let's handle this", pageResources.getPageName());
063            unknownActivationContextHandler.handleUnknownContext(pageResources, activationContext);
064            return true;
065        }
066
067        if (callback.isAborted())
068        {
069            callback.rethrow();
070            return true;
071        }
072
073        return false;
074    }
075
076}