001: /* *****************************************************************************
002: * ConnectionGroup.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.connection;
011:
012: import java.io.*;
013: import java.util.*;
014: import javax.servlet.http.*;
015: import javax.servlet.*;
016: import org.openlaszlo.utils.*;
017: import org.apache.log4j.*;
018:
019: public class ConnectionGroup {
020: private static Hashtable mGroups = new Hashtable();
021: static private Logger mLogger = Logger
022: .getLogger(ConnectionGroup.class);
023:
024: /** Group name */
025: private String mName;
026:
027: /** Applications in group */
028: private Set mApplications = new HashSet();
029:
030: static public ConnectionGroup getGroup(String name) {
031: return getGroup(name, true);
032: }
033:
034: synchronized static public ConnectionGroup getGroup(String name,
035: boolean create) {
036: ConnectionGroup group = (ConnectionGroup) mGroups.get(name);
037: if (group == null && create) {
038: group = new ConnectionGroup(name);
039: mGroups.put(name, group);
040: }
041: return group;
042: }
043:
044: synchronized static private void removeGroup(ConnectionGroup group) {
045: mGroups.remove(group.getName());
046: }
047:
048: private ConnectionGroup(String name) {
049: mName = name;
050: }
051:
052: public String getName() {
053: return mName;
054: }
055:
056: public void registerApplication(Application application) {
057: mLogger.debug(
058: /* (non-Javadoc)
059: * @i18n.test
060: * @org-mes="register(" + p[0] + ")"
061: */
062: org.openlaszlo.i18n.LaszloMessages.getMessage(
063: ConnectionGroup.class.getName(), "051018-63",
064: new Object[] { application }));
065: synchronized (mApplications) {
066: mApplications.add(application);
067: }
068: }
069:
070: public void unregisterApplication(Application application) {
071: mLogger.debug(
072: /* (non-Javadoc)
073: * @i18n.test
074: * @org-mes="unregisterApplication(" + p[0] + ")"
075: */
076: org.openlaszlo.i18n.LaszloMessages.getMessage(
077: ConnectionGroup.class.getName(), "051018-77",
078: new Object[] { application }));
079: synchronized (mApplications) {
080: mApplications.remove(application);
081: }
082: }
083:
084: public Set list(String users) {
085: mLogger.debug(
086: /* (non-Javadoc)
087: * @i18n.test
088: * @org-mes="list(users)"
089: */
090: org.openlaszlo.i18n.LaszloMessages.getMessage(
091: ConnectionGroup.class.getName(), "051018-92"));
092:
093: Set set = new HashSet();
094: Iterator iter = mApplications.iterator();
095: while (iter.hasNext()) {
096: Application app = (Application) iter.next();
097: set.addAll(app.list(users));
098: }
099: return set;
100: }
101:
102: public int sendMessage(String users, String mesg, String range,
103: StringBuffer xmlResult) {
104: mLogger.debug(
105: /* (non-Javadoc)
106: * @i18n.test
107: * @org-mes="sendMesage(users, mesg, range, xmlResult)"
108: */
109: org.openlaszlo.i18n.LaszloMessages.getMessage(
110: ConnectionGroup.class.getName(), "051018-112"));
111:
112: int count = 0;
113: Iterator iter = mApplications.iterator();
114: while (iter.hasNext()) {
115: Application app = (Application) iter.next();
116: count += app.sendMessage(users, mesg, range, xmlResult);
117: }
118: if (xmlResult != null) {
119: xmlResult.insert(0, "<send count=\"" + count + "\" >");
120: xmlResult.append("</send>");
121: }
122: return count;
123: }
124:
125: synchronized static public void dumpGroupsXML(StringBuffer buf,
126: boolean details) {
127: Application.dumpTableXML("group", mGroups, buf, details);
128: }
129:
130: public String toString() {
131: StringBuffer buf = new StringBuffer();
132: buf.append("<group ").append(" name=\"").append(mName).append(
133: "\"").append(" >");
134: Iterator iter = mApplications.iterator();
135: while (iter.hasNext()) {
136: Application app = (Application) iter.next();
137: buf
138: .append("<application name=\"" + app.getName()
139: + "\" />");
140: }
141: buf.append("</group>");
142: return buf.toString();
143: }
144: }
|