001/*
002 * #%L
003 * Netarchivesuite - common
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.common.distribute.monitorregistry;
024
025import java.io.Serializable;
026import java.util.Date;
027
028import dk.netarkivet.common.exceptions.ArgumentNotValid;
029
030/**
031 * Helper class to encapsulate information about one remote JmxConnection.
032 */
033@SuppressWarnings({"serial"})
034public class HostEntry implements Serializable {
035    /**
036     * The name of the remote host.
037     */
038    private final String name;
039    /**
040     * The JMX port allocated on the remote host.
041     */
042    private final int jmxPort;
043    /**
044     * The RMI port allocated on the remote host.
045     */
046    private final int rmiPort;
047    /**
048     * The time this host-entry was last seen alive.
049     */
050    private Date time;
051
052    /**
053     * Constructor for the HostEntry helper class.
054     *
055     * @param name The name of the remote host
056     * @param jmxPort The JMX port allocated on the remote host
057     * @param rmiPort The RMI port allocated on the remote host
058     */
059    public HostEntry(String name, int jmxPort, int rmiPort) {
060        ArgumentNotValid.checkNotNullOrEmpty(name, "String name");
061        ArgumentNotValid.checkPositive(jmxPort, "int jmxPort");
062        ArgumentNotValid.checkPositive(rmiPort, "int rmiPort");
063        this.name = name;
064        this.jmxPort = jmxPort;
065        this.rmiPort = rmiPort;
066        this.time = new Date();
067    }
068
069    /**
070     * Get the JMX port for connections.
071     *
072     * @return The jmx port.
073     */
074    public int getJmxPort() {
075        return jmxPort;
076    }
077
078    /**
079     * Get the host name.
080     *
081     * @return The name.
082     */
083    public String getName() {
084        return name;
085    }
086
087    /**
088     * Get the RMI port for connections.
089     *
090     * @return The rmi port.
091     */
092    public int getRmiPort() {
093        return rmiPort;
094    }
095
096    /**
097     * Get the time this host was last seen alive.
098     *
099     * @return The time this host was last seen alive.
100     */
101    public Date getTime() {
102        return time;
103    }
104
105    /**
106     * Return whether two hosts are equal.
107     * <p>
108     * Two hosts are considered equal, if they have the same name and JMX/RMI ports. However, the time last seen alive
109     * is not, and should not be, considered.
110     *
111     * @param obj The host to compare with.
112     * @return Whether the two objects represent the same host.
113     * @see Object#equals(Object)
114     */
115    public boolean equals(Object obj) {
116        if (this == obj) {
117            return true;
118        }
119        if (!(obj instanceof HostEntry)) {
120            return false;
121        }
122
123        final HostEntry hostEntry1 = (HostEntry) obj;
124
125        if (name != null ? !name.equals(hostEntry1.name) : hostEntry1.name != null) {
126            return false;
127        }
128
129        if (jmxPort != hostEntry1.jmxPort) {
130            return false;
131        }
132        if (rmiPort != hostEntry1.rmiPort) {
133            return false;
134        }
135        return true;
136    }
137
138    /**
139     * Return hash code. Coded to be consistent with equals.
140     *
141     * @return Hash code for this object.
142     * @see Object#hashCode()
143     */
144    public int hashCode() {
145        int result;
146        result = (name != null ? name.hashCode() : 0);
147        result = 29 * result + jmxPort * 1;
148        result = 29 * result + rmiPort * 2;
149        return result;
150    }
151
152    /**
153     * Get a human readable representation of this host and ports.
154     *
155     * @return A human readable string.
156     */
157    public String toString() {
158        return "Host=" + name + ", JMXport=" + jmxPort + ", RMIport=" + rmiPort + ", last seen live at " + time;
159    }
160
161    /**
162     * Update the time for when the host was last seen alive.
163     *
164     * @param time The time last seen alive.
165     * @throws ArgumentNotValid on null parameter.
166     */
167    public void setTime(Date time) {
168        ArgumentNotValid.checkNotNull(time, "Date time");
169        this.time = time;
170    }
171}