Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a custom resource also be lookuped in glassfish by the java:com/env-scheme?

I've created some JDBC resources and Custom resources in GlassFish. I named JDBC resource jdbc/mydb and the Custom resource service/test.

The JDBC connection is lookedup with the call ic.lookup("java:comp/env/jdbc/mydb"). It can also be found with ic.lookup("jdbc/mydb"), which has some disadvantages, see answer of Robin below.

But my custom resource has to be lookedup with ic.lookup("service/test"). The line ic.lookup("java:comp/env/service/test") does not work. Is it possible, and if yes: how?

like image 948
doekman Avatar asked Mar 11 '09 13:03

doekman


2 Answers

If you created your custom-resources in your domain.xml, maybe you forgot to add them as a resource-ref under the servers/server tag in your domain.xml:

    <resources>
        <!-- JDBC RESOURCE -->
        ...
        <custom-resource res-type="java.lang.String" jndi-name="MyCustomStringResource"
                         factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory">
            <property name="value" value="hello, this is a string resource"></property>
        </custom-resource>
        ...
    </resources>
    <servers>
        <server name="server" config-ref="server-config">
            <application-ref ref="__admingui" virtual-servers="__asadmin"></application-ref>
            ...
            <resource-ref ref="MyCustomStringResource"></resource-ref>
            ...
        </server>
    </servers>
like image 186
Palesz Avatar answered Nov 13 '22 03:11

Palesz


The prefix indicates the usage of a resource reference which has been defined for the Java EE entity from which the JNDI lookup is being made. You would have to define such a reference for you custom resource as well to look it up the same way.

The prefix is not necessary for the JDBC lookup either, as you can use the full jndi name instead. The problem with that approach, is if you move the resource such that it is no longer located in the local initial context, you will not find it without changing the lookup name in your code to include the location information. Using a reference shields your code from this, so only the mapping in the reference would change.

Some info here on usage

like image 3
Robin Avatar answered Nov 13 '22 01:11

Robin