This sample demonstrates how to create a log viewing tool that uses Swing components to render the user interface. Run the sample by entering the following command line:
java LogViewer
The main method invokes the constructor VWSample3A(), which sets up a dialog box that is the user interface to the log viewing tool. The dialog contains a system menu with "Select system/router", "logon", and "logoff" items, and it contains a log menu with "Select Log", "Create new log index", "Query Log", and "List Exposed Log Fields" items. The program responds to each of these menu selections according to the code in the actionPerformed method. Each program response is described in its own section below.
Selecting this menu choice allows the user to choose the system and router to which to connect. The input must be in the following form:
<server_name>:<port_number>/<router_instance_name>
The above format is identical to that of the router_URL parameter in the API VWSession() constructor or VWSession.logon. For a detailed explanation of the command, see the Run the sample application section of the Run the Unmodified Samples topic.
A Swing component collects the username and password. The program passes this information, along with the router_URL from the router selection (Select system/router, above) to the API VWSession constructor, as follows:
s = new VWSession(uname, pword, routerName);
Invoke the VWSession.logoff method to free system resources as follows:
s.logoff();
Object "s" represents the instance of the session that was created at the time of the logon.
The program creates an array of strings containing the log names with the API VWSession.fetchEventLogNames() method as follows:
String[] elogNames = s.fetchEventLogNames();
The program opens a dialog box that prompts the user to select a log by name, and when a name is selected, it creates an instance of the API VWLog class with that name and notifies the user with the following code:
if (logName != null){
try
{
vw_Log = s.fetchEventLog(logName);
isLogSelected = true;
JOptionPane.showInternalMessageDialog(dpane, "Opened
" + logName);
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showInternalMessageDialog(dpane, "Unable
to open " + logName);
isLogSelected = false;
}
}
Object "s" above represents the instance of the session that was created at the time of the logon.
When the user makes this menu selection, the program first creates a VWLogDefinition object with the VWLog.fetchLogDefinition() method. The VWLogDefinition.getIndexes() method creates an array of VWIndexDefinition objects as follows:
VWLogDefinition vw_LogDef = vw_Log.fetchLogDefinition();
VWIndexDefinition[] vw_Indexes = vw_LogDef.getIndexes();
Using the VWIndexDefinition object array, the program creates a dialog box from which the user may select an index for an index query. Store the select index in a VWIndexDefinition object name vw_Idx:
VWIndexDefinition vw_Idx = vw_Indexes[xix];
The next dialog the program displays asks the use to select a query method cancel. Cancellation results in the program returning from the LogViewer method; otherwise, the user selects either "Basic Query" or "Index-Based Query", and the program creates a log query object. Log query object vwq is initialized as follows:
VWLogQuery vwq = null;
If the user selects Index-based query, the logViewer method creates another dialog box that enables the user to select maximum and minimum values for each component field of the selected index. The first step is to load a string array with a sorted list of index field names, using the VWIndexDefinition.getFieldNames() method as follows:
String[] idxFieldNames = vw_Idx.getFieldNames();
Arrays.sort(idxFieldNames);
The code below creates an array of VWExposedFieldDefinition objects containing the exposed log definition fields, using the VWLogDefinition.getFields() method. The next line initializes an array of VWExposedFieldDefinition objects, fieldDefs, with the same number of elements.
VWExposedFieldDefinition[] vw_EFDef = vw_LogDef.getFields();
VWExposedFieldDefinition[] fieldDefs = new VWExposedFieldDefinition[idxFieldNames.length];
The objects for the exposed field definitions that are part of the selected index are loaded into the empty array fieldDefs[] with the same index as the index field names as follows:
for (int i=0;i<vw_EFDef.length;i++){
int x = Arrays.binarySearch(idxFieldNames, vw_EFDef[i].getName());
if (x >= 0){
fieldDefs[x] = vw_EFDef[i];
}
}
A dialog box displays field definitions and their types with text box pairs for the user to fill in minimum and maximum values. The program copies these minimum and maximum values into two object arrays, one for minimum values and the other for maximum values, respectively. These arrays, min and max, are arguments for the VWLOG.startQuery() method to create a log query object, as shown below:
vwq = vw_Log.startQuery(vw_Idx.getName(), min, max, 0, null, null);
If the Basic Query method is chosen, the program immediately creates the log query object. The min and max object arrays are replaced by NULLs, as follows:
vwq = vw_Log.startQuery(vw_Idx.getName(), null, null, 0, null, null);
The program employs a logElement object to load information from the log query object into ArrayList objects as follows:
VWLogElement le = null; //
Initialize data targets:
ArrayList eventTypes = new ArrayList();
ArrayList timeStamps = new ArrayList();
ArrayList seqNumber = new ArrayList(); // Load data targets:
while (vwq.hasNext()){
le = vwq.next();
eventTypes.add(VWLoggingOptionType.getLocalizedString(le.getEventType()));
timeStamps.add(le.getTimeStamp());
seqNumber.add(String.valueOf(le.getSequenceNumber()));
}
In the code above, the VWLogElement methods, getEventType(), getTimeStamp(), and getSequenceNumber() extract events, time stamps, and sequence numbers from an iterated series of log elements that are derived from the log query object.
The three types of information for each log element are loaded into a two-dimensional array as follows:
Object[] e1 = eventTypes.toArray();
Object[] e2 = timeStamps.toArray();
Object[] e3 = seqNumber.toArray();
Object[][] data = new Object[e1.length][3];
for (int i=0;i<e1.length;i++){
data[i][0] = e1[i];
data[i][1] = e3[i];
data[i][2] = e2[i];
}
Finally, the program uses this data array as input to Swing components that display event type, sequence number, and time stamp in a table, one row for each log element.
Indexes on exposed log fields serve to speed up queries, just as indexes on databases do. When the "Create new log index" option is selected, the program uses the API VWSystemConfiguration and VWLogDefinition classes to handle index information. Log index creation begins by creating a VWSystemConfiguration object with the VWSession.fetchSystemConfiguration method and a VWLogDefinition object with the VWLog.fetchLogDefinition() method:
VWSystemConfiguration sysConfig = s.fetchSystemConfiguration();
VWLogDefinition vw_LogDef = vw_Log.fetchLogDefinition();
The VWLogDefinition object method, vw_LogDef.getFields(), loads an exposed field definition object, vw_EFDef, whose getname() method subsequently loads a string array, EFDefNames[]:
VWExposedFieldDefinition[] vw_EFDef = vw_LogDef.getFields();
for (int i=0;i<vw_EFDef.length;i++){
EFDefNames[i] = vw_EFDef[i].getName();
}
A Swing component dialog prompts the user to choose indexes and specify a name. The components output an array of indexes, idx[], to the Exposed Field Definition Names array. The program loads string array "fields" with the chosen index names as follows:
String[] fields = new String[idx.length];
for (int i=0;i<idx.length;i++){
fields[i] = EFDefNames[idx[i]];
}
The program saves the user specified name in a string variable, "name". VWLogDefinition.createIndexDefinition(..., ...) uses this name and the fields string array created above to create an index for the log definition as follows:
vw_LogDef.createIndexDefinition(name, fields);
Finally, the program updates the system configuration with the new log definition. Note that the configuration must be committed with the VWSystemConfiguration.commit method:
sysConfig.updateLogDefinition(vw_LogDef);
String[] err = sysConfig.commit();
The program can use the returned string array for common error handling.
The code for this selection shows how to create a list of all exposed fields in the currently selected log. The program creates three objects: a VWLogDefinition object, a VWExposedFieldDefinition object array, and a String array of exposed field names. The VWLog.fetchLogDefinition() method loads the log definition object, and the VWLogDefinition.getFields() method loads an array of exposed fields as follows:
VWLogDefinition vw_LogDef = vw_Log.fetchLogDefinition();
VWExposedFieldDefinition[] vw_EFDef = vw_LogDef.getFields();
The exposed field's names are copied into a string array with the following code:
String[] EFDefNames = new String[vw_EFDef.length];
for (int i=0;i<vw_EFDef.length;i++){
EFDefNames[i] = vw_EFDef[i].getName();
}
Finally, the program creates a dialog box that inputs the log name and an array of Strings EFDefNames[] to display the exposed fields.