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 2008-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS 026 */ 027 028package org.opends.guitools.controlpanel.datamodel; 029 030import static org.opends.messages.AdminToolMessages.*; 031 032import java.net.InetAddress; 033import java.util.ArrayList; 034import java.util.Comparator; 035import java.util.HashSet; 036import java.util.Set; 037import java.util.TreeSet; 038 039/** 040 * The table model used by the table that displays the connection handlers. 041 * 042 */ 043public class ConnectionHandlerTableModel extends SortableTableModel 044implements Comparator<ConnectionHandlerDescriptor> 045{ 046 private static final long serialVersionUID = -1121308303480078376L; 047 private Set<ConnectionHandlerDescriptor> data = new HashSet<>(); 048 private ArrayList<String[]> dataArray = new ArrayList<>(); 049 private String[] COLUMN_NAMES; 050 private int sortColumn; 051 private boolean sortAscending = true; 052 053 /** 054 * Constructor for this table model. 055 */ 056 public ConnectionHandlerTableModel() 057 { 058 this(true); 059 } 060 061 /** 062 * Constructor for this table model. 063 * @param wrapHeader whether to wrap the headers or not. 064 * monitoring information or not. 065 */ 066 public ConnectionHandlerTableModel(boolean wrapHeader) 067 { 068 if (wrapHeader) 069 { 070 COLUMN_NAMES = new String[] { 071 getHeader(INFO_ADDRESS_PORT_COLUMN.get()), 072 getHeader(INFO_PROTOCOL_COLUMN.get()), 073 getHeader(INFO_STATE_COLUMN.get()) 074 }; 075 } 076 else 077 { 078 COLUMN_NAMES = new String[] { 079 INFO_ADDRESS_PORT_COLUMN.get().toString(), 080 INFO_PROTOCOL_COLUMN.get().toString(), 081 INFO_STATE_COLUMN.get().toString() 082 }; 083 } 084 } 085 086 /** 087 * Sets the data for this table model. 088 * @param newData the data for this table model. 089 */ 090 public void setData(Set<ConnectionHandlerDescriptor> newData) 091 { 092 if (!newData.equals(data)) 093 { 094 data.clear(); 095 data.addAll(newData); 096 updateDataArray(); 097 fireTableDataChanged(); 098 } 099 } 100 101 /** 102 * Updates the table model contents and sorts its contents depending on the 103 * sort options set by the user. 104 */ 105 public void forceResort() 106 { 107 updateDataArray(); 108 fireTableDataChanged(); 109 } 110 111 /** 112 * Comparable implementation. 113 * @param desc1 the first listener descriptor to compare. 114 * @param desc2 the second listener descriptor to compare. 115 * @return 1 if according to the sorting options set by the user the first 116 * listener descriptor must be put before the second descriptor, 0 if they 117 * are equivalent in terms of sorting and -1 if the second descriptor must 118 * be put before the first descriptor. 119 */ 120 public int compare(ConnectionHandlerDescriptor desc1, 121 ConnectionHandlerDescriptor desc2) 122 { 123 int result = 0; 124 if (sortColumn == 0) 125 { 126 if (desc1.getAddresses().equals(desc2.getAddresses())) 127 { 128 Integer port1 = Integer.valueOf(desc1.getPort()); 129 Integer port2 = Integer.valueOf(desc2.getPort()); 130 result = port1.compareTo(port2); 131 } 132 else 133 { 134 result = getAddressPortString(desc1).compareTo( 135 getAddressPortString(desc2)); 136 } 137 if (result == 0) 138 { 139 result = getProtocolString(desc1).compareTo( 140 getProtocolString(desc2)); 141 } 142 143 if (result == 0) 144 { 145 result = desc1.getState().compareTo(desc2.getState()); 146 } 147 } 148 else if (sortColumn == 1) 149 { 150 result = getProtocolString(desc1).compareTo( 151 getProtocolString(desc2)); 152 153 if (result == 0) 154 { 155 result = getAddressPortString(desc1).compareTo( 156 getAddressPortString(desc2)); 157 } 158 159 if (result == 0) 160 { 161 result = desc1.getState().compareTo(desc2.getState()); 162 } 163 } 164 else 165 { 166 result = desc1.getState().compareTo(desc2.getState()); 167 168 if (result == 0) 169 { 170 result = getAddressPortString(desc1).compareTo( 171 getAddressPortString(desc2)); 172 } 173 174 if (result == 0) 175 { 176 result = getProtocolString(desc1).compareTo( 177 getProtocolString(desc2)); 178 } 179 } 180 181 if (!sortAscending) 182 { 183 result = -result; 184 } 185 186 return result; 187 } 188 189 /** {@inheritDoc} */ 190 public int getColumnCount() 191 { 192 return 3; 193 } 194 195 /** {@inheritDoc} */ 196 public int getRowCount() 197 { 198 return dataArray.size(); 199 } 200 201 /** {@inheritDoc} */ 202 public Object getValueAt(int row, int col) 203 { 204 return dataArray.get(row)[col]; 205 } 206 207 /** {@inheritDoc} */ 208 public String getColumnName(int col) { 209 return COLUMN_NAMES[col]; 210 } 211 212 213 /** 214 * Returns whether the sort is ascending or descending. 215 * @return <CODE>true</CODE> if the sort is ascending and <CODE>false</CODE> 216 * otherwise. 217 */ 218 public boolean isSortAscending() 219 { 220 return sortAscending; 221 } 222 223 /** 224 * Sets whether to sort ascending of descending. 225 * @param sortAscending whether to sort ascending or descending. 226 */ 227 public void setSortAscending(boolean sortAscending) 228 { 229 this.sortAscending = sortAscending; 230 } 231 232 /** 233 * Returns the column index used to sort. 234 * @return the column index used to sort. 235 */ 236 public int getSortColumn() 237 { 238 return sortColumn; 239 } 240 241 /** 242 * Sets the column index used to sort. 243 * @param sortColumn column index used to sort.. 244 */ 245 public void setSortColumn(int sortColumn) 246 { 247 this.sortColumn = sortColumn; 248 } 249 250 private String getAddressPortString(ConnectionHandlerDescriptor desc) 251 { 252 Set<InetAddress> addresses = desc.getAddresses(); 253 if (!addresses.isEmpty()) 254 { 255 StringBuilder buf = new StringBuilder(); 256 buf.append("<html>"); 257 boolean added = false; 258 for (InetAddress address : addresses) 259 { 260 if (added) 261 { 262 buf.append("<br>"); 263 } 264 buf.append(address.getHostAddress()); 265 added = true; 266 if (desc.getPort() > 0) 267 { 268 buf.append(":").append(desc.getPort()); 269 } 270 } 271 return buf.toString(); 272 } 273 274 if (desc.getPort() > 0) 275 { 276 return String.valueOf(desc.getPort()); 277 } 278 return INFO_NOT_APPLICABLE_LABEL.get().toString(); 279 } 280 281 private String getProtocolString(ConnectionHandlerDescriptor desc) 282 { 283 String returnValue; 284 switch (desc.getProtocol()) 285 { 286 case OTHER: 287 returnValue = desc.getName(); 288 break; 289 default: 290 returnValue = desc.getProtocol().getDisplayMessage().toString(); 291 break; 292 } 293 return returnValue; 294 } 295 296 private void updateDataArray() 297 { 298 TreeSet<ConnectionHandlerDescriptor> sortedSet = new TreeSet<>(this); 299 sortedSet.addAll(data); 300 dataArray.clear(); 301 for (ConnectionHandlerDescriptor desc : sortedSet) 302 { 303 String[] s = new String[3]; 304 s[0] = getAddressPortString(desc); 305 s[1] = getProtocolString(desc); 306 307 switch (desc.getState()) 308 { 309 case ENABLED: 310 s[2] = INFO_ENABLED_LABEL.get().toString(); 311 break; 312 313 case DISABLED: 314 s[2] = INFO_DISABLED_LABEL.get().toString(); 315 break; 316 317 case UNKNOWN: 318 s[2] = INFO_UNKNOWN_LABEL.get().toString(); 319 break; 320 321 default: 322 throw new RuntimeException("Unknown state: "+desc.getState()); 323 } 324 dataArray.add(s); 325 } 326 } 327}