-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHttpConnector.java
More file actions
139 lines (119 loc) · 3.74 KB
/
HttpConnector.java
File metadata and controls
139 lines (119 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Copyright (C) 2012 Ness Computing, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nesscomputing.httpserver;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.eclipse.jetty.server.Connector;
/**
* Describes a connector used by a server.
*/
public class HttpConnector
{
private final boolean secure;
private final int port;
private final String address;
private final String scheme;
private final AtomicReference<Connector> connectorHolder = new AtomicReference<Connector>(null);
public HttpConnector(final boolean secure,
@Nonnull final String scheme,
@Nonnull final String address,
final int port)
{
Preconditions.checkNotNull(scheme, "the scheme can not be null");
Preconditions.checkNotNull(address, "the address can not be null");
this.secure = secure;
this.scheme = scheme;
this.address = address;
this.port = port;
}
/**
* Returns true if this is a secure connector.
*/
public boolean isSecure()
{
return secure;
}
/**
* Will be called when jetty should choose a port to update the port information.
*/
void setJettyConnector(final Connector connector)
{
this.connectorHolder.set(connector);
}
/**
* Returns the system port for this connector.
*/
public int getPort()
{
if (port != 0) {
return port;
}
else {
final Connector connector = connectorHolder.get();
if (connector != null) {
Preconditions.checkState(connector.getLocalPort() > 0, "no port was set and the connector is not yet started!");
return connector.getLocalPort();
}
else {
return 0;
}
}
}
/**
* Returns the address for this connector as a string.
*/
public String getAddress()
{
return address;
}
/**
* Returns the scheme for this connector.
*/
public String getScheme()
{
return scheme;
}
@Override
public boolean equals(final Object other)
{
if (!(other instanceof HttpConnector)) {
return false;
}
HttpConnector castOther = (HttpConnector) other;
return new EqualsBuilder().append(secure, castOther.secure).append(port, castOther.port).append(address, castOther.address).append(scheme, castOther.scheme).isEquals();
}
private transient int hashCode;
@Override
public int hashCode()
{
if (hashCode == 0) {
hashCode = new HashCodeBuilder().append(secure).append(port).append(address).append(scheme).toHashCode();
}
return hashCode;
}
private transient String toString;
@Override
public String toString()
{
if (toString == null) {
toString = scheme + "://" + address + ":" + port + " (" + (secure ? "secure" : "not secure") + ")";
}
return toString;
}
}