    001// Copyright 2008-2013 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;
    016
    017import java.io.IOException;
    018import java.io.Serializable;
    019import java.util.concurrent.atomic.AtomicBoolean;
    020
    021/**
    022 * Base implementation of
    023 * {@link org.apache.tapestry5.OptimizedSessionPersistedObject}. Subclasses
    024 * should invoke {@link #markDirty()} after the internal state of the object changes.
    025 * <p>
    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 * <p>
    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}