001//  Copyright 2008, 2009, 2012 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 org.apache.tapestry5.ComponentEventCallback;
018import org.apache.tapestry5.EventConstants;
019import org.apache.tapestry5.commons.services.InvalidationEventHub;
020import org.apache.tapestry5.commons.services.InvalidationListener;
021import org.apache.tapestry5.commons.services.TypeCoercer;
022import org.apache.tapestry5.commons.util.CollectionFactory;
023import org.apache.tapestry5.internal.structure.ComponentPageElement;
024import org.apache.tapestry5.internal.structure.Page;
025import org.apache.tapestry5.internal.util.Holder;
026import org.apache.tapestry5.ioc.annotations.ComponentClasses;
027import org.apache.tapestry5.ioc.annotations.PostInjection;
028import org.apache.tapestry5.model.ComponentModel;
029
030import java.util.Map;
031
032public class PageActivationContextCollectorImpl implements PageActivationContextCollector
033{
034    private final Object[] EMPTY = new Object[0];
035
036    private final TypeCoercer typeCoercer;
037
038    private final ComponentModelSource modelSource;
039
040    private final RequestPageCache requestPageCache;
041
042    /**
043     * Keyed on *canonical* page name, value indicates whether the page has a passivate event handler.
044     */
045    private final Map<String, Boolean> cache = CollectionFactory.newConcurrentMap();
046
047    public PageActivationContextCollectorImpl(TypeCoercer typeCoercer, RequestPageCache requestPageCache,
048                                              ComponentModelSource modelSource)
049    {
050        this.typeCoercer = typeCoercer;
051        this.requestPageCache = requestPageCache;
052        this.modelSource = modelSource;
053
054    }
055
056    @PostInjection
057    public void setupInvalidation(@ComponentClasses InvalidationEventHub invalidationEventHub)
058    {
059        invalidationEventHub.clearOnInvalidation(cache);
060    }
061
062    public Object[] collectPageActivationContext(String pageName)
063    {
064        Boolean hasHandler = cache.get(pageName);
065
066        if (hasHandler == null)
067        {
068            ComponentModel model = modelSource.getPageModel(pageName);
069
070            hasHandler = model.handlesEvent(EventConstants.PASSIVATE);
071
072            cache.put(pageName, hasHandler);
073        }
074
075        // If no handler for the event, then no need to fire the event (and more importantly,
076        // no need to obtain a page instance!)
077
078        if (!hasHandler)
079            return EMPTY;
080
081        // Get or create a page instance and trigger the event.
082
083        Page page = requestPageCache.get(pageName);
084
085        ComponentPageElement element = page.getRootElement();
086
087        final Holder<Object[]> holder = Holder.create();
088
089        ComponentEventCallback callback = new ComponentEventCallback()
090        {
091            public boolean handleResult(Object result)
092            {
093                holder.put(typeCoercer.coerce(result, Object[].class));
094
095                // We've got the value, stop the event.
096
097                return true;
098            }
099        };
100
101        element.triggerEvent(EventConstants.PASSIVATE, null, callback);
102
103        if (!holder.hasValue()) return EMPTY;
104
105        return holder.get();
106    }
107}