Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glassfish create JDBCResources, -Pools and Security Realms from application

How can I create JDBCResources, -Pools and Security Realms in a Glassfish 3.1 Server from within my Application, if they are not already created? I am writing an application that relies on this resources, however I don't want to configure the server manually every time the application is deployed on a different server.

Doing this with a shell script feels like a workaround.

like image 274
iuiz Avatar asked Dec 13 '25 00:12

iuiz


2 Answers

Glassfish provides a REST interface. You can create a new security (authentication) realm in a certain configuration (say, server-config in a DAS on localhost, admin port 4848) with a POST to:

http://localhost:4848/management/domain/configs/config/server-config/security-service/auth-realm

Do a GET to that resource to see the parameters.

You can use the same interface to create connection pools.

like image 134
Artefacto Avatar answered Dec 14 '25 21:12

Artefacto


Ok, I found a solution for half of the Question.

I created a file called glassfish-resources.xml in my WEB-INF folder and added the following content to it:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <jdbc-connection-pool
            name="java:app/jdbc/BeerUserPool"
            res-type="javax.sql.DataSource"
            datasource-classname="org.postgresql.ds.PGSimpleDataSource"
            pool-resize-quantity="2"
            max-pool-size="32"
            steady-pool-size="0"
            statement-timeout-in-seconds="30">
        <property name="User" value="USERNAME"></property>
        <property name="Password" value="PASSWORD"></property>
        <property name="PortNumber" value="12345678"></property>
        <property name="dataBaseName" value="DATABASE_NAME"></property>
        <property name="ServerName" value="yourDBUrl.com"></property>
        <property name="Ssl" value="false"></property>
        <property name="ProtocolVersion" value="0"></property>
    </jdbc-connection-pool>
    <jdbc-resource 
        pool-name="java:app/jdbc/BeerUserPool"
        jndi-name="java:app/jdbc/BeerUser"></jdbc-resource>
    <
</resources>

Addingt the java:app/ to the names is important, without it it won't work correctly. This connection pool is also only application scoped and gets destroyed after the application is undebloyed (except you add an additional argument).

This pool can now be accessed with JPA with the following persistence.xml.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="jsf-jpa-war" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>java:app/jdbc/BeerUser</jta-data-source>
        <properties>
            <property name="eclipselink.logging.level" value="FINE"/>
        </properties>
    </persistence-unit>
</persistence>

However I found no soultion how I can define the security realms in the same way.

like image 24
iuiz Avatar answered Dec 14 '25 22:12

iuiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!