001// Copyright 2005 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.tapestry.describe;
016
017import java.util.ArrayList;
018import java.util.Iterator;
019import java.util.List;
020
021import org.apache.hivemind.util.Defense;
022import org.apache.tapestry.IMarkupWriter;
023import org.apache.tapestry.event.ReportStatusEvent;
024import org.apache.tapestry.event.ReportStatusListener;
025
026/**
027 * Implementation of the tapestry.describe.ReportStatusHub service.
028 * 
029 * @author Howard M. Lewis Ship
030 * @since 4.0
031 */
032public class ReportStatusHubImpl implements ReportStatusHub
033{
034    private List _listeners = new ArrayList();
035
036    private RootDescriptionReceiverFactory _receiverFactory;
037
038    public synchronized void addReportStatusListener(ReportStatusListener listener)
039    {
040        Defense.notNull(listener, "listener");
041
042        _listeners.add(listener);
043    }
044
045    public synchronized void removeReportStatusListener(ReportStatusListener listener)
046    {
047        Defense.notNull(listener, "listener");
048
049        _listeners.remove(listener);
050    }
051
052    public synchronized void fireReportStatus(IMarkupWriter writer)
053    {
054        if (_listeners.isEmpty())
055            return;
056
057        RootDescriptionReciever receiver = _receiverFactory.newRootDescriptionReceiver(writer);
058
059        ReportStatusEvent event = new ReportStatusEvent(this, receiver);
060
061        Iterator i = _listeners.iterator();
062
063        while (i.hasNext())
064        {
065            ReportStatusListener listener = (ReportStatusListener) i.next();
066
067            listener.reportStatus(event);
068
069            receiver.finishUp();
070        }
071    }
072
073    public void setReceiverFactory(RootDescriptionReceiverFactory receiverFactory)
074    {
075        _receiverFactory = receiverFactory;
076    }
077
078}