Changing the value of XML file based on key using Ant - regex

I have a xml snippet like below
<bean id="aqConnectionFactoryInitialiser" class="foo.aqjms.spring.OracleAqConnectionFactoryInitialiser">
<property name="URL" value="jdbc:oracle:thin:#192.168.80.182:1234:foo"/>
<property name="username" value="foo"/>
<property name="password" value="xAU2oMLjNXU1GrKbcsjvMQ=="/>
</bean>
Using Ant, I only need to change value="foo" to value="bar" based on name="username" I tried the below snippet but it replaced all foo occurrences
<replace file="E:/vipul/drchanges_testing/testsnippet.xml">
<replacefilter token="foo" value="bar" />
</replace>
How can I achieve this using Ant?

first you need to select vlaue="foo" using this :
(?<=name="username")\s+(value=".*?")
then replace it by value="bar"
demo for regex

Related

Copy the value from a property in an Ant propertyfile to another property

I need to copy the value of one property in a propertyfile to a second property, and add that to the propertyfile.
For example, if I have a property file Test.properties containing
2018=jan;feb;mar
2019=jan;feb;mar
2020=jan;feb;mar
********************************
name=john,math,sudha
my input property is "2020" and output is "2021", after running Ant Test.properties should contain
2018=jan;feb;mar
2019=jan;feb;mar
2020=jan;feb;mar
2021=jan;feb;mar
********************************
name=john,math,sudha
with out changing the order How could I do that?
You might be able to use something based on the following:
The idea is to read the propertyfile, then use two <propertyset> instances with the <echoproperties> task to update the file.
<property name="prop.file" value="Test.properties" />
<property name="input" value="2020" />
<property name="output" value="2021" />
<property name="pf" value="prefix" />
<property name="input.prop" value="${pf}.${input}" />
<loadproperties srcfile="${prop.file}" prefix="${pf}" />
<echoproperties destfile="${prop.file}">
<propertyset>
<propertyref prefix="${pf}" />
<mapper type="glob" from="${pf}.*" to="*" />
</propertyset>
<propertyset>
<propertyref name="${input.prop}" />
<mapper type="glob" from="${input.prop}*" to="${output}*" />
</propertyset>
</echoproperties>
There's more code there than you might expect: the "prefix" is being used to ensure that the properties loaded from the file don't clash with any in your Ant buildfile as properties are imutable.
The order of the propertysets in the echoproperties task is important, especially if there is already a value for property "2021" in the file that you are updating. Where properties appear in both sets, the value in the last propertyset seen "wins" and is echoed to the output file.
<property name="prop.file" value="test.properties" />
<property name="input" value="2020" />
<property name="output" value="2021" />
<replaceregexp file="${prop.file}"
match="${input}(=)(.*)"
replace="${input}=\2${line.separator}${output}=\2"
flags="gi"
byline="true" />

In WSO2 EI / ESB how do I process array/object based query parameters? (i.e. with square brackets)

I have an API that uses query parameters as follows:
/events/search
?title=royal
&area=southeast
&maint[date]=20180823
&maint[user]=oscar
&maint[action]=release
(line breaks added for readability)
Processing the simple query params within WSO2 EI is straightforward. There are a few ways to do this using the property mediator:
<property name="title" expression="get-property('query.param.title')"/>
<property name="title" expression="$url:title"/>
<property name="title" expression="$ctx:query.param.title"/>
However, I have been unable to process the array/object based query params. I have tried to use the property mediator in various ways, non of which work:
<property name="maintDate" expression="get-property('query.param.maint[date]')"/>
<property name="maintDate" expression="get-property('query.param.maint.date')"/>
<property name="maintDate" expression="$url:maint[date]"/>
<property name="maintDate" expression="$url:maint.date"/>
<property name="maintDate" expression="$url:maint[date]"/>
<property name="maintDate" expression="$url:maint%5Bdate%5D"/>
Has anyone had an experience and any success in this area?
Your ESB API URL template will look like below:
uri-template="/test?title={t}&area={a}&maint[date]={date}&maint[user]={user}&maint[action]={action}
You can access the square bracket query params as below:
<property name="date" expression="$ctx:uri.var.date"/>
<property name="user" expression="$ctx:uri.var.user"/>
<property name="action" expression="$ctx:uri.var.action"/>

