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