1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program 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
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.statistics.detailed;
18
19 import java.io.Serializable;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22
23
24 /**
25 * A <code>MeasureID</code> uniquely identifies a global measure. <p>
26 *
27 * The ID of a measure is made unique through :
28 * <ul>
29 * <li>the hostname of the machine
30 * <li>the invocation time of the method
31 * </ul>
32 *
33 * Remark : the sequential number associated to each measure is not
34 * part of the measure ID.
35 *
36 * The ID of the measure is reused during the whole lifetime of an
37 * end-to-end measure (global measure with all its sub-measures). The
38 * sequential number will be increased for each sub-measure.
39 *
40 *
41 * This class was ported from Leaf 2.
42 * Original authors: YMA, DBA.
43 * Leaf2 package name: ch.elca.leaf.services.measuring
44 *
45 * @svnLink $Revision: 3880 $;$Date: 2009-08-04 15:17:52 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/detailed_statistics/common/src/main/java/ch/elca/el4j/services/statistics/detailed/MeasureId.java $
46 *
47 * @author Rashid Waraich (RWA)
48 */
49 public class MeasureId implements Serializable {
50
51 /**
52 * The separator used for the string represention.
53 */
54 private static final String FORMATTED_SEP = "::";
55
56
57 /**
58 * The prefixCounter is a module MAXSHORT counter. It is required,
59 * in order to ensure that no two requests have the same id.
60 * This was not ensured till now, because measureId generated within
61 * the same milli-second on the same host are not distinguishable!
62 *
63 * It is assumed, that the host, where this program is running can not
64 * perform more than MAXINT-1 operations and interceptions in one single
65 * milli-second (which seems reasonable at the moment).
66 *
67 */
68 private static short s_prefixCounter = 0;
69
70
71 /** The host name of the ID. */
72 private String m_host;
73
74
75 /** The invocation time of the ID. */
76 private long m_invocationTime;
77
78
79 /**
80 * The prefix of this MeasureId.
81 */
82 private short m_prefix;
83
84 /**
85 * Creates a MeasureID object for the localhost machine and with the current
86 * time.
87 */
88 public MeasureId() {
89 try {
90 m_host = InetAddress.getLocalHost().getHostName();
91 } catch (UnknownHostException e) {
92 // nothing
93 m_host = null;
94 }
95 s_prefixCounter = (short) ((s_prefixCounter + 1) % Short.MAX_VALUE);
96 m_prefix = s_prefixCounter;
97 m_invocationTime = System.currentTimeMillis();
98 }
99
100 /**
101 * Creates a MeasureID object for a given host and a given invocation time.
102 *
103 * @param host
104 * the host name of the ID
105 * @param invocationTime
106 * the invocation time of the ID
107 */
108 public MeasureId(String host, long invocationTime) {
109 m_host = host;
110 m_invocationTime = invocationTime;
111 }
112
113 /**
114 * Creates a new MeasureID object for the localhost machine and with the
115 * current time.
116 * <p>
117 * Remark : This method should be called only by the client.
118 *
119 * @return new MeasureID object
120 */
121 public static MeasureId createID() {
122 return new MeasureId();
123 }
124
125
126 /**
127 * Returns the host name of the measure ID.
128 *
129 * @return host name
130 */
131 public String getHost() {
132 return m_host;
133 }
134
135 /**
136 * Returns the invocation time of the measure ID.
137 *
138 * @return invocation time
139 */
140 public long getInvocationTime() {
141 return m_invocationTime;
142 }
143
144 /**
145 * Returns a human-readable representation of the measure ID.
146 *
147 * @return measure representation
148 */
149 public String getFormattedString() {
150 return m_host + FORMATTED_SEP + m_prefix + ":" + m_invocationTime;
151 }
152
153 /**
154 * Returns a human-readable representation of the object.
155 *
156 * @return human-readable representation
157 */
158 public String toString() {
159 return getFormattedString();
160 }
161 }