Skip to content

Commit

Permalink
JAXB and java 11
Browse files Browse the repository at this point in the history
fixes deepy#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.
  • Loading branch information
vai-frd committed Jul 8, 2019
1 parent 760bc07 commit 020274b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 54 deletions.
24 changes: 23 additions & 1 deletion pom.xml
Expand Up @@ -56,6 +56,7 @@
</issueManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.version>6.7</sonar.version>
<sonar.pluginName>Crowd</sonar.pluginName>
<sonar.pluginClass>org.sonar.plugins.crowd.CrowdPlugin</sonar.pluginClass>
Expand Down Expand Up @@ -91,6 +92,26 @@
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<!-- unit tests -->
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
Expand Down Expand Up @@ -118,12 +139,13 @@
<url>https://packages.atlassian.com/maven-public-legacy-local</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
Expand Down
52 changes: 31 additions & 21 deletions src/main/java/org/sonar/plugins/crowd/CrowdAuthenticator.java
Expand Up @@ -53,28 +53,38 @@ public boolean doAuthenticate(final Context context) {


public boolean authenticate(String login, String password) {
// Had to add that as from "not really a good idea" in
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
try {
client.authenticateUser(login, password);
return true;
} catch (UserNotFoundException e) {
LOG.debug("User {} not found", login);
return false;
} catch (InactiveAccountException e) {
LOG.debug("User {} is not active", login);
return false;
} catch (ExpiredCredentialException e) {
LOG.debug("Credentials of user {} have expired", login);
return false;
} catch (ApplicationPermissionException e) {
LOG.error("The application is not permitted to perform the requested operation"
+ " on the crowd server", e);
return false;
} catch (InvalidAuthenticationException e) {
LOG.debug("Invalid credentials for user {}", login);
return false;
} catch (OperationFailedException e) {
LOG.error("Unable to authenticate user " + login, e);
return false;
// This will enforce the crowClient to use the plugin classloader
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
try {
client.authenticateUser(login, password);
return true;
} catch (UserNotFoundException e) {
LOG.debug("User {} not found", login);
return false;
} catch (InactiveAccountException e) {
LOG.debug("User {} is not active", login);
return false;
} catch (ExpiredCredentialException e) {
LOG.debug("Credentials of user {} have expired", login);
return false;
} catch (ApplicationPermissionException e) {
LOG.error("The application is not permitted to perform the requested operation"
+ " on the crowd server", e);
return false;
} catch (InvalidAuthenticationException e) {
LOG.debug("Invalid credentials for user {}", login);
return false;
} catch (OperationFailedException e) {
LOG.error("Unable to authenticate user " + login, e);
return false;
}
} finally {
// Bring back the original class loader for the thread
Thread.currentThread().setContextClassLoader(threadClassLoader);
}
}
}
12 changes: 11 additions & 1 deletion src/main/java/org/sonar/plugins/crowd/CrowdGroupsProvider.java
Expand Up @@ -60,7 +60,17 @@ public CrowdGroupsProvider(CrowdClient crowdClient) {
private Collection<String> getGroupsForUser(String username, int start, int pageSize)
throws UserNotFoundException, OperationFailedException, InvalidAuthenticationException,
ApplicationPermissionException {
return transform(crowdClient.getGroupsForNestedUser(username, start, pageSize), GROUP_TO_STRING);
// Had to add that as from "not really a good idea" in
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
try {
// This will enforce the crowClient to use the plugin classloader
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
return transform(crowdClient.getGroupsForNestedUser(username, start, pageSize), GROUP_TO_STRING);
} finally {
// Bring back the original class loader for the thread
Thread.currentThread().setContextClassLoader(threadClassLoader);
}
}

private List<String> getGroupsForUser(String username)
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/org/sonar/plugins/crowd/CrowdRealm.java
Expand Up @@ -93,16 +93,26 @@ public void init() {
this.authenticator = new CrowdAuthenticator(crowdClient);
this.usersProvider = new CrowdUsersProvider(crowdClient);
this.groupsProvider = new CrowdGroupsProvider(crowdClient);
// Had to add that as from "not really a good idea" in
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
try {
crowdClient.testConnection();
LOG.info("Crowd configuration is valid, connection test successful.");
} catch (OperationFailedException e) {
throw new SonarException("Unable to test connection to crowd", e);
} catch (InvalidAuthenticationException e) {
throw new SonarException("Application name and password are incorrect", e);
} catch (ApplicationPermissionException e) {
throw new SonarException("The application is not permitted to perform the requested "
+ "operation on the crowd server", e);
// This will enforce the crowClient to use the plugin classloader
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
try {
crowdClient.testConnection();
LOG.info("Crowd configuration is valid, connection test successful.");
} catch (OperationFailedException e) {
throw new SonarException("Unable to test connection to crowd", e);
} catch (InvalidAuthenticationException e) {
throw new SonarException("Application name and password are incorrect", e);
} catch (ApplicationPermissionException e) {
throw new SonarException("The application is not permitted to perform the requested "
+ "operation on the crowd server", e);
}
} finally {
// Bring back the original class loader for the thread
Thread.currentThread().setContextClassLoader(threadClassLoader);
}
}

Expand Down
53 changes: 31 additions & 22 deletions src/main/java/org/sonar/plugins/crowd/CrowdUsersProvider.java
Expand Up @@ -47,30 +47,39 @@ public CrowdUsersProvider(CrowdClient crowdClient) {
@Override
public UserDetails doGetUserDetails(String username) {
LOG.debug("Looking up user details for user {}", username);

// Had to add that as from "not really a good idea" in
// https://stackoverflow.com/questions/51518781/jaxb-not-available-on-tomcat-9-and-java-9-10
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
try {
User user = crowdClient.getUser(username);
UserDetails details = new UserDetails();
if (user.getDisplayName() != null) {
details.setName(user.getDisplayName());
}
if (user.getEmailAddress() != null) {
details.setEmail(user.getEmailAddress());
// This will enforce the crowClient to use the plugin classloader
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
try {
User user = crowdClient.getUser(username);
UserDetails details = new UserDetails();
if (user.getDisplayName() != null) {
details.setName(user.getDisplayName());
}
if (user.getEmailAddress() != null) {
details.setEmail(user.getEmailAddress());
}
return details;
} catch (UserNotFoundException e) {
return null; // API contract for ExternalUsersProvider
} catch (OperationFailedException e) {
throw new SonarException("Unable to retrieve user details for user" + username
+ " from crowd.", e);
} catch (ApplicationPermissionException e) {
throw new SonarException(
"Unable to retrieve user details for user" + username
+ " from crowd. The application is not permitted to perform the "
+ "requested operation on the crowd server.", e);
} catch (InvalidAuthenticationException e) {
throw new SonarException("Unable to retrieve user details for user" + username
+ " from crowd. The application name and password are incorrect.", e);
}
return details;
} catch (UserNotFoundException e) {
return null; // API contract for ExternalUsersProvider
} catch (OperationFailedException e) {
throw new SonarException("Unable to retrieve user details for user" + username
+ " from crowd.", e);
} catch (ApplicationPermissionException e) {
throw new SonarException(
"Unable to retrieve user details for user" + username
+ " from crowd. The application is not permitted to perform the "
+ "requested operation on the crowd server.", e);
} catch (InvalidAuthenticationException e) {
throw new SonarException("Unable to retrieve user details for user" + username
+ " from crowd. The application name and password are incorrect.", e);
} finally {
// Bring back the original class loader for the thread
Thread.currentThread().setContextClassLoader(threadClassLoader);
}
}

Expand Down

1 comment on commit 020274b

@zrsm
Copy link

@zrsm zrsm commented on 020274b Jul 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I tried your original commits, it looks like this fixed the issue that was being experienced and now sonar-crowd is authenticating properly. I did have to remove the vbnet and csharp plugins -> Do you know if these new changes fixes the need to remove those plugins?

Please sign in to comment.