Glassfish create JDBCResources, -Pools and Security Realms from application - glassfish

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.

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.

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.

Related

TeamCity API method to get Slack notification info

Im trying to list my TeamCity builds in my own site with the TeamCity API to create dashboard. One feature I need is to notify the right person. To do so I set in the build setting the right Slack channel as the notification configuration.
My question is, is there a way to get this info from the TeamCity API? And if not, is there any way to get this info from other place?
Thanks!
UPD: This is how the Slack notifier is configured:
On this picture we choose the 'Slack' option in menu.
Here in bottom option we set the slack channel for this build
You get get the information about Build features via TeamCity REST API and the features will in turn contain information about configured Slack notifiers.
Example of a response for a GET request to https://<serverUrl>/app/rest/buildTypes/id:MY_BUILD_CONFIGURATION_ID:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<buildType id="MY_BUILD_CONFIGURATION_ID" name="Build configuration name" projectName="project name" projectId="PROJECT_ID" href="/app/rest/buildTypes/id:MY_BUILD_CONFIGURATION_ID" webUrl="https://<serverUrl>/viewType.html?buildTypeId=MY_BUILD_CONFIGURATION_ID">
...
<features count="1">
<feature id="BUILD_EXT_63" type="notifications">
<properties count="11">
<property name="branchFilter" value="+:<default>
+:br_name"/>
<property name="buildFinishedFailure" value="true"/>
<property name="buildFinishedSuccess" value="true"/>
<property name="firstSuccessAfterFailure" value="true"/>
<property name="notifier" value="jbSlackNotifier"/>
<property name="plugin:notificator:jbSlackNotifier:addBranch" value="true"/>
<property name="plugin:notificator:jbSlackNotifier:addBuildStatus" value="true"/>
<property name="plugin:notificator:jbSlackNotifier:channel" value="#slack-channel-name"/>
<property name="plugin:notificator:jbSlackNotifier:connection" value="PROJECT_EXT_10"/>
<property name="plugin:notificator:jbSlackNotifier:maximumNumberOfChanges" value="10"/>
<property name="plugin:notificator:jbSlackNotifier:messageFormat" value="verbose"/>
</properties>
</feature>
</features>
...
</buildType>
The property you are interested in is plugin:notificator:jbSlackNotifier:channel

No process engine found. camunda Webapp cannot work without a process engine

When I set up a camunda BPMN platform, I got the following error
HTTP Status 500 -
org.camunda.bpm.webapp.impl.IllegalWebAppConfigurationException: No
process engine found. camunda Webapp cannot work without a process
engine
Confirming that the both the '/camunda' and '/engine-rest' is deployed and running in Tomcat.
But /engine-rest returns 404 with following error.
description : The requested resource is not available.
Please help!.
You need to configure the shared process engine for your server as described in https://docs.camunda.org/manual/7.5/installation/full/tomcat/manual/#add-bpm-platform-xml
Have added bpm-platform.xml to '/conf'
<?xml version="1.0" encoding="UTF-8"?>
<bpm-platform xmlns="http://www.camunda.org/schema/1.0/BpmPlatform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.camunda.org/schema/1.0/BpmPlatform http://www.camunda.org/schema/1.0/BpmPlatform ">
<job-executor>
<job-acquisition name="default" />
</job-executor>
<process-engine name="default">
<job-acquisition>default</job-acquisition>
<configuration>org.camunda.bpm.engine.impl.cfg.StandaloneProcessEngineConfiguration</configuration>
<datasource>java:jdbc/ProcessEngine</datasource>
<properties>
<property name="history">full</property>
<property name="databaseSchemaUpdate">true</property>
<property name="authorizationEnabled">true</property>
<property name="jobExecutorDeploymentAware">true</property>
</properties>
</process-engine>
</bpm-platform>

Payara setup together with sniffy profiler

I'm trying to profile application running on Payara server with Sniffy profiler.
Maven dependency is added and file web.xml is modified according to the documentation.
I have added sniffy.jar to the payara\payara41\glassfish\domains\domain1\lib\ folder.
I have modified glassfish-resources.xml file as follows:
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/Agenda" object-type="user" pool-name="AgendaPool">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="oracle.jdbc.pool.OracleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="AgendaPool" non-transactional-connections="false" ping="false" pool-resize-quantity="2" pooling="true" res-type="javax.sql.DataSource" statement-cache-size="0" statement-leak-reclaim="false" statement-leak-timeout-in-seconds="0" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="URL" value="sniffer:jdbc:oracle:thin:#localhost:1521:XE"/>
<property name="User" value="XXX"/>
<property name="Password" value="XXX"/>
<property name="driverClass" value="io.sniffy.MockDriver"/>
</jdbc-connection-pool>
</resources>
I'm able to see HTTP methods response times but not queries response times:
No query response time.
Is my configuration correct? Why there are no response times for executed queries?
Sniffy doesn't work with oracle.jdbc.pool.OracleDataSource
See more details in this question: Invalid Oracle URL specified with Sniffy

Mule - make Global Elements more global

