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