View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.logging.jmx;
2   
3   import java.util.Map;
4   
5   import javax.management.MBeanServerConnection;
6   import javax.management.ObjectName;
7   
8   import cz.cuni.amis.introspection.jmx.DynamicProxy;
9   import cz.cuni.amis.pogamut.base.utils.logging.AbstractLogCategories;
10  import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
11  import cz.cuni.amis.utils.exception.PogamutJMXException;
12  import cz.cuni.amis.utils.maps.LazyMap;
13  
14  /**
15   *
16   * @author ik
17   */
18  public class LogCategoriesJMXProxy extends AbstractLogCategories {
19  
20      DynamicProxy proxy = null;
21      MBeanServerConnection mbsc = null;
22      ObjectName objectName = null;
23      Map<String, LogCategory> categories = new LazyMap<String, LogCategory>() {
24  
25          @Override
26          protected LogCategory create(String key) {
27              try {
28                  return new LogCategoryJMXProxy(mbsc, objectName, key);
29              } catch (Exception ex) {
30                  throw new RuntimeException(ex);
31              }
32          }
33      };
34  
35      public LogCategoriesJMXProxy(MBeanServerConnection mbsc, ObjectName parentName) throws PogamutJMXException {
36          objectName = JMXLogCategories.getJMXLogCategoriesName(parentName);
37          proxy = new DynamicProxy(objectName, mbsc);
38          this.mbsc = mbsc;
39      }
40  
41      @Override
42      protected Map<String, LogCategory> getCategoriesInternal() {
43          // update
44          for(String catName : getCategoryNames()) {
45              categories.get(catName);
46          }
47          return categories;
48      }
49  
50      @Override
51      public LogCategory getCategory(String name) {
52          return getCategoriesInternal().get(name);
53      }
54  
55      @Override
56      public String[] getCategoryNames() {
57          try {
58              return (String[]) proxy.getAttribute("CategoryNames");
59          } catch (Exception ex) {
60              throw new RuntimeException(ex);
61          }
62      }
63  }