    001    // Copyright 2011, 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
    015    package org.apache.tapestry5.alerts;
    016
    017    import org.apache.tapestry5.OptimizedSessionPersistedObject;
    018    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    019    import org.apache.tapestry5.ioc.internal.util.LockSupport;
    020
    021    import java.io.Serializable;
    022    import java.util.Iterator;
    023    import java.util.List;
    024
    025    /**
    026     * A stateless session object used to store Alerts between requests.
    027     *
    028     * @since 5.3
    029     */
    030    public class AlertStorage extends LockSupport implements Serializable, OptimizedSessionPersistedObject
    031    {
    032        private boolean dirty;
    033
    034        private final List<Alert> alerts = CollectionFactory.newList();
    035
    036        public boolean checkAndResetDirtyMarker()
    037        {
    038            try
    039            {
    040                takeWriteLock();
    041
    042                return dirty;
    043            } finally
    044            {
    045                dirty = false;
    046
    047                releaseWriteLock();
    048            }
    049        }
    050
    051
    052        public void add(Alert alert)
    053        {
    054            assert alert != null;
    055
    056            try
    057            {
    058                takeWriteLock();
    059
    060                alerts.add(alert);
    061
    062                dirty = true;
    063            } finally
    064            {
    065                releaseWriteLock();
    066            }
    067        }
    068
    069        /**
    070         * Dismisses all Alerts.
    071         */
    072        public void dismissAll()
    073        {
    074            try
    075            {
    076                takeWriteLock();
    077
    078                if (!alerts.isEmpty())
    079                {
    080                    alerts.clear();
    081                    dirty = true;
    082                }
    083            } finally
    084            {
    085                releaseWriteLock();
    086            }
    087        }
    088
    089        /**
    090         * Dismisses non-persistent Alerts; this is useful after rendering the {@link org.apache.tapestry5.corelib.components.Alerts}
    091         * component.
    092         */
    093        public void dismissNonPersistent()
    094        {
    095            try
    096            {
    097                takeWriteLock();
    098
    099                Iterator<Alert> i = alerts.iterator();
    100
    101                while (i.hasNext())
    102                {
    103                    if (!i.next().duration.persistent)
    104                    {
    105                        dirty = true;
    106                        i.remove();
    107                    }
    108                }
    109            } finally
    110            {
    111                releaseWriteLock();
    112            }
    113        }
    114
    115
    116        /**
    117         * Dismisses a single Alert, if present.
    118         */
    119        public void dismiss(long alertId)
    120        {
    121            try
    122            {
    123                takeWriteLock();
    124
    125                Iterator<Alert> i = alerts.iterator();
    126
    127                while (i.hasNext())
    128                {
    129                    if (i.next().id == alertId)
    130                    {
    131                        i.remove();
    132                        dirty = true;
    133                        return;
    134                    }
    135                }
    136            } finally
    137            {
    138                releaseWriteLock();
    139            }
    140        }
    141
    142
    143        /**
    144         * Returns all stored alerts.
    145         *
    146         * @return list of alerts (possibly empty)
    147         */
    148        public List<Alert> getAlerts()
    149        {
    150            try
    151            {
    152                acquireReadLock();
    153
    154                return alerts;
    155            } finally
    156            {
    157                releaseReadLock();
    158            }
    159        }
    160    }