Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolveEnvironment Issue #1

Open
milindbangar79 opened this issue Feb 1, 2019 · 11 comments
Open

resolveEnvironment Issue #1

milindbangar79 opened this issue Feb 1, 2019 · 11 comments
Assignees
Labels

Comments

@milindbangar79
Copy link

Hi,

I am using the following code within a POJO:

@Inject @Property(resolveEnvironment = true) private String abc;

I have created an environment variable of the same name, but always get an exception while Injecting the property and assigning the value.

Queries:

  1. Is the syntax correct as mentioned above?
  2. The code is deployed to JBOSS as WAR and have included the dependent JAR . Is it correct?

Kindly help.

Thanks
Milind

@MikeEdgar MikeEdgar self-assigned this Feb 4, 2019
@MikeEdgar
Copy link
Member

@milindbangar79, the resolveEnvironment attribute works in two phases. The true values indicates that the result of the lookup (phase 1) may contain environment references that should be resolved (phase 2).

For example, the simplest way to have it work as you expect is to specify a defaultValue that references the environment, assuming your environment variable is abc as well:

@Inject
@Property(resolveEnvironment = true, defaultValue = "${env.abc}") 
private String abc;

You could also specify a system property named abc with a value of "${env.abc}", but that would just be an additional step. If there is demand to use the environment directly without first resolving a property value, please let me know and I'll look in to it.

@milindbangar79
Copy link
Author

Hi Mike,

Thanks for the response.

Current requirement is to read the data from application.properties file in classpath.

We do have a requirement to set the data for password field(s) in POJO to use environment variables. Will the approach be same as mentioned in your response?

Kindly let me know.

Regards
Milind

@MikeEdgar
Copy link
Member

@milindbangar79 , yes you could use the same approach that I mentioned. Additionally, you could do something like the example below, provided that your application.properties has an entry named secretPasswordValue with a value that references the environment variable. That might give you some more flexibility depending on your needs.

Unfortuately, this library does not (yet) have a default properties file location, like application.properties, so you need to specify it with the resource attribute on the injection point.

POJO:

@Inject
@Property(resolveEnvironment = true,
          resource = @PropertyResource("classpath:application.properties"))
private String secretPasswordValue;

application.properties

prop1=something
prop2=something_else
secretPasswordValue=${env.PASSWORD_VARIABLE}

@milindbangar79
Copy link
Author

Hi Mike,

Thanks for the quick response. I will try and get back.

Regards
Milind

@milindbangar79
Copy link
Author

milindbangar79 commented Feb 6, 2019

Hi Mark,

I tried both the approaches.

Approach 1 : The first time calling the service endpoint, I get javax.enterprise.inject.InjectionException. When I call the endpoitn again, it does not give the exception. This behaviour I have observed while trying to set the values of class variables for which some are being resolved through application.properties and few through environment variables.

Approach 2: It's not working as expected and getting NullPointerException on the field in application.properties file used in code.

Kindly help.

Regards
Milind

@milindbangar79
Copy link
Author

milindbangar79 commented Feb 6, 2019

Hi Mark,

With second approach:

  1. Are we expecting the application.properties file to be part of WAR file deployed to server?
  2. Always get InjectionException stating while calling the servive using Property Injection: classpath resource not found : application.properties
  3. In local , I start the server as standalone.bat -P="path for application.properties"
  4. The value of environment variable is not getting set, leading to NullPointerException in the code

e.g. secretPasswordValue=${env.PASSWORD_VARIABLE} is returning the value ${env.PASSWORD_VARIABLE} instead of the value set in environment.

Am I missing anything??

Running the code on Wildfly V14 and using javax.inject library, testing on Windows platform, wherein manually setting the environment variables.

Thanks & Regards
Milind

@MikeEdgar
Copy link
Member

MikeEdgar commented Feb 7, 2019

@milindbangar79 ,

