Cargo unable to set the system properties values during bean injection

236 views Asked by At

What I am trying to do?
- For testing, I want to set serverKey and validity for my application and want cargo to inject into TokenUtils, so these values are available when the application is loaded

What I have tried?
I am using JBoss as Container and the relevant code is

@Stateless
public class TokenUtils {

    public static final String AUTH_TOKEN = "X-BB-AUTH";
    public static final String BB_AUTH_PRIVATE_KEY = "auth.key";
    public static final String BB_AUTH_VALIDITY = "auth.validity";
    private static final Pattern PATTERN = Pattern.compile(":");

    private String serverPrivateKey;
    private long validity;

    private static final Logger LOGGER = LoggerFactory.getLogger(TokenUtils.class);

    @SuppressWarnings("UnusedDeclaration")
    public TokenUtils() {
    }

    public TokenUtils(@Nonnull @Configuration(BB_AUTH_PRIVATE_KEY) final String serverPrivateKey,
                      @Configuration(BB_AUTH_VALIDITY) final long validity) {
        this.serverPrivateKey = serverPrivateKey;
        this.validity = validity;

        LOGGER.info("serverPrivateKey:" + serverPrivateKey);
        LOGGER.info("validity:" + validity);
    }
}

Also, I have META-INF/beans.xml in the src/main/resources of the same package where TokenUtils is created, so bean instantiation is not a problem I guess

I want the values to be injected when cargo instantiate the bean, so I set the values in pom.xml as

<container>
  <systemProperties>
    <auth.key>secure</auth.key>
    <auth.validity>720000</auth.validity>
  </systemProperties>
</container>

When I start cargo, I see the following happening

INFO] [talledLocalContainer] 06:07:08,068 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named TokenUtils in deployment unit deployment "market.war" are as follows:
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer]   java:global/market/TokenUtils!com.org.proj.service.authentication.TokenUtils
[INFO] [talledLocalContainer]   java:app/market/TokenUtils!com.com.proj.service.authentication.TokenUtils
[INFO] [talledLocalContainer]   java:module/TokenUtils!com.com.proj.service.authentication.TokenUtils
[INFO] [talledLocalContainer]   java:global/market/TokenUtils
[INFO] [talledLocalContainer]   java:app/market/TokenUtils
[INFO] [talledLocalContainer]   java:module/TokenUtils

Problem?
- But the values are serverPrivateKey=null and validity=0
- Moreover, during bean instantiation, I should be able to see LOGGER.info values, but I can't find them in logs

What is that I am doing wrong?

1

There are 1 answers

0
daydreamer On BEST ANSWER

I realized the fault. I was not using @Inject in my TokenUtils constructor. The following change fixed it

@Inject
public TokenUtils(@Nonnull @Configuration(BB_AUTH_PRIVATE_KEY) final String serverPrivateKey,
                      @Configuration(BB_AUTH_VALIDITY) final long validity) {
        this.serverPrivateKey = serverPrivateKey;
        this.validity = validity;

        LOGGER.info("serverPrivateKey:" + serverPrivateKey);
        LOGGER.info("validity:" + validity);
    }

With that I could see values in logs as following

[INFO] [talledLocalContainer] 06:39:30,042 INFO  [com.org.proj.commons.cdi.config.ConfigurationProvider] (http--0.0.0.0-9090-1) getProperty(auth.key) is secure
[INFO] [talledLocalContainer] 06:39:30,043 INFO  [com.org.proj.commons.cdi.config.ConfigurationProvider] (http--0.0.0.0-9090-1) getProperty(auth.validity) is 720000