1 package com.trendmicro.grid.acl.ds.jmx;
2
3 import com.trendmicro.grid.acl.ds.RepositoriesContext;
4 import com.trendmicro.grid.acl.ds.RepositorySelector;
5 import org.springframework.jmx.export.annotation.ManagedResource;
6 import org.springframework.jmx.export.annotation.ManagedAttribute;
7 import org.springframework.stereotype.Service;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import javax.annotation.Resource;
14 import javax.management.*;
15 import java.util.List;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18
19
20
21
22
23
24
25 @Service("jmxRepositoriesAdapter")
26 @ManagedResource(objectName = RepositoriesAdapter.OBJECT_NAME, description = "" +
27 "Shows the active repository implementations and allows " +
28 "to change the selection of the implementation that is currently active.")
29 public class RepositoriesAdapter {
30 private static final Logger log = LoggerFactory.getLogger(RepositoriesAdapter.class);
31
32 static final String JMX_DOMAIN = "com.trendmicro.grid.acl:";
33 static final String JMX_NAME = "RepositorySelection";
34 static final String OBJECT_NAME = JMX_DOMAIN + "type=" + JMX_NAME + ",name=Context";
35
36 @Resource
37 MBeanServer server;
38 @Resource
39 RepositoriesContext context;
40
41 private List<ObjectName> registeredNames = new ArrayList<ObjectName>();
42
43 @PostConstruct
44 synchronized void publishSelectorsToMonitoring() {
45 for (RepositorySelector<?> selector : context.getAllSelectors()) {
46 String className = selector.getRepositoryClass();
47 try {
48 className = className.substring(className.lastIndexOf('.') + 1);
49 ObjectName name = new ObjectName(JMX_DOMAIN + "type=" + JMX_NAME + ",name=" + className);
50 server.registerMBean(selector, name);
51 registeredNames.add(name);
52
53 if (log.isDebugEnabled())
54 log.debug("Registered the repository selector MBean '{}'", name);
55
56 } catch (Exception e) {
57 log.error("TMACL-00160:Failed to register selector for '" + className +
58 "' with JMX monitoring.", e);
59 }
60 }
61 }
62
63 @PreDestroy
64 synchronized void removeSelectorsFromMonitoring() {
65 for (Iterator<ObjectName> i = registeredNames.iterator(); i.hasNext();) {
66 ObjectName name = i.next();
67 try {
68 server.unregisterMBean(name);
69 i.remove();
70 } catch (Exception e) {
71 log.error("TMACL-00180:Failed to unregister selector '" + name + "' from JMX.", e);
72 }
73 }
74 }
75
76 @ManagedAttribute
77 public int getRegisteredRepositoryImplementationCount() {
78 int count = 0;
79 for (RepositorySelector<?> selector : context.getAllSelectors())
80 count += selector.getAvailableKeys().size();
81 return count;
82 }
83 }