Regarding approach 1 - can you provide the stack trace you are seeing? At least the non-sensitive lines involving packages in "io.xlate" and those involving the application server?

Regarding approach 2 - the properties file does not need to be in the WAR file. My understanding of the -P parameter for the standalone.bat script is that it sets the system properties (i.e. those that you would typically set using -D parameters to Java processes) and it does not place the file on the class path. If you're taking that approach, you should remove the resource = @PropertyResource("classpath:application.properties") attribute and the secretPasswordValue property will be found as a system property.

@milindbangar79
Copy link
Author

milindbangar79 commented Feb 7, 2019

Hi Mike,

Thanks for the response.

Regarding Approach 1 following is the stack trace when i invoke the service for first time:

Context Path:
Servlet Path:/rest
Path Info:/v1/emp/employees
Query String:null
Stack Trace:
org.jboss.resteasy.spi.UnhandledException: javax.enterprise.inject.InjectionException: javax.enterprise.inject.InjectionException: Class-path resource not found: application.properties
	at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:78)
	at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:222)
	at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:197)
	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:459)
	at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:233)
	at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:139)
	at org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:358)
	at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:142)
	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:219)
	at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:227)
	at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
	at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
	at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
	at io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:55)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.server.handlers.MetricsHandler.handleRequest(MetricsHandler.java:64)
	at io.undertow.servlet.core.MetricsChainHandler.handleRequest(MetricsChainHandler.java:59)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1349)
	at java.lang.Thread.run(Thread.java:745)
Caused by: javax.enterprise.inject.InjectionException: javax.enterprise.inject.InjectionException: Class-path resource not found: application.properties
	at io.xlate.inject.PropertyProducerBean.produceProperty(PropertyProducerBean.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:95)
	at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:85)
	at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:103)
	at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:161)
	at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:180)
	at org.jboss.weld.contexts.unbound.DependentContextImpl.get(DependentContextImpl.java:70)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:700)
	at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:800)
	at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
	at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:336)
	at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:347)
	at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:69)
	at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
	at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:71)
	at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)
	at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
	at org.jboss.weld.contexts.unbound.DependentContextImpl.get(DependentContextImpl.java:70)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:700)
	at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:800)
	at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
	at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:336)
	at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:347)
	at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:69)
	at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
	at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:71)
	at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)
	at org.jboss.resteasy.cdi.JaxrsInjectionTarget.inject(JaxrsInjectionTarget.java:44)
	at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
	at org.jboss.weld.contexts.AbstractContext.get(AbstractContext.java:96)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
	at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.get(ContextualInstanceStrategy.java:177)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:102)
	at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:131)
	at com.resteasy.mongodb.service.EmployeeServiceImpl$Proxy$_$$_WeldClientProxy.getAllEmployees(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140)
	at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:509)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:399)
	at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$0(ResourceMethodInvoker.java:363)
	at org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:358)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:365)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:337)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:310)
	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:443)
	... 57 more
Caused by: javax.enterprise.inject.InjectionException: Class-path resource not found: application.properties
	at io.xlate.inject.ClasspathURLStreamHandler.openConnection(ClasspathURLStreamHandler.java:24)
	at java.net.URL.openConnection(URL.java:979)
	at java.net.URL.openStream(URL.java:1045)
	at io.xlate.inject.PropertyFactory.getProperties(PropertyFactory.java:101)
	at io.xlate.inject.PropertyFactory.getProperty(PropertyFactory.java:85)
	at io.xlate.inject.PropertyProducerBean.getProperty(PropertyProducerBean.java:304)
	at io.xlate.inject.PropertyProducerBean.produceProperty(PropertyProducerBean.java:55)
	... 113 more

When I try for second time, it just works... Expected response from Rest service below:

