1 package org.musicontroller.streaming;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.musicontroller.DJ;
8 import org.musicontroller.DJFactory;
9
10
11 /**
12 * The StreamMaster keeps track of all userIds and DJs. It maintains the
13 * relation between DJ's and Users. GUI sessions may come and go, but the
14 * streammaster maintains a one-to-one relation between a user and a DJ.
15 *
16 * The UI uses this class to redirect requests to the right DJ.
17 * The StreamService uses this class to get the DJ from upon initialization.
18 * @author Arjan Verstoep
19 */
20 public class StreamMaster {
21
22 /**
23 * Spring Injected. Factory for DJ objects.
24 */
25 private static DJFactory _djFactory;
26
27 /**
28 * A Map containing all DJ-objects.
29 */
30 private static Map<Long,DJ> _djs = new HashMap<Long,DJ>();
31
32
33 /**
34 * Retrieve the DJ belonging to a user. Returns the DJ
35 * in the DJ map or creates a new DJ if the map does not
36 * contain a DJ for the user id.
37 *
38 * @param userid The id of the user.
39 * @return The DJ of that user.
40 */
41 public static DJ getDJByUser(long userid) {
42 DJ dj = _djs.get(userid);
43 if (dj==null) {
44 //There is no DJ for this userid yet, create one.
45 dj = _djFactory.create(userid);
46 if(dj!=null) {
47 _djs.put(userid,dj);
48 }
49 }
50 return dj;
51 }
52
53 /**
54 * Setter for the DJ factory.
55 * @param factory The DF factory.
56 */
57 public void setDjFactory(DJFactory factory) {
58 _djFactory = factory;
59 }
60
61 /**
62 * Getter for the DJ factory.
63 * @return The DJ factory.
64 */
65 public DJFactory getDjFactory() {
66 return _djFactory;
67 }
68
69 public static Collection<Long> getUserIDs() {
70 return _djs.keySet();
71 }
72
73 }