001    // Copyright 2006, 2007, 2008, 2011 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.internal.event;
016    
017    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
018    import org.apache.tapestry5.services.InvalidationEventHub;
019    import org.apache.tapestry5.services.InvalidationListener;
020    
021    import java.util.List;
022    
023    /**
024     * Base implementation class for classes (especially services) that need to manage a list of
025     * {@link org.apache.tapestry5.services.InvalidationListener}s.
026     */
027    public class InvalidationEventHubImpl implements InvalidationEventHub
028    {
029        private final List<InvalidationListener> listeners;
030    
031        protected InvalidationEventHubImpl(boolean productionMode)
032        {
033            if (productionMode)
034            {
035                listeners = null;
036            } else
037            {
038                listeners = CollectionFactory.newThreadSafeList();
039            }
040        }
041    
042        /**
043         * Notifies all {@link InvalidationListener listener}s.
044         */
045        protected final void fireInvalidationEvent()
046        {
047            if (listeners == null)
048            {
049                return;
050            }
051    
052            for (InvalidationListener listener : listeners)
053            {
054                listener.objectWasInvalidated();
055            }
056        }
057    
058        public final void addInvalidationListener(InvalidationListener listener)
059        {
060            if (listeners != null)
061            {
062                listeners.add(listener);
063            }
064        }
065    }