001package votorola.a.diff.harvest.kick;// Copyright 2012. Christian Weilbach.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software")
002
003import java.util.Collections;
004import java.util.List;
005import java.util.LinkedList;
006import java.util.logging.Level;
007import java.util.logging.Logger;
008
009import votorola.a.diff.harvest.PipermailHarvester;
010import votorola.g.logging.LoggerX;
011
012/**
013 * Service to pass kick-events from detectors to harvesters.
014 */
015public class Kicker {
016
017    private static final Logger LOGGER = LoggerX.i(Kicker.class);
018
019    /**
020     * Implemented as singleton.
021     */
022    public static Kicker i() {
023        if (instance == null) {
024            synchronized (Kicker.class) {
025                try {
026                    instance = new Kicker();
027                } catch (Exception e) {
028                    LOGGER.log(Level.SEVERE, "Could not initialize the kicker.");
029                    System.exit(1);
030                }
031                instance.initHarvesters();
032            }
033        }
034        return instance;
035    }
036
037    private static Kicker instance;
038
039    private Kicker() {
040    }
041    
042    private void initHarvesters() {
043        new PipermailHarvester();
044    }
045
046    final private List<KickReceiver> receivers = Collections
047            .synchronizedList(new LinkedList<KickReceiver>());
048
049    /**
050     * Passes a kick to all registered harvesters.
051     * 
052     * @param kick
053     */
054    public void broadcast(final Kick kick) {
055        for (final KickReceiver r : receivers) {
056            r.handle(kick);
057        }
058    }
059
060    /**
061     * Each Harvester has to register here. It then learns about each archive
062     * through a {@linkplain UpdateKick}.
063     * 
064     * @param receiver
065     *            to register.
066     */
067    public void register(KickReceiver receiver) {
068        receivers.add(receiver);
069    }
070}
071