    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
    019import org.apache.tapestry5.http.OptimizedSessionPersistedObject;
    020
    021/**
    022 * Base implementation of
    023 * {@link org.apache.tapestry5.http.OptimizedSessionPersistedObject}. Subclasses
    024 * should invoke {@link #markDirty()} after the internal state of the object changes.
    025 *
    026 * Due to the concurrent nature of session attributes it's important that markDirty occurs <strong>after</strong>
    027 * the object has been changed. If the change occurs before the object has been mutated it's possible that another
    028 * thread may re-store the object before the changes are actually made!
    029 *
    030 * @since 5.1.1.0
    031 */
    032public abstract class BaseOptimizedSessionPersistedObject implements OptimizedSessionPersistedObject, Serializable
    033{
    034    private static final long serialVersionUID = 172352928643322125L;
    035
    036    private transient AtomicBoolean dirty = new AtomicBoolean(false);
    037
    038    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    039        dirty = new AtomicBoolean(false);
    040    }
    041
    042    public final boolean checkAndResetDirtyMarker()
    043    {
    044        return dirty.getAndSet(false);
    045    }
    046
    047    /**
    048     * Invoked by the subclass after internal state of the object changes.
    049     */
    050    protected final void markDirty()
    051    {
    052        dirty.set(true);
    053    }
    054}