1
2
3
4
5 package net.sf.wfnm;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.ObjectOutputStream;
13
14 import java.util.Enumeration;
15
16
17 /***
18 * The statistics factory.<br> It allows to print out session size statistics.
19 *
20 * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
21 * @version 1.0.6
22 */
23 public class StatsFactory {
24 /***
25 * The statistics log category.
26 */
27 private static final String STATS_CATEGORY = "stats";
28
29 /***
30 * The statistics log.
31 */
32 private static Log statsLog = LogFactory.getLog(STATS_CATEGORY);
33
34 /***
35 * The statistics log.
36 */
37 private static Log log = LogFactory.getLog(StatsFactory.class);
38
39 /***
40 * The unique instance of this factory
41 */
42 private static StatsFactory instance = new StatsFactory();
43
44 /***
45 * Indicate if the header has been logged.
46 */
47 private boolean header = false;
48
49 /***
50 * Gets the unique instance of this Stats Factory.
51 *
52 * @return the StatsFactory unique instance
53 */
54 public static StatsFactory getInstance() {
55 return instance;
56 }
57
58 /***
59 * Log the statistics of a specific http session.
60 *
61 * @param container the attribute container
62 */
63 public void logStatistic(AttributeContainer container) {
64 if (!header) {
65 statsLog.info(",session id,wfnm active,page url,session size");
66 header = true;
67 }
68
69 String page = "?";
70
71 boolean wfnmActive = NavigationManagerFactory.existsInstance(container);
72
73 if (wfnmActive) {
74 NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
75 page = navigationManager.getCurrentPage();
76 }
77
78 int sessionSize = 0;
79
80 for (Enumeration e = container.getAttributeNames(); e.hasMoreElements();) {
81 String attName = (String) e.nextElement();
82
83 Object obj = container.getAttribute(attName);
84 sessionSize = sessionSize + getObjectSize(attName, obj);
85 }
86
87 statsLog.info("," + container.getId() + "," + wfnmActive + "," + page + "," + sessionSize);
88 }
89
90 /***
91 * Gets the size of a serialized object.
92 *
93 * @param objName the object name
94 * @param obj the object
95 *
96 * @return the size of the serialized object
97 */
98 private int getObjectSize(String objName, Object obj) {
99 int size = 0;
100 ByteArrayOutputStream baos = new ByteArrayOutputStream();
101
102 try {
103 ObjectOutputStream oos = new ObjectOutputStream(baos);
104 oos.writeObject(obj);
105 size = baos.size();
106 } catch (IOException ioe) {
107 log.warn("Unable to calculate the size of object with name " + objName);
108 }
109
110 return size;
111 }
112 }