0

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?

mohamnag
  • 2,709
  • 5
  • 27
  • 40

1 Answers1

0

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)

OndroMih
  • 7,280
  • 1
  • 26
  • 44
  • so directly calling the constructor of the resource is not possible? and I have to create one factory per resource type. – mohamnag Dec 14 '16 at 08:38
  • You could create a single generic factory for all classes and pass constructor arguments via properties. If you do, it would be also worth to contribute via a pull request. I don't think that Payara/GlassFish contains such factory out of the box. – OndroMih Dec 14 '16 at 23:41
  • Ok thanks for clarification, that would be lot of help. I have to find some time then to write such a proper generic factory maybe by looking at what [jetty has](https://github.com/eclipse/jetty.project/) – mohamnag Dec 15 '16 at 08:34
  • It seems there is already a [generic factory for Java Beans](https://docs.oracle.com/cd/E19798-01/821-1752/giyvw/index.html). I found out just now by searching the net. – OndroMih Dec 15 '16 at 09:54
  • java beans but not any class. As in beans you need a default parameterless ctor which not an acceptable standard for me. – mohamnag Dec 15 '16 at 14:53
  • I see. Having a factory to create resources using constructor would be nice. As Payara only accepts properties which don't retain order, I recommend to accept a property with a name of a JSON/XML file that contains the definition of the resource. If you publish your code, I'm willing to contribute. – OndroMih Dec 16 '16 at 12:47