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