Not able to read environment variable in persistence.xml - environment-variables

I have a Maven3 project where I'm using the tomcat7-maven-plugin. I would like to set the path for the embedded database via an environment variable argument to the jvm.
Reading the variable with System.getenv("myDataDir") within a Java-Method returns the correct path.
But when I try to set the variable ${myDataDir} in my persistence.xml and then I start tomcat with "mvn tomcat:run" I get FileNotFoundExceptions because the variable is not replaced with the actual value (it says e.g. Cannot find path for ${myDataDir}\derby.log)
I don't know what's causing this - if it's the persistence provider (EclipseLink) that doesn't support this or if it's something else.
My persistence.xml looks like this:
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="myPersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:${myDataDir}/DB;create=true;upgrade=true" />
<property name="javax.persistence.jdbc.user" value="admin" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="both" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.logging.file" value="${myDataDir}/derby.log" />
<property name="eclipselink.application-location" value="${myDataDir}/dbScripts" />
</properties>
</persistence-unit>
</persistence>
EDIT:
I forgot to mention, that I'm in a Spring 3 Framework environment. According to the examples on the web, this should be capable of using environment variables in persistence.xml...

Using ${myDataDir} in a database URL is not valid, unless supported by your database, or unless you are translating the variable during your own build scripts.
What you can do is pass a properties map to Persistence.createEntityManagerFactory() that has the URL you want, that you must build at runtime in code.

Related

Ant property value as parameter variable In JENKINS

I am working on Jenkins.
I have built a job1 with secret text: username and Password variable as
APP1_Dev_password
And using this variable from my ANT script by sending this variable in the predefined parameter to my other job2. I am accessing this variable using
<property name="DBPassword" value="${APP1_Dev_password}"/>
This works well.
But my ant script is a single generalized script for all my applications.
So I have get this APP1_Dev_password string constructed automatically from my ant script using
<property name="constructPasswordVariable" value="${APPLICATIONNAME}_${ENVIRON}_password"/>
<echo message= "constructPasswordVariable: ${constructPasswordVariable}" />
This clearly prints me constructPasswordVariable as APP1_Dev_password.
Now i have to use this value of the constructPasswordVariable property as a variable to fetch from the job1.
<echo message= "PasswordValue: ${${constructPasswordVariable}}" />
This statement fails. Can you guide me of how to work on this.
SOLUTION
<property name="constructPasswordVariable" value="${env.Module}_${env.Environment}_password"/>
<echo message= "constructPasswordVariable: ${constructPasswordVariable}" />
<propertycopy name="prop" from="${constructPasswordVariable}"/>
<echo message= "ENV VALUE: ${prop}" />
Output
constructPasswordVariable: APP1_Dev_password
ENV VALUE: asdhasd
Ant says nested property is not directly supported. Referring documentation from here
However, it can be achieved using library Flaka
Sample: from above reference
<project xmlns:fl="antlib:it.haefelinger.flaka">
<fl:install-property-handler/>
<property name="foo" value="foo.value"/>
<property name="var" value="foo" />
<property name="buildtype" value="test"/>
<property name="appserv_test" value="//testserver"/>
<echo>
#{${var}} = foo.value
<!-- nested property -->
#{appserv_${buildtype}}
</echo>
</project>
There is another reference here which make it possible without additional library as well.
Sample:
<project default="test">
<property name="foo" value="ABC"/>
<property name="pfoo" value="foo"/>
<target name="test">
<echo file="deref.properties">
deref: $${${pfoo}}
</echo>
<property file="deref.properties"/>
</target>

Getting error like weblogic.home env variable must be set to create osb12 config jar while executing ant script Following is the script I am using

