001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5;
014
015import java.io.IOException;
016import java.io.Serializable;
017import java.util.concurrent.atomic.AtomicBoolean;
018
019/**
020 * Base implementation of
021 * {@link org.apache.tapestry5.OptimizedSessionPersistedObject}. Subclasses
022 * should invoke {@link #markDirty()} after the internal state of the object changes.
023 *
024 * Due to the concurrent nature of session attributes it's important that markDirty occurs <strong>after</strong>
025 * the object has been changed. If the change occurs before the object has been mutated it's possible that another
026 * thread may re-store the object before the changes are actually made!
027 *
028 * @since 5.1.1.0
029 */
030public abstract class BaseOptimizedSessionPersistedObject implements OptimizedSessionPersistedObject, Serializable
031{
032    private static final long serialVersionUID = 172352928643322125L;
033
034    private transient AtomicBoolean dirty = new AtomicBoolean(false);
035
036    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
037        dirty = new AtomicBoolean(false);
038    }
039
040    public final boolean checkAndResetDirtyMarker()
041    {
042        return dirty.getAndSet(false);
043    }
044
045    /**
046     * Invoked by the subclass after internal state of the object changes.
047     */
048    protected final void markDirty()
049    {
050        dirty.set(true);
051    }
052}