activemq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From roelens8 <...@git.apache.org>
Subject [GitHub] activemq-artemis pull request #1366: ARTEMIS-1252 Add service loading of pas...
Date Thu, 29 Jun 2017 18:25:16 GMT
Github user roelens8 commented on a diff in the pull request:

    https://github.com/apache/activemq-artemis/pull/1366#discussion_r124877640
  
    --- Diff: artemis-commons/src/main/java/org/apache/activemq/artemis/utils/PasswordMaskingUtil.java
---
    @@ -108,22 +109,34 @@ public static HashProcessor getHashProcessor() {
     
           // semi colons
           String[] parts = codecDesc.split(";");
    -
           if (parts.length < 1)
              throw new ActiveMQException(ActiveMQExceptionType.ILLEGAL_STATE, "Invalid PasswordCodec
value: " + codecDesc);
     
           final String codecClassName = parts[0];
     
           // load class
    -      codecInstance = AccessController.doPrivileged(new PrivilegedAction<SensitiveDataCodec<String>>()
{
    -         @Override
    -         public SensitiveDataCodec<String> run() {
    -            ClassLoader loader = Thread.currentThread().getContextClassLoader();
    +      codecInstance = AccessController.doPrivileged((PrivilegedAction<SensitiveDataCodec<String>>)
() -> {
    +         ServiceLoader<SensitiveDataCodec> serviceLoader = ServiceLoader.load(SensitiveDataCodec.class,
PasswordMaskingUtil.class.getClassLoader());
    +         try {
    +            // Service load the codec, if a service is available
    +            for (SensitiveDataCodec<String> codec : serviceLoader) {
    +               if ((codec.getClass().getCanonicalName()).equals(codecClassName)) {
    +                  return codec.getClass().newInstance();
    +               }
    +            }
    +            // If a service is not available, load the codec class using this class's
class loader
    +            return (SensitiveDataCodec<String>) PasswordMaskingUtil.class.getClassLoader().loadClass(codecClassName).newInstance();
    +         } catch (Exception e) {
                 try {
    -               Class<?> clazz = loader.loadClass(codecClassName);
    -               return (SensitiveDataCodec<String>) clazz.newInstance();
    -            } catch (Exception e) {
    -               throw ActiveMQUtilBundle.BUNDLE.errorCreatingCodec(e, codecClassName);
    +               // If service loading failed, load the codec class using this class's
class loader
    +               return (SensitiveDataCodec<String>) PasswordMaskingUtil.class.getClassLoader().loadClass(codecClassName).newInstance();
    --- End diff --
    
    I have cleaned up the code here. It should not attempt to load the codec twice using this
class's class loader, if it fail had the first time. 
    
    I also updated the documentation to mention that the custom password codec can be service
loaded.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message