01: package org.cougaar.core.qos.profile;
02:
03: import java.lang.reflect.*;
04: import java.io.*;
05: import java.text.*;
06: import java.util.*;
07: import java.util.regex.*;
08: import org.cougaar.core.agent.*;
09: import org.cougaar.core.component.*;
10: import org.cougaar.core.mts.*;
11: import org.cougaar.core.node.*;
12: import org.cougaar.core.qos.metrics.*;
13: import org.cougaar.core.service.*;
14: import org.cougaar.core.service.wp.*;
15: import org.cougaar.core.thread.*;
16: import org.cougaar.core.wp.resolver.*;
17: import org.cougaar.util.*;
18:
19: /**
20: * This component is the common base class for profiler components.
21: */
22: public abstract class ProfilerBase extends GenericStateModelAdapter
23: implements Component, ProfilerService.Client {
24:
25: protected ServiceBroker sb;
26: private ProfilerService ps;
27:
28: protected boolean header;
29: protected boolean align;
30:
31: private LoggingService log;
32:
33: public void setServiceBroker(ServiceBroker sb) {
34: this .sb = sb;
35: }
36:
37: public void load() {
38: super .load();
39:
40: ps = (ProfilerService) sb.getService(this ,
41: ProfilerService.class, null);
42: if (ps == null) {
43: throw new RuntimeException(
44: "Unable to obtain the ProfilerService");
45: }
46: header = ps.logHeader();
47: align = ps.logAlign();
48: }
49:
50: protected int getRunCount() {
51: return ps.getRunCount();
52: }
53:
54: public abstract void run();
55:
56: public static final String toHeader(String[] fields) {
57: StringBuffer buf = new StringBuffer();
58: buf.append("#");
59: for (int i = 0; i < fields.length; i++) {
60: buf.append(fields[i]).append(", ");
61: }
62: return buf.toString();
63: }
64:
65: protected void log(String cat, String h, String value) {
66: if (log == null) {
67: log = (LoggingService) sb.getService(cat,
68: LoggingService.class, null);
69: if (header) {
70: log.shout(h);
71: }
72: if (align) {
73: // ALIGN as current value
74: for (int i = 0, n = getRunCount(); i < n; i++) {
75: log.shout(value);
76: }
77: }
78: }
79: log.shout(value);
80: }
81:
82: protected void findServiceLater(String fname, String cname) {
83: ServiceFinder.findServiceLater(fname, cname, sb, this);
84: }
85: }
|