[{"id":{"timestamp":1547214901,"machineIdentifier":16386846,"processIdentifier":10489,"counter":13452982,"time":1547214901000,"date":"2019-01-11T13:55:01.000+0000","timeSecond":1547214901},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1547214916,"machineIdentifier":16386846,"processIdentifier":10489,"counter":13452984,"time":1547214916000,"date":"2019-01-11T13:55:16.000+0000","timeSecond":1547214916},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1547465571,"machineIdentifier":16386846,"processIdentifier":17431,"counter":7236929,"time":1547465571000,"date":"2019-01-14T11:32:51.000+0000","timeSecond":1547465571},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1547466583,"machineIdentifier":16386846,"processIdentifier":17431,"counter":5641735,"time":1547466583000,"date":"2019-01-14T11:49:43.000+0000","timeSecond":1547466583},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1547550647,"machineIdentifier":16386846,"processIdentifier":1363,"counter":4916737,"time":1547550647000,"date":"2019-01-15T11:10:47.000+0000","timeSecond":1547550647},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1547658691,"machineIdentifier":2199010,"processIdentifier":8982,"counter":2804426,"time":1547658691000,"date":"2019-01-16T17:11:31.000+0000","timeSecond":1547658691},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548343346,"machineIdentifier":16386846,"processIdentifier":2774,"counter":11920523,"time":1548343346000,"date":"2019-01-24T15:22:26.000+0000","timeSecond":1548343346},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548706395,"machineIdentifier":16386846,"processIdentifier":9058,"counter":2168682,"time":1548706395000,"date":"2019-01-28T20:13:15.000+0000","timeSecond":1548706395},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548707178,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427072,"time":1548707178000,"date":"2019-01-28T20:26:18.000+0000","timeSecond":1548707178},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548707279,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427075,"time":1548707279000,"date":"2019-01-28T20:27:59.000+0000","timeSecond":1548707279},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548707651,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427078,"time":1548707651000,"date":"2019-01-28T20:34:11.000+0000","timeSecond":1548707651},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548707654,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427080,"time":1548707654000,"date":"2019-01-28T20:34:14.000+0000","timeSecond":1548707654},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548707656,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427082,"time":1548707656000,"date":"2019-01-28T20:34:16.000+0000","timeSecond":1548707656},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548707658,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427084,"time":1548707658000,"date":"2019-01-28T20:34:18.000+0000","timeSecond":1548707658},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548708026,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427086,"time":1548708026000,"date":"2019-01-28T20:40:26.000+0000","timeSecond":1548708026},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548708039,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427088,"time":1548708039000,"date":"2019-01-28T20:40:39.000+0000","timeSecond":1548708039},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548708140,"machineIdentifier":16386846,"processIdentifier":9058,"counter":14427092,"time":1548708140000,"date":"2019-01-28T20:42:20.000+0000","timeSecond":1548708140},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1548973049,"machineIdentifier":16386846,"processIdentifier":5841,"counter":16761484,"time":1548973049000,"date":"2019-01-31T22:17:29.000+0000","timeSecond":1548973049},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}},{"id":{"timestamp":1549529113,"machineIdentifier":16386846,"processIdentifier":-29567,"counter":763279,"time":1549529113000,"date":"2019-02-07T08:45:13.000+0000","timeSecond":1549529113},"version":1,"empID":"134898","salary":120000.23,"address":{"number":"9","street":"Craw Drive","town":"Preston","postcode":"EX12 8DU"}}]

For Approach 2 : I will work and share the outcome soon.

Regards
Milind

@milindbangar79
Copy link
Author

milindbangar79 commented Feb 7, 2019

Hi Mark,

Regarding Approach 2, wherein I am using mix of application.properties and environment variable, got the following exception:

