001: package com.sun.portal.rproxy.monitoring;
002:
003: import com.sun.portal.monitoring.Subsystem;
004: import com.sun.portal.monitoring.statistics.OpenStatisticMBeanRegistration;
005: import com.sun.portal.monitoring.statistics.OpenStatistic;
006: import com.sun.portal.monitoring.utilities.ActivityTime;
007: import com.sun.portal.rproxy.monitoring.statistics.InternalServerStatisticImpl;
008: import com.sun.portal.rproxy.monitoring.statistics.InternalServerStatisticWrapper;
009: import com.sun.portal.rproxy.monitoring.util.RProxyEvent;
010: import com.sun.portal.util.SRAEventListener;
011: import com.sun.portal.util.SRAEvent;
012:
013: import javax.management.MalformedObjectNameException;
014: import javax.management.ObjectName;
015: import java.util.HashMap;
016: import java.util.Map;
017:
018: import javax.management.openmbean.OpenDataException;
019: import javax.management.openmbean.CompositeType;
020:
021: /**
022: * author: Noble Paul
023: * Date: Feb 10, 2005, 11:30:43 AM
024: */
025: public class InternalServerStatistic implements SRAEventListener {
026: private Subsystem subsystem;
027: private Map internalServerStatistics = new HashMap();
028: private static ThreadLocal threadLocal = new ThreadLocal();
029: private static ActivityTime activityTime = new ActivityTime();
030:
031: public InternalServerStatistic(Subsystem subsystem) {
032: this .subsystem = subsystem;
033: }
034:
035: private OpenStatistic getOpenStatistic(String destinationHostPort) {
036: InternalServerStatisticWrapper internalServerStatisticWrapper = new InternalServerStatisticWrapper();
037: internalServerStatisticWrapper.setCompositeTypeName(getClass()
038: .getName());
039: internalServerStatisticWrapper
040: .setResourceBundleBaseName("InternalServerStatistics");
041: try {
042: CompositeType compositeType = internalServerStatisticWrapper
043: .getCompositeType();
044: internalServerStatisticWrapper
045: .getStatisticImpl()
046: .setDescription(
047: compositeType.getDescription("Description"));
048: internalServerStatisticWrapper.getStatisticImpl().setName(
049: compositeType.getDescription("Name"));
050: internalServerStatisticWrapper.getStatisticImpl().setUnit(
051: compositeType.getDescription("Unit"));
052: } catch (OpenDataException e) {
053: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
054: }
055: return new OpenStatistic(internalServerStatisticWrapper);
056: }
057:
058: private ObjectName getObjectName(String destination)
059: throws MalformedObjectNameException {
060: return new ObjectName(subsystem.getNamingDomain()
061: + ":type=InternalServerStatistics,destination="
062: + destination);
063: }
064:
065: public void removeServerStatistic(ObjectName name) {
066: internalServerStatistics.remove(name);
067: }
068:
069: public void markDestination(String hostPort) {
070: InternalServerStatisticImpl statistic = (InternalServerStatisticImpl) internalServerStatistics
071: .get(hostPort);
072: if (statistic == null) {
073: OpenStatistic openStatistic = getOpenStatistic(hostPort);
074: statistic = (InternalServerStatisticImpl) openStatistic
075: .getStatisticWrapper().getStatisticImpl();
076: statistic.setDestination(hostPort);
077: internalServerStatistics.put(hostPort, statistic);
078: try {
079: subsystem.registerMBean(openStatistic, getObjectName(
080: hostPort).toString());
081: } catch (Exception e) {
082: e.printStackTrace();//TODO handle Exception properly
083: }
084: }
085: threadLocal.set(statistic);
086: }
087:
088: public void handleEvent(SRAEvent event, Object obj) {
089: if (event == RProxyEvent.IDENTIFY_DESTINATION) {
090: markDestination((String) obj);
091: return;
092: }
093: InternalServerStatisticImpl statistic = (InternalServerStatisticImpl) threadLocal
094: .get();
095: if (statistic == null)
096: return;
097:
098: if (event == RProxyEvent.RETRIEVAL_ERROR) {
099: statistic.incrementServerError();
100: return;
101: }
102: if (event == RProxyEvent.REQUEST_SENT) {
103: statistic.incrementTotalRequests();
104: activityTime.mark();
105: return;
106: }
107: if (event == RProxyEvent.RESP_READ) {
108: statistic.incrementBytesSent(((Long) (obj)).longValue());
109: return;
110: }
111: if (event == RProxyEvent.BAD_REQUEST) {
112: statistic.incrementBadRequests();
113: return;
114: }
115: if (event == RProxyEvent.RESPONSE_RECVD) {
116: statistic.getResponseTime().setTime(activityTime.measure());
117: statistic.incrementTotalResponses();
118: return;
119: }
120: if (event == RProxyEvent.REQUEST_READ) {
121: statistic
122: .incrementBytesReceived(((Long) (obj)).longValue());
123: return;
124: }
125: }
126:
127: public SRAEvent[] getInterestedEvents() {
128: return new SRAEvent[] { RProxyEvent.RETRIEVAL_ERROR,
129: RProxyEvent.REQUEST_SENT, RProxyEvent.RESP_READ,
130: RProxyEvent.BAD_REQUEST,
131: RProxyEvent.IDENTIFY_DESTINATION,
132: RProxyEvent.RESPONSE_RECVD, RProxyEvent.REQUEST_READ };
133: }
134: }
|