eclipse-ee4j / glassfish

Eclipse GlassFish
https://eclipse-ee4j.github.io/glassfish/
378 stars 144 forks source link

JNDI Lookup Failure - Glassfish 7.0.3 #24381

Closed cws-khuntly closed 1 year ago

cws-khuntly commented 1 year ago

Environment Details


Problem Description

After creating JNDI resources using the following asadmin commands:

create-custom-resource --restype java.lang.String --factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property "value=dev" environment create-custom-resource --restype java.lang.String --factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property "value=www.mydomain.com" webURL create-custom-resource --restype java.lang.Boolean --factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property "value=true" isTestSystem create-custom-resource --restype java.lang.Boolean --factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property "value=true" isResetAvailable

They appear in domain.xml and in the console under JNDI -> Custom Resources, as expected. Lookup fails with a NameNotFoundException, with the following code:

` try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:comp/env/");

        environment = (String) envContext.lookup("environment");
    }
    catch (final NamingException nx)
    {
        ERROR_RECORDER.error(nx.getMessage(), nx);
    }

`

Steps to reproduce

Add JNDI Custom Resources Install application that utilizes these resources Get NameNotFoundException

The relevant code block is above, but if you need to see all the code it's in my code repo - specifically this filter:

https://github.com/cwsus/eSolutionsShared/blob/main/eSolutionsCore/src/main/java/com/cws/esolutions/core/filters/ResponseTimeFilter.java

Though there are others. I don't see a way to attach files here, otherwise I'd give a WAR file to reproduce

Impact of Issue

Trying to separate production vs development environment variables utilizing the container instead of build properties.

OndroMih commented 1 year ago

Hi, thank you for raising this and all the details.

Until this is fixed, have you considered using Microprofile Config to separate the configuration from your application?

