KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.logging.Log;
10 import org.exoplatform.faces.FacesConstants;
11 import org.exoplatform.faces.core.component.UIRadioBox;
12 import org.exoplatform.faces.core.component.UISimpleForm;
13 import org.exoplatform.faces.core.component.model.*;
14 import org.exoplatform.services.log.LogService;
15 import org.exoplatform.services.log.impl.ExoLog;
16
17 /**
18  * @email: tuan08@users.sourceforge.net
19  * @version: $Id: UIConfig.java,v 1.7 2004/06/30 19:54:39 tuan08 Exp $
20  */

21 public class UILogConfig extends UISimpleForm {
22   protected static Log log_ = getLog("org.exoplatform.portal.portal.portlets.log") ;
23   private static List LEVELS ;
24  
25   static {
26     LEVELS = new ArrayList(6) ;
27     LEVELS.add(new SelectItem("fatal", "0")) ;
28     LEVELS.add(new SelectItem("error", "1")) ;
29     LEVELS.add(new SelectItem("warn", "2")) ;
30     LEVELS.add(new SelectItem("info", "3")) ;
31     LEVELS.add(new SelectItem("debug", "4")) ;
32     LEVELS.add(new SelectItem("trace", "5")) ;
33   }
34    
35   private LogService service_ ;
36   private String JavaDoc name_ ;
37   private int availableLog_ ;
38   private boolean admin_ ;
39   
40   public UILogConfig(LogService service) {
41     super("configForm", "post", null) ;
42     service_ = service ;
43     name_ = "Configuration" ;
44     setId("UILogConfig");
45     admin_ = hasRole("admin");
46     update() ;
47   }
48
49   private void update() {
50     clear() ;
51     Collection logs = service_.getLogs() ;
52     availableLog_ = logs.size() ;
53     add(new HeaderRow().
54         add(new Cell("#{UIConfig.header.log-name}")).
55         add(new Cell("#{UIConfig.header.log-level}")));
56     Iterator i = logs.iterator() ;
57     while(i.hasNext()) {
58       ExoLog log = (ExoLog) i.next() ;
59       String JavaDoc category = log.getLogCategory() ;
60       String JavaDoc level = Integer.toString(log.getLevel()) ;
61       UIRadioBox uiRadio = new UIRadioBox(category,level, LEVELS);
62       uiRadio.setClazz("radio") ;
63       add(new Row().
64           add(new LabelCell(category)).
65           add(new ComponentCell(this, uiRadio).
66               addAlign("center")));
67     }
68     add(new Row().
69         add(new ListComponentCell().
70                 add(new FormButton("#{UIConfig.button.save}", SAVE_ACTION)).
71             addColspan("2").addAlign("center").addHeight("30"))) ;
72   }
73   
74   public String JavaDoc getComponentType() { return COMPONENT_TYPE; }
75
76   public void decode(FacesContext context) {
77     Map paramMap = context.getExternalContext().getRequestParameterMap() ;
78     String JavaDoc action = (String JavaDoc) paramMap.get(FacesConstants.ACTION) ;
79     if (SAVE_ACTION.equals(action)) {
80       if (!admin_) return ;
81       List children = getChildren() ;
82       try {
83         for(int i = 0; i < children.size(); i++) {
84           Object JavaDoc o = children.get(i) ;
85           if (o instanceof UIRadioBox) {
86             UIRadioBox child = (UIRadioBox) o ;
87             int level = Integer.parseInt(child.getValue()) ;
88             service_.setLogLevel(child.getName(), level, false ) ;
89           }
90         }
91         update() ;
92       } catch (Exception JavaDoc ex) {
93         log_.error("Error: ", ex) ;
94       }
95     } else {
96       Collection logs = service_.getLogs() ;
97       if(availableLog_ < logs.size()) {
98         update() ;
99       }
100     }
101   }
102 }
Popular Tags