001 // Copyright 2006, 2007, 2010 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.ioc.internal.services;
016
017 import java.lang.ref.WeakReference;
018 import java.util.Iterator;
019 import java.util.List;
020
021 import org.apache.tapestry5.ioc.annotations.PreventServiceDecoration;
022 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
023 import org.apache.tapestry5.services.UpdateListener;
024 import org.apache.tapestry5.services.UpdateListenerHub;
025
026 @PreventServiceDecoration
027 public class UpdateListenerHubImpl implements UpdateListenerHub
028 {
029 private final List<WeakReference<UpdateListener>> listeners = CollectionFactory.newThreadSafeList();
030
031 public void addUpdateListener(UpdateListener listener)
032 {
033 assert listener != null;
034 listeners.add(new WeakReference<UpdateListener>(listener));
035 }
036
037 /**
038 * Notifies all {@link UpdateListener}s.
039 */
040 public void fireCheckForUpdates()
041 {
042 List<WeakReference<UpdateListener>> deadReferences = CollectionFactory.newList();
043
044 Iterator<WeakReference<UpdateListener>> i = listeners.iterator();
045
046 while (i.hasNext())
047 {
048 WeakReference<UpdateListener> reference = i.next();
049
050 UpdateListener listener = reference.get();
051
052 if (listener == null)
053 deadReferences.add(reference);
054 else
055 listener.checkForUpdates();
056 }
057
058 if (!deadReferences.isEmpty())
059 listeners.removeAll(deadReferences);
060 }
061 }