/
WebSocketCockpitConnector.java
155 lines (135 loc) · 6.13 KB
/
WebSocketCockpitConnector.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.cockpit.connectors.ws;
import io.gravitee.cockpit.api.CockpitConnector;
import io.gravitee.cockpit.api.command.websocket.CockpitExchangeSerDe;
import io.gravitee.cockpit.connectors.ws.command.CockpitConnectorCommandContext;
import io.gravitee.common.service.AbstractService;
import io.gravitee.exchange.api.command.Command;
import io.gravitee.exchange.api.command.CommandAdapter;
import io.gravitee.exchange.api.command.CommandHandler;
import io.gravitee.exchange.api.command.Reply;
import io.gravitee.exchange.api.command.ReplyAdapter;
import io.gravitee.exchange.api.connector.ConnectorCommandHandlersFactory;
import io.gravitee.exchange.api.connector.ExchangeConnector;
import io.gravitee.exchange.api.connector.ExchangeConnectorManager;
import io.gravitee.exchange.api.websocket.command.ExchangeSerDe;
import io.gravitee.exchange.api.websocket.protocol.ProtocolVersion;
import io.gravitee.exchange.connector.websocket.WebSocketExchangeConnector;
import io.gravitee.exchange.connector.websocket.client.WebSocketConnectorClientFactory;
import io.reactivex.rxjava3.core.Single;
import io.vertx.rxjava3.core.Vertx;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
/**
* @author Jeoffrey HAEYAERT (jeoffrey.haeyaert at graviteesource.com)
* @author GraviteeSource Team
*/
@Slf4j
@RequiredArgsConstructor
public class WebSocketCockpitConnector extends AbstractService<CockpitConnector> implements CockpitConnector {
public static final ProtocolVersion PROTOCOL_VERSION = ProtocolVersion.V1;
@Autowired
private Vertx vertx;
@Autowired
private final ExchangeConnectorManager exchangeConnectorManager;
@Autowired
@Lazy
@Qualifier("cockpitConnectorCommandHandlersFactory")
private final ConnectorCommandHandlersFactory cockpitConnectorCommandHandlersFactory;
@Autowired
@Qualifier("cockpitWebsocketConnectorClientFactory")
private final WebSocketConnectorClientFactory cockpitWebsocketConnectorClientFactory;
@Autowired
@Qualifier("cockpitExchangeSerDe")
private final CockpitExchangeSerDe cockpitExchangeSerDe;
@Value("${cockpit.enabled:false}")
private boolean enabled;
private WebSocketExchangeConnector websocketExchangeConnector;
@Override
protected void doStart() throws Exception {
super.doStart();
if (enabled) {
log.info("Cockpit connector is enabled. Starting connector...");
CockpitConnectorCommandContext integrationConnectorCommandContext = new CockpitConnectorCommandContext();
List<CommandHandler<? extends Command<?>, ? extends Reply<?>>> connectorCommandHandlers = cockpitConnectorCommandHandlersFactory.buildCommandHandlers(
integrationConnectorCommandContext
);
List<CommandAdapter<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> connectorCommandAdapters = cockpitConnectorCommandHandlersFactory.buildCommandAdapters(
integrationConnectorCommandContext,
PROTOCOL_VERSION
);
List<ReplyAdapter<? extends Reply<?>, ? extends Reply<?>>> connectorReplyAdapters = cockpitConnectorCommandHandlersFactory.buildReplyAdapters(
integrationConnectorCommandContext,
PROTOCOL_VERSION
);
websocketExchangeConnector =
new WebSocketExchangeConnector(
PROTOCOL_VERSION,
connectorCommandHandlers,
connectorCommandAdapters,
connectorReplyAdapters,
vertx,
cockpitWebsocketConnectorClientFactory,
cockpitExchangeSerDe
);
exchangeConnectorManager.register(websocketExchangeConnector).blockingAwait();
log.info("Cockpit connector started successfully.");
// Register shutdown hook
Thread shutdownHook = new ContainerShutdownHook(websocketExchangeConnector);
Runtime.getRuntime().addShutdownHook(shutdownHook);
} else {
log.info("Cockpit connector is disabled.");
}
}
@Override
protected void doStop() throws Exception {
super.doStop();
}
@Override
public Single<Reply<?>> sendCommand(final Command<?> command) {
return Single
.fromCallable(() -> this.websocketExchangeConnector.isActive())
.flatMap(isActive -> {
if (isActive) {
return this.websocketExchangeConnector.sendCommand(command);
} else {
return Single.error(new IllegalStateException("CockpitConnector is not ready yet."));
}
});
}
private class ContainerShutdownHook extends Thread {
private final ExchangeConnector connector;
private ContainerShutdownHook(ExchangeConnector connector) {
super("graviteeio-cockpit-connector-finalizer");
this.connector = connector;
}
@Override
public void run() {
try {
connector.close().blockingAwait();
} catch (Exception ex) {
LoggerFactory.getLogger(this.getClass()).error("Unexpected error while stopping {}", name(), ex);
}
}
}
}