- package org.apache.pivot.scala.log
- import scala.reflect.BeanProperty
- import io.Source
- import org.apache.pivot.wtk.content.ListViewItemRenderer
- import java.lang.String
- import org.apache.pivot.wtkx.{WTKX, WTKXSerializer}
- /*為了避免和scala.Application的名稱沖突,這里修改了別名*/
- import org.apache.pivot.wtk.{ Application => PivotApplication, _}
- import org.apache.pivot.collections.{ArrayList, Map}
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2010-8-26
- * Time: 10:36:45
- * To change this template use File | Settings | File Templates.
- */
- /*日志記錄Bean對(duì)象,由于Scala標(biāo)準(zhǔn)生成的字段不使用getter/setter的格式,
- 但是提供了注解 @scala.reflect.BeanProperty ,使得編譯器可以生成標(biāo)準(zhǔn)的JavaBean對(duì)象的getter/setter接口
- val 只生成了getter
- var 同時(shí)生成了getter和setter */
- class LogRecord ( @BeanProperty val threadName : String,
- @BeanProperty val date : String,
- @BeanProperty val time : String,
- @BeanProperty val module : String,
- @BeanProperty val level : String,
- @BeanProperty val content : String){
- /*
- 重載了 Any的 toString接口
- override關(guān)鍵字是必須的,在java中使用的是注解 @override,但是java中@override并不是必須的。
- 省略了 函數(shù)還回值,由編譯器進(jìn)行類型推演得到
- */
- override def toString() = {
- threadName +" "+date +" "+ time +" "+module +" "+level+" "+content
- }
- }
- /*
- LogRecord 類的半生對(duì)象
- 定義了 scala中的魔術(shù)接口 apply ,使得可以使用 LogRecord("sting 文本") 可以創(chuàng)建一個(gè)class LogRecord對(duì)象。
- apply方法的調(diào)用由編譯器自動(dòng)調(diào)用,我們只負(fù)責(zé)定義即可
- 我們要解析的日志格式
- threadName date time module Level content
- DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - Debug level: 2
- DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - *=ERROR WARNING EXCEPT
- DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - LM=*
- */
- object LogRecord {
- def apply( line : String ) : LogRecord = {
- /*生成一個(gè) Regex對(duì)象,用于模式匹配*/
- val logRegex = """([A-Za-z0-9]+) +([0-9/]*) +([0-9:]*) +([A-Z]*) +: *([A-Z_]+).*""".r
- line match {
- /*如果模式匹配成功,threadName,date,time,module,level 分配按次序綁定到模式匹配表達(dá)式中()的內(nèi)容 */
- case logRegex(threadName,date,time,module,level) =>
- val logRecord: LogRecord = new LogRecord( threadName, date, time, module, level,line)
- logRecord
- /*模式匹配通配符,在沒(méi)有匹配到的情況下,做通用處理。如果不添加,在匹配不到時(shí)會(huì)拋出 MatchError異常*/
- case _ =>
- val logRecord: LogRecord = new LogRecord("N/A","N/A","N/A","N/A","N/A","N/A")
- logRecord
- }
- }
- }
- /*
- Apache Pivot ListView ItemRenderer
- 重新定義了如何顯示 LogRecord對(duì)象的 列表項(xiàng)目的渲染。
- 如果使用默認(rèn)的,那么ListView顯示對(duì)象時(shí),直接調(diào)用對(duì)象的toString獲得其字符串表示
- */
- class LogListViewItemRenderer extends ListViewItemRenderer {
- imageView.setVisible(false)
- override def render(item: AnyRef, index: Int, listView: ListView, selected: Boolean, checked: Boolean, highlighted: Boolean, disabled: Boolean) = {
- if ( item != null && item.isInstanceOf[LogRecord])
- {
- val log = item.asInstanceOf[LogRecord]
- label.setText(log.content)
- }
- }
- }
- /**
- 定義主窗口界面代碼,必須繼承自 org.apache.pivot.Application
- 使用了 @WTKX注解,該注解屬于 wtkx的命名對(duì)象的綁定語(yǔ)法,在從wtkx文件加載GUI時(shí),
- 調(diào)用bind接口可以自動(dòng)和wtkx文件中聲明的wtkx:id對(duì)象進(jìn)行綁定,無(wú)需在掉嗎中調(diào)用 get("name")
- */
- class MainWindow extends PivotApplication {
- var window : Window = null
- @WTKX var textInputFilePath : TextInput = null
- @WTKX var browsePushButton : PushButton = null
- @WTKX var loadPushButton : PushButton = null
- @WTKX var textInputThreadName :TextInput = null
- @WTKX var textInputModule : TextInput = null
- @WTKX var textInputLevel : TextInput = null
- @WTKX var textInputContent : TextInput = null
- @WTKX var logListView : ListView = null
- def resume = {}
- def suspend = {}
- def shutdown(optional: Boolean) = {
- if ( window != null)
- {
- window.close
- true
- }
- false
- }
- def startup(display: Display, properties: Map[String, String]) = {
- val wtkxSerializer = new WTKXSerializer()
- var matchString : String = null
- /*從xml(wtkx)文件加載GUI*/
- window = wtkxSerializer.readObject(this,"MainWindow.xml").asInstanceOf[Window]
- wtkxSerializer.bind(this)
- if ( properties containsKey "logfile")
- {
- textInputFilePath setText ( properties get "logfile")
- }
- /*給 Button添加事件處理函數(shù)*/
- browsePushButton.getButtonPressListeners.add( function2Listener (browseButtonPressed ) )
- loadPushButton.getButtonPressListeners.add( function2Listener(loadButtonPressed ))
- window.open(display)
- }
- /*瀏覽按鈕事件處理,打開(kāi)一個(gè)文件瀏覽窗口,讓用戶選擇文件,并在用戶關(guān)閉對(duì)話框時(shí),捕獲用戶選擇的文件名*/
- def browseButtonPressed( button : Button ) : Unit = {
- val dialog : FileBrowserSheet = new FileBrowserSheet(FileBrowserSheet.Mode.OPEN)
- dialog.open( window, new SheetCloseListener() {
- def sheetClosed(sheet: Sheet) = {
- if ( sheet.getResult)
- {
- val fileBrowseSheet = sheet.asInstanceOf[FileBrowserSheet]
- textInputFilePath.setText( fileBrowseSheet.getSelectedFile.getPath.toString)
- }
- }
- })
- }
- /*從log文件加載內(nèi)容,每一行是一個(gè)日志記錄
- for中使用了 if過(guò)濾器,只有條件符合了,才會(huì)進(jìn)入for循環(huán)體。
- scala沒(méi)有提供continu,而是在 for中提供了條件過(guò)濾來(lái)替代
- */
- def loadButtonPressed( button : Button ) : Unit = {
- val logFile = Source.fromFile(textInputFilePath.getText)
- val list = new ArrayList[LogRecord]
- for ( line <- logFile.getLines ; logRecord = LogRecord(line.trim);
- if ( textInputThreadName.getText == "" || textInputThreadName.getText.contains(logRecord.threadName) );
- if ( textInputModule.getText == "" || textInputModule.getText.contains(logRecord.module));
- if ( textInputLevel.getText == "" || textInputLevel.getText.contains(logRecord.level))){
- list add logRecord
- }
- logListView.setListData( list)
- }
- /*按鈕事件輔助接口,用于把一個(gè) 事件處理函數(shù)轉(zhuǎn)換為ButtonPressListener對(duì)象,也可以采取更高級(jí)的內(nèi)容,使用implicit,這里沒(méi)有使用*/
- def function2Listener( fun : Button => Unit ) :ButtonPressListener = {
- val listener = new ButtonPressListener()
- {
- def buttonPressed(button: Button) = {
- fun(button)
- }
- }
- listener
- }
- }
- /*
- 主函數(shù)
- 符合Pivot主函數(shù)入口規(guī)則
- */
- object LogAnalyse {
- def main ( args : Array[String]) : Unit = {
- DesktopApplicationContext.main( classOf[MainWindow], args)
- }
- }
run:
使用 java時(shí):
java -classpath scala-library.jar;pivot-core-1.5.jar;pivot-tools-1.5.jar;pivot-wtk-1.5.jar;pivot-wtk-terra-1.5.jar;. org.apache.pivot.scala.log.LogAnalyse
使用 scala時(shí)
java -classpath pivot-core-1.5.jar;pivot-tools-1.5.jar;pivot-wtk-1.5.jar;pivot-wtk-terra-1.5.jar;. org.apache.pivot.scala.log.LogAnalyse