View Javadoc
1   /*
2    * Copyright (C) 2005-2015 Schlichtherle IT Services.
3    * All rights reserved. Use is subject to license terms.
4    */
5   package net.java.truevfs.comp.jmx;
6   
7   import java.util.Date;
8   import java.util.Objects;
9   import javax.annotation.concurrent.ThreadSafe;
10  import javax.management.MBeanAttributeInfo;
11  import javax.management.MBeanInfo;
12  import javax.management.StandardMBean;
13  import static net.java.truecommons.cio.Entry.Access.*;
14  import static net.java.truecommons.cio.Entry.Size.DATA;
15  import static net.java.truecommons.cio.Entry.Size.STORAGE;
16  import static net.java.truecommons.cio.Entry.UNKNOWN;
17  import net.java.truecommons.cio.IoBuffer;
18  
19  /**
20   * A view for an {@linkplain IoBuffer I/O buffer}.
21   *
22   * @param  <B> the type of the I/O buffer.
23   * @author Christian Schlichtherle
24   */
25  @ThreadSafe
26  public class JmxBufferView<B extends IoBuffer>
27  extends StandardMBean implements JmxBufferMXBean {
28  
29      protected final B buffer;
30  
31      public JmxBufferView(B buffer) { this(JmxBufferMXBean.class, buffer); }
32  
33      protected JmxBufferView(
34              final Class<? extends JmxBufferMXBean> type,
35              final B buffer) {
36          super(type, true);
37          this.buffer = Objects.requireNonNull(buffer);
38      }
39  
40      @Override
41      protected String getDescription(MBeanInfo info) {
42          return "An I/O buffer.";
43      }
44  
45      @Override
46      protected String getDescription(final MBeanAttributeInfo info) {
47          switch (info.getName()) {
48          case "Name":
49              return "The name of this buffer.";
50          case "SizeOfData":
51              return "The data size of this buffer.";
52          case "SizeOfStorage":
53              return "The storage size of this buffer.";
54          case "TimeCreatedDate":
55              return "The time this buffer has been created.";
56          case "TimeCreatedMillis":
57              return "The time this buffer has been created in milliseconds.";
58          case "TimeReadDate":
59              return "The last time this buffer has been read or accessed.";
60          case "TimeReadMillis":
61              return "The last time this buffer has been read or accessed in milliseconds.";
62          case "TimeWrittenDate":
63              return "The last time this buffer has been written.";
64          case "TimeWrittenMillis":
65              return "The last time this buffer has been written in milliseconds.";
66          default:
67              return null;
68          }
69      }
70  
71      @Override
72      public String getName() {
73          return buffer.getName();
74      }
75  
76      @Override
77      public long getSizeOfData() {
78          return buffer.getSize(DATA);
79      }
80  
81      @Override
82      public long getSizeOfStorage() {
83          return buffer.getSize(STORAGE);
84      }
85  
86      @Override
87      public String getTimeCreatedDate() {
88          final long time = buffer.getTime(CREATE);
89          return UNKNOWN == time ? null : new Date(time).toString();
90      }
91  
92      @Override
93      public Long getTimeCreatedMillis() {
94          final long time = buffer.getTime(CREATE);
95          return UNKNOWN == time ? null : time;
96      }
97  
98      @Override
99      public String getTimeReadDate() {
100         final long time = buffer.getTime(READ);
101         return UNKNOWN == time ? null : new Date(time).toString();
102     }
103 
104     @Override
105     public Long getTimeReadMillis() {
106         final long time = buffer.getTime(READ);
107         return UNKNOWN == time ? null : time;
108     }
109 
110     @Override
111     public String getTimeWrittenDate() {
112         final long time = buffer.getTime(WRITE);
113         return UNKNOWN == time ? null : new Date(time).toString();
114     }
115 
116     @Override
117     public Long getTimeWrittenMillis() {
118         final long time = buffer.getTime(WRITE);
119         return UNKNOWN == time ? null : time;
120     }
121 }