Skip to content

Commit 485cfcc

Browse files
Merge pull request #16 from Appdynamics/SERVER-9525_support_for_Windows_auth
SQL extensions support for Windows auth in the case of MSSQL DB
2 parents a52f4a2 + a61b99b commit 485cfcc

File tree

5 files changed

+99
-13
lines changed

5 files changed

+99
-13
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
<version>4.11</version>
4545
<scope>test</scope>
4646
</dependency>
47+
<dependency>
48+
<groupId>com.microsoft.sqlserver</groupId>
49+
<artifactId>mssql-jdbc</artifactId>
50+
<version>12.6.2.jre11</version>
51+
</dependency>
52+
4753
<dependency>
4854
<groupId>org.powermock</groupId>
4955
<artifactId>powermock-api-mockito</artifactId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.sql.*;
2+
import java.util.Properties;
3+
4+
public class SQLDatabaseConnection {
5+
6+
// Connect to your database.
7+
// Replace server name, username, and password with your credentials
8+
public static void main(String[] args) {
9+
String connectionUrl =
10+
args.length == 3 ? args[2] : "jdbc:sqlserver://localhost:1434;servername=localhost\\MSSQLSERVER01;database=master;integratedSecurity=true;encrypt=false;trustServerCertificate=false";
11+
12+
ResultSet resultSet = null;
13+
try{
14+
//System.loadLibrary(args[2]);
15+
System.setProperty("java.library.path", args[0]);
16+
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
17+
Connection connection = DriverManager.getConnection(connectionUrl);
18+
DatabaseMetaData metadata = connection.getMetaData();
19+
Statement statement = connection.createStatement();
20+
21+
// Create and execute a SELECT SQL statement.
22+
String selectSql = "SELECT TOP (1000) [lastrun]" +
23+
" ,[cpu_busy]" +
24+
" ,[io_busy]" +
25+
" ,[idle]" +
26+
" ,[pack_received]" +
27+
" ,[pack_sent]" +
28+
" ,[connections]" +
29+
" ,[pack_errors]" +
30+
" ,[total_read]" +
31+
" ,[total_write]" +
32+
" ,[total_errors]" +
33+
" FROM [master].[dbo].[spt_monitor]";
34+
resultSet = statement.executeQuery(selectSql);
35+
36+
// Print results from select statement
37+
while (resultSet.next()) {
38+
System.out.println(resultSet.getString(Integer.parseInt(args[1])));
39+
}
40+
}
41+
catch (Exception e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
}

src/main/java/com/appdynamics/extensions/sql/JDBCConnectionAdapter.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
import com.google.common.base.Strings;
1313
import org.slf4j.Logger;
1414

15-
import java.sql.*;
15+
import java.sql.Connection;
16+
import java.sql.DriverManager;
17+
import java.sql.ResultSet;
18+
import java.sql.SQLException;
19+
import java.sql.Statement;
1620
import java.util.Map;
1721
import java.util.Properties;
22+
import java.util.logging.Level;
1823

1924

2025
public class JDBCConnectionAdapter {
@@ -23,6 +28,10 @@ public class JDBCConnectionAdapter {
2328
private final String connUrl;
2429
private final Map<String, String> connectionProperties;
2530

31+
private String winLibPath;
32+
33+
private boolean enableWindowsAuthentication;
34+
2635

2736
private JDBCConnectionAdapter(String connStr, Map<String, String> connectionProperties) {
2837
this.connUrl = connStr;
@@ -36,20 +45,26 @@ static JDBCConnectionAdapter create(String connUrl, Map<String, String> connecti
3645

3746
Connection open(String driver) throws SQLException, ClassNotFoundException {
3847
Connection connection;
48+
java.util.logging.Logger log = java.util.logging.Logger.getLogger("com.microsoft.sqlserver.jdbc");
49+
log.setLevel(Level.FINE);
3950
Class.forName(driver);
40-
41-
Properties properties = new Properties();
42-
43-
if (connectionProperties != null) {
44-
for (String key : connectionProperties.keySet()) {
45-
if (!Strings.isNullOrEmpty(connectionProperties.get(key)))
46-
properties.put(key, connectionProperties.get(key));
51+
logger.info("driver====>"+driver);
52+
logger.info("Passed all checks for properties and attempting to connect to =====>"+ connUrl);
53+
long timestamp1 = System.currentTimeMillis();
54+
if(enableWindowsAuthentication){
55+
System.setProperty("java.library.path", winLibPath);
56+
logger.info("setting the libreary path :"+ winLibPath);
57+
connection = DriverManager.getConnection(connUrl);
58+
} else {
59+
Properties properties = new Properties();
60+
if (connectionProperties != null) {
61+
for (String key : connectionProperties.keySet()) {
62+
if (!Strings.isNullOrEmpty(connectionProperties.get(key)))
63+
properties.put(key, connectionProperties.get(key));
64+
}
4765
}
66+
connection = DriverManager.getConnection(connUrl, properties);
4867
}
49-
50-
logger.debug("Passed all checks for properties and attempting to connect to: "+ connUrl);
51-
long timestamp1 = System.currentTimeMillis();
52-
connection = DriverManager.getConnection(connUrl, properties);
5368
long timestamp2 = System.currentTimeMillis();
5469
logger.debug("Connection received in JDBC ConnectionAdapter in :"+ (timestamp2-timestamp1)+ " ms");
5570

@@ -67,4 +82,12 @@ void closeStatement(Statement statement) throws SQLException {
6782
void closeConnection(Connection connection) throws SQLException {
6883
connection.close();
6984
}
85+
86+
public void setWinLibPath(String winLibPath) {
87+
this.winLibPath = winLibPath;
88+
}
89+
90+
public void setEnableWindowsAuthentication(boolean enableWindowsAuthentication) {
91+
this.enableWindowsAuthentication = enableWindowsAuthentication;
92+
}
7093
}

src/main/java/com/appdynamics/extensions/sql/SQLMonitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ private SQLMonitorTask createTask(Map<String, ?> server, TasksExecutionServicePr
8080

8181
Map<String, String> connectionProperties = getConnectionProperties(server);
8282
JDBCConnectionAdapter jdbcAdapter = JDBCConnectionAdapter.create(connUrl, connectionProperties);
83+
boolean windowsAuthentication = (System.getProperty("os.name").toLowerCase().contains("win") && connUrl.contains("integratedSecurity"));
84+
logger.info("setting the connUrl==============================>"+ connUrl);
85+
if(windowsAuthentication) {
86+
jdbcAdapter.setEnableWindowsAuthentication(windowsAuthentication);
87+
jdbcAdapter.setWinLibPath(getWinLibPath(server));
88+
}
89+
8390

8491
logger.debug("Task Created for "+server.get("displayName"));
8592

@@ -108,6 +115,11 @@ private String createConnectionUrl(Map<String, ?> server) {
108115
return url;
109116
}
110117

118+
private String getWinLibPath(Map<String, ?> server) {
119+
String url = Util.convertToString(server.get("driverDllFolderPath"), "");
120+
return url;
121+
}
122+
111123
private Map<String, String> getConnectionProperties(Map<String, ?> server) {
112124
Map<String, String> connectionProperties = (Map<String, String>) server.get("connectionProperties");
113125
String password = connectionProperties.get("password");

src/main/java/com/appdynamics/extensions/sql/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static String convertToString(final Object field,final String defaultStr){
1515
if(field == null){
1616
return defaultStr;
1717
}
18-
return field.toString();
18+
return field+"";
1919
}
2020

2121

0 commit comments

Comments
 (0)