001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui;
018
019import java.awt.Component;
020import java.awt.GridBagConstraints;
021import java.awt.GridBagLayout;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Set;
025import java.util.SortedSet;
026import java.util.TreeSet;
027
028import javax.swing.Box;
029import javax.swing.JComponent;
030import javax.swing.JEditorPane;
031import javax.swing.JLabel;
032import javax.swing.JPanel;
033import javax.swing.border.EmptyBorder;
034import javax.swing.event.ChangeEvent;
035import javax.swing.event.ChangeListener;
036import javax.swing.text.JTextComponent;
037
038import org.opends.guitools.controlpanel.datamodel.BasicMonitoringAttributes;
039import org.opends.guitools.controlpanel.datamodel.CustomSearchResult;
040import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
041import org.opends.guitools.controlpanel.ui.components.BasicExpander;
042import org.opends.guitools.controlpanel.util.Utilities;
043import org.opends.server.util.CollectionUtils;
044
045import static org.opends.guitools.controlpanel.util.Utilities.*;
046import static org.opends.messages.AdminToolMessages.*;
047import static org.opends.server.util.ServerConstants.*;
048
049/** The panel displaying the java monitoring information. */
050public class JavaInformationMonitoringPanel extends GeneralMonitoringPanel
051{
052  private static final long serialVersionUID = 9031734563799969830L;
053
054  private final List<BasicMonitoringAttributes> generalAttributes = CollectionUtils.newArrayList(
055      BasicMonitoringAttributes.JVM_VERSION,
056      BasicMonitoringAttributes.JVM_VENDOR,
057      BasicMonitoringAttributes.JVM_ARCHITECTURE,
058      BasicMonitoringAttributes.JVM_ARGUMENTS,
059      BasicMonitoringAttributes.CLASS_PATH,
060      BasicMonitoringAttributes.JAVA_VERSION,
061      BasicMonitoringAttributes.JAVA_VENDOR);
062  private final List<BasicMonitoringAttributes> extraAttributes = CollectionUtils.newArrayList(
063      BasicMonitoringAttributes.CLASS_PATH,
064      BasicMonitoringAttributes.JAVA_VERSION,
065      BasicMonitoringAttributes.JAVA_VENDOR);
066  private final List<JComponent> generalMonitoringComps = new ArrayList<>();
067  {
068    for (int i=0; i<generalAttributes.size(); i++)
069    {
070      if (generalAttributes.get(i) == BasicMonitoringAttributes.CLASS_PATH ||
071          generalAttributes.get(i) == BasicMonitoringAttributes.JVM_ARGUMENTS)
072      {
073        JEditorPane pane = new JEditorPane();
074        pane.setEditable(false);
075        pane.setBorder(new EmptyBorder(0, 0, 0, 0));
076        pane.setOpaque(false);
077        pane.setFocusCycleRoot(false);
078        generalMonitoringComps.add(pane);
079      }
080      else
081      {
082        generalMonitoringComps.add(Utilities.createDefaultLabel());
083      }
084    }
085  }
086
087  private final List<String> memoryAttributes = new ArrayList<>();
088  private final List<JLabel> memoryLabels = new ArrayList<>();
089  private JPanel memoryPanel;
090
091  /** Default constructor. */
092  public JavaInformationMonitoringPanel()
093  {
094    super();
095    createLayout();
096  }
097
098  @Override
099  public Component getPreferredFocusComponent()
100  {
101    return generalMonitoringComps.get(0);
102  }
103
104  /** Creates the layout of the panel (but the contents are not populated here). */
105  private void createLayout()
106  {
107    GridBagConstraints gbc = new GridBagConstraints();
108    JLabel lTitle = Utilities.createTitleLabel(
109        INFO_CTRL_PANEL_JAVA_INFORMATION.get());
110    gbc.fill = GridBagConstraints.NONE;
111    gbc.anchor = GridBagConstraints.WEST;
112    gbc.gridwidth = 2;
113    gbc.gridx = 0;
114    gbc.gridy = 0;
115    gbc.insets.top = 5;
116    gbc.insets.bottom = 7;
117    add(lTitle, gbc);
118
119    gbc.insets.bottom = 0;
120    gbc.insets.top = 10;
121    gbc.gridy ++;
122    gbc.anchor = GridBagConstraints.WEST;
123    gbc.gridwidth = 1;
124    for (int i=0; i<generalAttributes.size(); i++)
125    {
126      if (extraAttributes.contains(generalAttributes.get(i)))
127      {
128        continue;
129      }
130      JLabel l = Utilities.createPrimaryLabel(
131          getLabel(generalAttributes.get(i)));
132      gbc.gridy ++;
133      gbc.insets.left = 0;
134      gbc.insets.right = 0;
135      gbc.gridx = 0;
136      gbc.weightx = 0.0;
137      gbc.gridwidth = 1;
138      gbc.fill = GridBagConstraints.NONE;
139      boolean isTextComponent =
140        generalMonitoringComps.get(i) instanceof JTextComponent;
141      if (isTextComponent)
142      {
143        gbc.anchor = GridBagConstraints.NORTHWEST;
144      }
145      else
146      {
147        gbc.anchor = GridBagConstraints.WEST;
148      }
149      add(l, gbc);
150      gbc.insets.left = 10;
151      gbc.gridx = 1;
152      if (isTextComponent)
153      {
154        gbc.insets.right = 10;
155        gbc.weightx = 1.0;
156        gbc.fill = GridBagConstraints.BOTH;
157      }
158      else
159      {
160        gbc.weightx = 0.0;
161        gbc.fill = GridBagConstraints.HORIZONTAL;
162      }
163      add(generalMonitoringComps.get(i), gbc);
164    }
165
166    final BasicExpander extraExpander = new BasicExpander(
167        INFO_CTRL_PANEL_EXTRA_JAVA_ATTRIBUTES.get());
168    gbc.gridwidth = 2;
169    gbc.gridx = 0;
170    gbc.weighty = 0.0;
171    gbc.insets.left = 0;
172    gbc.weightx = 1.0;
173    gbc.fill = GridBagConstraints.BOTH;
174    gbc.gridy ++;
175    add(extraExpander, gbc);
176    final JPanel extraGeneralPanel = new JPanel(new GridBagLayout());
177    gbc.insets.left = 15;
178    gbc.gridy ++;
179    add(extraGeneralPanel, gbc);
180    extraGeneralPanel.setOpaque(false);
181    extraGeneralPanel.setVisible(false);
182
183    final BasicExpander memoryExpander = new BasicExpander(
184        INFO_CTRL_PANEL_JAVA_MEMORY_ATTRIBUTES.get());
185    gbc.gridy ++;
186    gbc.insets.left = 0;
187    add(memoryExpander, gbc);
188    memoryPanel = new JPanel(new GridBagLayout());
189    gbc.insets.left = 15;
190    gbc.gridy ++;
191    add(memoryPanel, gbc);
192    memoryPanel.setOpaque(false);
193    memoryPanel.setVisible(false);
194
195    GridBagConstraints gbc1 = new GridBagConstraints();
196    gbc1.fill = GridBagConstraints.HORIZONTAL;
197    gbc1.gridy = 0;
198    gbc1.gridx = 0;
199    gbc1.gridwidth = 1;
200
201    for (int i=0; i<extraAttributes.size(); i++)
202    {
203      int index = generalAttributes.indexOf(extraAttributes.get(i));
204      JLabel l = Utilities.createPrimaryLabel(
205          getLabel(extraAttributes.get(i)));
206      gbc1.insets.left = 0;
207      gbc1.insets.right = 0;
208      gbc1.gridx = 0;
209      gbc1.weightx = 0.0;
210      gbc1.gridwidth = 1;
211      gbc1.fill = GridBagConstraints.NONE;
212      boolean isTextComponent =
213        generalMonitoringComps.get(index) instanceof JTextComponent;
214      if (isTextComponent)
215      {
216        gbc1.anchor = GridBagConstraints.NORTHWEST;
217      }
218      else
219      {
220        gbc1.anchor = GridBagConstraints.WEST;
221      }
222      extraGeneralPanel.add(l, gbc1);
223      gbc1.insets.left = 10;
224      gbc1.gridx = 1;
225      if (isTextComponent)
226      {
227        gbc1.insets.right = 10;
228        gbc1.weightx = 1.0;
229        gbc1.fill = GridBagConstraints.BOTH;
230      }
231      else
232      {
233        gbc1.weightx = 1.0;
234        gbc1.fill = GridBagConstraints.HORIZONTAL;
235      }
236      extraGeneralPanel.add(generalMonitoringComps.get(index), gbc1);
237      gbc1.insets.top = 10;
238      gbc1.gridy ++;
239    }
240    ChangeListener changeListener = new ChangeListener()
241    {
242      @Override
243      public void stateChanged(ChangeEvent e)
244      {
245        extraGeneralPanel.setVisible(extraExpander.isSelected());
246      }
247    };
248    extraExpander.addChangeListener(changeListener);
249
250    changeListener = new ChangeListener()
251    {
252      @Override
253      public void stateChanged(ChangeEvent e)
254      {
255        memoryPanel.setVisible(memoryExpander.isSelected());
256      }
257    };
258    memoryExpander.addChangeListener(changeListener);
259
260    gbc.gridx = 0;
261    gbc.gridy ++;
262    gbc.fill = GridBagConstraints.BOTH;
263    gbc.weightx = 1.0;
264    gbc.weighty = 1.0;
265    gbc.gridwidth = 2;
266    add(Box.createGlue(), gbc);
267
268    setBorder(PANEL_BORDER);
269  }
270
271  @Override
272  public void updateContents()
273  {
274    ServerDescriptor server = null;
275    if (getInfo() != null)
276    {
277      server = getInfo().getServerDescriptor();
278    }
279    CustomSearchResult csrSystem = null;
280    CustomSearchResult csrMemory = null;
281    if (server != null)
282    {
283      csrSystem = server.getSystemInformationMonitor();
284      csrMemory = server.getJvmMemoryUsageMonitor();
285    }
286    if (csrSystem != null)
287    {
288      for (int i=0 ; i<generalAttributes.size(); i++)
289      {
290        String value =
291          getMonitoringValue(generalAttributes.get(i), csrSystem);
292        JComponent l = generalMonitoringComps.get(i);
293        if (l instanceof JLabel)
294        {
295          ((JLabel)l).setText(value);
296        }
297        else if (l instanceof JTextComponent)
298        {
299          ((JTextComponent)l).setText(value);
300        }
301        else
302        {
303          throw new RuntimeException("Unexpected component: "+l);
304        }
305      }
306    }
307    else
308    {
309      for (JComponent l : generalMonitoringComps)
310      {
311        if (l instanceof JLabel)
312        {
313          ((JLabel)l).setText(NO_VALUE_SET.toString());
314        }
315        else if (l instanceof JTextComponent)
316        {
317          ((JTextComponent)l).setText(NO_VALUE_SET.toString());
318        }
319        else
320        {
321          throw new RuntimeException("Unexpected component: "+l);
322        }
323      }
324    }
325    if (csrMemory != null)
326    {
327      if (memoryAttributes.isEmpty())
328      {
329        Set<String> allNames = csrMemory.getAttributeNames();
330        SortedSet<String> sortedNames = new TreeSet<>();
331        for (String attrName : allNames)
332        {
333          if (!OBJECTCLASS_ATTRIBUTE_TYPE_NAME.equalsIgnoreCase(attrName)
334              && !ATTR_COMMON_NAME.equalsIgnoreCase(attrName))
335          {
336            sortedNames.add(attrName);
337          }
338        }
339        memoryAttributes.addAll(sortedNames);
340
341        GridBagConstraints gbc = new GridBagConstraints();
342        gbc.gridx = 0;
343        gbc.gridy = 0;
344        gbc.anchor = GridBagConstraints.WEST;
345        gbc.gridwidth = 1;
346
347        for (String attrName : memoryAttributes)
348        {
349          JLabel l = Utilities.createPrimaryLabel(
350              INFO_CTRL_PANEL_OPERATION_NAME_AS_LABEL.get(attrName));
351          gbc.insets.left = 0;
352          gbc.insets.right = 0;
353          gbc.gridx = 0;
354          gbc.weightx = 0.0;
355          gbc.fill = GridBagConstraints.NONE;
356          memoryPanel.add(l, gbc);
357          gbc.insets.left = 10;
358          gbc.gridx = 1;
359          gbc.weightx = 1.0;
360          gbc.fill = GridBagConstraints.HORIZONTAL;
361          JLabel valueLabel = Utilities.createDefaultLabel();
362          memoryLabels.add(valueLabel);
363          memoryPanel.add(valueLabel, gbc);
364          gbc.gridy ++;
365          gbc.insets.top = 10;
366        }
367      }
368
369      for (int i=0; i<memoryAttributes.size() ; i++)
370      {
371        String value = getFirstValueAsString(csrMemory, memoryAttributes.get(i));
372        if (value != null)
373        {
374          memoryLabels.get(i).setText(value);
375        }
376        else
377        {
378          memoryLabels.get(i).setText(NO_VALUE_SET.toString());
379        }
380      }
381    }
382    else
383    {
384      for (JLabel l : memoryLabels)
385      {
386        l.setText(NO_VALUE_SET.toString());
387      }
388    }
389  }
390}