1 25 package org.snipsnap.app; 26 27 import org.radeox.util.logging.Logger; 28 import org.snipsnap.config.Configuration; 29 import org.snipsnap.config.ConfigurationProxy; 30 import org.snipsnap.container.Components; 31 import org.snipsnap.user.AuthenticationService; 32 import org.snipsnap.user.User; 33 import org.snipsnap.user.UserManagerFactory; 34 import org.snipsnap.util.ApplicationAwareMap; 35 36 import javax.servlet.http.HttpSession ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Map ; 42 43 49 public class Application { 50 public final static String OID = "_applicationOid"; 51 public final static String URL = "_url"; 52 53 private static ApplicationAwareMap currentUsers = 54 new ApplicationAwareMap(HashMap .class, HashMap .class); 55 56 private static ThreadLocal instance = new ThreadLocal () { 57 protected synchronized Object initialValue() { 58 Logger.debug("creating new initial application value"); 59 return new Application(); 60 } 61 }; 62 63 private Map objectStore = new HashMap (); 65 private User user; 66 private Configuration config; 67 private List log = new ArrayList (); 68 private Map params; 70 71 public static Application get() { 72 Application app = (Application) instance.get(); 73 if (null == app) { 74 app = new Application(); 75 instance.set(app); 76 } 77 return app; 78 } 79 80 public static Application forceGet() { 81 instance.set(null); 82 return get(); 83 } 84 85 public static void set(Application application) { 86 instance.set(application); 87 } 88 89 public void clearLog() { 90 log = new ArrayList (); 91 } 92 93 public List getLog() { 94 return log; 95 } 96 97 public void log(String output) { 98 log.add(output); 99 } 100 101 public long start() { 102 return System.currentTimeMillis(); 103 } 104 105 public void stop(long start, String output) { 106 Logger.log(Logger.PERF, output + " - " + (System.currentTimeMillis() - start)); 107 } 108 109 public User getUser() { 110 return user; 111 } 112 113 public void setUser(User user) { 114 this.user = user; 115 } 116 117 public Map getParameters() { 118 return params; 119 } 120 121 public void setParameters(Map parameters) { 122 this.params = parameters; 123 } 124 125 public void setUser(User user, HttpSession session) { 126 if (this.user == user) { 127 return; 128 } 129 130 if (this.user != null) { 131 Application.removeCurrentUser(session); 132 } 133 setUser(user); 134 Application.addCurrentUser(user, session); 135 if(user.isGuest() || user.isNonUser()) { 136 session.setMaxInactiveInterval(120); 137 } 138 return; 139 } 140 141 public static synchronized void addCurrentUser(User user, HttpSession session) { 142 currentUsers.getMap().put(session, user); 144 } 145 146 public static List getCurrentUsers() { 147 List users = new ArrayList (); 148 Iterator iterator = currentUsers.getMap().values().iterator(); 149 while (iterator.hasNext()) { 150 User user = (User) iterator.next(); 151 if (!(user.isGuest() || user.isNonUser() || users.contains(user))) { 152 users.add(user); 153 } 154 } 155 return users; 156 } 157 158 public static List getCurrentNonUsers() { 159 List users = new ArrayList (); 160 Iterator iterator = currentUsers.getMap().values().iterator(); 161 while (iterator.hasNext()) { 162 User user = (User) iterator.next(); 163 if (user.isNonUser() && !users.contains(user) && !"IGNORE".equals(user.getEmail())) { 164 users.add(user); 165 } 166 } 167 return users; 168 } 169 170 public static int getGuestCount() { 171 int count = 0; 172 Iterator iterator = currentUsers.getMap().values().iterator(); 173 while (iterator.hasNext()) { 174 User user = (User) iterator.next(); 175 if (user.isGuest() && !user.isNonUser()) { 176 count++; 177 } 178 } 179 return count; 180 } 181 182 public static synchronized void removeCurrentUser(HttpSession session) { 183 if (null == currentUsers || null == session) { 184 return; 185 } 186 187 String appOid = (String ) Application.get().getObject(Application.OID); 188 Map currentUsersMap = null; 189 if (null != appOid) { 190 currentUsersMap = currentUsers.getMap(appOid); 191 } else { 192 currentUsersMap = currentUsers.findMap(session); 193 } 194 195 if (null != currentUsersMap && currentUsersMap.containsKey(session)) { 196 User user = (User) currentUsersMap.get(session); 197 AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class); 198 199 if (service.isAuthenticated(user)) { 200 Logger.debug("Removing authenticated user from session: " + user); 201 user.setLastLogout(user.getLastAccess()); 202 Application.get().storeObject(Application.OID, user.getApplication()); 204 UserManagerFactory.getInstance().systemStore(user); 205 } else { 206 Logger.debug("Removing unauthenticated user from session: " + user); 207 } 208 currentUsersMap.remove(session); 209 } else { 210 Logger.warn("Unable to remove current user from session '" + session + "'"); 211 } 212 } 213 214 public void storeObject(String key, Object value) { 215 objectStore.put(key, value); 216 } 217 218 public Object getObject(String key) { 219 return objectStore.get(key); 220 } 221 222 public void setConfiguration(Configuration config) { 223 this.config = config; 224 } 225 226 public Configuration getConfiguration() { 227 if (null == config) { 228 config = ConfigurationProxy.getInstance(); 229 } 230 return config; 231 } 232 } 233 | Popular Tags |