Author: sviluppatorefico
Date: 2008-05-18 07:01:48 -0400 (Sun, 18 May 2008)
New Revision: 10773
Modified:
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsService.java
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsServiceImpl.java
Log:
added support for sessionId and guest
Modified:
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsService.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsService.java 2008-05-18
11:00:50 UTC (rev 10772)
+++
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsService.java 2008-05-18
11:01:48 UTC (rev 10773)
@@ -32,6 +32,8 @@
{
public Set getActiveUsersIds(long period);
+
+ public int getActiveSessionCount(long period);
public Set getActiveUsersNames(long period);
Modified:
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsServiceImpl.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsServiceImpl.java 2008-05-18
11:00:50 UTC (rev 10772)
+++
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/core/identity/UsersActivityStatsServiceImpl.java 2008-05-18
11:01:48 UTC (rev 10773)
@@ -1,24 +1,24 @@
/*
-* JBoss, a division of Red Hat
-* Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
-*/
+ * JBoss, a division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
package org.jboss.portal.core.identity;
@@ -42,280 +42,249 @@
import java.util.Set;
/**
- * @author <a href="mailto:boleslaw dot dawidowicz at redhat anotherdot
com">Boleslaw Dawidowicz</a>
+ * @author <a href="mailto:boleslaw dot dawidowicz at redhat anotherdot
+ * com">Boleslaw Dawidowicz</a>
* @author <a href="mailto:jedim@vige.it">Luca Stancapiano</a>
* @version $Revision$
*/
-public class UsersActivityStatsServiceImpl extends AbstractJBossService implements
UsersActivityStatsService, NotificationListener
-{
- /** Our logger. */
- private static final Logger log =
Logger.getLogger(UsersActivityStatsServiceImpl.class);
+public class UsersActivityStatsServiceImpl extends AbstractJBossService
+ implements UsersActivityStatsService, NotificationListener {
+ /** Our logger. */
+ private static final Logger log = Logger
+ .getLogger(UsersActivityStatsServiceImpl.class);
- //TODO: some value just to begin - find some good default
- private int userTrackerThreadsNumber = 10;
+ // TODO: some value just to begin - find some good default
+ private int userTrackerThreadsNumber = 10;
- private int updaterThreadsNumber = 1;
+ private int updaterThreadsNumber = 1;
- private int updaterInterval = 1000;
+ private int updaterInterval = 1000;
- private int activityQueueLimit = 1000;
+ private int activityQueueLimit = 1000;
- private long activityTimeout = 1800000;
+ private long activityTimeout = 1800000;
- private Executor userTrackerExecutor;
+ private Executor userTrackerExecutor;
- private ScheduledExecutorService updaterExecutor;
+ private ScheduledExecutorService updaterExecutor;
- private Queue activityQueue;
+ private Queue activityQueue;
- private volatile Set activityResults = new HashSet();
+ private volatile Set activityResults = new HashSet();
- private String activityBroadcasterName;
+ private String activityBroadcasterName;
- public UsersActivityStatsServiceImpl()
- {
- }
+ public UsersActivityStatsServiceImpl() {
+ }
- protected void startService() throws Exception
- {
- super.startService();
+ protected void startService() throws Exception {
+ super.startService();
- activityQueue = new LinkedBlockingQueue(getActivityQueueLimit());
+ activityQueue = new LinkedBlockingQueue(getActivityQueueLimit());
- userTrackerExecutor = Executors.newFixedThreadPool(getUserTrackerThreadsNumber());
+ userTrackerExecutor = Executors
+ .newFixedThreadPool(getUserTrackerThreadsNumber());
- updaterExecutor = Executors.newScheduledThreadPool(getUpdaterThreadsNumber());
+ updaterExecutor = Executors
+ .newScheduledThreadPool(getUpdaterThreadsNumber());
- updaterExecutor.scheduleWithFixedDelay(new Updater(activityQueue),
getUpdaterInterval(), getUpdaterInterval(), TimeUnit.MILLISECONDS);
+ updaterExecutor.scheduleWithFixedDelay(new Updater(activityQueue),
+ getUpdaterInterval(), getUpdaterInterval(),
+ TimeUnit.MILLISECONDS);
- if (activityBroadcasterName != null)
- {
- server.addNotificationListener(new ObjectName(activityBroadcasterName), this,
null, null);
- }
- else
- {
- addNotificationListener(this, null, null);
- }
+ if (activityBroadcasterName != null) {
+ server.addNotificationListener(new ObjectName(
+ activityBroadcasterName), this, null, null);
+ } else {
+ addNotificationListener(this, null, null);
+ }
+ }
- }
+ protected void stopService() throws Exception {
+ super.stopService();
- protected void stopService() throws Exception
- {
- super.stopService();
+ // /TODO: stop all the threads
+ }
- // /TODO: stop all the threads
- }
+ public Set getActiveUsersIds(long period) {
+ long currentTime = System.currentTimeMillis();
- public Set getActiveUsersIds(long period)
- {
- long currentTime = System.currentTimeMillis();
+ Set results = new HashSet();
+ for (Iterator iterator = activityResults.iterator(); iterator.hasNext();) {
+ UserActivity ua = (UserActivity) iterator.next();
+ if (currentTime - ua.getTimestamp() < period
+ && !ua.getId().equals(UserActivity.GUEST)) {
+ results.add(ua.getSessionId());
+ }
+ }
+ return results;
+ }
- Set results = new HashSet();
- for (Iterator iterator = activityResults.iterator(); iterator.hasNext();)
- {
- UserActivity ua = (UserActivity)iterator.next();
- if (currentTime - ua.getTimestamp() < period)
- {
- results.add(ua.getId());
- }
- }
- return results;
- }
+ public int getActiveSessionCount(long period) {
+ long currentTime = System.currentTimeMillis();
- public Set getActiveUsersNames(long period)
- {
- long currentTime = System.currentTimeMillis();
- Set results = new HashSet();
- for (Iterator iterator = activityResults.iterator(); iterator.hasNext();)
- {
- UserActivity ua = (UserActivity)iterator.next();
- if (currentTime - ua.getTimestamp() < period)
- {
- results.add(ua.getId());
- }
- }
- return results;
- }
+ int results = 0;
+ for (Iterator iterator = activityResults.iterator(); iterator.hasNext();) {
+ UserActivity ua = (UserActivity) iterator.next();
+ if (currentTime - ua.getTimestamp() < period
+ && ua.getId().equals(UserActivity.GUEST)) {
+ results++;
+ }
+ }
+ return results;
+ }
- public Set getUsersActivities(long period)
- {
- long currentTime = System.currentTimeMillis();
- Set results = new HashSet();
- for (Iterator iterator = activityResults.iterator(); iterator.hasNext();)
- {
- UserActivity ua = (UserActivity)iterator.next();
- if (currentTime - ua.getTimestamp() < period)
- {
- results.add(ua);
- }
- }
- return results;
- }
+ public Set getActiveUsersNames(long period) {
+ long currentTime = System.currentTimeMillis();
+ Set results = new HashSet();
+ for (Iterator iterator = activityResults.iterator(); iterator.hasNext();) {
+ UserActivity ua = (UserActivity) iterator.next();
+ if (currentTime - ua.getTimestamp() < period
+ && !ua.getId().equals(UserActivity.GUEST)) {
+ results.add(ua.getId());
+ }
+ }
+ return results;
+ }
- public void registerActivity(final UserActivity userActivity)
- {
- try
- {
- Notification notification = new
Notification(Integer.toString(userActivity.getType()),
- this.getServiceName(),
- userActivity.getTimestamp(),
- userActivity.getTimestamp(),
- userActivity.getId());
+ public Set getUsersActivities(long period) {
+ long currentTime = System.currentTimeMillis();
+ Set results = new HashSet();
+ for (Iterator iterator = activityResults.iterator(); iterator.hasNext();) {
+ UserActivity ua = (UserActivity) iterator.next();
+ if (currentTime - ua.getTimestamp() < period) {
+ results.add(ua);
+ }
+ }
+ return results;
+ }
+ public void registerActivity(final UserActivity userActivity) {
+ try {
+ Notification notification = new Notification(Integer
+ .toString(userActivity.getType()), this.getServiceName(),
+ userActivity.getTimestamp(), userActivity.getTimestamp(),
+ userActivity.getId() + "_" + userActivity.getSessionId());
- if (activityBroadcasterName != null)
- {
- log.debug("Broadcasting user activity notification ");
+ if (activityBroadcasterName != null) {
+ log.debug("Broadcasting user activity notification ");
- server.invoke(new ObjectName(activityBroadcasterName),
- "sendNotification",
- new Object[]{notification},
- new String[]{Notification.class.getName()});
- }
- else
- {
- log.debug("Sending local user activity notification ");
- sendNotification(notification);
- }
+ server.invoke(new ObjectName(activityBroadcasterName),
+ "sendNotification", new Object[] { notification },
+ new String[] { Notification.class.getName() });
+ } else {
+ log.debug("Sending local user activity notification ");
+ sendNotification(notification);
+ }
- }
- catch (Exception e)
- {
- log.error("Failed to send user activity notification: ", e);
- }
+ } catch (Exception e) {
+ log.error("Failed to send user activity notification: ", e);
+ }
+ }
- }
+ public void handleNotification(Notification notification, Object object) {
+ log.debug("Handling user activity notification ");
+ final UserActivity ac = new UserActivity(notification);
- public void handleNotification(Notification notification, Object object)
- {
- log.debug("Handling user activity notification ");
- final UserActivity ac = new UserActivity(notification);
+ FutureTask task = new FutureTask(new Callable() {
+ public Object call() throws Exception {
- FutureTask task = new FutureTask(new Callable()
- {
- public Object call() throws Exception
- {
+ boolean success = activityQueue.offer(ac);
+ if (log.isTraceEnabled()) {
+ if (!success) {
+ log
+ .trace("Failed track user activity - activityQueue is full ");
+ }
+ }
+ return null;
+ }
+ });
- boolean success = activityQueue.offer(ac);
- if (log.isTraceEnabled())
- {
- if (!success)
- {
- log.trace("Failed track user activity - activityQueue is full
");
- }
- }
- return null;
- }
- });
+ userTrackerExecutor.execute(task);
+ }
- userTrackerExecutor.execute(task);
- }
+ public int getUserTrackerThreadsNumber() {
+ return userTrackerThreadsNumber;
+ }
+ public void setUserTrackerThreadsNumber(int userTrackerThreadsNumber) {
+ this.userTrackerThreadsNumber = userTrackerThreadsNumber;
+ }
- public int getUserTrackerThreadsNumber()
- {
- return userTrackerThreadsNumber;
- }
+ public int getUpdaterThreadsNumber() {
+ return updaterThreadsNumber;
+ }
- public void setUserTrackerThreadsNumber(int userTrackerThreadsNumber)
- {
- this.userTrackerThreadsNumber = userTrackerThreadsNumber;
- }
+ public void setUpdaterThreadsNumber(int updaterThreadsNumber) {
+ this.updaterThreadsNumber = updaterThreadsNumber;
+ }
- public int getUpdaterThreadsNumber()
- {
- return updaterThreadsNumber;
- }
+ public int getUpdaterInterval() {
+ return updaterInterval;
+ }
- public void setUpdaterThreadsNumber(int updaterThreadsNumber)
- {
- this.updaterThreadsNumber = updaterThreadsNumber;
- }
+ public void setUpdaterInterval(int updaterInterval) {
+ this.updaterInterval = updaterInterval;
+ }
- public int getUpdaterInterval()
- {
- return updaterInterval;
- }
+ public int getActivityQueueLimit() {
+ return activityQueueLimit;
+ }
- public void setUpdaterInterval(int updaterInterval)
- {
- this.updaterInterval = updaterInterval;
- }
+ public void setActivityQueueLimit(int activityQueueLimit) {
+ this.activityQueueLimit = activityQueueLimit;
+ }
- public int getActivityQueueLimit()
- {
- return activityQueueLimit;
- }
+ public long getActivityTimeout() {
+ return activityTimeout;
+ }
- public void setActivityQueueLimit(int activityQueueLimit)
- {
- this.activityQueueLimit = activityQueueLimit;
- }
+ public void setActivityTimeout(long activityTimeout) {
+ this.activityTimeout = activityTimeout;
+ }
- public long getActivityTimeout()
- {
- return activityTimeout;
- }
+ public String getActivityBroadcasterName() {
+ return activityBroadcasterName;
+ }
- public void setActivityTimeout(long activityTimeout)
- {
- this.activityTimeout = activityTimeout;
- }
+ public void setActivityBroadcasterName(String activityBroadcasterName) {
+ this.activityBroadcasterName = activityBroadcasterName;
+ }
- public String getActivityBroadcasterName()
- {
- return activityBroadcasterName;
- }
+ private class Updater implements Runnable {
+ private final Queue activityQueue;
- public void setActivityBroadcasterName(String activityBroadcasterName)
- {
- this.activityBroadcasterName = activityBroadcasterName;
- }
+ public Updater(Queue activityQueue) {
+ this.activityQueue = activityQueue;
+ }
- private class Updater implements Runnable
- {
- private final Queue activityQueue;
+ // never run
+ private Updater() {
+ this.activityQueue = null;
+ }
- public Updater(Queue activityQueue)
- {
- this.activityQueue = activityQueue;
- }
+ public void run() {
+ long currentTime = System.currentTimeMillis();
- //never run
- private Updater()
- {
- this.activityQueue = null;
- }
+ Set stillActive = getUsersActivities(activityTimeout);
- public void run()
- {
- long currentTime = System.currentTimeMillis();
+ while (!activityQueue.isEmpty()) {
+ UserActivity activity = (UserActivity) activityQueue.poll();
+ if (activity != null
+ && ((currentTime - activity.getTimestamp()) < activityTimeout)) {
+ if (activity.getType() != UserActivity.EXIT) {
+ stillActive.add(activity);
+ } else {
+ stillActive.remove(activity);
+ }
+ }
+ }
- Set stillActive = getUsersActivities(activityTimeout);
+ activityResults = Collections.unmodifiableSet(stillActive);
- while (!activityQueue.isEmpty())
- {
- UserActivity activity = (UserActivity)activityQueue.poll();
- if (activity != null && ((currentTime - activity.getTimestamp()) <
activityTimeout))
- {
- if (activity.getType() != UserActivity.SIGN_OUT)
- {
- stillActive.add(activity);
- }
- else
- {
- stillActive.remove(activity);
- }
- }
- }
+ }
+ }
- activityResults = Collections.unmodifiableSet(stillActive);
-
- }
- }
-
-
}
-