Skip to content

Commit

Permalink
Implement client special registers for Mapepire server
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrez committed Jan 2, 2025
1 parent 9a3125a commit 7d71a1e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 29 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/github/ibm/mapepire/ClientSpecialRegisters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.ibm.mapepire;

import java.util.Properties;

public interface ClientSpecialRegisters {
static final String CLIENT_APP_NAME = "ApplicationName"; // AS400JDBCConnectionImpl.applicationNamePropertyName_
static final String CLIENT_USER = "ClientUser"; // AS400JDBCConnectionImpl.clientUserPropertyName_
static final String CLIENT_HOST_NAME = "ClientHostname"; // AS400JDBCConnectionImpl.clientHostnamePropertyName_
static final String CLIENT_ACCOUNTING = "ClientAccounting"; // AS400JDBCConnectionImpl.clientAccountingPropertyName_
static final String CLIENT_PGM_ID = "ClientProgramID"; // AS400JDBCConnectionImpl.clientProgramIDPropertyName_

public Properties getProperties(final String _applicationName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.ibm.mapepire;

import java.util.Properties;

public class ClientSpecialRegistersRemote implements ClientSpecialRegisters {

final String m_clientIP;
final String m_accountingString;
private String m_user;

public ClientSpecialRegistersRemote(final String _clientHost, final String _clientIP, final String _user) {
m_clientIP = _clientIP;
m_user = _user;
m_accountingString = "hostname: " + _clientHost;
}

@Override
public Properties getProperties(final String _applicationName) {
Properties ret = new Properties();
ret.put(CLIENT_USER, m_user);
ret.put(CLIENT_APP_NAME, _applicationName);
ret.put(CLIENT_HOST_NAME, m_clientIP);
ret.put(CLIENT_PGM_ID, getProgramString());
ret.put(CLIENT_ACCOUNTING, m_accountingString);
return ret;
}

private String getProgramString() {
return String.format("Mapepire server connector | Version %s", Version.s_version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
import com.github.theprez.jcmdutils.StringUtils;

public class ClientSpecialRegistersVSCode implements ClientSpecialRegisters {

private static final String CLIENT_APP_NAME = "ApplicationName"; // AS400JDBCConnectionImpl.applicationNamePropertyName_
private static final String CLIENT_USER = "ClientUser"; // AS400JDBCConnectionImpl.clientUserPropertyName_
private static final String CLIENT_HOST_NAME = "ClientHostname"; // AS400JDBCConnectionImpl.clientHostnamePropertyName_
private static final String CLIENT_ACCOUNTING = "ClientAccounting"; // AS400JDBCConnectionImpl.clientAccountingPropertyName_
private static final String CLIENT_PGM_ID = "ClientProgramID"; // AS400JDBCConnectionImpl.clientProgramIDPropertyName_
final String m_clientIP;
final String m_accountingString;
private String m_applicationName = "I dunno, maybe VSCode or something";

public ClientSpecialRegistersVSCode() {
String sshConnectionEnv = System.getenv("SSH_CONNECTION");
Expand All @@ -36,15 +29,11 @@ public ClientSpecialRegistersVSCode() {
m_accountingString = "location: "+location;
}

public ClientSpecialRegistersVSCode setApplicationName(String _appName) {
m_applicationName = _appName;
return this;
}

public Properties getProperties() {
@Override
public Properties getProperties(final String _applicationName) {
Properties ret = new Properties();
ret.put(CLIENT_USER, System.getProperty("user.name", "<unknown>"));
ret.put(CLIENT_APP_NAME, m_applicationName);
ret.put(CLIENT_APP_NAME, _applicationName);
ret.put(CLIENT_HOST_NAME, m_clientIP);
ret.put(CLIENT_PGM_ID, getProgramString());
ret.put(CLIENT_ACCOUNTING, m_accountingString);
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/com/github/ibm/mapepire/SystemConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public String getConnectionString(ConnectionMethod method) throws IOException {

private String m_jdbcProps = "";
private ConnectionMethod m_connectionMethod = ConnectionMethod.CLI;
private ClientSpecialRegisters m_clientRegs = new ClientSpecialRegisters();

String getJdbcProperties() {
return m_jdbcProps;
Expand All @@ -93,26 +92,28 @@ public void setJdbcProperties(final String _props) {
public ConnectionMethod getConnectionMethod() {
return m_connectionMethod;
}

public void setCSRApplicationName(String _appName) {
m_clientRegs.setApplicationName(_appName);
}
}

private Connection m_conn;
private ConnectionOptions m_connectionOptions = null;
private String host;
private String userProfile;
private String password;
private final ClientSpecialRegisters m_clientRegs;
private String m_applicationName;

public SystemConnection() throws IOException {
if (!MapepireServer.isSingleMode()) {
throw new IOException("Improper usage");
}
this.m_clientRegs = new ClientSpecialRegistersVSCode();
}

public SystemConnection(String host, String user, String pass) throws IOException {
public SystemConnection(String clientHost, String clientAddress, String host, String user, String pass) throws IOException {
super();
if (MapepireServer.isSingleMode()) {
throw new IOException("Improper usage");
}
this.host = host;
if (StringUtils.isEmpty(user) || user.contains("*")) {
throw new IOException("Invalid Username");
Expand All @@ -125,6 +126,7 @@ public SystemConnection(String host, String user, String pass) throws IOExceptio
}
this.userProfile = user;
this.password = pass;
this.m_clientRegs = new ClientSpecialRegistersRemote(clientHost, clientAddress, user);
}

public static boolean isRunningOnIBMi() {
Expand All @@ -136,7 +138,7 @@ public synchronized Connection getJdbcConnection() throws SQLException {
return m_conn;
}
if (Boolean.getBoolean("codeserver.jdbc.autoconnect")) {
return reconnect(m_connectionOptions);
return reconnect(m_connectionOptions, m_applicationName);
}
throw new SQLException("Not connected");
}
Expand Down Expand Up @@ -172,12 +174,15 @@ public synchronized void close() {
}
}

public synchronized Connection reconnect(final ConnectionOptions _opts) throws SQLException {
public synchronized Connection reconnect(final ConnectionOptions _opts, final String _applicationName) throws SQLException {
if (null != m_conn) {
final Connection cpy = m_conn;
m_conn = null;
cpy.close();
}
if (StringUtils.isNonEmpty(_applicationName)) {
m_applicationName = _applicationName;
}
try {
DriverManager.registerDriver(new AS400JDBCDriver());
m_connectionOptions = _opts;
Expand All @@ -187,7 +192,7 @@ public synchronized Connection reconnect(final ConnectionOptions _opts) throws S
m_connectionOptions.setPassword(this.password);

m_conn = DriverManager.getConnection(m_connectionOptions.getConnectionString(m_connectionOptions.m_connectionMethod) + ";" + _opts.getJdbcProperties());
m_conn.setClientInfo(_opts.m_clientRegs.getProperties());
m_conn.setClientInfo(this.m_clientRegs.getProperties(_applicationName));
return m_conn;
} catch (Exception e) {
throw new SQLException(e);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/github/ibm/mapepire/requests/Reconnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ protected void go() throws Exception {
if (null != props) {
opts.setJdbcProperties(props.getAsString());
}
String appName = null;
if (null != applicationName) {
opts.setCSRApplicationName(applicationName.getAsString());
appName = applicationName.getAsString();
}
getSystemConnection().reconnect(opts);
getSystemConnection().reconnect(opts, appName);
addReplyData("job", getSystemConnection().getJdbcJobName());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.ibm.mapepire.ws;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

Expand Down Expand Up @@ -82,7 +81,7 @@ public Object createWebSocket(ServletUpgradeRequest jettyServerUpgradeRequest, S
}

try {
return new DbWebsocketClient(DbSocketCreator.getHost(), parts[0], parts[1]);
return new DbWebsocketClient(jettyServerUpgradeRequest.getRemoteHostName(), jettyServerUpgradeRequest.getRemoteAddress(), DbSocketCreator.getHost(), parts[0], parts[1]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class DbWebsocketClient extends WebSocketAdapter {
private final CountDownLatch closureLatch = new CountDownLatch(1);
private final DataStreamProcessor io;

DbWebsocketClient(String host, String user, String pass) throws IOException {
DbWebsocketClient(String clientHost, String clientAddress, String host, String user, String pass) throws IOException {
super();
SystemConnection conn = new SystemConnection(host, user, pass);
SystemConnection conn = new SystemConnection(clientHost, clientAddress,host, user, pass);
io = getDataStream(this, conn);
}

Expand Down

0 comments on commit 7d71a1e

Please sign in to comment.