KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > app > Application


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /**
44  * The application object contains information about current users and other
45  * session specific information.
46  * @author Stephan J. Schmidt
47  * @version $Id: Application.java 1606 2004-05-17 10:56:18Z leo $
48  */

49 public class Application {
50   public final static String JavaDoc OID = "_applicationOid";
51   public final static String JavaDoc URL = "_url";
52
53   private static ApplicationAwareMap currentUsers =
54     new ApplicationAwareMap(HashMap JavaDoc.class, HashMap JavaDoc.class);
55
56   private static ThreadLocal JavaDoc instance = new ThreadLocal JavaDoc() {
57     protected synchronized Object JavaDoc initialValue() {
58       Logger.debug("creating new initial application value");
59       return new Application();
60     }
61   };
62
63   // TODO make this an application-aware map to get old functionality
64
private Map objectStore = new HashMap JavaDoc();
65   private User user;
66   private Configuration config;
67   private List JavaDoc log = new ArrayList JavaDoc();
68   // TODO use private NotificationService notification;
69
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 JavaDoc();
91   }
92
93   public List JavaDoc getLog() {
94     return log;
95   }
96
97   public void log(String JavaDoc output) {
98     log.add(output);
99   }
100
101   public long start() {
102     return System.currentTimeMillis();
103   }
104
105   public void stop(long start, String JavaDoc 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 JavaDoc 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 JavaDoc session) {
142 // Logger.debug("Binding user to session: "+user+": "+session);
143
currentUsers.getMap().put(session, user);
144   }
145
146   public static List JavaDoc getCurrentUsers() {
147     List JavaDoc users = new ArrayList JavaDoc();
148     Iterator JavaDoc 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 JavaDoc getCurrentNonUsers() {
159     List JavaDoc users = new ArrayList JavaDoc();
160     Iterator JavaDoc 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 JavaDoc 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 JavaDoc session) {
183     if (null == currentUsers || null == session) {
184       return;
185     }
186
187     String JavaDoc appOid = (String JavaDoc) 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         // we ensure we remove the correct user by setting the OID from the user object
203
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 JavaDoc key, Object JavaDoc value) {
215     objectStore.put(key, value);
216   }
217
218   public Object JavaDoc getObject(String JavaDoc 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