Skip to content
This repository was archived by the owner on Aug 21, 2021. It is now read-only.

Commit 020274b

Browse files
committed
JAXB and java 11
fixes #25 Added javax.xml.bind:jaxb-api:2.3.0, javax.activation:activation:1.1, com.sun.xml.bind:jaxb-core:2.3.0 and org.glassfish.jaxb:jaxb-runtime:2.3.0 dependencies. Forced the thread context loader (use when authenticating) to use the plugin context loader.
1 parent 760bc07 commit 020274b

File tree

5 files changed

+115
-54
lines changed

5 files changed

+115
-54
lines changed

pom.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
</issueManagement>
5757

5858
<properties>
59+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5960
<sonar.version>6.7</sonar.version>
6061
<sonar.pluginName>Crowd</sonar.pluginName>
6162
<sonar.pluginClass>org.sonar.plugins.crowd.CrowdPlugin</sonar.pluginClass>
@@ -91,6 +92,26 @@
9192
<artifactId>jcl-over-slf4j</artifactId>
9293
<version>1.7.25</version>
9394
</dependency>
95+
<dependency>
96+
<groupId>javax.xml.bind</groupId>
97+
<artifactId>jaxb-api</artifactId>
98+
<version>2.3.0</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>javax.activation</groupId>
102+
<artifactId>activation</artifactId>
103+
<version>1.1</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.glassfish.jaxb</groupId>
107+
<artifactId>jaxb-runtime</artifactId>
108+
<version>2.3.0</version>
109+
</dependency>
110+
<dependency>
111+
<groupId>com.sun.xml.bind</groupId>
112+
<artifactId>jaxb-core</artifactId>
113+
<version>2.3.0</version>
114+
</dependency>
94115
<!-- unit tests -->
95116
<dependency>
96117
<groupId>org.sonarsource.sonarqube</groupId>
@@ -118,12 +139,13 @@
118139
<url>https://packages.atlassian.com/maven-public-legacy-local</url>
119140
</repository>
120141
</repositories>
121-
142+
122143
<build>
123144
<plugins>
124145
<plugin>
125146
<groupId>org.apache.maven.plugins</groupId>
126147
<artifactId>maven-compiler-plugin</artifactId>
148+
<version>3.8.1</version>
127149
<configuration>
128150
<source>8</source>
129151
<target>8</target>

src/main/java/org/sonar/plugins/crowd/CrowdAuthenticator.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,38 @@ public boolean doAuthenticate(final Context context) {
5353

5454

5555
public boolean authenticate(String login, String password) {
56+
// Had to add that as from "not really a good idea" in
57+
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
58+
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
5659
try {
57-
client.authenticateUser(login, password);
58-
return true;
59-
} catch (UserNotFoundException e) {
60-
LOG.debug("User {} not found", login);
61-
return false;
62-
} catch (InactiveAccountException e) {
63-
LOG.debug("User {} is not active", login);
64-
return false;
65-
} catch (ExpiredCredentialException e) {
66-
LOG.debug("Credentials of user {} have expired", login);
67-
return false;
68-
} catch (ApplicationPermissionException e) {
69-
LOG.error("The application is not permitted to perform the requested operation"
70-
+ " on the crowd server", e);
71-
return false;
72-
} catch (InvalidAuthenticationException e) {
73-
LOG.debug("Invalid credentials for user {}", login);
74-
return false;
75-
} catch (OperationFailedException e) {
76-
LOG.error("Unable to authenticate user " + login, e);
77-
return false;
60+
// This will enforce the crowClient to use the plugin classloader
61+
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
62+
try {
63+
client.authenticateUser(login, password);
64+
return true;
65+
} catch (UserNotFoundException e) {
66+
LOG.debug("User {} not found", login);
67+
return false;
68+
} catch (InactiveAccountException e) {
69+
LOG.debug("User {} is not active", login);
70+
return false;
71+
} catch (ExpiredCredentialException e) {
72+
LOG.debug("Credentials of user {} have expired", login);
73+
return false;
74+
} catch (ApplicationPermissionException e) {
75+
LOG.error("The application is not permitted to perform the requested operation"
76+
+ " on the crowd server", e);
77+
return false;
78+
} catch (InvalidAuthenticationException e) {
79+
LOG.debug("Invalid credentials for user {}", login);
80+
return false;
81+
} catch (OperationFailedException e) {
82+
LOG.error("Unable to authenticate user " + login, e);
83+
return false;
84+
}
85+
} finally {
86+
// Bring back the original class loader for the thread
87+
Thread.currentThread().setContextClassLoader(threadClassLoader);
7888
}
7989
}
8090
}

