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-2015 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;
043
044import static org.opends.guitools.controlpanel.util.Utilities.*;
045import static org.opends.messages.AdminToolMessages.*;
046import static org.opends.server.util.ServerConstants.*;
047
048/**
049 * The panel displaying the java monitoring information.
050 */
051public class JavaInformationMonitoringPanel extends GeneralMonitoringPanel
052{
053  private static final long serialVersionUID = 9031734563799969830L;
054  private List<BasicMonitoringAttributes> generalAttributes = new ArrayList<>();
055  {
056    generalAttributes.add(BasicMonitoringAttributes.JVM_VERSION);
057    generalAttributes.add(BasicMonitoringAttributes.JVM_VENDOR);
058    generalAttributes.add(BasicMonitoringAttributes.JVM_ARCHITECTURE);
059    generalAttributes.add(BasicMonitoringAttributes.JVM_ARGUMENTS);
060    generalAttributes.add(BasicMonitoringAttributes.CLASS_PATH);
061    generalAttributes.add(BasicMonitoringAttributes.JAVA_VERSION);
062    generalAttributes.add(BasicMonitoringAttributes.JAVA_VENDOR);
063  }
064  private List<BasicMonitoringAttributes> extraAttributes = new ArrayList<>();
065  {
066    extraAttributes.add(BasicMonitoringAttributes.CLASS_PATH);
067    extraAttributes.add(BasicMonitoringAttributes.JAVA_VERSION);
068    extraAttributes.add(BasicMonitoringAttributes.JAVA_VENDOR);
069  }
070  private ArrayList<JComponent> generalMonitoringComps = new ArrayList<>();
071  {
072    for (int i=0; i<generalAttributes.size(); i++)
073    {
074      if (generalAttributes.get(i) == BasicMonitoringAttributes.CLASS_PATH ||
075          generalAttributes.get(i) == BasicMonitoringAttributes.JVM_ARGUMENTS)
076      {
077        JEditorPane pane = new JEditorPane();
078        pane.setEditable(false);
079        pane.setBorder(new EmptyBorder(0, 0, 0, 0));
080        pane.setOpaque(false);
081        pane.setFocusCycleRoot(false);
082        generalMonitoringComps.add(pane);
083      }
084      else
085      {
086        generalMonitoringComps.add(Utilities.createDefaultLabel());
087      }
088    }
089  }
090
091  private List<String> memoryAttributes = new ArrayList<>();
092  private List<JLabel> memoryLabels = new ArrayList<>();
093  private JPanel memoryPanel;
094
095  /**
096   * Default constructor.
097   */
098  public JavaInformationMonitoringPanel()
099  {
100    super();
101    createLayout();
102  }
103
104  /** {@inheritDoc} */
105  @Override
106  public Component getPreferredFocusComponent()
107  {
108    return generalMonitoringComps.get(0);
109  }
110
111  /**
112   * Creates the layout of the panel (but the contents are not populated here).
113   */
114  private void createLayout()
115  {
116    GridBagConstraints gbc = new GridBagConstraints();
117    JLabel lTitle = Utilities.createTitleLabel(
118        INFO_CTRL_PANEL_JAVA_INFORMATION.get());
119    gbc.fill = GridBagConstraints.NONE;
120    gbc.anchor = GridBagConstraints.WEST;
121    gbc.gridwidth = 2;
122    gbc.gridx = 0;
123    gbc.gridy = 0;
124    gbc.insets.top = 5;
125    gbc.insets.bottom = 7;
126    add(lTitle, gbc);
127
128    gbc.insets.bottom = 0;
129    gbc.insets.top = 10;
130    gbc.gridy ++;
131    gbc.anchor = GridBagConstraints.WEST;
132    gbc.gridwidth = 1;
133    for (int i=0; i<generalAttributes.size(); i++)
134    {
135      if (extraAttributes.contains(generalAttributes.get(i)))
136      {
137        continue;
138      }
139      JLabel l = Utilities.createPrimaryLabel(
140          getLabel(generalAttributes.get(i)));
141      gbc.gridy ++;
142      gbc.insets.left = 0;
143      gbc.insets.right = 0;
144      gbc.gridx = 0;
145      gbc.weightx = 0.0;
146      gbc.gridwidth = 1;
147      gbc.fill = GridBagConstraints.NONE;
148      boolean isTextComponent =
149        generalMonitoringComps.get(i) instanceof JTextComponent;
150      if (isTextComponent)
151      {
152        gbc.anchor = GridBagConstraints.NORTHWEST;
153      }
154      else
155      {
156        gbc.anchor = GridBagConstraints.WEST;
157      }
158      add(l, gbc);
159      gbc.insets.left = 10;
160      gbc.gridx = 1;
161      if (isTextComponent)
162      {
163        gbc.insets.right = 10;
164        gbc.weightx = 1.0;
165        gbc.fill = GridBagConstraints.BOTH;
166      }
167      else
168      {
169        gbc.weightx = 0.0;
170        gbc.fill = GridBagConstraints.HORIZONTAL;
171      }
172      add(generalMonitoringComps.get(i), gbc);
173    }
174
175    final BasicExpander extraExpander = new BasicExpander(
176        INFO_CTRL_PANEL_EXTRA_JAVA_ATTRIBUTES.get());
177    gbc.gridwidth = 2;
178    gbc.gridx = 0;
179    gbc.weighty = 0.0;
180    gbc.insets.left = 0;
181    gbc.weightx = 1.0;
182    gbc.fill = GridBagConstraints.BOTH;
183    gbc.gridy ++;
184    add(extraExpander, gbc);
185    final JPanel extraGeneralPanel = new JPanel(new GridBagLayout());
186    gbc.insets.left = 15;
187    gbc.gridy ++;
188    add(extraGeneralPanel, gbc);
189    extraGeneralPanel.setOpaque(false);
190    extraGeneralPanel.setVisible(false);
191
192    final BasicExpander memoryExpander = new BasicExpander(
193        INFO_CTRL_PANEL_JAVA_MEMORY_ATTRIBUTES.get());
194    gbc.gridy ++;
195    gbc.insets.left = 0;
196    add(memoryExpander, gbc);
197    memoryPanel = new JPanel(new GridBagLayout());
198    gbc.insets.left = 15;
199    gbc.gridy ++;
200    add(memoryPanel, gbc);
201    memoryPanel.setOpaque(false);
202    memoryPanel.setVisible(false);
203
204    GridBagConstraints gbc1 = new GridBagConstraints();
205    gbc1.fill = GridBagConstraints.HORIZONTAL;
206    gbc1.gridy = 0;
207    gbc1.gridx = 0;
208    gbc1.gridwidth = 1;
209
210    for (int i=0; i<extraAttributes.size(); i++)
211    {
212      int index = generalAttributes.indexOf(extraAttributes.get(i));
213      JLabel l = Utilities.createPrimaryLabel(
214          getLabel(extraAttributes.get(i)));
215      gbc1.insets.left = 0;
216      gbc1.insets.right = 0;
217      gbc1.gridx = 0;
218      gbc1.weightx = 0.0;
219      gbc1.gridwidth = 1;
220      gbc1.fill = GridBagConstraints.NONE;
221      boolean isTextComponent =
222        generalMonitoringComps.get(index) instanceof JTextComponent;
223      if (isTextComponent)
224      {
225        gbc1.anchor = GridBagConstraints.NORTHWEST;
226      }
227      else
228      {
229        gbc1.anchor = GridBagConstraints.WEST;
230      }
231      extraGeneralPanel.add(l, gbc1);
232      gbc1.insets.left = 10;
233      gbc1.gridx = 1;
234      if (isTextComponent)
235      {
236        gbc1.insets.right = 10;
237        gbc1.weightx = 1.0;
238        gbc1.fill = GridBagConstraints.BOTH;
239      }
240      else
241      {
242        gbc1.weightx = 1.0;
243        gbc1.fill = GridBagConstraints.HORIZONTAL;
244      }
245      extraGeneralPanel.add(generalMonitoringComps.get(index), gbc1);
246      gbc1.insets.top = 10;
247      gbc1.gridy ++;
248    }
249    ChangeListener changeListener = new ChangeListener()
250    {
251      /** {@inheritDoc} */
252      @Override
253      public void stateChanged(ChangeEvent e)
254      {
255        extraGeneralPanel.setVisible(extraExpander.isSelected());
256      }
257    };
258    extraExpander.addChangeListener(changeListener);
259
260    changeListener = new ChangeListener()
261    {
262      /** {@inheritDoc} */
263      @Override
264      public void stateChanged(ChangeEvent e)
265      {
266        memoryPanel.setVisible(memoryExpander.isSelected());
267      }
268    };
269    memoryExpander.addChangeListener(changeListener);
270
271    gbc.gridx = 0;
272    gbc.gridy ++;
273    gbc.fill = GridBagConstraints.BOTH;
274    gbc.weightx = 1.0;
275    gbc.weighty = 1.0;
276    gbc.gridwidth = 2;
277    add(Box.createGlue(), gbc);
278
279    setBorder(PANEL_BORDER);
280  }
281
282  /**
283   * Updates the contents of the panel.
284   *
285   */
286  public void updateContents()
287  {
288    ServerDescriptor server = null;
289    if (getInfo() != null)
290    {
291      server = getInfo().getServerDescriptor();
292    }
293    CustomSearchResult csrSystem = null;
294    CustomSearchResult csrMemory = null;
295    if (server != null)
296    {
297      csrSystem = server.getSystemInformationMonitor();
298      csrMemory = server.getJvmMemoryUsageMonitor();
299    }
300    if (csrSystem != null)
301    {
302      for (int i=0 ; i<generalAttributes.size(); i++)
303      {
304        String value =
305          getMonitoringValue(generalAttributes.get(i), csrSystem);
306        JComponent l = generalMonitoringComps.get(i);
307        if (l instanceof JLabel)
308        {
309          ((JLabel)l).setText(value);
310        }
311        else if (l instanceof JTextComponent)
312        {
313          ((JTextComponent)l).setText(value);
314        }
315        else
316        {
317          throw new RuntimeException("Unexpected component: "+l);
318        }
319      }
320    }
321    else
322    {
323      for (JComponent l : generalMonitoringComps)
324      {
325        if (l instanceof JLabel)
326        {
327          ((JLabel)l).setText(NO_VALUE_SET.toString());
328        }
329        else if (l instanceof JTextComponent)
330        {
331          ((JTextComponent)l).setText(NO_VALUE_SET.toString());
332        }
333        else
334        {
335          throw new RuntimeException("Unexpected component: "+l);
336        }
337      }
338    }
339    if (csrMemory != null)
340    {
341      if (memoryAttributes.isEmpty())
342      {
343        Set<String> allNames = csrMemory.getAttributeNames();
344        SortedSet<String> sortedNames = new TreeSet<>();
345        for (String attrName : allNames)
346        {
347          if (!OBJECTCLASS_ATTRIBUTE_TYPE_NAME.equalsIgnoreCase(attrName)
348              && !ATTR_COMMON_NAME.equalsIgnoreCase(attrName))
349          {
350            sortedNames.add(attrName);
351          }
352        }
353        memoryAttributes.addAll(sortedNames);
354
355        GridBagConstraints gbc = new GridBagConstraints();
356        gbc.gridx = 0;
357        gbc.gridy = 0;
358        gbc.anchor = GridBagConstraints.WEST;
359        gbc.gridwidth = 1;
360
361        for (String attrName : memoryAttributes)
362        {
363          JLabel l = Utilities.createPrimaryLabel(
364              INFO_CTRL_PANEL_OPERATION_NAME_AS_LABEL.get(attrName));
365          gbc.insets.left = 0;
366          gbc.insets.right = 0;
367          gbc.gridx = 0;
368          gbc.weightx = 0.0;
369          gbc.fill = GridBagConstraints.NONE;
370          memoryPanel.add(l, gbc);
371          gbc.insets.left = 10;
372          gbc.gridx = 1;
373          gbc.weightx = 1.0;
374          gbc.fill = GridBagConstraints.HORIZONTAL;
375          JLabel valueLabel = Utilities.createDefaultLabel();
376          memoryLabels.add(valueLabel);
377          memoryPanel.add(valueLabel, gbc);
378          gbc.gridy ++;
379          gbc.insets.top = 10;
380        }
381      }
382
383      for (int i=0; i<memoryAttributes.size() ; i++)
384      {
385        String value = getFirstValueAsString(csrMemory, memoryAttributes.get(i));
386        if (value != null)
387        {
388          memoryLabels.get(i).setText(value);
389        }
390        else
391        {
392          memoryLabels.get(i).setText(NO_VALUE_SET.toString());
393        }
394      }
395    }
396    else
397    {
398      for (JLabel l : memoryLabels)
399      {
400        l.setText(NO_VALUE_SET.toString());
401      }
402    }
403  }
404}