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 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui;
018
019import java.awt.Component;
020import java.awt.GridBagConstraints;
021import java.util.Date;
022
023import javax.swing.Box;
024import javax.swing.JLabel;
025
026import org.forgerock.i18n.LocalizableMessage;
027import org.opends.guitools.controlpanel.datamodel.BasicMonitoringAttributes;
028import org.opends.guitools.controlpanel.datamodel.CustomSearchResult;
029import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
030import org.opends.guitools.controlpanel.util.ConfigFromDirContext;
031import org.opends.guitools.controlpanel.util.Utilities;
032
033import static org.opends.guitools.controlpanel.datamodel.BasicMonitoringAttributes.*;
034import static org.opends.guitools.controlpanel.util.Utilities.*;
035import static org.opends.messages.AdminToolMessages.*;
036import static org.opends.messages.BackendMessages.*;
037
038/**
039 * The panel displaying the root monitor panel.
040 */
041public class RootMonitoringPanel extends GeneralMonitoringPanel
042{
043  private static final long serialVersionUID = 9031734563746269830L;
044
045  private JLabel openConnections = Utilities.createDefaultLabel();
046  private JLabel maxConnections = Utilities.createDefaultLabel();
047  private JLabel totalConnections = Utilities.createDefaultLabel();
048  private JLabel startTime = Utilities.createDefaultLabel();
049  private JLabel upTime = Utilities.createDefaultLabel();
050  private JLabel version = Utilities.createDefaultLabel();
051
052  /** Default constructor. */
053  public RootMonitoringPanel()
054  {
055    super();
056    createLayout();
057  }
058
059  /** {@inheritDoc} */
060  @Override
061  public Component getPreferredFocusComponent()
062  {
063    return openConnections;
064  }
065
066  /**
067   * Creates the layout of the panel (but the contents are not populated here).
068   */
069  private void createLayout()
070  {
071    GridBagConstraints gbc = new GridBagConstraints();
072    gbc.gridy ++;
073    JLabel lTitle = Utilities.createTitleLabel(
074        INFO_CTRL_PANEL_GENERAL_MONITORING_ROOT.get());
075    gbc.fill = GridBagConstraints.NONE;
076    gbc.anchor = GridBagConstraints.WEST;
077    gbc.gridwidth = 2;
078    gbc.gridy = 0;
079    gbc.insets.top = 5;
080    gbc.insets.bottom = 7;
081    add(lTitle, gbc);
082
083    gbc.insets.bottom = 0;
084    gbc.insets.top = 10;
085    LocalizableMessage[] labels = {
086        INFO_CTRL_PANEL_OPEN_CONNECTIONS_LABEL.get(),
087        INFO_CTRL_PANEL_MAX_CONNECTIONS_LABEL.get(),
088        INFO_CTRL_PANEL_TOTAL_CONNECTIONS_LABEL.get(),
089        INFO_CTRL_PANEL_START_TIME_LABEL.get(),
090        INFO_CTRL_PANEL_UP_TIME_LABEL.get(),
091        INFO_CTRL_PANEL_OPENDS_VERSION_LABEL.get()
092    };
093    JLabel[] values = {
094        openConnections,
095        maxConnections,
096        totalConnections,
097        startTime,
098        upTime,
099        version
100        };
101    gbc.gridy ++;
102    gbc.anchor = GridBagConstraints.WEST;
103    gbc.gridwidth = 1;
104    for (int i=0; i < labels.length; i++)
105    {
106      gbc.insets.left = 0;
107      gbc.gridx = 0;
108      JLabel l = Utilities.createPrimaryLabel(labels[i]);
109      add(l, gbc);
110      gbc.insets.left = 10;
111      gbc.gridx = 1;
112      add(values[i], gbc);
113      gbc.gridy ++;
114    }
115
116    gbc.gridwidth = 2;
117    gbc.gridx = 0;
118    gbc.gridy ++;
119    gbc.fill = GridBagConstraints.BOTH;
120    gbc.weightx = 1.0;
121    gbc.weighty = 1.0;
122    add(Box.createGlue(), gbc);
123
124    setBorder(PANEL_BORDER);
125  }
126
127  /**
128   * Updates the contents of the panel.
129   *
130   */
131  public void updateContents()
132  {
133    ServerDescriptor server = null;
134    if (getInfo() != null)
135    {
136      server = getInfo().getServerDescriptor();
137    }
138    CustomSearchResult csr = null;
139    if (server != null)
140    {
141      csr = server.getRootMonitor();
142    }
143    if (csr != null)
144    {
145      JLabel[] ls =
146      {
147          openConnections,
148          maxConnections,
149          totalConnections,
150          startTime
151      };
152      BasicMonitoringAttributes[] attrs =
153      {
154        BasicMonitoringAttributes.CURRENT_CONNECTIONS,
155        BasicMonitoringAttributes.MAX_CONNECTIONS,
156        BasicMonitoringAttributes.TOTAL_CONNECTIONS,
157        BasicMonitoringAttributes.START_DATE
158      };
159      for (int i=0; i<ls.length; i++)
160      {
161        ls[i].setText(getMonitoringValue(attrs[i], csr));
162      }
163      version.setText(server.getOpenDSVersion());
164      try
165      {
166        String start = getFirstValueAsString(csr, START_DATE.getAttributeName());
167        String current = getFirstValueAsString(csr, CURRENT_DATE.getAttributeName());
168        Date startTime = ConfigFromDirContext.utcParser.parse(start);
169        Date currentTime = ConfigFromDirContext.utcParser.parse(current);
170
171        long upSeconds = (currentTime.getTime() - startTime.getTime()) / 1000;
172        long upDays = upSeconds / 86400;
173        upSeconds %= 86400;
174        long upHours = upSeconds / 3600;
175        upSeconds %= 3600;
176        long upMinutes = upSeconds / 60;
177        upSeconds %= 60;
178        LocalizableMessage upTimeStr =
179          INFO_MONITOR_UPTIME.get(upDays, upHours, upMinutes, upSeconds);
180
181        upTime.setText(upTimeStr.toString());
182      }
183      catch (Throwable t)
184      {
185        upTime.setText(NO_VALUE_SET.toString());
186      }
187    }
188    else
189    {
190      openConnections.setText(NO_VALUE_SET.toString());
191      maxConnections.setText(NO_VALUE_SET.toString());
192      totalConnections.setText(NO_VALUE_SET.toString());
193      startTime.setText(NO_VALUE_SET.toString());
194      upTime.setText(NO_VALUE_SET.toString());
195      version.setText(NO_VALUE_SET.toString());
196    }
197  }
198}