KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > log > component > UILog


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.log.component;
6
7 import java.util.* ;
8 import javax.faces.context.FacesContext ;
9
10 import org.apache.commons.logging.Log;
11 import org.exoplatform.faces.core.component.*;
12 import org.exoplatform.faces.core.component.model.*;
13 import org.exoplatform.services.log.LogMessage;
14 import org.exoplatform.services.log.LogService;
15
16 /**
17  * @email: tuan08@users.sourceforge.net
18  * @version: $Id: UILog.java,v 1.7 2004/08/05 04:16:47 tuan08 Exp $
19  */

20 public class UILog extends UISimpleForm {
21   protected static Log log_ = getLog("org.exoplatform.portal.portlets.log") ;
22
23   protected static List NUM_OF_MESSAGES_OPTIONS ;
24   private static List LOG_LEVEL_OPTIONS ;
25
26   static {
27     NUM_OF_MESSAGES_OPTIONS = new ArrayList(10) ;
28     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 50", "50")) ;
29     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 100", "100")) ;
30     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 200", "200")) ;
31     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 300", "300")) ;
32     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 500", "500")) ;
33     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 750", "750")) ;
34     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 1000", "1000")) ;
35     NUM_OF_MESSAGES_OPTIONS.add(new SelectItem("last 1500", "1500")) ;
36
37     LOG_LEVEL_OPTIONS = new ArrayList(5) ;
38     LOG_LEVEL_OPTIONS.add(new SelectItem("fatal", "0")) ;
39     LOG_LEVEL_OPTIONS.add(new SelectItem("error", "1")) ;
40     LOG_LEVEL_OPTIONS.add(new SelectItem("warn", "2")) ;
41     LOG_LEVEL_OPTIONS.add(new SelectItem("info", "3")) ;
42     LOG_LEVEL_OPTIONS.add(new SelectItem("debug", "4")) ;
43     LOG_LEVEL_OPTIONS.add(new SelectItem("trace", "5")) ;
44   }
45    
46   protected LogService service_ ;
47   protected UILogMessages uiLogMessages_ ;
48   protected UISelectBox uiNumberOfMessages_ ;
49   protected UISelectBox uiLogLevel_ ;
50
51   public UILog(LogService service) {
52     super("logForm", "post", null) ;
53     setId("UILog");
54     service_ = service ;
55     createUIComponents() ;
56     add(new Row().
57         add(new ComponentCell(this, uiLogMessages_)));
58     add(new Row().
59         add(new ListComponentCell().
60             add(this, uiLogLevel_).
61             add(this, uiNumberOfMessages_).
62             add(new FormButton("#{UILog.button.refresh}", "refresh")).
63             addAlign("center").addHeight("30"))) ;
64   }
65   
66   protected void createUIComponents() {
67     uiLogMessages_ = new UILogMessages() ;
68     List list = getLogMessages(4 , 50) ;
69     uiLogMessages_.setLogMessages(list) ;
70     uiNumberOfMessages_ = new UISelectBox("numberOfMessages", "50", NUM_OF_MESSAGES_OPTIONS) ;
71     uiLogLevel_ = new UISelectBox("level", "4", LOG_LEVEL_OPTIONS) ;
72   }
73
74   public void decode(FacesContext context) {
75     try {
76       int number = Integer.parseInt(uiNumberOfMessages_.getValue()) ;
77       int level = Integer.parseInt(uiLogLevel_.getValue()) ;
78       List list = getLogMessages(level , number) ;
79       uiLogMessages_.setLogMessages(list) ;
80     } catch (Exception JavaDoc ex) {
81     }
82   }
83
84   protected List getLogMessages(int level, int number) {
85     LinkedList list = new LinkedList() ;
86     List logBuffer = getLogBuffer() ;
87     int counter = 0;
88     for (int i = logBuffer.size() - 1; i >= 0 ; i--) {
89       LogMessage lm = (LogMessage) logBuffer.get(i) ;
90       if (lm.getType() <= level) {
91         list.addFirst(lm) ;
92         counter++ ;
93         if (counter == number) break ;
94       }
95     }
96     return list ;
97   }
98
99   protected List getLogBuffer() { return service_.getLogBuffer() ; }
100 }
Popular Tags