CXF Transform feature

I am new to CXF,I have a requirement to drop few tags from the input XML .so I am using CXF Transform feature ,which should drop the version tag from my input XML ,I am able change but not drop. Kindly let me know how can I achieve it
<bean id="transformFeature" class="org.`enter code here`apache`enter code here`.cxf.feature.StaxTransformFeature">
<property name="inTransformElem`enter code here`ents">
<map>
<entry key="version" value=""/>
</map>
</property>
</bean>
You need to specify the namespace of the element. For example, if the version element has a namespace of http://www.example.org/test, you would need to configure the CXF transformation feature as follows:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="inTransformElements">
<map>
<entry key="{http://www.example.org/test}version value=""/>
</map>
</property>
</bean>
You also need to add the feature to your jaxws:endpoint configuration, if you have not already done so.
<jaxws:endpoint ...>
<jaxws:features>
<ref bean="transformFeature" />
</jaxws:features>
</jaxws:endpoint>

Handling multiple Rest web services using Spring and Castor

Would anyone be able to advise me on the best way of handling access to multiple Rest web services using Springs RestTemplate?
I know that the RestTemplate object has a message converter reference (MarshallingHttpMessageConverter) which in turn has a reference to an unmarshaller. In my case I am using the Spring Frameworks CastorMarshaller object with associated mapping file.
Normally I could have just added all my mappings to one Castor mapping file. However in my case all the web services are of this format (block below) with the < rows ... /> holding different entities depending on the service called.
<data>
<output>
<dataset>
<row id="" .... />
<row id="" .... />
<row id="" .... />
<row id="" .... />
<row id="" .... />
</dataset>
</output>
<nextUpdate><nextUpdate/>
</data>
The CastorMarshaller is injected into the MessageConverter which itself is injected into the RestTemplate in the application context configuration file.
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="castorMarshaller"/>
<property name="unmarshaller" ref="castorMarshaller"/>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="xml"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="xml"/>
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocation" value="classpath:oxm-mapping-worldweather.xml"/>
</bean>
Possible options that I have been thinking about:
1 Create multiple RestTemplates for each Rest service.
2 Create multiple MessageConverters for the different services and change the message converters on the template when accessing a different service.
3 Create multiple CasterMarshaller objects for the different services and update the message converter with the new unmarshaller
What is the best way to approach handling multiple services like this with the same root and sub elements?
Thanks in advance
Sman UK
If specifying multiple mapping files is the issue then below is the solution.
Use mappingLocations property instead of mappingLocation as given below,
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocations">
<list>
<value>classpath:oxm-mapping-worldweather.xml</value>
<value>classpath:sample-mapping.xml</value>
</list>
</property>
</bean>

Jpa2/hibernate c3p0 pool configuration lead connection and thread leaks

I am trying to use c3p0 as connection pool provider in my application (jpa 2 and hibernate core 3.3.2) but c3p0 getting too much connection from database.This is same problem but answer not helped.I am getting entitymanager from entitymanager factory and storing entitymanager in a thread local variable,thus a am using a fresh entiymanager for each request.(by the way if i use hibernate default pool no problem)
<property name="hibernate.connection.provider_class"
value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.min_size" value="1" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate. c3p0.initialPoolSize" value="3" />
<property name="hibernate.c3p0.numHelperThreads" value="3" />
<property name="hibernate.c3p0.maxPoolSize" value="10" />
<property name="hibernate.c3p0.minPoolSize" value="1" />
<property name="hibernate.c3p0.maxIdleTime" value="3600" />
<property name="hibernate.c3p0.maxIdleTimeExcessConnections" value="300" />
<property name="hibernate.c3p0.unreturnedConnectionTimeout" value="3600" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.breakAfterAcquireFailure" value="false" />
<property name="hibernate.c3p0.acquireRetryAttempts" value="1" />
<property name="hibernate.c3p0.acquireRetryDelay" value="100" / >
Can you ensure that you are not defining pool at both datasource and sessionFactory level.