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 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.datamodel;
018
019import static org.opends.messages.AdminToolMessages.*;
020
021import java.net.InetAddress;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.Set;
025import java.util.SortedSet;
026import java.util.TreeSet;
027
028import org.forgerock.i18n.LocalizableMessage;
029import org.forgerock.opendj.server.config.meta.AdministrationConnectorCfgDefn;
030
031/**
032 * This class is used to represent a Listener and is aimed to be used by the
033 * classes in the ListenersTableModel class.
034 */
035public class ConnectionHandlerDescriptor
036{
037  private Set<CustomSearchResult> monitoringEntries = Collections.emptySet();
038
039  /** Enumeration used to represent the state of the listener. */
040  public enum State
041  {
042    /** The listener is enabled. */
043    ENABLED,
044    /** The listener is disabled. */
045    DISABLED,
046    /** The state of the listener is unknown. */
047    UNKNOWN
048  }
049
050  /** Enumeration used to represent the Protocol of the listener. */
051  public enum Protocol
052  {
053    /** LDAP protocol. */
054    LDAP(INFO_CTRL_PANEL_CONN_HANDLER_LDAP.get()),
055    /** LDAP accepting Start TLS protocol. */
056    LDAP_STARTTLS(INFO_CTRL_PANEL_CONN_HANDLER_LDAP_STARTTLS.get()),
057    /** LDAP secure protocol. */
058    LDAPS(INFO_CTRL_PANEL_CONN_HANDLER_LDAPS.get()),
059    /** HTTP protocol. */
060    HTTP(INFO_CTRL_PANEL_CONN_HANDLER_HTTP.get()),
061    /** HTTP secure protocol. */
062    HTTPS(INFO_CTRL_PANEL_CONN_HANDLER_HTTPS.get()),
063    /** JMX protocol. */
064    JMX(INFO_CTRL_PANEL_CONN_HANDLER_JMX.get()),
065    /** JMX secure protocol. */
066    JMXS(INFO_CTRL_PANEL_CONN_HANDLER_JMXS.get()),
067    /** LDIF protocol. */
068    LDIF(INFO_CTRL_PANEL_CONN_HANDLER_LDIF.get()),
069    /** SNMP protocol. */
070    SNMP(INFO_CTRL_PANEL_CONN_HANDLER_SNMP.get()),
071    /**
072     * Replication protocol.  Even if in the configuration is not considered
073     * as a listener, we display it on the table.
074     */
075    REPLICATION(INFO_CTRL_PANEL_CONN_HANDLER_REPLICATION.get()),
076    /** Secure replication protocol. */
077    REPLICATION_SECURE(INFO_CTRL_PANEL_CONN_HANDLER_REPLICATION_SECURE.get()),
078    /** Admin connector protocol. */
079    ADMINISTRATION_CONNECTOR(INFO_CTRL_PANEL_CONN_HANDLER_ADMINISTRATION.get()),
080    /** Other protocol. */
081    OTHER(INFO_CTRL_PANEL_CONN_HANDLER_OTHER.get());
082
083    private LocalizableMessage displayMessage;
084
085    private Protocol(LocalizableMessage displayMessage)
086    {
087      this.displayMessage = displayMessage;
088    }
089
090    /**
091     * Returns the display LocalizableMessage to be used for the protocol.
092     * @return the display LocalizableMessage to be used for the protocol.
093     */
094    public LocalizableMessage getDisplayMessage()
095    {
096      return displayMessage;
097    }
098  }
099
100  private State state;
101  private SortedSet<InetAddress> addresses = new TreeSet<>(
102      AdministrationConnectorCfgDefn.getInstance().
103      getListenAddressPropertyDefinition());
104  private int port;
105  private Protocol protocol;
106  private String toString;
107  private String name;
108
109  private int hashCode;
110
111  /**
112   * Constructor for the connection handler..
113   * @param addresses the list of InetAdresses of the listener.
114   * @param port the port of the connection handler.
115   * @param protocol the protocol of the listener.
116   * @param state the state of the connection handler (enabled, disabled, etc.).
117   * @param name the name of the listener.
118   * @param monitoringEntries the LDAP entries containing the monitoring
119   * information.
120   */
121  public ConnectionHandlerDescriptor(Collection<InetAddress> addresses,
122      int port, Protocol protocol, State state, String name,
123      Set<CustomSearchResult> monitoringEntries)
124  {
125    this.addresses.addAll(addresses);
126    this.port = port;
127    this.protocol = protocol;
128    this.state = state;
129    this.name = name;
130    this.monitoringEntries = Collections.unmodifiableSet(monitoringEntries);
131
132    StringBuilder builder = new StringBuilder();
133    builder.append(getProtocol()).append(" ").append(getState()).append(" ");
134    for (InetAddress address : addresses)
135    {
136      builder.append(address);
137    }
138    builder.append(" Port: ").append(port);
139    toString = builder.toString();
140    hashCode = toString.hashCode();
141  }
142
143  /**
144   * Returns the address port representation of the listener.
145   * @return the address port representation of the listener.
146   */
147  public SortedSet<InetAddress> getAddresses()
148  {
149    return addresses;
150  }
151
152  /**
153   * Returns the protocol of the listener.
154   * @return the protocol of the listener.
155   */
156  public Protocol getProtocol()
157  {
158    return protocol;
159  }
160
161  /**
162   * Returns the state of the listener.
163   * @return the state of the listener.
164   */
165  public State getState()
166  {
167    return state;
168  }
169
170  /**
171   * Returns the monitoring entries.
172   * @return the monitoring entries.
173   */
174  public Set<CustomSearchResult> getMonitoringEntries()
175  {
176    return monitoringEntries;
177  }
178
179  /**
180   * Sets the monitoring entries.
181   * @param monitoringEntries the monitoring entries.
182   */
183  public void setMonitoringEntries(Set<CustomSearchResult> monitoringEntries)
184  {
185    this.monitoringEntries = Collections.unmodifiableSet(monitoringEntries);
186  }
187
188  @Override
189  public int hashCode()
190  {
191    return hashCode;
192  }
193
194  @Override
195  public String toString()
196  {
197    return toString;
198  }
199
200  @Override
201  public boolean equals(Object o)
202  {
203    if (o == this)
204    {
205      return true;
206    }
207    if (o instanceof ConnectionHandlerDescriptor)
208    {
209      ConnectionHandlerDescriptor ch = (ConnectionHandlerDescriptor) o;
210      return toString.equals(o.toString())
211          && getMonitoringEntries().equals(ch.getMonitoringEntries());
212    }
213    return false;
214  }
215
216  /**
217   * Returns the port of the connection handler.
218   * @return the port of the connection handler.
219   */
220  public int getPort()
221  {
222    return port;
223  }
224
225  /**
226   * Returns the name of the connection handler.
227   * @return the name of the connection handler.
228   */
229  public String getName()
230  {
231    return name;
232  }
233}