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
015package org.apache.tapestry.binding;
016
017import org.apache.hivemind.ApplicationRuntimeException;
018import org.apache.hivemind.Location;
019import org.apache.hivemind.util.Defense;
020import org.apache.tapestry.coerce.ValueConverter;
021import org.apache.tapestry.engine.state.ApplicationStateManager;
022
023/**
024 * Binding used to efficiently query whether an application state object (visit, global and friends)
025 * exists without actually creating it (or creating a session).
026 * 
027 * @author Howard M. Lewis Ship
028 * @since 4.0
029 */
030public class StateBinding extends AbstractBinding
031{
032    private final ApplicationStateManager _applicationStateManager;
033
034    private final String _objectName;
035
036    public StateBinding(String description, ValueConverter valueConverter, Location location,
037            ApplicationStateManager applicationStateManager, String objectName)
038    {
039        super(description, valueConverter, location);
040
041        Defense.notNull(applicationStateManager, "applicationStateManager");
042        Defense.notNull(objectName, "objectName");
043
044        _applicationStateManager = applicationStateManager;
045        _objectName = objectName;
046    }
047
048    /**
049     * Returns false; these ASO objects can be created at any time.
050     */
051    public boolean isInvariant()
052    {
053        return false;
054    }
055
056    public Object getObject()
057    {
058        try
059        {
060            boolean exists = _applicationStateManager.exists(_objectName);
061
062            return exists ? Boolean.TRUE : Boolean.FALSE;
063        }
064        catch (Exception ex)
065        {
066            throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
067        }
068    }
069}