Java tutorial
/** * Copyright (c) 2006-2011 Berlin Brown. All Rights Reserved * * http://www.opensource.org/licenses/bsd-license.php * All rights reserved. * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the Botnode.com (Berlin Brown) nor * the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Date: 8/15/2011 * * Description: LogFile Searcher. Search log file and build statistics from the scan. * * Contact: Berlin Brown <berlin dot brown at gmail.com> */ package org.berlin.network.client; import java.io.Serializable; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.berlin.network.client.session.SessionService; import org.berlin.network.client.session.SessionServiceAsync; import org.berlin.network.client.stats.BasicStatsService; import org.berlin.network.client.stats.BasicStatsServiceAsync; import org.berlin.network.shared.FieldVerifier; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.user.cellview.client.CellTable; import com.google.gwt.user.cellview.client.ColumnSortEvent.AsyncHandler; import com.google.gwt.user.cellview.client.ColumnSortList; import com.google.gwt.user.cellview.client.TextColumn; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.IsSerializable; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.view.client.AsyncDataProvider; import com.google.gwt.view.client.HasData; import com.google.gwt.view.client.Range; /** * Entry point classes define <code>onModuleLoad()</code>. */ public class ParseLogCriticalErrors implements EntryPoint { /** * The message displayed to the user when the server cannot be reached or * returns an error. */ private static final String SERVER_ERROR = "An error occurred while attempting to contact the server. Please check your network connection and try again."; /** * Create a remote service proxy to talk to the server-side Greeting service. */ private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class); private final BasicStatsServiceAsync statsService = GWT.create(BasicStatsService.class); private final SessionServiceAsync sessionService = GWT.create(SessionService.class); /** * A simple data type that represents property data. */ public static class PropertyEntry implements IsSerializable, Serializable { private static final long serialVersionUID = 1L; private String name; private String value; public PropertyEntry(final String name, final String value) { this.name = name; this.value = value; } public PropertyEntry() { this.name = ""; this.value = ""; } public String getName() { return name; } public String getValue() { return value; } public void setName(String name) { this.name = name; } public void setValue(String value) { this.value = value; } } // End of Class Property Entry // public static class SessionUIInfo implements IsSerializable, Serializable { private static final long serialVersionUID = 1L; private String sessionId = ""; private String criticalError = ""; private String filename = ""; private String javaDate = ""; /** * @return the sessionId */ public String getSessionId() { return sessionId; } /** * @param sessionId the sessionId to set */ public void setSessionId(String sessionId) { this.sessionId = sessionId; } /** * @return the criticalError */ public String getCriticalError() { return criticalError; } /** * @param criticalError the criticalError to set */ public void setCriticalError(String criticalError) { this.criticalError = criticalError; } /** * @return the filename */ public String getFilename() { return filename; } /** * @param filename the filename to set */ public void setFilename(String filename) { this.filename = filename; } /** * @return the javaDate */ public String getJavaDate() { return javaDate; } /** * @param javaDate the javaDate to set */ public void setJavaDate(String javaDate) { this.javaDate = javaDate; } } // End of the Class // /** * This is the entry point method. */ public void onModuleLoad() { final Button sendButton = new Button("Execute Command"); final TextBox nameField = new TextBox(); final TextBox sessionLookupField = new TextBox(); final TextBox expressionField = new TextBox(); nameField.setText("help"); sessionLookupField.setText(""); expressionField.setText(""); final Label errorLabel = new Label(); final Label statusMessageLabel = new Label(); statusMessageLabel.setText("Status command area - waiting for command..."); // We can add style names to widgets sendButton.addStyleName("sendButton"); // Add the nameField and sendButton to the RootPanel // Use RootPanel.get() to get the entire body element RootPanel.get("nameFieldContainer").add(nameField); RootPanel.get("sessionFieldContainer").add(sessionLookupField); RootPanel.get("expressionFieldContainer").add(expressionField); RootPanel.get("sendButtonContainer").add(sendButton); RootPanel.get("errorLabelContainer").add(errorLabel); RootPanel.get("statusLabelContainer").add(statusMessageLabel); // Focus the cursor on the name field when the app loads nameField.setFocus(true); nameField.selectAll(); // Create the popup dialog box final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Remote Procedure Call"); dialogBox.setAnimationEnabled(true); final Button closeButton = new Button("Close"); // We can set the id of a widget by accessing its Element closeButton.getElement().setId("closeButton"); final Label textToServerLabel = new Label(); final HTML serverResponseLabel = new HTML(); final VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.addStyleName("dialogVPanel"); dialogVPanel.add(new HTML("<b>Sending command to the server:</b>")); dialogVPanel.add(textToServerLabel); dialogVPanel.add(new HTML("<br><b>Server replies:</b>")); dialogVPanel.add(serverResponseLabel); dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); // Add a handler to close the DialogBox closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); sendButton.setEnabled(true); sendButton.setFocus(true); } }); final CellTable<SessionUIInfo> tableSessionInfo = new CellTable<SessionUIInfo>(); // Create a handler for the sendButton and nameField final class MyHandler implements ClickHandler, KeyUpHandler { /** * Fired when the user clicks on the sendButton. */ public void onClick(ClickEvent event) { sendCommandToServer(); } /** * Fired when the user types in the nameField. */ public void onKeyUp(KeyUpEvent event) { if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { sendCommandToServer(); } } /** * Send the name from the nameField to the server and wait for a response. */ private void sendCommandToServer() { final String cmdToServer = nameField.getText(); final String sessionIdData = sessionLookupField.getText(); // First, we validate the input. errorLabel.setText(""); statusMessageLabel.setText("Running command [" + cmdToServer + "]..."); tableSessionInfo.setVisible(true); if (!FieldVerifier.isValidName(cmdToServer)) { errorLabel.setText("Please enter a command (Example = help)"); return; } // Then, we send the input to the server. sendButton.setEnabled(false); textToServerLabel.setText(cmdToServer); serverResponseLabel.setText(""); /********************************** * Basic Greet Service Action **********************************/ greetingService.greetServer(cmdToServer, sessionIdData, new AsyncCallback<String>() { public void onFailure(final Throwable caught) { // Show the RPC error message to the user dialogBox.setText("Remote Procedure Call - Failure"); serverResponseLabel.addStyleName("serverResponseLabelError"); serverResponseLabel.setHTML(SERVER_ERROR); statusMessageLabel.setText("Error at command"); dialogBox.center(); closeButton.setFocus(true); } public void onSuccess(final String result) { dialogBox.setText("Remote Procedure Call"); serverResponseLabel.removeStyleName("serverResponseLabelError"); serverResponseLabel.setHTML(result); dialogBox.center(); closeButton.setFocus(true); statusMessageLabel.setText("Message command run - [" + cmdToServer + "]"); System.out.println("On success of command from server "); } }); final String expr = expressionField.getText(); System.out.println("on command, sending request to server"); sessionService.sessionServer(cmdToServer, sessionIdData, expr, sessionCallbackFromCommand(tableSessionInfo)); } } // End of class // // Add a handler to send the name to the server final MyHandler handler = new MyHandler(); sendButton.addClickHandler(handler); nameField.addKeyUpHandler(handler); /**************************************************************** * Add property data table ****************************************************************/ { // Create a CellTable. final CellTable<PropertyEntry> tableProperty = new CellTable<PropertyEntry>(); // Create name column. final TextColumn<PropertyEntry> nameColumn = new TextColumn<PropertyEntry>() { @Override public String getValue(final PropertyEntry contact) { return contact.name; } }; // Make the name column sortable. nameColumn.setSortable(true); // Create address column. final TextColumn<PropertyEntry> addressColumn = new TextColumn<PropertyEntry>() { @Override public String getValue(final PropertyEntry contact) { return contact.value; } }; // Add the columns. tableProperty.addColumn(nameColumn, "Name"); tableProperty.addColumn(addressColumn, "Value"); // Set the total row count. You might send an RPC request to determine the // total row count. tableProperty.setRowCount(34, false); // Set the range to display. In this case, our visible range is smaller than // the data set. tableProperty.setVisibleRange(0, 34); final AsyncDataProvider<PropertyEntry> dataProviderForPropertyList = new AsyncDataProvider<PropertyEntry>() { @Override protected void onRangeChanged(final HasData<PropertyEntry> display) { final Range range = display.getVisibleRange(); // Get the ColumnSortInfo from the table. final ColumnSortList sortList = tableProperty.getColumnSortList(); new Timer() { @Override public void run() { final AsyncCallback<List<PropertyEntry>> callbackPropertyChange = new AsyncCallback<List<PropertyEntry>>() { @Override public void onFailure(final Throwable caught) { System.out.println("Error - " + caught); dialogBox.setText("Remote Procedure Call - Failure"); serverResponseLabel.addStyleName("serverResponseLabelError"); serverResponseLabel.setHTML(SERVER_ERROR); dialogBox.center(); closeButton.setFocus(true); } @Override public void onSuccess(final List<PropertyEntry> result) { if (result == null) { System.out.println("Invalid result set"); return; } System.out.println("onSuccess of Timer Callback Table Property Set"); final int start = range.getStart(); final int end = start + range.getLength(); final List<PropertyEntry> statsList = result; Collections.sort(statsList, new Comparator<PropertyEntry>() { public int compare(final PropertyEntry o1, final PropertyEntry o2) { if (o1 == o2) { return 0; } int diff = -1; if (o1 != null) { diff = (o2 != null) ? o1.name.compareTo(o2.name) : 1; } return sortList.get(0).isAscending() ? diff : -diff; } }); final List<PropertyEntry> dataInRange = statsList.subList(start, end); tableProperty.setRowData(start, dataInRange); } }; // End of Callback // /********************************** * Stats Service Action **********************************/ statsService.statsServer("invalid", "invalid", callbackPropertyChange); } // End of Run method // }.schedule(100); } // onRange Changed // }; // Connect the list to the data provider. dataProviderForPropertyList.addDataDisplay(tableProperty); final AsyncHandler columnSortHandler = new AsyncHandler(tableProperty); tableProperty.addColumnSortHandler(columnSortHandler); // We know that the data is sorted alphabetically by default. tableProperty.getColumnSortList().push(nameColumn); // Add it to the root panel. RootPanel.get("listViewContainer").add(tableProperty); /********************************** * End of adding property table data ***********************************/ } // End - property table // /**************************************************************** * Add session lookup data table ****************************************************************/ { final TextColumn<SessionUIInfo> sessionColumn = new TextColumn<SessionUIInfo>() { @Override public String getValue(final SessionUIInfo info) { return info.sessionId; } }; sessionColumn.setSortable(true); final TextColumn<SessionUIInfo> dateColumn = new TextColumn<SessionUIInfo>() { @Override public String getValue(final SessionUIInfo info) { return info.getJavaDate(); } }; final TextColumn<SessionUIInfo> filenameColumn = new TextColumn<SessionUIInfo>() { @Override public String getValue(final SessionUIInfo info) { return info.filename; } }; // Add the columns. tableSessionInfo.addColumn(sessionColumn, "Session"); tableSessionInfo.addColumn(dateColumn, "Date"); tableSessionInfo.addColumn(filenameColumn, "LogFilename"); tableSessionInfo.setRowCount(24, false); tableSessionInfo.setVisibleRange(0, 24); final AsyncDataProvider<SessionUIInfo> dataProviderForSessionList = new AsyncDataProvider<SessionUIInfo>() { @Override protected void onRangeChanged(final HasData<SessionUIInfo> display) { new Timer() { @Override public void run() { /********************************** * Session Service Action **********************************/ final String cmd = nameField.getText(); final String sessionIdData = sessionLookupField.getText(); final String expr = expressionField.getText(); System.out.println("on Range changed, sending request to server"); sessionService.sessionServer(cmd, sessionIdData, expr, sessionCallback(tableSessionInfo, display)); } // End of Run method // }.schedule(100); } // onRange Changed // }; dataProviderForSessionList.addDataDisplay(tableSessionInfo); final AsyncHandler columnSortHandler = new AsyncHandler(tableSessionInfo); tableSessionInfo.addColumnSortHandler(columnSortHandler); tableSessionInfo.getColumnSortList().push(sessionColumn); // Add it to the root panel. RootPanel.get("sessionViewContainer").add(tableSessionInfo); tableSessionInfo.setVisible(false); } // End of add results table } // End of method // private AsyncCallback<List<SessionUIInfo>> sessionCallback(final CellTable<SessionUIInfo> tableSessionInfo, final HasData<SessionUIInfo> display) { final Range range = display.getVisibleRange(); final ColumnSortList sortList = tableSessionInfo.getColumnSortList(); final int start = range.getStart(); final int end = range.getLength(); final AsyncCallback<List<SessionUIInfo>> callbackSessionChange = new AsyncCallback<List<SessionUIInfo>>() { @Override public void onFailure(final Throwable caught) { System.out.println("Error - " + caught); } @Override public void onSuccess(final List<SessionUIInfo> result) { if (result == null) { System.out.println("Invalid result set"); return; } System.out.println("onSuccess of Timer Callback Table Session - " + start + " /// " + end + " size=" + result.size()); final List<SessionUIInfo> sessionList = result; Collections.sort(sessionList, new Comparator<SessionUIInfo>() { public int compare(final SessionUIInfo o1, final SessionUIInfo o2) { if (o1 == o2) { return 0; } int diff = -1; if (o1 != null) { diff = (o2 != null) ? o1.sessionId.compareTo(o2.sessionId) : 1; } return sortList.get(0).isAscending() ? diff : -diff; } }); final List<SessionUIInfo> dataInRange; if (sessionList.size() == 0) { dataInRange = sessionList; } else { dataInRange = sessionList.subList(start, sessionList.size() <= end ? sessionList.size() : end - 1); } tableSessionInfo.setRowData(start, dataInRange); } }; // End of Callback // return callbackSessionChange; } private AsyncCallback<List<SessionUIInfo>> sessionCallbackFromCommand( final CellTable<SessionUIInfo> tableSessionInfo) { final AsyncCallback<List<SessionUIInfo>> callbackSessionChange = new AsyncCallback<List<SessionUIInfo>>() { @Override public void onFailure(final Throwable caught) { System.out.println("Error - " + caught); } @Override public void onSuccess(final List<SessionUIInfo> result) { if (result == null) { System.out.println("Invalid result set"); return; } System.out.println("Basic callback - sessioninfo lookup - " + result.size()); final List<SessionUIInfo> sessionList = result; final List<SessionUIInfo> dataInRange; if (sessionList.size() == 0) { dataInRange = sessionList; tableSessionInfo.setRowCount(0, true); } else { final int sz = sessionList.size() <= 20 ? sessionList.size() : 19; dataInRange = sessionList.subList(0, sz); tableSessionInfo.setRowCount(sz, true); } tableSessionInfo.setRowData(0, dataInRange); tableSessionInfo.redraw(); } }; // End of Callback // return callbackSessionChange; } } // End of the Class //