001: package com.sun.portal.rproxy.monitoring;
002:
003: import com.sun.portal.monitoring.Subsystem;
004: import com.sun.portal.monitoring.statistics.OpenStatistic;
005: import com.sun.portal.rproxy.monitoring.statistics.ThreadActivityStatisticImpl;
006: import com.sun.portal.rproxy.monitoring.statistics.ThreadActivityStatisticWrapper;
007: import com.sun.portal.rproxy.monitoring.util.RProxyEvent;
008: import com.sun.portal.util.SRAEvent;
009: import com.sun.portal.util.SRAEventListener;
010: import com.sun.portal.util.ThreadMonitorContext;
011:
012: import javax.management.MalformedObjectNameException;
013: import javax.management.ObjectName;
014:
015: import javax.management.openmbean.OpenDataException;
016: import javax.management.openmbean.CompositeType;
017:
018: /**
019: * author: Noble Paul
020: * Date: Feb 15, 2005, 11:05:25 AM
021: */
022: public class ThreadActivityStatistic implements SRAEventListener {
023: Subsystem subsystem;
024: private static Object OPEN_STAT_KEY = new Object();
025:
026: public ThreadActivityStatistic(Subsystem subSystem) {
027: this .subsystem = subSystem;
028: }
029:
030: public void taskStarts(ThreadMonitorContext threadContext) {
031: OpenStatistic openStat = (OpenStatistic) ThreadMonitorContext
032: .retrieveAttribute(OPEN_STAT_KEY);
033: if (openStat == null) {
034: ThreadActivityStatisticWrapper tasw = new ThreadActivityStatisticWrapper();
035: //tasw.setCompositeTypeName(getClass().getName()+ threadContext.getThread().getName());
036:
037: ThreadActivityStatisticImpl tasi = new ThreadActivityStatisticImpl(
038: threadContext);
039: tasi.setObjectName(getObjectName(threadContext.getThread()
040: .getName()));
041: tasw.setStatisticImpl(tasi);
042: tasw.setCompositeTypeName(getClass().getName());
043: tasw.setResourceBundleBaseName("ThreadActivityStatistic");
044: try {
045: CompositeType compositeType = tasw.getCompositeType();
046: tasw.getStatisticImpl().setDescription(
047: compositeType.getDescription("Description"));
048: tasw.getStatisticImpl().setName(
049: compositeType.getDescription("Name"));
050: tasw.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:
056: openStat = new OpenStatistic(tasw);
057: ThreadMonitorContext
058: .storeAttribute(OPEN_STAT_KEY, openStat);
059: }
060: registerMBean(openStat);
061: }
062:
063: private ObjectName getObjectName(String threadName) {
064: try {
065: return new ObjectName(subsystem.getNamingDomain()
066: + ":type=ThreadActivity,ThreadName=" + threadName);
067: } catch (MalformedObjectNameException e) {
068: return null;
069: }
070: }
071:
072: private void registerMBean(OpenStatistic openStat) {
073: ThreadActivityStatisticImpl tasi = (ThreadActivityStatisticImpl) openStat
074: .getStatisticWrapper().getStatisticImpl();
075: tasi.setDestination(null);
076: try {
077: subsystem.registerMBean(openStat, tasi.getObjectName()
078: .toString());
079: } catch (Exception e) {
080: e.printStackTrace();//TODO handle Exception properly
081: }
082: }
083:
084: public void taskEnds(ThreadMonitorContext threadMonitorContext) {
085: OpenStatistic openStat = (OpenStatistic) ThreadMonitorContext
086: .retrieveAttribute(OPEN_STAT_KEY);
087: if (openStat != null) {
088: try {
089: ThreadActivityStatisticImpl tasi = (ThreadActivityStatisticImpl) openStat
090: .getStatisticWrapper().getStatisticImpl();
091: subsystem.unregisterMBean(tasi.getObjectName()
092: .toString());
093: } catch (Exception e) {
094: e.printStackTrace();//TODO handle Exception properly
095: }
096: }
097: }
098:
099: public SRAEvent[] getInterestedEvents() {
100: return new SRAEvent[] { SRAEvent.END_TASK, SRAEvent.START_TASK,
101: RProxyEvent.IDENTIFY_DESTINATION };
102: }
103:
104: public void handleEvent(SRAEvent event, Object obj) {
105: if (event == SRAEvent.START_TASK) {
106: taskStarts((ThreadMonitorContext) obj);
107: return;
108: }
109: if (event == SRAEvent.END_TASK) {
110: taskEnds((ThreadMonitorContext) obj);
111: return;
112: }
113: if (event == RProxyEvent.IDENTIFY_DESTINATION) {
114: OpenStatistic openStat = (OpenStatistic) ThreadMonitorContext
115: .retrieveAttribute(OPEN_STAT_KEY);
116: if (openStat == null)
117: return;
118: ThreadActivityStatisticImpl tasi = (ThreadActivityStatisticImpl) openStat
119: .getStatisticWrapper().getStatisticImpl();
120: tasi.setDestination((String) obj);
121: return;
122: }
123:
124: }
125: }
|