In mule I have many applications running on the same container that access a jdbc connector with the same connection string/user/password set.
Of course any app has configured the same global connector in its xml configuration file, so there is code duplication.
Is there a way to define only once per container the connection and access it from any app?
I would try this: have one app create the datasource and store it in JNDI and have the other apps pick it up from JNDI.
Since there is no strong guarantee of app start ordering, it's possible that one app that needs the JNDI datasource would start too soon. You would need to configure Spring to be able to perform the JNDI lookup again in case of failure and configure a threaded retry policy on the Mule JDBC connector.
Also you will need to install the datasource and database JARs in lib/user so all apps could use them.
Just create a spring bean for your JDBC connector in xml some where in your system and have all your applications load it in your apps:
<spring:import resource="JDBC-beans.xml" />
and the xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
</beans>
I have solved this kind of problem using a Domain project, where I inserted all database configurations that have been used by other projects.

How to setup a JTA data source in EclipseLink/JPA 2.0 in an Java EE 6 application for derby embedded database?

I am not able to find ecbDB JTA data source in the unit test.
For RESOURCE_LOCAL the same persistence.xml works. Only within an EJB container it fails to locate a JTA data source.
I get the following exception when the unit test is executed..
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
java.lang.RuntimeException: javax.naming.NamingException: Lookup failed for 'ecbDB' in SerialContext [Root exception is javax.naming.NameNotFoundException: ecbDB not found]
at org.glassfish.persistence.jpa.PersistenceUnitInfoImpl.<init>(PersistenceUnitInfoImpl.java:111)
Here is persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ecbPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>ecbDB</jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property
name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property
name="javax.persistence.jdbc.url"
value="jdbc:derby:target/ecbDB;create=true" />
<!-- EclipseLink 2.0 properties -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="sql-script" />
<!-- <property name="eclipselink.ddl-generation.output-mode" value="database"
/> -->
<property name="eclipselink.application-location" value="./target/ddl" />
<property name="eclipselink.create-ddl-jdbc-file-name"
value="create.sql" />
<property name="eclipselink.drop-ddl-jdbc-file-name" value="drop.sql" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.logging.file" value="./target/eclipselink.logs" />
</properties>
</persistence-unit>
</persistence>
Here is a step-by-step description I have used to set up JTA based connection with EclipseLink.
First, make sure that:
GlassFish server is up and running
asadmin list-domains
Apache Derby server is up and running
NetworkServerControl.bat sysinfo -h localhost -p 1527
Next, create JDBC Connection Pool and JDBC Resource
create JDBC Connection Pool that uses org.apache.derby.jdbc.EmbeddedDataSource
asadmin create-jdbc-connection-pool
--datasourceclassname org.apache.derby.jdbc.EmbeddedDataSource
--restype javax.sql.XADataSource
--property databaseName=ecbDB:serverName=localhost:portNumber=1527:password=APP: user=APP:connectionAttributes=\;create\=true EmbeddedPool
create JDBC Resource
asadmin create-jdbc-resource --connectionpoolid EmbeddedPool jdbc/EmbeddedResource
If you take a look at GlassFish Administration Console (localhost:4848) you will notice new nodes:
Resources > JDBC > JDBC Connection Pools > EmbeddedPool
Resources > JDBC > JDBC Resources > jdbc/EmbeddedResource
As you can see jdbc/EmbeddedResource has been assigned to EmbeddedPool.
Then, create a minimal version of persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ecbPU">
<jta-data-source>jdbc/EmbeddedResource</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
Use stateless/stateful session beans as components to manage persistence operations (as far as I know this is the preferred strategy for Java EE applications):
public interface ECBService {
}
#Stateless
public class ECBServiceBean implements ECBService {
#PersistenceContext(unitName = "ecbPU")
protected EntityManager em;
}
Use setter injection to assign environment naming context dependency into a field (useful in case of unit testing):
public class ECBServlet extends HttpServlet {
private ECBService service;
#EJB
public void setECBService(EcbService service) {
this.service = service;
}
}
Finally, deployment produces the following:
INFO: EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507
FINE: Detected database platform: org.eclipse.persistence.platform.database.JavaDBPlatform
CONFIG: connecting(DatabaseLogin(
platform=>DatabasePlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
CONFIG: Connected: jdbc:derby:ecbDB
User: APP
Database: Apache Derby Version: 10.10.1.1 - (1458268)
Driver: Apache Derby Embedded JDBC Driver Version: 10.10.1.1 - (1458268)
CONFIG: connecting(DatabaseLogin(
platform=>JavaDBPlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
CONFIG: Connected: jdbc:derby:ecbDB
User: APP
Database: Apache Derby Version: 10.10.1.1 - (1458268)
Driver: Apache Derby Embedded JDBC Driver Version: 10.10.1.1 - (1458268)
I hope it helps.
First make sure your derby configuration have a suitable JDBC resource (with the shell command "asadmin list-jdbc-resources"). If not, make one with "asadmin create-jdbc-resource". For help type "asadmin list-commands".
Are you running this under embedded Glassfish? If so the problem you are is seeing is because he has a JTA datasource ecbDB is not defined in your domain.xml. In that case Kaster's answer is on the right track and you should create your embedded connection pool and associated JDBC resource. You can use the asadmin command or Glassfish web admin GUI. At that point you should also remove the two javax.persistence.jdbc properties from your persistence.xml because those settings will be convered by your connection pool resource settings.