// MessBean.java 6.2.01 v45 15:17
// Funktion: Messpunkte von Anwendungen werden aufgenommen und
// in periodischen Abstaenden in ein Logfile geschrieben
// Autor: Erika Kalix
// Projekt: Fopra: Implementierung der Performance-Ueberwachung
// Javabean-basierter Anwendungen
// Version 1.0 fuer BThread, StartButton v1.0
// Datum: 06.02.2001
// Uebersetzen: javac MessBean.java
// Als Javabean muss MessBean.java in eine jar-datei verpackt werden:
// jar cfm messbean.jar manifest.ta4 MessBean.class
// Datei manifest.ta4 ist nur fuer Windows95 noetig, wird in Linux mit
// Makefile automatisch erzeugt.
// cp messbean.jar /BDK1.1/jars
// -----------------------------------------------------------------------
import java.util.*;
import java.io.*;
import java.awt.*;
import java.beans.*;
import java.io.Serializable;
import java.lang.Thread;
public class MessBean extends Component implements Runnable {
static String mT [] = new String [40];
static int nt; // Index der TA-Tabelle
static int ni; // Index der BTA-Tabelle
static int nAnf; // aktueller Anfangsindex fuer Schreiben in Logfile
static int nEnd; // aktueller Endeindex fuer Schreiben in Logfile
// TA-Tabelle fuer Messpunkte:
static long tifld[] = new long [100];
static String rifld[] = new String [100];
static String isfld[] = new String [100];
static String scfld[] = new String [100];
static String tgfld[] = new String [100];
static String okfld[] = new String [100];
// BTA-Tabelle fuer BTA-ID und zugehoerige Threads:
static String bta_tab [][] = new String [100][2];
static String BTA_instanz = new String();
static String mainTh = new String();
java.io.FileWriter flog; // Schreiben in logfile
java.io.PrintWriter log; // Text an Logfile anhaengen
private String beanName ;
private int baseline;
private String ourLabel = " MessBean ";
private transient Thread tm; // eigener Thread
// -----------------------------------------------------------------------
// Konstruktor:
// Parameter : keine fuer Beanbox
// -----------------------------------------------------------------------
public MessBean() {
String tstr = toString();
int endpos = tstr.indexOf('[');
beanName = tstr.substring(0,endpos);
}
// -----------------------------------------------------------------------
// run: ewige Schleife: Aufruf von doLog zum Schreiben ins Logfile
// legt sich schlafen und wird periodisch geweckt, um neue Daten
// auszugeben
// -----------------------------------------------------------------------
public void run(){
while (true){
try{
Thread.sleep(350); // schlafen, Wecker setzen
} catch (InterruptedException ei){ // Weckevent abfangen
}
nEnd = nt-1;
doLog(nAnf, nEnd); // neue Eintraege ins Logfile schreiben
nAnf = nEnd+1; // Anfang neu setzen
try{
Thread.sleep(1000);
} catch (InterruptedException ei){
}
} //While
}
// ------------------------------------------------------------------------
// BTA_start: Eintrag der BTA-Instanz BTA_ID, MainThread in BTA-Tabelle,
// ersten Messpunkt speichern
// Parameter: BTA_name: Name der BTA
// BName: Name der meldenden Javabean
// id: Thread-Id
// -----------------------------------------------------------------------
public static void BTA_start(String BTA_name, String BName, String id){
Thread th = Thread.currentThread(); // get Mainthread
Date myDate = new Date(); // aktuelle Zeit/Datum
String chtim = Long.toString(myDate.getTime());
String tid = new String("t"+chtim);
th.setName(tid); // eindeutiger Threadname
BTA_instanz = new String(BTA_name+tid);// eindeutige BTA-ID
bta_tab[ni][0] = BTA_instanz; // Eintrag BTA_ID
bta_tab[ni][1] = tid; // Eintrag mainthreadname
if (ni < 100) ni++;
if (ni == 100) System.out.println("Ueberlauf BTA-Tabelle!\n");
TA_start(tid, BName);
MessBean log = new MessBean();
Thread tm = new Thread(log);
tm.setPriority(Thread.MIN_PRIORITY);
tm.start(); // start eigenen Thread zur Logfile-Ausgabe
}
// ------------------------------------------------------------------
// BTA_end: BTA-Ende (Ergebnisausgabe) kennzeichnen:
// Messpunkt in TA-Tabelle schreiben
// Parameter: Bname: Name der meldenden Javabean
// mainTh: Name des MainThreads
// success: Erfolg, (false, falls Fehler entdeckt)
// -----------------------------------------------------------------------
public static void BTA_end(String Bname, String mainTh, boolean success){
Date ptDate = new Date();
long ptTime = ptDate.getTime();
int ptdut = 10000;
// find BTA-Instanz:
for (int i=0; i<ni;i++){
if (mainTh.compareTo(bta_tab[i][1]) == 0)
ptdut = i;
}
if (ptdut < 10000){ // BTA-Instanz: gefunden
isfld[nt] = bta_tab[ptdut][0];
}
tifld[nt] = ptTime;
rifld[nt] = "BTA_end";
tgfld[nt] = Bname;
if (success == true) okfld[nt] = "erfolgreich";
else okfld[nt] = "nicht erfolgreich";
if (nt < 100) nt++;
else
System.out.println("BTA_end : Ueberlauf TA-Tabelle\n");
}
// ----------------------------------------------------------------------
// TA_start: Messpunkt vor Eintritt des Kontrollflusses in eine TargetBean
// setzen
// Parameter: th : Name des Kontrollflusses
// t : Name der TargetBean
// -----------------------------------------------------------------------
public static void TA_start(String th, String t){
Date inDate = new Date();
long inTime = inDate.getTime();
int index = 10000;
// find BTA-Instanz:
for (int i=0; i<ni;i++){
if (th.compareTo(bta_tab[i][1]) == 0)
index = i; // gefunden
}
if (index < 10000){ // gefunden
isfld[nt] = bta_tab[index][0];}
tifld[nt] = inTime;
if (nt > 0)
rifld[nt] = "TA_start";
else
rifld[nt] = "BTA_start"; // 1. Eintrag
tgfld[nt] = t;
okfld[nt] = "-"; // success noch nicht bekannt
if (nt < 100) nt++;
else
System.out.println("TA_start : Ueberlauf TA-Tabelle\n");
}
// -----------------------------------------------------------------
// TA_stop: Messpunkt nach Austritt des Kontrollflusses in eine TargetBean
// eintragen
// Parameter: th : Name des Kontrollflusses
// t : Name der TargetBean
// success: Erfolg, (false, falls Fehler entdeckt)
// -----------------------------------------------------------------------
public static void TA_stop(String th, String t, boolean success){
Date utDate = new Date();
long utTime = utDate.getTime();
int indut = 10000;
// find BTA-Instanz:
for (int i=0; i<ni;i++){
if (th.compareTo(bta_tab[i][1]) == 0)
indut = i;
}
if (indut < 10000){ // BTA-Instanz: gefunden
isfld[nt] = bta_tab[indut][0];}
tifld[nt] = utTime;
rifld[nt] = "TA_stop";
tgfld[nt] = t;
if (success == true) okfld[nt] = "erfolgreich";
else okfld[nt] = "nicht erfolgreich";
if (nt < 100) nt++;
else
System.out.println("TA_stop : Ueberlauf TA-Tabelle\n");
}
// -------------------------------------------------------------------
// add_control_flow: Neuen Thread bei Start eines SubThreads eintragen:
// Messpunkt setzen
// Parameter: mainth : Name des Hauptkontrollflusses
// netTh : Name des neuen Kontrollflusses
// last update: 6.2.01 Version fuer BThread, StartButton v1.0
// -----------------------------------------------------------------------
public static void add_control_flow(String mainth, String newTh){
// mainth kann fuer jdk aus Schnittstelle entfernt werden, Aenderung in
// BThread noetig
int m=0;
boolean found = false;
Thread th = Thread.currentThread(); // HauptThread ermitteln
String mainTh = th.getName();
for (int j=0;j < ni; j++){ // find BTA-ID
if (mainTh.compareTo(bta_tab[j][1]) == 0) {
m = j;
found = true;
}
}
if (found){ // Eintrag in BTA-Tabelle:
bta_tab[ni][0] = bta_tab[m][0];
bta_tab[ni][1] = newTh;
if(ni < 100) ni++;
else
System.out.println("add_control_flow : Ueberlauf TA-Tabelle\n");
}
TA_start(newTh, "dummy"); // Messpunkt in TA-Tabelle eintragen
}
// ------------------------------------------------------------------
// rem_control_flow: Messpunkt bei Ende eines SubThreads setzen
// SubThread aus BTA-Tabelle austragen
// Parameter: mainth : Name des Hauptkontrollflusses
// netTh : Name des neuen Kontrollflusses
// success: Erfolg, (false, falls Fehler entdeckt)
// last update: 6.2.01 Version fuer BThread, StartButton v1.0
// -----------------------------------------------------------------------
public static void rem_control_flow(String mainth, String newTh,
boolean success){
// mainth kann fuer jdk aus Schnittstelle entfernt werden, Aenderung in
// BThread und StartButton noetig
Thread th = Thread.currentThread(); // HauptThread ermitteln
String mainTh = th.getName();
if (mainTh.compareTo(newTh) != 0) // nicht MainThread
TA_stop(newTh, "dummy", success); // Subthread
else
TA_stop(newTh, tgfld[0], success); // Startbutton 1.Eintrag
int m=0;
boolean found = false; // find BTA-ID zu MainThread
for (int j=0; j<ni; j++){
if (mainTh.compareTo(bta_tab[j][1]) == 0) {
m = j;
found = true;
}
}
if (found){
String BTA_id = bta_tab[m][0];
found = false;
for (int j=0;j<ni;j++){ // BTA_ID zu Subthread suchen
if ((BTA_id.compareTo(bta_tab[j][0]) == 0) &&
(newTh.compareTo(bta_tab[j][1]) == 0)) {
m = j;
found = true;
}
}
}
if (found){ // Subthread aus BTA-Tabelle austragen
bta_tab[m][0] = "";
bta_tab[m][1] = "";
}
}
// ------------------------------------------------------------------
// ATA_start: Messpunkt bei Start eines AktivBean-Auftrags setzen:
// Parameter: BTAid : BTA-Id der BTA, die Auftrag an Aktive Bean gab
// newTh : Name des neuen Kontrollflusses
// -----------------------------------------------------------------------
public static void ATA_start(String BTAid, String newTh){
bta_tab[ni][0] = BTAid; // BTA_ID eintragen in BTA-Tabelle
bta_tab[ni][1] = newTh; // neuen Thread eintragen in BTA-Tabelle
if(ni < 100) ni++;
else
System.out.println("ATA_start : Ueberlauf BTA-Tabelle\n");
Date inDate = new Date();
long inTime = inDate.getTime();
isfld[nt] = BTAid;
tifld[nt] = inTime;
rifld[nt] = "ATA_start";
tgfld[nt] = "dummy";
okfld[nt] = "-";
if (nt < 100) nt++;
else
System.out.println("ATA_start : Ueberlauf TA-Tabelle\n");
}
// -----------------------------------------------------------------------
// ATA_stop: Messpunkt bei Ende des von der aktiven Bean ausgefuehrten
// Auftrags setzen
// Parameter: BTA_ID : BTA-Id der BTA, die Auftrag an Aktive Bean gab
// newTh : Name des neuen Kontrollflusses
// success: Erfolg, (false, falls Fehler entdeckt)
// -----------------------------------------------------------------------
public static void ATA_stop(String BTA_ID, String newTh,
boolean success){
Date utDate = new Date(); // Zeitpunkt ermitteln
long utTime = utDate.getTime();
isfld[nt] = BTA_ID; // Eintag in Ta-Tabelle
tifld[nt] = utTime;
rifld[nt] = "ATA_stop";
tgfld[nt] = "dummy";
okfld[nt] = "-";
if (nt < 100) nt++;
else
System.out.println("ATA_stop : Ueberlauf TA-Tabelle\n");
// BTA_ID aus bta-tab austragen:
int m=0;
boolean found = false; // find BTA-ID in bta_tab
for (int j=0; j<ni; j++){
if ((BTA_ID.compareTo(bta_tab[j][0]) == 0) &&
(newTh.compareTo(bta_tab[j][1]) == 0)){
m = j;
found = true;
}
}
if (found){
bta_tab[m][0] = ""; // BTA_ID austragen
bta_tab[m][1] = ""; // zugehoerigen ATA-Thread austragen
}
}
// -----------------------------------------------------------------------
// logInfo: Annahme eines Info-Strings, Schreiben in TA-Tabelle
// Parameter: info: String mit Informationen
// target: Name der meldenden Javabean
// -----------------------------------------------------------------------
public static void logInfo(String info, String target){
// get Mainthread, Date, :
Thread th = Thread.currentThread();
String thName = th.getName();
int indut = 10000;
Date imDate = new Date();
long imTime = imDate.getTime();
// find BTA-Instanz
for (int i=0; i<ni;i++){
if (thName.compareTo(bta_tab[i][1]) == 0)
indut = i;
}
if (indut < 10000) // MainThread in BTA-Tabelle gefunden
{
isfld[nt] = bta_tab[indut][0]; // Eintragen in TA-Tabelle
tifld[nt] = imTime;
rifld[nt] = "Info: "+info;
tgfld[nt] = target;
okfld[nt] = "-";
if (nt < 100) nt++;
else
System.out.println("logInfo : Ueberlauf TA-Tabelle\n");
}
}
// ------------------------------------------------------------------
// getBTA_ID: Zurueckgeben der BTA-Instanz an Aktive Bean
// Parameter: keine
// returns : Instanzname der BTA
// -----------------------------------------------------------------------
public static String getBTA_ID(){
// find Mainthread
Thread th = Thread.currentThread();
String thName = th.getName();
int indut = 10000;
// find BTA-Instanz:
for (int i=0; i<ni;i++){
if (thName.compareTo(bta_tab[i][1]) == 0)
indut = i;
}
if (indut < 10000) // Thread gefunden
return bta_tab[indut][0];
else return "ERROR";
}
// ------------------------------------------------------------------
// doLog: Schreiben der Messdaten in ein Logfile
// Diese Methode wird zyklisch in der run-Methode aufgerufen
// Parameter: j: aktueller Anfangsindex
// m: aktueller Endeindex
// -----------------------------------------------------------------------
private void doLog(int j, int m){
if (m>j){ // EndeIndex > Anfangsindex
if (log == null) { // noch kein Ausgabefile-ID
try {
java.io.FileWriter flog = new java.io.FileWriter("kxbea",true);
log = new java.io.PrintWriter(flog, true); // Text anhaengen
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}//try
} //if
for(int i = j; i <= m; i++){ // in logfile schreiben
log.println(isfld[i]);
log.println(rifld[i]);
log.println(tifld[i]);
log.println(okfld[i]);
log.println(tgfld[i]+"\n");
}
}// if m>j
} //doLog
} //class