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.datamodel; 028 029import java.net.InetAddress; 030import java.util.ArrayList; 031import java.util.HashSet; 032import java.util.Objects; 033import java.util.Set; 034 035import org.forgerock.i18n.LocalizableMessage; 036import org.opends.guitools.controlpanel.datamodel.ConnectionHandlerDescriptor.Protocol; 037 038import static org.opends.guitools.controlpanel.util.Utilities.*; 039import static org.opends.messages.AdminToolMessages.*; 040 041/** 042 * The table model used to display the monitoring information of connection 043 * handlers. 044 */ 045public class ConnectionHandlersMonitoringTableModel extends 046MonitoringTableModel<ConnectionHandlerDescriptor, 047AddressConnectionHandlerDescriptor> 048{ 049 private static final long serialVersionUID = -8891998773191495L; 050 051 /** {@inheritDoc} */ 052 @Override 053 protected Set<AddressConnectionHandlerDescriptor> convertToInternalData( 054 Set<ConnectionHandlerDescriptor> newData) 055 { 056 Set<AddressConnectionHandlerDescriptor> newAddresses = new HashSet<>(); 057 for (ConnectionHandlerDescriptor ch : newData) 058 { 059 if (ch.getAddresses().isEmpty()) 060 { 061 newAddresses.add(new AddressConnectionHandlerDescriptor(ch, null, 062 getMonitoringEntry(null, ch))); 063 } 064 else 065 { 066 for (InetAddress address : ch.getAddresses()) 067 { 068 newAddresses.add(new AddressConnectionHandlerDescriptor(ch, address, 069 getMonitoringEntry(address, ch))); 070 } 071 } 072 } 073 return newAddresses; 074 } 075 076 /** {@inheritDoc} */ 077 @Override 078 public int compare(AddressConnectionHandlerDescriptor desc1, 079 AddressConnectionHandlerDescriptor desc2) 080 { 081 ArrayList<Integer> possibleResults = new ArrayList<>(); 082 083 possibleResults.add(compareNames(desc1, desc2)); 084 possibleResults.addAll(getMonitoringPossibleResults( 085 desc1.getMonitoringEntry(), desc2.getMonitoringEntry())); 086 087 int result = possibleResults.get(getSortColumn()); 088 if (result == 0) 089 { 090 for (int i : possibleResults) 091 { 092 if (i != 0) 093 { 094 result = i; 095 break; 096 } 097 } 098 } 099 if (!isSortAscending()) 100 { 101 result = -result; 102 } 103 return result; 104 } 105 106 private int compareNames(AddressConnectionHandlerDescriptor ach1, 107 AddressConnectionHandlerDescriptor ach2) 108 { 109 if (Objects.equals(ach1.getAddress(), ach2.getAddress())) 110 { 111 Integer port1 = Integer.valueOf(ach1.getConnectionHandler().getPort()); 112 Integer port2 = Integer.valueOf(ach2.getConnectionHandler().getPort()); 113 return port1.compareTo(port2); 114 } 115 return getName(ach1).compareTo(getName(ach2)); 116 } 117 118 /** {@inheritDoc} */ 119 @Override 120 protected CustomSearchResult getMonitoringEntry( 121 AddressConnectionHandlerDescriptor ach) 122 { 123 return ach.getMonitoringEntry(); 124 } 125 126 /** {@inheritDoc} */ 127 @Override 128 protected String getName(AddressConnectionHandlerDescriptor ach) 129 { 130 StringBuilder sb = new StringBuilder(); 131 ConnectionHandlerDescriptor ch = ach.getConnectionHandler(); 132 if (ch.getProtocol() == Protocol.ADMINISTRATION_CONNECTOR) 133 { 134 sb.append(INFO_CTRL_PANEL_ADMINISTRATION_CONNECTOR_NAME.get(ch.getPort())); 135 } 136 else 137 { 138 if (ach.getAddress() != null) 139 { 140 sb.append(ach.getAddress().getHostAddress()).append(":").append(ch.getPort()); 141 } 142 else 143 { 144 sb.append(ch.getPort()); 145 } 146 sb.append(" - "); 147 switch (ch.getProtocol()) 148 { 149 case OTHER: 150 sb.append(ch.getName()); 151 break; 152 default: 153 sb.append(ch.getProtocol().getDisplayMessage()); 154 break; 155 } 156 } 157 return sb.toString(); 158 } 159 160 private CustomSearchResult getMonitoringEntry(InetAddress address, 161 ConnectionHandlerDescriptor cch) 162 { 163 for (CustomSearchResult sr : cch.getMonitoringEntries()) 164 { 165 String cn = getFirstValueAsString(sr, "cn"); 166 if (cn != null) 167 { 168 if (address == null) 169 { 170 return sr; 171 } 172 if (cn.endsWith(" " + address.getHostAddress() + " port " + cch.getPort() + " Statistics")) 173 { 174 return sr; 175 } 176 } 177 } 178 return null; 179 } 180 181 /** {@inheritDoc} */ 182 @Override 183 protected LocalizableMessage getNameHeader() 184 { 185 return INFO_CTRL_PANEL_CONNECTION_HANDLER_HEADER.get(); 186 } 187} 188 189/** 190 * The table model has one line per address, this object represents that 191 * address and all the associated monitoring information. 192 * 193 */ 194class AddressConnectionHandlerDescriptor 195{ 196 private ConnectionHandlerDescriptor ch; 197 private InetAddress address; 198 private CustomSearchResult monitoringEntry; 199 private int hashCode; 200 201 /** 202 * Constructor of this data structure. 203 * @param ch the connection handler descriptor. 204 * @param address the address. 205 * @param monitoringEntry the monitoringEntry. 206 */ 207 public AddressConnectionHandlerDescriptor( 208 ConnectionHandlerDescriptor ch, 209 InetAddress address, 210 CustomSearchResult monitoringEntry) 211 { 212 this.ch = ch; 213 this.address = address; 214 this.monitoringEntry = monitoringEntry; 215 216 if (address != null) 217 { 218 hashCode = ch.hashCode() + address.hashCode(); 219 } 220 else 221 { 222 hashCode = ch.hashCode(); 223 } 224 } 225 226 /** 227 * Returns the address. 228 * @return the address. 229 */ 230 public InetAddress getAddress() 231 { 232 return address; 233 } 234 235 /** 236 * Returns the connection handler descriptor. 237 * @return the connection handler descriptor. 238 */ 239 public ConnectionHandlerDescriptor getConnectionHandler() 240 { 241 return ch; 242 } 243 244 /** 245 * Returns the monitoring entry. 246 * @return the monitoring entry. 247 */ 248 public CustomSearchResult getMonitoringEntry() 249 { 250 return monitoringEntry; 251 } 252 253 /** {@inheritDoc} */ 254 @Override 255 public int hashCode() 256 { 257 return hashCode; 258 } 259 260 /** {@inheritDoc} */ 261 @Override 262 public boolean equals(Object o) 263 { 264 if (o != this) 265 { 266 return true; 267 } 268 if (!(o instanceof AddressConnectionHandlerDescriptor)) 269 { 270 return false; 271 } 272 AddressConnectionHandlerDescriptor ach = (AddressConnectionHandlerDescriptor) o; 273 return Objects.equals(getAddress(), ach.getAddress()) 274 && ach.getConnectionHandler().equals(getConnectionHandler()); 275 } 276}