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