config.xml (build file)
Ant script to create An Oracle Service Bus Config Jar from file system.
<target name="run">
<taskdef name="configjar" classname="com.bea.alsb.tools.configjar.ant.ConfigJarTask"/>
<property name="task.failonerror" value="true" />
<property name="task.errorproperty" value="" />
<property name="settingsFile" value="C:\Oracle\Middleware\Oracle_Home\osb\tools\configjar\config.xml"/>
<property name="WL_HOME" value="C:\Oracle\Middleware\Oracle_Home\wlserver" />
<property name="MW_HOME" value="C:\Oracle\Middleware\Oracle_Home\" />
<property name="OSB_HOME" value="C:\Oracle\Middleware\Oracle_Home\osb" />
<!--configjar failonerror="${task.failonerror}" errorProperty="${task.errorproperty}" settingsFile="${settingsFile}" -->
<configjar settingsFile="${settingsFile}" >
</configjar>
</target>
my settings file :
<?xml version="1.0" encoding="utf-8"?>
<configjarSettings xmlns="http://www.bea.com/alsb/tools/configjar/config">
<source>
<project dir="D:\JDeveloper\mywork\ServiceBusApplication2" />
<!--extensionMapping>
<mapping type="str1234" extensions="str1234" />
</extensionMapping-->
<!--fileset>
<include name="str1234" />
<exclude name="str1234" />
</fileset-->
</source>
<configjar jar="SBProject.jar" overwrite="false">
<projectLevel includeSystem="true">
<project>SBProject</project>
</projectLevel>
</configjar>
</configjarSettings>
I have set weblogic environment variable on command prompt , globally using environment variable section of Windows but still not luck.
I am stuck at this point.Need solution on the same.
I am using soa12c.And this is osb script for creation of configuration jar file for osb application.
Go to D:\Oracles\Middleware12c\osb\tools\configjar and run setenv.cmd. On the same cmd try running your ant script. It might ask for MW_HOME, set that also.

can I define the activemq admin console url?

Activemq's admin console, as standard, points to 0.0.0.0:8161.
I know I can change the port from 8161 in the jetty.xml config file.
Is it possible to change the URL from 0.0.0.0?
The answer was pretty obvious. In jetty.xml:
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="port" value="8161" />
<property name="host" value="HOSTNAME" />
</bean>

Running Eclipse WindowTester Pro test from ant script

I am using this script to launch WTP test
<ant target="ui-test" antfile="${eclipse-test-library-file}" dir="${eclipse-test-home}">
<property name="product" value="${productName}" />
<property name="application" value="${applicationName}" />
<property name="os" value="${baseos}"/>
<property name="ws" value="${basews}"/>
<property name="arch" value="${basearch}"/>
<property name="data-dir" value="${eclipse-test-home}/junit-workspace -clean" />
<property name="plugin-name" value="${pluginName}" />
<property name="classname" value="${testClassName}" />
</ant>
The workbench launched but the test did not run. Am I missing something in the ant property?
It's hard to judge what's missing in your setup, but there is an example project for running WindowTester tests from an Ant script here:
http://code.google.com/p/windowtester/downloads/detail?name=WindowTester_AntExample_v1.0.zip
If the example works for you, it should be easy to compare it to your project and find out the difference.

error in persistence.xml

I am trying to deploy a simple EJB project onto Jboss 7.1.1. I have a separate installation of H2 database.
So I changed the standalone.xml as follows:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:tcp://localhost/~/test</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
Now I have also edited the persistence.xml to match the names in the standalone.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="scube" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.sample.model.Property</class>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Eclipse, points an error at line: java:jboss/datasources/ExampleDS
Error is as follows:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'jta-data-source'. One of '{"http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/
xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":shared-cache-mode, "http://java.sun.com/xml/ns/persistence":validation-mode,
"http://java.sun.com/xml/ns/persistence":properties}' is expected.
I searched for similar errors and all the resolutions said that either the order of xml elements were important, which I checked or the jndi name should match with standalone.xml, which does match.
Can someone help me with this?
The right order of XML elements (according to schema document) is:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>com.sample.model.Property</class>

Resources