GlassFish provides MicroProfile Config 3.0 (https://github.com/eclipse/microprofile-config/releases/tag/3.0.2) using the Helidon MicroProfile Config implementation. You can refer to Helidon documentation to find out how to use it: https://helidon.io/docs/latest/#/mp/config/introduction

In short, instead of defining a JNDI resource, you can specify a system property or an environment variable. A system property can be specified either using -D JVM option or as a system property on the server - in Admin Console: server -> properties tab, System properties tab. Then you can inject the value value in your application or retrieve it programmatically:

Programmatically:

Config config = ConfigProvider.getConfig();
String value = config.getOptionalValue("my.configuration", String.class).orElse("No value in configuration");

Inject into a CDI bean:

@Inject @ConfigProperty(name = "my.configuration", defaultValue = "No value in configuration") String value;
cws-khuntly commented 1 year ago

Is this cross-container compatible? e.g. can I use the same code with, say tomcat?

On Tue, Apr 18, 2023, 21:58 Ondro Mihályi @.***> wrote:

Hi, thank you for raising this and all the details.

Until this is fixed, have you considered using Microprofile Config to separate the configuration from your application?

GlassFish provides MicroProfile Config 3.0 ( https://github.com/eclipse/microprofile-config/releases/tag/3.0.2) using the Helidon MicroProfile Config implementation. You can refer to Helidon documentation to find out how to use it: https://helidon.io/docs/latest/#/mp/config/introduction

In short, instead of defining a JNDI resource, you can specify a system property or an environment variable. A system property can be specified either using -D JVM option or as a system property on the server - in Admin Console: server -> properties tab, System properties tab. Then you can inject the value value in your application or retrieve it programmatically:

Programmatically:

Config config = ConfigProvider.getConfig(); String value = config.getOptionalValue("my.configuration", String.class).orElse("No value in configuration");

Inject into a CDI bean:

@Inject @ConfigProperty(name = "my.configuration", defaultValue = "No value in configuration") String value;

— Reply to this email directly, view it on GitHub https://github.com/eclipse-ee4j/glassfish/issues/24381#issuecomment-1514027290, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIJB4OWRZ6F6DGKVSTL3CDXB5BE7ANCNFSM6AAAAAAXDEGY3Y . You are receiving this because you authored the thread.Message ID: @.***>

OndroMih commented 1 year ago

MicroProfile Config solution is compatible only with containers that implement also MicroProfile, alongside Jakarta EE. Such as GlassFish, OpenLiberty, Payara, and WildFly. Tomcat doesn't provide MicroProfile out of the box, it also doesn't provide most of Jakarta EE out of the box. But you can add this funcitonality by adding the helidon-microprofile-config JAR into the Tomcat's classpath or into your application. At least the programmatic way should definitely work. Injection doesn't work on Tomcat without a CDI container added to Tomcat.

cws-khuntly commented 1 year ago

Okay, thank you. I will wait for the fix to advise that our app is glassfish compatible.

thanks!


Kevin Huntly Email: @.*** Cell: 716/424-3311


-----BEGIN GEEK CODE BLOCK----- Version: 1.0 GCS/IT d+ s a C++ UL+++$ P+(++) L+++ E--- W+++ N+ o K(+) w--- O- M-- V-- PS+ PE Y(+) PGP++(+++) t+ 5-- X-- R+ tv+ b++ DI++ D++ G++ e(+) h--- r+++ y+++* ------END GEEK CODE BLOCK------

On Wed, Apr 19, 2023 at 5:58 AM Ondro Mihályi @.***> wrote:

MicroProfile Config solution is compatible only with containers that implement also MicroProfile, alongside Jakarta EE. Such as GlassFish, OpenLiberty, Payara, and WildFly. Tomcat doesn't provide MicroProfile out of the box, it also doesn't provide most of Jakarta EE out of the box. But you can add this funcitonality by adding the helidon-microprofile-config JAR into the Tomcat's classpath or into your application. At least the programmatic way should definitely work. Injection doesn't work on Tomcat without a CDI container added to Tomcat.

— Reply to this email directly, view it on GitHub https://github.com/eclipse-ee4j/glassfish/issues/24381#issuecomment-1514458310, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIJB4O3EIUQ6N77LTXA733XB6ZKVANCNFSM6AAAAAAXDEGY3Y . You are receiving this because you authored the thread.Message ID: @.***>

avpinchuk commented 1 year ago

@cws-khuntly, please try this:

  1. Remove trailing / in naming context, should be java:comp/env
  2. Add resource-ref section to web.xml for each your custom resource. For example:
<resource-ref>
    <res-ref-name>environment</res-ref-name>
    <res-type>java.lang.String</res-type>
    <lookup-name>environment</lookup-name>
</resource-ref>
cws-khuntly commented 1 year ago

hi, I already have those in place and did remove the trailing / (it was a typo tbh)

avpinchuk commented 1 year ago

It works?

cws-khuntly commented 1 year ago

In Tomcat and WebSphere, yes, but not in Glassfish.

From: Alexander Pinčuk @.> Sent: Wednesday, April 26, 2023 4:40 PM To: eclipse-ee4j/glassfish @.> Cc: Kevin Huntly @.>; Mention @.> Subject: Re: [eclipse-ee4j/glassfish] JNDI Lookup Failure - Glassfish 7.0.3 (Issue #24381)

It works?

- Reply to this email directly, view it on GitHub https://github.com/eclipse-ee4j/glassfish/issues/24381#issuecomment-1524016 723 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIJB4JITSDR333OBKY2N6LXD GBYLANCNFSM6AAAAAAXDEGY3Y . You are receiving this because you were mentioned. https://github.com/notifications/beacon/ABIJB4OGXYGZQ7SJDWU425DXDGBYLA5CNFS M6AAAAAAXDEGY32WGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTS 222TFG.gif Message ID: @. @.> >

avpinchuk commented 1 year ago

@cws-khuntly, I wrote simple test app. Looks like the JNDI lookup is working. Need more info to try to solve your problem. Thanks.