KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > connection > ConnectionGroup


1 /* *****************************************************************************
2  * ConnectionGroup.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.connection;
11
12 import java.io.*;
13 import java.util.*;
14 import javax.servlet.http.*;
15 import javax.servlet.*;
16 import org.openlaszlo.utils.*;
17 import org.apache.log4j.*;
18
19 public class ConnectionGroup
20 {
21     private static Hashtable mGroups = new Hashtable();
22     static private Logger mLogger = Logger.getLogger(ConnectionGroup.class);
23
24     /** Group name */
25     private String JavaDoc mName;
26
27     /** Applications in group */
28     private Set mApplications = new HashSet();
29
30     static public ConnectionGroup getGroup(String JavaDoc name) {
31         return getGroup(name, true);
32     }
33
34     synchronized static public ConnectionGroup getGroup(String JavaDoc name, boolean create) {
35         ConnectionGroup group = (ConnectionGroup) mGroups.get(name);
36         if (group == null && create) {
37             group = new ConnectionGroup(name);
38             mGroups.put(name, group);
39         }
40         return group;
41     }
42
43
44     synchronized static private void removeGroup(ConnectionGroup group) {
45         mGroups.remove(group.getName());
46     }
47
48
49     private ConnectionGroup(String JavaDoc name) {
50         mName = name;
51     }
52
53     public String JavaDoc getName() {
54         return mName;
55     }
56
57     public void registerApplication(Application application) {
58         mLogger.debug("register(" + application + ")");
59         synchronized (mApplications) {
60             mApplications.add(application);
61         }
62     }
63
64     public void unregisterApplication(Application application) {
65         mLogger.debug("unregisterApplication(" + application + ")");
66         synchronized (mApplications) {
67             mApplications.remove(application);
68         }
69     }
70
71
72     public Set list(String JavaDoc users) {
73         mLogger.debug("list(users)");
74
75         Set set = new HashSet();
76         Iterator iter = mApplications.iterator();
77         while (iter.hasNext()) {
78             Application app = (Application)iter.next();
79             set.addAll(app.list(users));
80         }
81         return set;
82     }
83
84     public int sendMessage(String JavaDoc users, String JavaDoc mesg, String JavaDoc range,
85                            StringBuffer JavaDoc xmlResult) {
86         mLogger.debug("sendMesage(users, mesg, range, xmlResult)");
87
88         int count = 0;
89         Iterator iter = mApplications.iterator();
90         while (iter.hasNext()) {
91             Application app = (Application)iter.next();
92             count += app.sendMessage(users, mesg, range, xmlResult);
93         }
94         if (xmlResult != null) {
95             xmlResult.insert(0, "<send count=\"" + count + "\" >");
96             xmlResult.append("</send>");
97         }
98         return count;
99     }
100
101
102     synchronized static public void dumpGroupsXML(StringBuffer JavaDoc buf, boolean details)
103     {
104         Application.dumpTableXML("group", mGroups, buf, details);
105     }
106
107     public String JavaDoc toString() {
108         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
109         buf.append("<group ")
110             .append(" name=\"").append(mName).append("\"")
111             .append(" >");
112             Iterator iter = mApplications.iterator();
113             while (iter.hasNext()) {
114                 Application app = (Application)iter.next();
115                 buf.append("<application name=\"" + app.getName() + "\" />");
116             }
117         buf.append("</group>");
118         return buf.toString();
119     }
120 }
121
Popular Tags