Search code examples
glassfishpayara-micro

Custom resources equivalent to jetty JNDI resource


I have a web application which is until now deployed to a jetty server but now in order to use other JavaEE services I'm shifting to Payara (Glassfish) container. However I can't find how to provide additional JNDI resources as configuration to paraya. Currently I have some resources like this defined in my jetty's context.xml file:

<New id="some_resource" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>
        <Ref refid='wac'/>
    </Arg>
    <Arg>resource/path</Arg>
    <Arg>
        <New class="com.example.some.Class">
            <!-- constructor parameters -->
            <Arg type="java.lang.String">some string</Arg>
            <Arg type="java.lang.Integer">0</Arg>
        </New>
    </Arg>
</New>

this simply calls the custom class constructor with given parameters and puts the result into given resource/path address.

Is this possible to be done in payara micro?


Solution

  • It's possible to create a custom resource that is exposed via JNDI, though GlassFish/Payara support only primitive types by default. For other types you would need to add a custom factory in the server classpath.

    Custom resources in Payara/GlassFish are defined in domain.xml, in element custom-resource. Best way to define a custom resource is either using Admin Console (Resources -> JNDI -> Custom resources) or asadmin command.

    For a string value "some string" under a JNDI resource/path, the asadmin would look like this:

    asadmin> create-custom-resource --restype java.lang.String --factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property value="some string" "resource/path"
    

    In Payara Micro, you can either pass domain.xml using --domainConfig argument, or you can execute the same asadmin command from within your application, using PayaraMicroRuntime.run() (documented here)