001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.kernel.standard;
018    
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    import java.util.List;
022    
023    import java.util.concurrent.CopyOnWriteArrayList;
024    import org.apache.xbean.kernel.KernelMonitor;
025    import org.apache.xbean.kernel.ServiceMonitor;
026    import org.apache.xbean.kernel.ServiceEvent;
027    import org.apache.xbean.kernel.KernelErrorsError;
028    
029    /**
030     * The KernelMonitorBroadcaster broadcasts kernel events to registered kernel monitors.
031     *
032     * @author Dain Sundstrom
033     * @version $Id$
034     * @since 2.0
035     */
036    public class KernelMonitorBroadcaster implements KernelMonitor {
037        /**
038         * The monitors of kernel events.
039         */
040        private final CopyOnWriteArrayList kernelMonitors = new CopyOnWriteArrayList();
041    
042        /**
043         * Adds a kernel monitor.
044         *
045         * @param kernelMonitor the kernel monitor to add
046         */
047        public void addKernelMonitor(KernelMonitor kernelMonitor) {
048            kernelMonitors.addIfAbsent(kernelMonitor);
049        }
050    
051        /**
052         * Removes a kernel monitor.
053         *
054         * @param kernelMonitor the kernel monitor to remove
055         */
056        public void removeKernelMonitor(KernelMonitor kernelMonitor) {
057            kernelMonitors.remove(kernelMonitor);
058        }
059    
060        /**
061         * {@inheritDoc}
062         */
063        public void serviceNotificationError(ServiceMonitor serviceMonitor, ServiceEvent serviceEvent, Throwable throwable) {
064            List errors = new ArrayList();
065            for (Iterator iterator = kernelMonitors.iterator(); iterator.hasNext();) {
066                KernelMonitor kernelMonitor = (KernelMonitor) iterator.next();
067                try {
068                    kernelMonitor.serviceNotificationError(serviceMonitor, serviceEvent, throwable);
069                } catch (RuntimeException ignored) {
070                    // ignore - we did our best to notify the world
071                } catch (Error e) {
072                    errors.add(e);
073                }
074            }
075            if (!errors.isEmpty()) {
076                throw new KernelErrorsError(errors);
077            }
078        }
079    }