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