1 |
|
package net.technearts.rip; |
2 |
|
|
3 |
|
import static java.util.Optional.ofNullable; |
4 |
|
import static spark.Service.ignite; |
5 |
|
|
6 |
|
import java.net.URISyntaxException; |
7 |
|
import java.nio.file.Path; |
8 |
|
import java.nio.file.Paths; |
9 |
|
import java.util.HashMap; |
10 |
|
import java.util.Map; |
11 |
|
import java.util.Objects; |
12 |
|
|
13 |
|
import org.slf4j.Logger; |
14 |
|
import org.slf4j.LoggerFactory; |
15 |
|
|
16 |
|
import com.google.common.collect.ComparisonChain; |
17 |
|
|
18 |
|
import spark.Service; |
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
|
|
| 46.8% |
Uncovered Elements: 25 (47) |
Complexity: 16 |
Complexity Density: 0.55 |
|
25 |
|
public class RipServer implements Comparable<RipServer>, AutoCloseable { |
26 |
|
private static final Logger logger = LoggerFactory.getLogger(RipServer.class); |
27 |
|
private static final Map<Integer, RipServer> instance = new HashMap<>(); |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
@return |
33 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
34 |
8 |
public static RipRoute localhost() {... |
35 |
8 |
return localhost(7777); |
36 |
|
} |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
@param |
42 |
|
@return |
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
44 |
9 |
public static RipRoute localhost(final int port) {... |
45 |
9 |
return localhost(port, "/"); |
46 |
|
} |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@param |
52 |
|
@param |
53 |
|
@return |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
55 |
9 |
public static RipRoute localhost(final int port, final String location) {... |
56 |
9 |
if (!instance.containsKey(port)) { |
57 |
1 |
logger.debug("Criando servidor local na porta {}", port); |
58 |
1 |
instance.put(port, new RipServer(port, location)); |
59 |
|
} |
60 |
9 |
return new RipRoute(instance.get(port)); |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@param |
67 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
68 |
0 |
public static void stop(final int port) {... |
69 |
0 |
if (instance.containsKey(port)) { |
70 |
0 |
instance.get(port).service.stop(); |
71 |
|
} |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@param |
78 |
|
@return |
79 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
80 |
2 |
public static Path withFile(final String fileName) {... |
81 |
2 |
try { |
82 |
2 |
return Paths.get(RipServer.class.getResource(fileName).toURI()); |
83 |
|
} catch (final URISyntaxException e) { |
84 |
0 |
return null; |
85 |
|
} |
86 |
|
} |
87 |
|
|
88 |
|
private final int port; |
89 |
|
private final String location; |
90 |
|
Service service; |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
92 |
1 |
private RipServer(final int port, final String location) {... |
93 |
1 |
this.port = port; |
94 |
1 |
this.location = location; |
95 |
1 |
service = ignite(); |
96 |
1 |
service.port(port); |
97 |
1 |
service.staticFiles.location(location); |
98 |
|
} |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
100 |
0 |
@Override... |
101 |
|
public void close() throws Exception { |
102 |
0 |
instance.values().stream().parallel() |
103 |
|
.forEach(server -> server.service.stop()); |
104 |
0 |
instance.clear(); |
105 |
|
|
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
0 |
@Override... |
109 |
|
public int compareTo(final RipServer that) { |
110 |
0 |
return ComparisonChain.start().compare(port, that.port).result(); |
111 |
|
} |
112 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
113 |
0 |
@Override... |
114 |
|
public boolean equals(final Object object) { |
115 |
0 |
if (object instanceof RipServer) { |
116 |
0 |
final RipServer that = (RipServer) object; |
117 |
0 |
return port == that.port; |
118 |
|
} |
119 |
0 |
return false; |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
7836 |
@Override... |
123 |
|
public int hashCode() { |
124 |
7788 |
return Objects.hashCode(port); |
125 |
|
} |
126 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
127 |
0 |
void reset() {... |
128 |
0 |
logger.info("Apagando rotas no servidor local na porta {}", port); |
129 |
0 |
ofNullable(instance.remove(port)) |
130 |
|
.ifPresent(server -> server.service.stop()); |
131 |
0 |
logger.info("Recriando servidor local na porta {} sem rotas", port); |
132 |
0 |
instance.put(port, new RipServer(port, location)); |
133 |
|
} |
134 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
0 |
@Override... |
136 |
|
public String toString() { |
137 |
0 |
return String.format("RipServer %s", port); |
138 |
|
} |
139 |
|
} |