KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > stats > webapp > WebAppStatisticsMessageSender


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package za.org.coefficient.util.stats.webapp;
20
21 import net.sf.hibernate.util.HibernateUtil;
22
23 import za.org.coefficient.core.Constants;
24 import za.org.coefficient.interfaces.StatisticsMessageSender;
25 import za.org.coefficient.util.common.InvokerFactory;
26
27 /**
28  * This is the webapp implementation of the statisticMessageSender. This
29  * implementation does not use JMS, instead it spawns a new thread and invokes
30  * the service directly within that thread.
31  */

32 public class WebAppStatisticsMessageSender implements StatisticsMessageSender {
33     //~ Static fields/initializers =============================================
34

35     //~ Methods ================================================================
36

37     public void reportStatistic(Long JavaDoc projectId, String JavaDoc moduleName, String JavaDoc action) {
38         if ((projectId == null) || (action == null)) {
39             throw new IllegalArgumentException JavaDoc("project and action must not be null to send a message");
40         } else {
41             try {
42                 new StatsThread(projectId, moduleName, action).start();
43             } catch (Exception JavaDoc e) {
44                 System.out.println("WebAppStatisticMessageSender: error invoking stats service - " + e);
45                 e.printStackTrace();
46             }
47         }
48     }
49
50     private class StatsThread extends Thread JavaDoc {
51
52         private Long JavaDoc projectId;
53         private String JavaDoc moduleName;
54         private String JavaDoc action;
55
56         public StatsThread(Long JavaDoc projectId, String JavaDoc moduleName, String JavaDoc action) {
57             this.projectId = projectId;
58             this.action = action;
59             if(moduleName == null) {
60                 this.moduleName = "";
61             } else {
62                 this.moduleName = moduleName;
63             }
64         }
65
66         public void run() {
67             try {
68                 InvokerFactory.getInvoker()
69                     .invokeMethodOnService("StatisticsService", "doStatistics",
70                                        new Object JavaDoc[]{moduleName, projectId, action});
71                 // This is done because when running as webapp you must finalize
72
// the hibernate session for each thread.
73
HibernateUtil.finalizeSession();
74             } catch(Exception JavaDoc e) {
75                 e.printStackTrace();
76                 System.err.println("<< WebAppStatisticsMessageSender - problem with thread");
77             }
78         }
79     }
80 }
81
Popular Tags