-
Notifications
You must be signed in to change notification settings - Fork 229
/
TransportService.java
331 lines (292 loc) · 12.8 KB
/
TransportService.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.jboss;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import org.jboss.as.network.SocketBinding;
import org.jboss.modules.Module;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.teiid.adminapi.AdminComponentException;
import org.teiid.adminapi.AdminException;
import org.teiid.adminapi.impl.RequestMetadata;
import org.teiid.adminapi.impl.SessionMetadata;
import org.teiid.client.DQP;
import org.teiid.client.security.ILogon;
import org.teiid.client.util.ExceptionUtil;
import org.teiid.common.buffer.BufferManager;
import org.teiid.deployers.VDBRepository;
import org.teiid.dqp.internal.process.DQPCore;
import org.teiid.dqp.internal.process.DQPWorkContext;
import org.teiid.dqp.service.SessionServiceException;
import org.teiid.jdbc.ConnectionImpl;
import org.teiid.jdbc.ConnectionProfile;
import org.teiid.jdbc.TeiidDriver;
import org.teiid.jdbc.TeiidSQLException;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.logging.MessageLevel;
import org.teiid.net.CommunicationException;
import org.teiid.net.ConnectionException;
import org.teiid.net.socket.AuthenticationType;
import org.teiid.services.SessionServiceImpl;
import org.teiid.transport.*;
public class TransportService extends ClientServiceRegistryImpl implements Service<ClientServiceRegistry> {
private transient LogonImpl logon;
private SocketConfiguration socketConfig;
private String authenticationDomain;
private long sessionMaxLimit;
private long sessionExpirationTimeLimit;
private SocketListener socketListener;
private transient SessionServiceImpl sessionService;
private AuthenticationType authenticationType;
private int maxODBCLobSizeAllowed = 5*1024*1024; // 5 MB
private boolean embedded;
private InetSocketAddress address = null;
private String transportName;
private final InjectedValue<SocketBinding> socketBindingInjector = new InjectedValue<SocketBinding>();
private final InjectedValue<VDBRepository> vdbRepositoryInjector = new InjectedValue<VDBRepository>();
private final InjectedValue<DQPCore> dqpInjector = new InjectedValue<DQPCore>();
private final InjectedValue<BufferManager> bufferManagerInjector = new InjectedValue<BufferManager>();
public TransportService(String transportName) {
this.transportName = transportName;
}
@Override
public ClientServiceRegistry getValue() throws IllegalStateException, IllegalArgumentException {
return this;
}
@Override
public void waitForFinished(String vdbName, int vdbVersion,
int timeOutMillis) throws ConnectionException {
VDBRepository repo = this.vdbRepositoryInjector.getValue();
repo.waitForFinished(vdbName, vdbVersion, timeOutMillis);
}
@Override
public ClassLoader getCallerClassloader() {
return Module.getCallerModule().getClassLoader();
}
@Override
public void start(StartContext context) throws StartException {
this.setSecurityHelper(new JBossSecurityHelper());
this.setVDBRepository(this.getVdbRepository());
this.sessionService = new JBossSessionService();
if (this.authenticationDomain != null) {
this.sessionService.setSecurityDomain(this.authenticationDomain);
this.sessionService.setAuthenticationHandler((JBossSessionService)this.sessionService);
}
this.sessionService.setSessionExpirationTimeLimit(this.sessionExpirationTimeLimit);
this.sessionService.setSessionMaxLimit(this.sessionMaxLimit);
this.sessionService.setDqp(getDQP());
this.sessionService.setVDBRepository(getVdbRepository());
this.sessionService.setSecurityHelper(this.getSecurityHelper());
this.sessionService.setAuthenticationType(getAuthenticationType());
this.sessionService.start();
this.setAuthenticationType(this.sessionService.getDefaultAuthenticationType());
// create the necessary services
this.logon = new LogonImpl(this.sessionService, "teiid-cluster"); //$NON-NLS-1$
DQP dqpProxy = proxyService(DQP.class, getDQP(), LogConstants.CTX_DQP);
this.registerClientService(ILogon.class, logon, LogConstants.CTX_SECURITY);
this.registerClientService(DQP.class, dqpProxy, LogConstants.CTX_DQP);
if (this.socketConfig != null) {
/*
try {
// this is to show the bound socket port in the JMX console
SocketBinding socketBinding = getSocketBindingInjector().getValue();
ManagedServerSocketBinding ss = (ManagedServerSocketBinding)socketBinding.getSocketBindings().getServerSocketFactory().createServerSocket(socketBinding.getName());
socketBinding.getSocketBindings().getNamedRegistry().registerBinding(ss);
} catch (IOException e) {
throw new StartException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50013));
}
*/
this.address = getSocketBindingInjector().getValue().getSocketAddress();
this.socketConfig.setBindAddress(this.address.getHostName());
this.socketConfig.setPortNumber(this.address.getPort());
boolean sslEnabled = false;
if (this.socketConfig.getSSLConfiguration() != null) {
sslEnabled = this.socketConfig.getSSLConfiguration().isSslEnabled();
}
if (socketConfig.getProtocol() == WireProtocol.teiid) {
this.socketListener = new SocketListener(address, this.socketConfig, this, getBufferManagerInjector().getValue());
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50012, this.transportName, address.getHostName(), String.valueOf(address.getPort()), (sslEnabled?"ON":"OFF"), this.authenticationDomain)); //$NON-NLS-1$ //$NON-NLS-2$
}
else if (socketConfig.getProtocol() == WireProtocol.pg) {
getVdbRepository().odbcEnabled();
TeiidDriver driver = new TeiidDriver();
driver.setEmbeddedProfile(new ConnectionProfile() {
@Override
public ConnectionImpl connect(String url, Properties info) throws TeiidSQLException {
try {
LocalServerConnection sc = new LocalServerConnection(info, true){
@Override
protected ClientServiceRegistry getClientServiceRegistry(String name) {
return TransportService.this;
}
};
return new ConnectionImpl(sc, info, url);
} catch (CommunicationException e) {
throw TeiidSQLException.create(e);
} catch (ConnectionException e) {
throw TeiidSQLException.create(e);
}
}
});
ODBCSocketListener odbc = new ODBCSocketListener(address, this.socketConfig, this, getBufferManagerInjector().getValue(), getMaxODBCLobSizeAllowed(), this.logon, driver);
this.socketListener = odbc;
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50037, this.transportName, address.getHostName(), String.valueOf(address.getPort()), (sslEnabled?"ON":"OFF"), this.authenticationDomain)); //$NON-NLS-1$ //$NON-NLS-2$
}
else {
throw new StartException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50013));
}
}
else {
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50038, LocalServerConnection.jndiNameForRuntime(transportName)));
}
}
@Override
public void stop(StopContext context) {
// Stop socket transport(s)
if (this.socketListener != null) {
this.socketListener.stop();
this.socketListener = null;
}
this.sessionService.stop();
if (this.socketConfig != null) {
if (socketConfig.getProtocol() == WireProtocol.teiid) {
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50039, this.transportName, this.address.getHostName(), String.valueOf(this.address.getPort())));
}
else if (socketConfig.getProtocol() == WireProtocol.pg) {
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50040, this.address.getHostName(), String.valueOf(this.address.getPort())));
}
}
else {
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50041, LocalServerConnection.jndiNameForRuntime(transportName)));
}
}
/**
* Creates an proxy to validate the incoming session
*/
private <T> T proxyService(final Class<T> iface, final T instance, String context) {
return iface.cast(Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {iface}, new LogManager.LoggingProxy(instance, context, MessageLevel.TRACE) {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Throwable exception = null;
try {
sessionService.validateSession(DQPWorkContext.getWorkContext().getSessionId());
return super.invoke(proxy, method, args);
} catch (InvocationTargetException e) {
exception = e.getTargetException();
} catch(Throwable t){
exception = t;
}
throw ExceptionUtil.convertException(method, exception);
}
}));
}
public List<RequestMetadata> getRequestsUsingVDB(String vdbName, int vdbVersion) {
List<RequestMetadata> requests = new ArrayList<RequestMetadata>();
Collection<SessionMetadata> sessions = this.sessionService.getActiveSessions();
for (SessionMetadata session:sessions) {
if (vdbName.equals(session.getVDBName()) && session.getVDBVersion() == vdbVersion) {
requests.addAll(getDQP().getRequestsForSession(session.getSessionId()));
}
}
return requests;
}
public void terminateSession(String terminateeId) {
this.sessionService.terminateSession(terminateeId, DQPWorkContext.getWorkContext().getSessionId());
}
public Collection<SessionMetadata> getActiveSessions(){
return this.sessionService.getActiveSessions();
}
public int getActiveSessionsCount() throws AdminException{
try {
return this.sessionService.getActiveSessionsCount();
} catch (SessionServiceException e) {
throw new AdminComponentException(IntegrationPlugin.Event.TEIID50056, e);
}
}
public InjectedValue<SocketBinding> getSocketBindingInjector() {
return this.socketBindingInjector;
}
public SocketConfiguration getSocketConfig() {
return socketConfig;
}
public void setSocketConfig(SocketConfiguration socketConfig) {
this.socketConfig = socketConfig;
}
public String getAuthenticationDomain() {
return authenticationDomain;
}
public void setAuthenticationDomain(String authenticationDomain) {
this.authenticationDomain = authenticationDomain;
}
public void setSessionMaxLimit(long limit) {
this.sessionMaxLimit = limit;
}
public void setSessionExpirationTimeLimit(long limit) {
this.sessionExpirationTimeLimit = limit;
}
@Override
public AuthenticationType getAuthenticationType() {
return authenticationType;
}
@Override
public void setAuthenticationType(AuthenticationType authenticationType) {
this.authenticationType = authenticationType;
}
public InjectedValue<VDBRepository> getVdbRepositoryInjector() {
return vdbRepositoryInjector;
}
private VDBRepository getVdbRepository() {
return vdbRepositoryInjector.getValue();
}
private DQPCore getDQP() {
return getDqpInjector().getValue();
}
public InjectedValue<DQPCore> getDqpInjector() {
return dqpInjector;
}
public InjectedValue<BufferManager> getBufferManagerInjector() {
return bufferManagerInjector;
}
private int getMaxODBCLobSizeAllowed() {
return this.maxODBCLobSizeAllowed;
}
public void setMaxODBCLobSizeAllowed(int lobSize) {
this.maxODBCLobSizeAllowed = lobSize;
}
public void setEmbedded(boolean v) {
this.embedded = v;
}
public boolean isEmbedded() {
return this.embedded;
}
}