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.auth.*; 17 import org.openlaszlo.utils.*; 18 import org.apache.log4j.*; 19 20 public class Application 21 { 22 private static final int RANGE_ALL = 0; 23 private static final int RANGE_USER = 1; 24 private static final int RANGE_AGENT = 2; 25 26 private static Hashtable mApplications = new Hashtable(); 27 static private Logger mLogger = Logger.getLogger(Application.class); 28 29 static public Application getApplication(String name) 30 { 31 return getApplication(name, true); 32 } 33 34 synchronized static public Application getApplication(String name, boolean create) 35 { 36 Application application = (Application)mApplications.get(name); 37 if (application == null && create) { 38 application = new Application(name); 39 mApplications.put(name, application); 40 } 41 return application; 42 } 43 44 synchronized static public void removeApplication(Application application) { 45 ConnectionGroup group = application.getConnectionGroup(); 46 if (group != null) { 47 group.unregisterApplication(application); 48 } 49 mApplications.remove(application); 50 } 51 52 private String mName; 53 private long mLastModifiedTime = 0; 54 private long mHeartbeat = 0; 55 private Authentication mAuthenticator = null; 56 private boolean mSendUserDisconnect = false; 57 private ConnectionGroup mGroup = null; 58 59 60 private MultiMap mUsers = new MultiMap(); 61 62 63 private Hashtable mConnections = new Hashtable(); 64 65 66 private Hashtable mAgents = new Hashtable(); 67 68 private Application(String name) { 69 mName = name; 70 } 71 72 public String getName() { 73 return mName; 74 } 75 76 public long getLastModifiedTime() { 77 return mLastModifiedTime; 78 } 79 80 public void setLastModifiedTime(long lmt) { 81 mLastModifiedTime = lmt; 82 } 83 84 public long getHeartbeat() { 85 return mHeartbeat; 86 } 87 88 public void setHeartbeat(long heartbeat) { 89 mHeartbeat = heartbeat; 90 } 91 92 public Authentication getAuthenticator() { 93 return mAuthenticator; 94 } 95 96 public void setAuthenticator(Authentication authenticator) { 97 mAuthenticator = authenticator; 98 } 99 100 public boolean doSendUserDisconnect() { 101 return mSendUserDisconnect; 102 } 103 104 public void setSendUserDisconnect(boolean sud) { 105 mSendUserDisconnect = sud; 106 } 107 108 public ConnectionGroup getConnectionGroup() { 109 return mGroup; 110 } 111 112 public void setConnectionGroup(ConnectionGroup group) { 113 if (mGroup != null) { 114 mGroup.unregisterApplication(this); 115 } 116 group.registerApplication(this); 117 mGroup = group; 118 } 119 120 121 private void checkGroup() { 125 if (mGroup == null) 126 throw new RuntimeException ("connection group not set"); 127 } 128 129 public void register(HTTPConnection connection) { 130 synchronized (mUsers) { 131 mUsers.put(connection.getUsername(), connection); 132 mConnections.put(connection.getCID(), connection); 133 } 134 } 135 136 public void unregister(HTTPConnection connection) { 137 synchronized (mUsers) { 138 mUsers.remove(connection.getUsername(), connection); 139 mConnections.remove(connection.getCID()); 140 } 141 } 142 143 public void setAgents(Set agentSet) { 144 mAgents = new Hashtable(); 145 if (agentSet != null) { 146 synchronized (mAgents) { 147 Iterator iter = agentSet.iterator(); 148 while (iter.hasNext()) { 149 ConnectionAgent agent = 150 ConnectionAgent.getAgent((String )iter.next()); 151 mAgents.put(agent.getURL(), agent); 152 } 153 } 154 } 155 } 156 157 158 164 public int sendMessage(String users, String mesg, String range, 165 StringBuffer xmlResult) 166 { 167 mLogger.debug("sendMessage(users=" + users + ",range=" + range + ",mesg=" + mesg + ")"); 168 169 if (users == null || users.equals("")) 170 return 0; 171 172 int r = RANGE_ALL; 173 if (range == null || range.equals("")) { 174 r = RANGE_ALL; 175 } else if (range.equals("user")) { 176 r = RANGE_USER; 177 } else if (range.equals("agent")) { 178 r = RANGE_AGENT; 179 } 180 181 if (users.equals("*")) { 182 return sendAllMessage(mesg, r, xmlResult); 183 } 184 185 int count = 0; 186 StringTokenizer st = new StringTokenizer(users, ", "); 187 while (st.hasMoreTokens()) { 188 String username = (String )st.nextToken(); 189 190 if (r == RANGE_ALL || r == RANGE_USER) { 191 synchronized (mUsers) { 192 Set usernameSet = (Set)mUsers.get(username); 193 if (usernameSet != null) { 194 Iterator iter = usernameSet.iterator(); 195 196 while (iter.hasNext()) { 197 HTTPConnection connection = (HTTPConnection)iter.next(); 198 try { 199 mLogger.debug("send to " + connection.getUsername()); 200 connection.send(mesg); 201 ++count; 202 } catch (IOException e) { 203 mLogger.debug("user " + connection.getUsername() + 204 " not connected"); 205 iter.remove(); 206 } 207 } 208 209 if (usernameSet.size() == 0) 210 mUsers.remove(username); 211 } 212 } 213 } 214 215 if (r == RANGE_ALL || r == RANGE_AGENT) { 216 ConnectionAgent agent = null; 217 try { 218 synchronized (mAgents) { 219 agent = (ConnectionAgent)mAgents.get(username); 220 } 221 if (agent != null) { 222 if (xmlResult != null) { 223 StringBuffer tmp = 224 new StringBuffer ("<agent url=\"" + agent.getURL() + "\" >"); 225 tmp.append(agent.send(mesg)); 226 tmp.append("</agent>"); 227 xmlResult.append(tmp.toString()); 228 } else { 229 agent.send(mesg); 230 } 231 ++count; 232 } 233 } catch (IOException e) { 234 mLogger.warn("IOException: agent " + agent.getURL()); 235 } 236 } 237 238 } 239 return count; 240 } 241 242 private int sendAllMessage(String mesg, int range, StringBuffer xmlResult) 243 { 244 int count = 0; 245 Iterator iter; 246 247 if (range == RANGE_ALL || range == RANGE_USER) { 248 synchronized (mUsers) { 249 iter = mConnections.entrySet().iterator(); 250 while (iter.hasNext()) { 251 Map.Entry entry = (Map.Entry)iter.next(); 252 HTTPConnection connection = (HTTPConnection)entry.getValue(); 253 try { 254 connection.send(mesg); 255 ++count; 256 mLogger.debug(connection.getUsername() + " sent message"); 257 } catch (IOException e) { 258 iter.remove(); 259 mLogger.debug(connection.getUsername() + " not connected"); 260 } 261 } 262 } 263 } 264 265 if (range == RANGE_ALL || range == RANGE_AGENT) { 266 synchronized (mAgents) { 267 iter = mAgents.entrySet().iterator(); 268 while (iter.hasNext()) { 269 Map.Entry entry = (Map.Entry)iter.next(); 270 ConnectionAgent agent = (ConnectionAgent)entry.getValue(); 271 try { 272 if (xmlResult != null) { 273 String result = agent.send(mesg); 274 StringBuffer tmp = 275 new StringBuffer ("<agent url=\"" + agent.getURL() + "\" >"); 276 tmp.append(result); 277 tmp.append("</agent>"); 278 xmlResult.append(tmp.toString()); 279 } else { 280 agent.send(mesg); 281 } 282 ++count; 283 } catch (IOException e) { 284 mLogger.warn("IOException: agent " + agent.getURL()); 285 } 286 } 287 } 288 } 289 290 if (xmlResult != null) { 291 xmlResult.insert(0, "<send count=\"" + count + "\" >"); 292 xmlResult.append("</send>"); 293 } 294 295 return count; 296 } 297 298 299 303 public Set list(String users) { 304 synchronized (mUsers) { 305 Set set = new HashSet(); 306 mLogger.debug("list(users=" + users + ")"); 307 308 if (users.equals("*")) { 309 Iterator iter = mUsers.keySet().iterator(); 310 while (iter.hasNext()) { 311 set.add((String )iter.next()); 312 } 313 } else { 314 StringTokenizer st = new StringTokenizer(users, ", "); 315 while (st.hasMoreTokens()) { 316 String username = (String )st.nextToken(); 317 if (mUsers.containsKey(username)) 318 set.add(username); 319 } 320 } 321 return set; 322 } 323 } 324 325 326 327 public HTTPConnection getConnection(String cid) { 328 synchronized (mUsers) { 329 if (cid == null || cid.equals("")) 330 return null; 331 return (HTTPConnection) mConnections.get(cid); 332 } 333 } 334 335 336 340 synchronized static public void dumpApplicationsXML(StringBuffer buf, 341 boolean details) { 342 Set s = mApplications.entrySet(); 343 Iterator iter = s.iterator(); 344 buf.append("<applications>"); 345 while (iter.hasNext()) { 346 Map.Entry e = (Map.Entry) iter.next(); 347 Application app = (Application)e.getValue(); 348 app.dumpXML(buf, details); 349 } 350 buf.append("</applications>"); 351 } 352 353 public void dumpXML(StringBuffer buf, boolean details) { 354 Authentication a = mAuthenticator; 355 ConnectionGroup g = mGroup; 356 buf.append("<application ") 357 .append(" name=\"").append(mName).append("\"") 358 .append(" group=\"").append(( g!=null ? g.getName() : "none" )).append("\"") 359 .append(" heartbeat=\"").append(mHeartbeat).append("\"") 360 .append(" authenticator=\"").append(( a!=null ? a.getClass().toString() : "none" )).append("\"") 361 .append(" senduserdisconnect=\"").append(mSendUserDisconnect).append("\"") 362 .append(" >"); 363 dumpConnectionsXML(buf, details); 365 dumpAgentsXML(buf, details); 366 buf.append("</application>"); 367 } 368 369 public void dumpConnectionsXML(StringBuffer buf, boolean details) { 370 synchronized (mUsers) { 371 dumpTableXML("connection", mConnections, buf, details); 372 } 373 } 374 375 public void dumpAgentsXML(StringBuffer buf, boolean details) { 376 synchronized (mAgents) { 377 dumpTableXML("agent", mAgents, buf, details); 378 } 379 } 380 381 public void dumpUsersXML(StringBuffer buf, boolean details) { 382 synchronized (mUsers) { 383 dumpMultiTableXML("user", mUsers, buf, details); 384 } 385 } 386 387 public static void dumpMultiTableXML(String table, Map map, 388 StringBuffer buf, boolean details) 389 { 390 Set s = map.entrySet(); 391 Iterator iter = s.iterator(); 392 buf.append("<").append(table).append("-table>"); 393 while (iter.hasNext()) { 394 Map.Entry e = (Map.Entry)iter.next(); 395 String k = (String )e.getKey(); 396 if (details) { 397 buf.append("<" + table + " name=\"" + k + "\">"); 398 Set set = (Set)e.getValue(); 399 Iterator iter1 = set.iterator(); 400 while (iter1.hasNext()) { 401 Object o = iter1.next(); 402 buf.append(o.toString()); 403 } 404 buf.append("</" + table + ">\n"); 405 } else { 406 buf.append("<" + table + " name=\"").append(k).append("\" />\n"); 407 } 408 409 } 410 buf.append("</").append(table).append("-table>\n"); 411 } 412 413 414 public static void dumpTableXML(String table, Map map, 415 StringBuffer buf, boolean details) 416 { 417 Set s = map.entrySet(); 418 Iterator iter = s.iterator(); 419 buf.append("<").append(table).append("-table>"); 420 while (iter.hasNext()) { 421 Map.Entry e = (Map.Entry)iter.next(); 422 String k = (String )e.getKey(); 423 if (details) { 424 Object v = e.getValue(); 425 buf.append(v.toString()); 426 } else { 427 buf.append("<" + table + " name=\"").append(k).append("\" />"); 428 } 429 430 } 431 buf.append("</").append(table).append("-table>\n"); 432 } 433 } 434 | Popular Tags |