View Javadoc
1   /*
2    * Copyright 2022-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.demo.jmx.mbeans;
6   
7   import java.beans.IntrospectionException;
8   import java.beans.Introspector;
9   import java.beans.PropertyDescriptor;
10  import java.util.ArrayList;
11  import java.util.List;
12  import java.util.Locale;
13  
14  import javax.management.Attribute;
15  import javax.management.AttributeList;
16  import javax.management.AttributeNotFoundException;
17  import javax.management.DynamicMBean;
18  import javax.management.InvalidAttributeValueException;
19  import javax.management.MBeanAttributeInfo;
20  import javax.management.MBeanException;
21  import javax.management.MBeanInfo;
22  import javax.management.MBeanNotificationInfo;
23  import javax.management.ReflectionException;
24  
25  import oshi.demo.jmx.demo.PropertiesAvailable;
26  
27  public class Baseboard implements DynamicMBean, PropertiesAvailable {
28  
29      private oshi.hardware.Baseboard baseboard;
30      private MBeanInfo dMBeanInfo = null;
31      private List<String> propertiesAvailable = new ArrayList<>();
32      private final String PROPERTIES = "Properties";
33  
34      private void setUpMBean() throws IntrospectionException, javax.management.IntrospectionException {
35  
36          PropertyDescriptor[] methods = Introspector.getBeanInfo(baseboard.getClass()).getPropertyDescriptors();
37          MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[methods.length];
38  
39          for (int i = 0; i < methods.length; i++) {
40              attributeInfos[i] = new MBeanAttributeInfo(methods[i].getName(), methods[i].getShortDescription(),
41                      methods[i].getReadMethod(), null);
42              propertiesAvailable.add(
43                      methods[i].getName().substring(0, 1).toUpperCase(Locale.ROOT) + methods[i].getName().substring(1));
44          }
45  
46          dMBeanInfo = new MBeanInfo(baseboard.getClass().getName(), null, attributeInfos, null, null,
47                  new MBeanNotificationInfo[0]);
48      }
49  
50      public Baseboard(oshi.hardware.Baseboard baseboard)
51              throws IntrospectionException, javax.management.IntrospectionException {
52          this.baseboard = baseboard;
53          this.setUpMBean();
54      }
55  
56      @Override
57      public Object getAttribute(String attribute) {
58          switch (attribute) {
59          case PROPERTIES:
60              return this.getProperties();
61          case "Manufacturer":
62              return baseboard.getManufacturer();
63          case "Model":
64              return baseboard.getModel();
65          case "Version":
66              return baseboard.getVersion();
67          case "SerialNumber":
68              return baseboard.getSerialNumber();
69          default:
70              throw new IllegalArgumentException("No attribute " + attribute);
71          }
72      }
73  
74      @Override
75      public MBeanInfo getMBeanInfo() {
76          return dMBeanInfo;
77      }
78  
79      @Override
80      public List<String> getProperties() {
81          return propertiesAvailable;
82      }
83  
84      @Override
85      public void setAttribute(Attribute attribute)
86              throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
87  
88      }
89  
90      @Override
91      public AttributeList getAttributes(String[] attributes) {
92          return null;
93      }
94  
95      @Override
96      public AttributeList setAttributes(AttributeList attributes) {
97          return null;
98      }
99  
100     @Override
101     public Object invoke(String actionName, Object[] params, String[] signature)
102             throws MBeanException, ReflectionException {
103         return null;
104     }
105 
106 }