1 4 5 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 25 private String mName; 26 27 28 private Set mApplications = new HashSet(); 29 30 static public ConnectionGroup getGroup(String name) { 31 return getGroup(name, true); 32 } 33 34 synchronized static public ConnectionGroup getGroup(String 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 name) { 50 mName = name; 51 } 52 53 public String 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 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 users, String mesg, String range, 85 StringBuffer 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 buf, boolean details) 103 { 104 Application.dumpTableXML("group", mGroups, buf, details); 105 } 106 107 public String toString() { 108 StringBuffer buf = new StringBuffer (); 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 |