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 final String host;
38  	private final int port;
39  	private final int port2;
40  	private final boolean tls;
41  	private final Parser parser;
42  	private final LowerLayerProtocol protocol;
43  	private SocketFactory socketFactory;
44      private final boolean lazy;
45  
46  	public ConnectionData(String host, int port, Parser parser, LowerLayerProtocol protocol) {
47  		this(host, port, parser, protocol, false);
48  	}
49  
50  	public ConnectionData(String host, int port, Parser parser, LowerLayerProtocol protocol,
51  			boolean tls) {
52  		this(host, port, 0, parser, protocol, tls);
53  	}
54  
55  	public ConnectionData(String host, int outboundPort, int inboundPort, Parser parser,
56  			LowerLayerProtocol protocol, boolean tls) {
57  		this(host, outboundPort, inboundPort, parser, protocol, tls, null, false);
58  	}
59  
60  	public ConnectionData(String host, int outboundPort, int inboundPort, Parser parser,
61  			LowerLayerProtocol protocol, boolean tls, SocketFactory socketFactory, boolean lazy) {
62  		this.host = host;
63  		this.port = outboundPort;
64  		this.port2 = inboundPort;
65  		this.parser = parser;
66  		this.protocol = protocol;
67  		this.tls = tls;
68  		this.socketFactory = socketFactory;
69  		if (this.socketFactory == null) {
70  			this.socketFactory = new StandardSocketFactory();
71  		}
72          this.lazy = lazy;
73  	}
74  
75  	public String getHost() {
76  		return host;
77  	}
78  
79  	public int getPort() {
80  		return port;
81  	}
82  
83  	public int getPort2() {
84  		return port2;
85  	}
86  
87  	public boolean isTls() {
88  		return tls;
89  	}
90  
91      boolean isLazy() {
92          return lazy;
93      }
94  
95      public Parser getParser() {
96  		return parser;
97  	}
98  
99  	public LowerLayerProtocol getProtocol() {
100 		return protocol;
101 	}
102 
103 	public SocketFactory getSocketFactory() {
104 		return socketFactory;
105 	}
106 
107 	public void setSocketFactory(SocketFactory theSocketFactory) {
108 		socketFactory = theSocketFactory;
109 	}
110 
111 	@Override
112 	public int hashCode() {
113 		final int prime = 31;
114 		int result = 1;
115 		result = prime * result + ((host == null) ? 0 : host.hashCode());
116 		result = prime * result + ((parser == null) ? 0 : parser.hashCode());
117 		result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
118 		result = prime * result + port;
119 		result = prime * result + port2;
120 		return result;
121 	}
122 
123 	@Override
124 	public boolean equals(Object obj) {
125 		if (this == obj)
126 			return true;
127 		if (obj == null)
128 			return false;
129 		if (getClass() != obj.getClass())
130 			return false;
131 		ConnectionData other = (ConnectionData) obj;
132 		if (host == null) {
133 			if (other.host != null)
134 				return false;
135 		} else if (!host.equals(other.host))
136 			return false;
137 		if (parser == null) {
138 			if (other.parser != null)
139 				return false;
140 		} else {
141 			if (other.parser == null)
142 				return false;
143 			else if (!parser.getClass().equals(other.parser.getClass()))
144 				return false;
145 		}
146 		if (protocol == null) {
147 			if (other.protocol != null)
148 				return false;
149 		} else {
150 			if (other.protocol == null)
151 				return false;
152 			else if (!protocol.getClass().equals(other.protocol.getClass()))
153 				return false;
154 		}
155 		if (port != other.port)
156 			return false;
157 		return port2 == other.port2;
158 	}
159 
160 	@Override
161 	public String toString() {
162 		StringBuilder buf = new StringBuilder();
163 		buf.append(getHost()).append(":").append(getPort());
164 		if (port2 > 0)
165 			buf.append(",").append(port2);
166 		if (isTls())
167 			buf.append("(s)");
168 		return buf.toString();
169 	}
170 
171 }