src/main/java/org/sonar/plugins/crowd/CrowdGroupsProvider.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,17 @@ public CrowdGroupsProvider(CrowdClient crowdClient) {
6060
private Collection<String> getGroupsForUser(String username, int start, int pageSize)
6161
throws UserNotFoundException, OperationFailedException, InvalidAuthenticationException,
6262
ApplicationPermissionException {
63-
return transform(crowdClient.getGroupsForNestedUser(username, start, pageSize), GROUP_TO_STRING);
63+
// Had to add that as from "not really a good idea" in
64+
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
65+
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
66+
try {
67+
// This will enforce the crowClient to use the plugin classloader
68+
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
69+
return transform(crowdClient.getGroupsForNestedUser(username, start, pageSize), GROUP_TO_STRING);
70+
} finally {
71+
// Bring back the original class loader for the thread
72+
Thread.currentThread().setContextClassLoader(threadClassLoader);
73+
}
6474
}
6575

6676
private List<String> getGroupsForUser(String username)

src/main/java/org/sonar/plugins/crowd/CrowdRealm.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,26 @@ public void init() {
9393
this.authenticator = new CrowdAuthenticator(crowdClient);
9494
this.usersProvider = new CrowdUsersProvider(crowdClient);
9595
this.groupsProvider = new CrowdGroupsProvider(crowdClient);
96+
// Had to add that as from "not really a good idea" in
97+
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
98+
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
9699
try {
97-
crowdClient.testConnection();
98-
LOG.info("Crowd configuration is valid, connection test successful.");
99-
} catch (OperationFailedException e) {
100-
throw new SonarException("Unable to test connection to crowd", e);
101-
} catch (InvalidAuthenticationException e) {
102-
throw new SonarException("Application name and password are incorrect", e);
103-
} catch (ApplicationPermissionException e) {
104-
throw new SonarException("The application is not permitted to perform the requested "
105-
+ "operation on the crowd server", e);
100+
// This will enforce the crowClient to use the plugin classloader
101+
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
102+
try {
103+
crowdClient.testConnection();
104+
LOG.info("Crowd configuration is valid, connection test successful.");
105+
} catch (OperationFailedException e) {
106+
throw new SonarException("Unable to test connection to crowd", e);
107+
} catch (InvalidAuthenticationException e) {
108+
throw new SonarException("Application name and password are incorrect", e);
109+
} catch (ApplicationPermissionException e) {
110+
throw new SonarException("The application is not permitted to perform the requested "
111+
+ "operation on the crowd server", e);
112+
}
113+
} finally {
114+
// Bring back the original class loader for the thread
115+
Thread.currentThread().setContextClassLoader(threadClassLoader);
106116
}
107117
}
108118

src/main/java/org/sonar/plugins/crowd/CrowdUsersProvider.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,39 @@ public CrowdUsersProvider(CrowdClient crowdClient) {
4747
@Override
4848
public UserDetails doGetUserDetails(String username) {
4949
LOG.debug("Looking up user details for user {}", username);
50-
50+
// Had to add that as from "not really a good idea" in
51+
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
52+
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
5153
try {
52-
User user = crowdClient.getUser(username);
53-
UserDetails details = new UserDetails();
54-
if (user.getDisplayName() != null) {
55-
details.setName(user.getDisplayName());
56-
}
57-
if (user.getEmailAddress() != null) {
58-
details.setEmail(user.getEmailAddress());
54+
// This will enforce the crowClient to use the plugin classloader
55+
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
56+
try {
57+
User user = crowdClient.getUser(username);
58+
UserDetails details = new UserDetails();
59+
if (user.getDisplayName() != null) {
60+
details.setName(user.getDisplayName());
61+
}
62+
if (user.getEmailAddress() != null) {
63+
details.setEmail(user.getEmailAddress());
64+
}
65+
return details;
66+
} catch (UserNotFoundException e) {
67+
return null; // API contract for ExternalUsersProvider
68+
} catch (OperationFailedException e) {
69+
throw new SonarException("Unable to retrieve user details for user" + username
70+
+ " from crowd.", e);
71+
} catch (ApplicationPermissionException e) {
72+
throw new SonarException(
73+
"Unable to retrieve user details for user" + username
74+
+ " from crowd. The application is not permitted to perform the "
75+
+ "requested operation on the crowd server.", e);
76+
} catch (InvalidAuthenticationException e) {
77+
throw new SonarException("Unable to retrieve user details for user" + username
78+
+ " from crowd. The application name and password are incorrect.", e);
5979
}
60-
return details;
61-
} catch (UserNotFoundException e) {
62-
return null; // API contract for ExternalUsersProvider
63-
} catch (OperationFailedException e) {
64-
throw new SonarException("Unable to retrieve user details for user" + username
65-
+ " from crowd.", e);
66-
} catch (ApplicationPermissionException e) {
67-
throw new SonarException(
68-
"Unable to retrieve user details for user" + username
69-
+ " from crowd. The application is not permitted to perform the "
70-
+ "requested operation on the crowd server.", e);
71-
} catch (InvalidAuthenticationException e) {
72-
throw new SonarException("Unable to retrieve user details for user" + username
73-
+ " from crowd. The application name and password are incorrect.", e);
80+
} finally {
81+
// Bring back the original class loader for the thread
82+
Thread.currentThread().setContextClassLoader(threadClassLoader);
7483
}
7584
}
7685

0 commit comments

Comments
 (0)