001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ExchangeEntry.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.proxy;
030:
031: import com.sun.jbi.binding.proxy.connection.ClientConnection;
032:
033: import com.sun.jbi.messaging.MessageExchange;
034:
035: import java.net.URI;
036:
037: import java.util.Date;
038:
039: import java.text.SimpleDateFormat;
040:
041: import javax.jbi.messaging.ExchangeStatus;
042:
043: import javax.xml.namespace.QName;
044:
045: /**
046: * Records information about MessageExchanges that are in progress.
047: * @author Sun Microsystems, Inc
048: */
049: public class ExchangeEntry {
050: private String mId;
051: private MessageExchange mME;
052: private MessageExchange mRelatedME;
053: private ClientConnection mCC;
054: private QName mService;
055: private String mEndpoint;
056: private QName mOperation;
057: private String mConnection;
058: private URI mPattern;
059: private long mTime;
060: private boolean mConsumer;
061: private long mBytesSent;
062: private long mBytesReceived;
063: private int mMessagesSent;
064: private int mMessagesReceived;
065: private boolean mFaulted;
066: private ExchangeStatus mMEStatus;
067: private int mState;
068: public final static int STATE_FIRST = 1;
069: public final static int STATE_ONGOING = 2;
070: public final static int STATE_LAST = 3;
071: private int mStatus;
072: public final static int STATUS_FIRSTSENT = 1;
073: public final static int STATUS_INSENT = 2;
074: public final static int STATUS_OUTSENT = 4;
075: public final static int STATUS_FAULTSENT = 8;
076: public final static int STATUS_ERRORSENT = 16;
077: public final static int STATUS_STATUSSENT = 32;
078:
079: public final static int FIELD_SERVICE = 1;
080: public final static int FIELD_ENDPOINT = 2;
081: public final static int FIELD_SERVICE_AND_ENDPOINT = 3;
082: public final static int FIELD_OPERATION = 4;
083: public final static int FIELD_CONNECTION = 5;
084: public final static int FIELD_PATTERN = 6;
085: public final static int FIELD_STATUS = 7;
086: public final static int FIELD_BYTESSENT = 8;
087: public final static int FIELD_BYTESRECEIVED = 9;
088: public final static int FIELD_MESSAGESSENT = 10;
089: public final static int FIELD_MESSAGESRECEIVED = 11;
090: public final static int FIELD_DURATION = 12;
091:
092: /** Creates a new instance of ExchangeEntry */
093: ExchangeEntry(String id, MessageExchange me, boolean consumer) {
094: mId = id;
095: mME = me;
096: mState = STATE_FIRST;
097: mStatus = 0;
098: mTime = System.currentTimeMillis();
099: mOperation = me.getOperation();
100: mService = me.getEndpoint().getServiceName();
101: mEndpoint = me.getEndpoint().getEndpointName();
102: mPattern = me.getPattern();
103: mConsumer = consumer;
104: }
105:
106: public MessageExchange getMessageExchange() {
107: return (mME);
108: }
109:
110: public void setClientConnection(ClientConnection cc) {
111: mCC = cc;
112: mConnection = mCC.getInstanceId();
113: }
114:
115: ClientConnection getClientConnection() {
116: return (mCC);
117: }
118:
119: public void sendBytes(int bytes) {
120: mBytesSent += bytes;
121: mMessagesSent++;
122: }
123:
124: public void receivedBytes(int bytes) {
125: mBytesReceived += bytes;
126: mMessagesReceived++;
127: }
128:
129: public long getBytesSent() {
130: return (mBytesSent);
131: }
132:
133: public long getBytesReceived() {
134: return (mBytesReceived);
135: }
136:
137: public int getMessagesSent() {
138: return (mMessagesSent);
139: }
140:
141: public int getMessagesReceived() {
142: return (mMessagesReceived);
143: }
144:
145: public MessageExchange getRelatedExchange() {
146: return (mRelatedME);
147: }
148:
149: void setRelatedExchange(MessageExchange me) {
150: mRelatedME = me;
151: }
152:
153: void updateExchange(MessageExchange me) {
154: mME = me;
155: }
156:
157: QName getService() {
158: return (mService);
159: }
160:
161: QName getOperation() {
162: return (mOperation);
163: }
164:
165: long getTime() {
166: return (mTime);
167: }
168:
169: boolean isConsumer() {
170: return (mConsumer);
171: }
172:
173: boolean isFaulted() {
174: boolean fault = mFaulted;
175:
176: if (mME != null) {
177: fault = mME.getFault() != null;
178: }
179: return (fault);
180: }
181:
182: boolean isError() {
183: ExchangeStatus status = mMEStatus;
184:
185: if (mME != null) {
186: status = mME.getStatus();
187: }
188: return (status == ExchangeStatus.ERROR);
189: }
190:
191: boolean isDone() {
192: ExchangeStatus status = mMEStatus;
193:
194: if (mME != null) {
195: status = mME.getStatus();
196: }
197: return (status == ExchangeStatus.DONE);
198: }
199:
200: public void setState(int state) {
201: if ((mState = state) == STATE_LAST) {
202: mTime = System.currentTimeMillis() - mTime;
203: mFaulted = mME.getFault() != null;
204: mMEStatus = mME.getStatus();
205: mME = null;
206: mCC = null;
207: }
208: }
209:
210: public void setStatus(int status) {
211: mStatus |= status;
212: }
213:
214: public void setService(QName service) {
215: mService = service;
216: }
217:
218: public boolean checkStatus(int status) {
219: return ((mStatus & status) != 0);
220: }
221:
222: public boolean checkState(int state) {
223: return (mState == state);
224: }
225:
226: public Object getField(int name) {
227: switch (name) {
228: case FIELD_SERVICE:
229: return (mService.toString());
230: case FIELD_ENDPOINT:
231: return (mEndpoint);
232: case FIELD_SERVICE_AND_ENDPOINT:
233: return (mService.toString() + mEndpoint);
234: case FIELD_OPERATION:
235: return (mOperation.toString());
236: case FIELD_CONNECTION:
237: return (mConnection);
238: case FIELD_PATTERN:
239: return (mPattern);
240: case FIELD_STATUS:
241: return (mMEStatus.toString());
242: case FIELD_BYTESSENT:
243: return (new Long(mBytesSent));
244: case FIELD_BYTESRECEIVED:
245: return (new Long(mBytesReceived));
246: case FIELD_MESSAGESSENT:
247: return (new Long(mMessagesSent));
248: case FIELD_MESSAGESRECEIVED:
249: return (new Long(mMessagesReceived));
250: case FIELD_DURATION:
251: return (new Long(mTime));
252: }
253: return (null);
254: }
255:
256: public String getStringField(int name) {
257: switch (name) {
258: case FIELD_SERVICE:
259: return (mService.toString());
260: case FIELD_ENDPOINT:
261: return (mEndpoint);
262: case FIELD_SERVICE_AND_ENDPOINT:
263: return (mService.toString() + mEndpoint);
264: case FIELD_OPERATION:
265: return (mOperation.toString());
266: case FIELD_CONNECTION:
267: return (mConnection);
268: case FIELD_PATTERN:
269: return (mPattern.toString());
270: case FIELD_STATUS:
271: return (mMEStatus.toString());
272: }
273:
274: return (Integer.toString(name));
275: }
276:
277: public long getLongField(int name) {
278: switch (name) {
279: case FIELD_BYTESSENT:
280: return (mBytesSent);
281: case FIELD_BYTESRECEIVED:
282: return (mBytesReceived);
283: case FIELD_MESSAGESSENT:
284: return (mMessagesSent);
285: case FIELD_MESSAGESRECEIVED:
286: return (mMessagesReceived);
287: case FIELD_DURATION:
288: return (mTime);
289: }
290: return (0);
291: }
292:
293: public static String fieldToString(int name) {
294: switch (name) {
295: case FIELD_SERVICE:
296: return ("Service");
297: case FIELD_ENDPOINT:
298: return ("Endpoint");
299: case FIELD_SERVICE_AND_ENDPOINT:
300: return ("Service&Endpoint");
301: case FIELD_OPERATION:
302: return ("Operation");
303: case FIELD_CONNECTION:
304: return ("Connection");
305: case FIELD_PATTERN:
306: return ("Pattern");
307: case FIELD_STATUS:
308: return ("Status");
309: case FIELD_BYTESSENT:
310: return ("BytesSent");
311: case FIELD_BYTESRECEIVED:
312: return ("BytesReceived");
313: case FIELD_MESSAGESSENT:
314: return ("Messages_ent");
315: case FIELD_MESSAGESRECEIVED:
316: return ("MessagesReceived");
317: case FIELD_DURATION:
318: return ("Duration");
319: }
320: return (Integer.toString(name));
321: }
322:
323: public String toString() {
324: StringBuilder sb = new StringBuilder(200);
325:
326: sb.append(" MessageExchange Id(");
327: sb.append(mId);
328: sb.append(")\n Pattern (");
329: sb.append(mPattern.toString());
330: sb.append(")\n Service (");
331: sb.append(mService.toString());
332: sb.append(")\n Endpoint (");
333: sb.append(mEndpoint);
334: sb.append(")\n Operation (");
335: sb.append(mOperation.toString());
336: if (mRelatedME != null) {
337: sb.append(")\n RelatedId (");
338: sb.append(mRelatedME.getExchangeId());
339: }
340: sb.append(")\n Status (");
341: sb.append(isDone() ? "DONE" : (isError() ? "ERROR" : "ACTIVE"));
342: sb.append(") Role(");
343: sb.append(mConsumer ? "Consumer" : "Provider");
344: sb.append(") Faulted(");
345: sb.append(isFaulted());
346: sb.append(")\n Messages (");
347: sb.append(mMessagesSent + mMessagesReceived);
348: sb.append(") Bytes Sent(");
349: sb.append(mBytesSent);
350: sb.append(") Received(");
351: sb.append(mBytesReceived);
352: sb.append(")\n State (");
353: sb.append(mState == STATE_LAST ? "LAST"
354: : (mState == STATE_FIRST ? "FIRST" : "ONGOING"));
355: if (mState != STATE_LAST) {
356: sb.append(") StartTime (");
357: sb
358: .append(new SimpleDateFormat(
359: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
360: .format(new Date(mTime)));
361: } else {
362: sb.append(") LifeTimeMs (");
363: sb.append(mTime);
364: }
365: sb.append(")\n Connection (");
366: sb.append(mConnection);
367: sb.append(")\n");
368: return (sb.toString());
369: }
370:
371: }
|