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