1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3 
4 package nl.justobjects.pushlet.core;
5 
6 import nl.justobjects.pushlet.util.Log;
7 import nl.justobjects.pushlet.util.Sys;
8 
9 import java.util.Enumeration;
10import java.util.Properties;
11import java.util.Vector;
12import java.io.File;
13
14/**
15 * Maintains lifecycle of event sources.
16 *
17 * @author Just van den Broecke - Just Objects &copy;
18 * @version $Id: EventSourceManager.java,v 1.14 2007/11/10 13:44:02 justb Exp $
19 */
20public class EventSourceManager {
21    private static Vector eventSources = new Vector(0);
22    private static final String PROPERTIES_FILE = "sources.properties";
23
24    /**
25     * Initialize event sources from properties file.
26     */
27    public static void start(String aDirPath) {
28        // Load Event sources using properties file.
29        Log.info("EventSourceManager: start");
30
31        Properties properties = null;
32
33        try {
34            properties = Sys.loadPropertiesResource(PROPERTIES_FILE);
35        } catch (Throwable t) {
36            // Try from provided dir (e.g. WEB_INF/pushlet.properties)
37            String filePath = aDirPath + File.separator + PROPERTIES_FILE;
38            Log.info("EventSourceManager: cannot load " + PROPERTIES_FILE + " from classpath, will try from " + filePath);
39
40            try {
41                properties = Sys.loadPropertiesFile(filePath);
42            } catch (Throwable t2) {
43                Log.fatal("EventSourceManager: cannot load properties file from " + filePath, t);
44
45                // Give up
46                Log.warn("EventSourceManager: not starting local event sources (maybe that is what you want)");
47                return;
48            }
49        }
50
51        // Create event source collection
52        eventSources = new Vector(properties.size());
53
54        // Add the configured sources
55        for (Enumeration e = properties.keys(); e.hasMoreElements();) {
56            String nextKey = (String) e.nextElement();
57            String nextClass = properties.getProperty(nextKey);
58            EventSource nextEventSource = null;
59            try {
60                nextEventSource = (EventSource) Class.forName(nextClass).newInstance();
61                Log.info("created EventSource: key=" + nextKey + " class=" + nextClass);
62                eventSources.addElement(nextEventSource);
63            } catch (Exception ex) {
64                Log.warn("Cannot create EventSource: class=" + nextClass, ex);
65            }
66        }
67
68        activate();
69    }
70
71    /**
72     * Activate all event sources.
73     */
74    public static void activate() {
75        Log.info("Activating " + eventSources.size() + " EventSources");
76        for (int i = 0; i < eventSources.size(); i++) {
77            ((EventSource) eventSources.elementAt(i)).activate();
78        }
79        Log.info("EventSources activated");
80    }
81
82    /**
83     * Deactivate all event sources.
84     */
85    public static void passivate() {
86        Log.info("Passivating " + eventSources.size() + " EventSources");
87        for (int i = 0; i < eventSources.size(); i++) {
88            ((EventSource) eventSources.elementAt(i)).passivate();
89        }
90        Log.info("EventSources passivated");
91    }
92
93    /**
94     * Halt event sources.
95     */
96    public static void stop() {
97        Log.info("Stopping " + eventSources.size() + " EventSources...");
98        for (int i = 0; i < eventSources.size(); i++) {
99            ((EventSource) eventSources.elementAt(i)).stop();
00        }
01        Log.info("EventSources stopped");
02    }
03
04}
05
06/*
07 * $Log: EventSourceManager.java,v $
08 * Revision 1.14  2007/11/10 13:44:02  justb
09 * pushlet.properties and sources.properties can now also be put under WEB-INF
10 *
11 * Revision 1.13  2005/02/21 11:50:46  justb
12 * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
13 *
14 * Revision 1.12  2005/02/18 12:36:47  justb
15 * changes for renaming and configurability
16 *
17 * Revision 1.11  2005/02/18 10:07:23  justb
18 * many renamings of classes (make names compact)
19 *
20 * Revision 1.10  2005/02/15 13:29:49  justb
21 * use Sys.loadPropertiesResource()
22 *
23 * Revision 1.9  2004/09/20 22:01:38  justb
24 * more changes for new protocol
25 *
26 * Revision 1.8  2004/09/03 22:35:37  justb
27 * Almost complete rewrite, just checking in now
28 *
29 * Revision 1.7  2004/08/15 16:00:15  justb
30 * enhancements to pull mode
31 *
32 * Revision 1.6  2004/08/13 23:36:05  justb
33 * rewrite of Pullet into Pushlet "pull" mode
34 *
35 * Revision 1.5  2004/08/12 13:18:54  justb
36 * cosmetic changes
37 *
38 * Revision 1.4  2003/08/15 08:37:40  justb
39 * fix/add Copyright+LGPL file headers and footers
40 *
41 * Revision 1.3  2003/08/12 09:41:35  justb
42 * replace static initalizer with explicit init()
43 *
44 * Revision 1.2  2003/05/18 16:15:08  justb
45 * support for XML encoded Events
46 *
47 * Revision 1.1.1.1  2002/09/24 21:02:31  justb
48 * import to sourceforge
49 *
50 * Revision 1.1.1.1  2002/09/20 22:48:17  justb
51 * import to SF
52 *
53 * Revision 1.1.1.1  2002/09/20 14:19:03  justb
54 * first import into SF
55 *
56 * Revision 1.5  2002/04/15 20:42:41  just
57 * reformatting and renaming GuardedQueue to EventQueue
58 *
59 * Revision 1.4  2000/10/30 14:15:47  just
60 * no message
61 *
62 * Revision 1.3  2000/08/31 08:26:54  just
63 * Changed classloader that loads eventsources.properties to use EventSourceManager's classloader
64 *
65 * Revision 1.2  2000/08/21 20:48:29  just
66 * added CVS log and id tags plus copyrights
67 *
68 *
69 */
70