KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > ApplicationContext


1 /*
2  * Copyright 2004 JavaFree.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.javabb.infra;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.TreeMap JavaDoc;
25
26 import javax.servlet.http.HttpSession JavaDoc;
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 /**
38  * @author Ronald Tetsuo Miura
39  * @since 14/02/2005
40  */

41 public class ApplicationContext {
42     private static final Log LOG = LogFactory.getLog(ApplicationContext.class);
43
44     private static final String JavaDoc KEY_APPLICATION_CONTEXT = "javabb.application.context";
45
46     /**
47      * @return application context for the current thread
48      */

49     public synchronized static ApplicationContext getContext() {
50         try {
51             Map JavaDoc 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 JavaDoc e) {
57             return null;
58         }
59     }
60
61     /**
62      * @return user list
63      */

64     public synchronized Collection JavaDoc getOnlineGuests() {
65         try {
66             Map JavaDoc clickMap = (Map JavaDoc) ActionContext.getContext()
67                 .getApplication()
68                 .get(ClickstreamListener.CLICKSTREAMS_ATTRIBUTE_KEY);
69             
70             Map JavaDoc clickstreams = new TreeMap JavaDoc(clickMap);
71             
72             UserContext.getContext();
73
74             synchronized (clickstreams) {
75                 // make copy for thread safety of the map
76
// clickstreams = new HashMap(clickstreams);
77

78                 Collection JavaDoc users = new ArrayList JavaDoc();
79                 Iterator JavaDoc it = clickstreams.keySet().iterator();
80                 while (it.hasNext()) {
81                     try {
82                         String JavaDoc key = (String JavaDoc) it.next();
83                         Clickstream stream = (Clickstream) clickstreams.get(key);
84                         HttpSession JavaDoc 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 JavaDoc ex) { }
95                 }
96                 return users;
97             }
98         } catch (NullPointerException JavaDoc e) {
99             return null;
100         }
101     }
102
103     /**
104      * @return user list
105      */

106     public synchronized Collection JavaDoc getOnlineRegisteredUsers() {
107         Map JavaDoc clickMap = (Map JavaDoc) ActionContext.getContext()
108             .getApplication()
109             .get(ClickstreamListener.CLICKSTREAMS_ATTRIBUTE_KEY);
110         
111         Map JavaDoc clickstreams = new TreeMap JavaDoc(clickMap);
112
113         synchronized (clickstreams) {
114             // make copy for thread safety of the map
115
// clickstreams = new HashMap(clickstreams);
116

117             Collection JavaDoc users = new HashSet JavaDoc();
118             try {
119                 Iterator JavaDoc it = clickstreams.keySet().iterator();
120                 while (it.hasNext()) {
121                     try {
122                         String JavaDoc key = (String JavaDoc) it.next();
123                         Clickstream stream = (Clickstream) clickstreams.get(key);
124                         HttpSession JavaDoc 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 JavaDoc ex) { }
135                 }
136             } catch (RuntimeException JavaDoc e) { }
137             return users;
138         }
139     }
140     
141     /**
142      * If some user is Online
143      * @param user
144      * @return
145      */

146     public boolean isOnLine(User user){
147         HashSet JavaDoc users = (HashSet JavaDoc) getOnlineRegisteredUsers();
148         Iterator JavaDoc 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 JavaDoc e) { }
157             }
158         }
159         return false;
160     }
161     
162 }
163
Popular Tags