1 16 17 package org.javabb.infra; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.TreeMap ; 25 26 import javax.servlet.http.HttpSession ; 27 28 import org.apache.commons.lang.StringUtils; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.javabb.vo.User; 32 33 import com.opensymphony.clickstream.Clickstream; 34 import com.opensymphony.clickstream.ClickstreamListener; 35 import com.opensymphony.xwork.ActionContext; 36 37 41 public class ApplicationContext { 42 private static final Log LOG = LogFactory.getLog(ApplicationContext.class); 43 44 private static final String KEY_APPLICATION_CONTEXT = "javabb.application.context"; 45 46 49 public synchronized static ApplicationContext getContext() { 50 try { 51 Map application = ActionContext.getContext().getApplication(); 52 if (!application.containsKey(KEY_APPLICATION_CONTEXT)) { 53 application.put(KEY_APPLICATION_CONTEXT, new ApplicationContext()); 54 } 55 return (ApplicationContext) application.get(KEY_APPLICATION_CONTEXT); 56 } catch (RuntimeException e) { 57 return null; 58 } 59 } 60 61 64 public synchronized Collection getOnlineGuests() { 65 try { 66 Map clickMap = (Map ) ActionContext.getContext() 67 .getApplication() 68 .get(ClickstreamListener.CLICKSTREAMS_ATTRIBUTE_KEY); 69 70 Map clickstreams = new TreeMap (clickMap); 71 72 UserContext.getContext(); 73 74 synchronized (clickstreams) { 75 78 Collection users = new ArrayList (); 79 Iterator it = clickstreams.keySet().iterator(); 80 while (it.hasNext()) { 81 try { 82 String key = (String ) it.next(); 83 Clickstream stream = (Clickstream) clickstreams.get(key); 84 HttpSession session = stream.getSession(); 85 UserContext userContext = (UserContext) session.getAttribute(UserContext.KEY_USER_CONTEXT); 86 if(userContext != null){ 87 User user = userContext.getUser(); 88 if (user == null || StringUtils.isBlank(user.getUser())) { 89 users.add(user); 90 } 91 } else{ 92 users.add(null); 93 } 94 } catch (IllegalStateException ex) { } 95 } 96 return users; 97 } 98 } catch (NullPointerException e) { 99 return null; 100 } 101 } 102 103 106 public synchronized Collection getOnlineRegisteredUsers() { 107 Map clickMap = (Map ) ActionContext.getContext() 108 .getApplication() 109 .get(ClickstreamListener.CLICKSTREAMS_ATTRIBUTE_KEY); 110 111 Map clickstreams = new TreeMap (clickMap); 112 113 synchronized (clickstreams) { 114 117 Collection users = new HashSet (); 118 try { 119 Iterator it = clickstreams.keySet().iterator(); 120 while (it.hasNext()) { 121 try { 122 String key = (String ) it.next(); 123 Clickstream stream = (Clickstream) clickstreams.get(key); 124 HttpSession session = stream.getSession(); 125 UserContext userContext = (UserContext) session.getAttribute(UserContext.KEY_USER_CONTEXT); 126 if(userContext != null){ 127 User user = userContext.getUser(); 128 if (user != null && StringUtils.isNotBlank(user.getUser())) { 129 users.add(user); 130 } 131 } else { 132 users.add(null); 133 } 134 } catch (IllegalStateException ex) { } 135 } 136 } catch (RuntimeException e) { } 137 return users; 138 } 139 } 140 141 146 public boolean isOnLine(User user){ 147 HashSet users = (HashSet ) getOnlineRegisteredUsers(); 148 Iterator it = users.iterator(); 149 if(users != null && !users.isEmpty()){ 150 while (it.hasNext()) { 151 User hashUser = (User)it.next(); 152 try { 153 if(hashUser.getId().intValue() == user.getId().intValue()){ 154 return true; 155 } 156 } catch (RuntimeException e) { } 157 } 158 } 159 return false; 160 } 161 162 } 163 | Popular Tags |