Servlet Path:/rest
Path Info:/v1/emp/employees
Query String:null
Stack Trace:
org.jboss.resteasy.spi.UnhandledException: javax.enterprise.inject.InjectionException: javax.enterprise.inject.InjectionException: Class-path resource not found: com/resteasy/mongodb/db/DBConnector.properties
	at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:78)
	at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:222)
	at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:197)
	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:459)
	at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:233)
	at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:139)
	at org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:358)
	at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:142)
	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:219)
	at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:227)
	at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
	at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
	at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
	at io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:55)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.server.handlers.MetricsHandler.handleRequest(MetricsHandler.java:64)
	at io.undertow.servlet.core.MetricsChainHandler.handleRequest(MetricsChainHandler.java:59)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1349)
	at java.lang.Thread.run(Thread.java:745)
Caused by: javax.enterprise.inject.InjectionException: javax.enterprise.inject.InjectionException: Class-path resource not found: com/resteasy/mongodb/db/DBConnector.properties
	at io.xlate.inject.PropertyProducerBean.produceProperty(PropertyProducerBean.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:95)
	at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:85)
	at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:103)
	at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:161)
	at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:180)
	at org.jboss.weld.contexts.unbound.DependentContextImpl.get(DependentContextImpl.java:70)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:700)
	at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:800)
	at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
	at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:336)
	at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:347)
	at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:69)
	at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
	at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:71)
	at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)
	at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
	at org.jboss.weld.contexts.unbound.DependentContextImpl.get(DependentContextImpl.java:70)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:700)
	at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:800)
	at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
	at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:336)
	at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:347)
	at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:69)
	at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
	at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:71)
	at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)
	at org.jboss.resteasy.cdi.JaxrsInjectionTarget.inject(JaxrsInjectionTarget.java:44)
	at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
	at org.jboss.weld.contexts.AbstractContext.get(AbstractContext.java:96)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
	at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.get(ContextualInstanceStrategy.java:177)
	at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
	at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:102)
	at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:131)
	at com.resteasy.mongodb.service.EmployeeServiceImpl$Proxy$_$$_WeldClientProxy.getAllEmployees(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140)
	at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:509)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:399)
	at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$0(ResourceMethodInvoker.java:363)
	at org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:358)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:365)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:337)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:310)
	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:443)
	... 57 more
**Caused by: javax.enterprise.inject.InjectionException: Class-path resource not found: com/resteasy/mongodb/db/DBConnector.properties**
	at io.xlate.inject.ClasspathURLStreamHandler.openConnection(ClasspathURLStreamHandler.java:24)
	at java.net.URL.openConnection(URL.java:979)
	at java.net.URL.openStream(URL.java:1045)
	at io.xlate.inject.PropertyFactory.getProperties(PropertyFactory.java:101)
	at io.xlate.inject.PropertyFactory.getProperty(PropertyFactory.java:85)
	at io.xlate.inject.PropertyProducerBean.getProperty(PropertyProducerBean.java:304)
	at io.xlate.inject.PropertyProducerBean.produceProperty(PropertyProducerBean.java:55)
	... 113 more

My Properties File:
env.mongodb.host=localhost port=${env.MONGODB_PORT} env.mongodb.dbname=test env.mongodb.username="" env.mongodb.password=""

Had set the MONGODB_PORT as environment variable on my local machine.
Code in class:
@Inject @Property(resolveEnvironment = true) private String port;

Thanks
Milind

@milindbangar79
Copy link
Author

Hi Mark,

For the second approach wherein I have removed @PropertyResource annotation from all class variables , how does the library resolves the value from environment variable?

As the value is being set in properties file 'classVariable=${env.ENV_VALUE}' , what should be the approach to set the values class it's referenced?

I believe, I am missing some config here...

Thanks
Milind

@MikeEdgar
Copy link
Member

@milindbangar79 - does this file exist on your application's class path? com/resteasy/mongodb/db/DBConnector.properties ? For the second approach, since you are adding the application.properties to the system properties, they will be found by the framework there (i.e. using System.getProperties()).

It seems that neither of the properties files are on the class path based on the stack traces.

I published an updated version with some refactoring done to the resource (properties file) lookup code. Please try updating to version 1.0.4 and see if that helps. https://github.com/xlate/property-inject/wiki/Maven-Configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants