View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "Connection.java".  Description: 
10  "A TCP/IP connection to a remote HL7 server." 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2002.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the  "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25   */
26  package ca.uhn.hl7v2.app;
27  
28  import ca.uhn.hl7v2.llp.LowerLayerProtocol;
29  import ca.uhn.hl7v2.parser.Parser;
30  import ca.uhn.hl7v2.util.SocketFactory;
31  import ca.uhn.hl7v2.util.StandardSocketFactory;
32  
33  /**
34   * Connection meta data class.
35   */
36  class ConnectionData {
37  	private String host;
38  	private int port;
39  	private int port2;
40  	private boolean tls;
41  	private Parser parser;
42  	private LowerLayerProtocol protocol;
43  	private SocketFactory socketFactory;
44  
45  	public ConnectionData(String host, int port, Parser parser, LowerLayerProtocol protocol) {
46  		this(host, port, parser, protocol, false);
47  	}
48  
49  	public ConnectionData(String host, int port, Parser parser, LowerLayerProtocol protocol,
50  			boolean tls) {
51  		this(host, port, 0, parser, protocol, tls);
52  	}
53  
54  	public ConnectionData(String host, int outboundPort, int inboundPort, Parser parser,
55  			LowerLayerProtocol protocol, boolean tls) {
56  		this(host, outboundPort, inboundPort, parser, protocol, tls, null);
57  	}
58  
59  	public ConnectionData(String host, int outboundPort, int inboundPort, Parser parser,
60  			LowerLayerProtocol protocol, boolean tls, SocketFactory socketFactory) {
61  		this.host = host;
62  		this.port = outboundPort;
63  		this.port2 = inboundPort;
64  		this.parser = parser;
65  		this.protocol = protocol;
66  		this.tls = tls;
67  		this.socketFactory = socketFactory;
68  		if (this.socketFactory == null) {
69  			this.socketFactory = new StandardSocketFactory();
70  		}
71  	}
72  
73  	public String getHost() {
74  		return host;
75  	}
76  
77  	public int getPort() {
78  		return port;
79  	}
80  
81  	public int getPort2() {
82  		return port2;
83  	}
84  
85  	public boolean isTls() {
86  		return tls;
87  	}
88  
89  	public Parser getParser() {
90  		return parser;
91  	}
92  
93  	public LowerLayerProtocol getProtocol() {
94  		return protocol;
95  	}
96  
97  	public SocketFactory getSocketFactory() {
98  		return socketFactory;
99  	}
100 
101 	public void setSocketFactory(SocketFactory theSocketFactory) {
102 		socketFactory = theSocketFactory;
103 	}
104 
105 	@Override
106 	public int hashCode() {
107 		final int prime = 31;
108 		int result = 1;
109 		result = prime * result + ((host == null) ? 0 : host.hashCode());
110 		result = prime * result + ((parser == null) ? 0 : parser.hashCode());
111 		result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
112 		result = prime * result + port;
113 		result = prime * result + port2;
114 		return result;
115 	}
116 
117 	@Override
118 	public boolean equals(Object obj) {
119 		if (this == obj)
120 			return true;
121 		if (obj == null)
122 			return false;
123 		if (getClass() != obj.getClass())
124 			return false;
125 		ConnectionData other = (ConnectionData) obj;
126 		if (host == null) {
127 			if (other.host != null)
128 				return false;
129 		} else if (!host.equals(other.host))
130 			return false;
131 		if (parser == null) {
132 			if (other.parser != null)
133 				return false;
134 		} else {
135 			if (other.parser == null)
136 				return false;
137 			else if (!parser.getClass().equals(other.parser.getClass()))
138 				return false;
139 		}
140 		if (protocol == null) {
141 			if (other.protocol != null)
142 				return false;
143 		} else {
144 			if (other.protocol == null)
145 				return false;
146 			else if (!protocol.getClass().equals(other.protocol.getClass()))
147 				return false;
148 		}
149 		if (port != other.port)
150 			return false;
151 		if (port2 != other.port2)
152 			return false;
153 		return true;
154 	}
155 
156 	@Override
157 	public String toString() {
158 		StringBuilder buf = new StringBuilder();
159 		buf.append(getHost()).append(":").append(getPort());
160 		if (port2 > 0)
161 			buf.append(",").append(port2);
162 		if (isTls())
163 			buf.append("(s)");
164 		return buf.toString();
165 	}
166 
167 }