How to call many databases without hard-coding? - apache

I have following scenario:
There are many sites/departments and every site has it's own ServiceMix and different number of databases. Those databases are grouped, for example there are two groups: production_one and production_two.
On every site I deployed bundles as a datasource, one datasource for every database.
Datasource: blueprint.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<!-- Properties -->
<!-- blueprint property placeholders, that will use etc/datasources.cfg as the properties file -->
<cm:property-placeholder id="db.placeholder" persistent-id="datasources">
</cm:property-placeholder>
<!-- Datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://${bundle_ds_1_db_host}:${bundle_ds_1_db_port};databaseName=${bundle_ds_1_db_database};" />
<property name="username" value="${bundle_ds_1_db_username}" />
<property name="password" value="${bundle_ds_1_db_password}" />
</bean>
<!-- Init database-->
<bean id="initDatabase"
class="com.xyz.mssql.ds.DatabaseInitBean"
init-method="create" destroy-method="destroy">
<property name="ds" ref="dataSource" />
</bean>
<service ref="dataSource" interface="javax.sql.DataSource">
<service-properties>
<entry key="datasource.name" value="${bundle_ds_1_ds_name}" />
<entry key="datasource.type" value="${bundle_ds_1_ds_type}" />
<entry key="datasource.id" value="${bundle_ds_1_ds_id}" />
<entry key="datasource.group_id" value="${bundle_ds_1_ds_group_id}" />
</service-properties>
</service>
There is also bundle, which receives a SOAP message via cxf and process it by calling database:
CXF: blueprint.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<cxf:cxfEndpoint id="processXEndpoint" address="/mssql-soap1/"
serviceClass="com.xyz.mssql.soap1.ProcessXService" />
<bean id="saveDataProcessor"
class="com.xyz.mssql.soap1.SaveDataProcessor">
<property name="ds" ref="dataSource" />
</bean>
<bean id="getDataProcessor"
class="com.xyz.mssql.soap1.GetDataProcessor">
<property name="ds" ref="dataSource" />
</bean>
<!-- DataSource -->
<reference id="dataSource" interface="javax.sql.DataSource"
filter="(datasource.name=mssql-ds-1)">
<reference-listener bind-method="onBind"
unbind-method="onUnbind">
<bean class="com.xyz.mssql.soap1.ListenerBean" />
</reference-listener>
</reference>
<!-- Camel Context -->
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="cxf">
<from uri="cxf:bean:processXEndpoint" />
<recipientList>
<simple>direct:${header.operationName}</simple>
</recipientList>
</route>
<route id="saveRoute">
<from uri="direct:saveData" />
<log message="Calling saveData" />
<process ref="saveDataProcessor" />
</route>
<route id="getRoute">
<from uri="direct:getData" />
<log message="Call getData" />
<process ref="getDataProcessor" />
</route>
</camelContext>
</blueprint>
The problem is, that following code inject to bean (ex: SaveDataProcessor) only one datasource resolved by it's name "datasource.name=mssql-ds-1", but I need to call many databases and on every servicemix installation there will be different number of them.
I need to call all databases (datasources) which have same type, for example "datasource.type=production_one".
Is it possible? How to do it?
How to pass to bean an array of datasources, or call this bean against every datasource which meet same conditions (name, type and so on).

Related

SQL Delete query is not working in apache camel

SQL Delete query is not working in camel, as it stuck at the SQL query and nothing happens. Have tried to execute same query in DB directly, it is working fine as expected.
Below is the glimpse of code.
<?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.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close" id="OracleConnection">
<property name="driverClassName"
value="${Oracle.DB.DriverClassName}" />
<property name="url" value="${Oracle.DB.Url}" />
<property name="username" value="${Oracle.DB.Username}" />
<property name="password" value="${Oracle.DB.Password}" />
<property name="initialSize" value="${Oracle.DB.InitialSize}" />
<property name="maxTotal" value="${Oracle.DB.MaxTotal}" />
<property name="minIdle" value="${Oracle.DB.MinIdle}" />
<property name="maxIdle" value="${Oracle.DB.MaxIdle}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="removeAbandonedOnMaintenance" value="true" />
<property name="defaultAutoCommit" value="true" />
<property name="maxWaitMillis"
value="${Oracle.DB.MaxWaitMillis}" />
</bean>
<!-- configure the Camel SQL component to use the JDBC data source -->
<bean class="org.apache.camel.component.sql.SqlComponent" id="sql">
<property name="dataSource" ref="OracleConnection" />
</bean>
<camelContext id="GenerateOTPContext"
xmlns="http://camel.apache.org/schema/spring">
<route id="GenerateOTPMainRoute" streamCache="true">
<from id="_from1"
uri="restlet:http://localhost:9092/ESB/dbserviceapi/generateOTP?restletMethod=POST" />
<setHeader headerName="deleteQuery" id="_setHeader2">
<simple>sql:DELETE FROM MOBILE_OTP_DETAILS WHERE created_dt < SYSDATE - INTERVAL '20' MINUTE</simple>
</setHeader>
<log id="_log2" message="Delete query ${header.deleteQuery}" />
<recipientList delimiter="~" id="_recipientList2"
ignoreInvalidEndpoints="true">
<simple>${header.deleteQuery}</simple>
</recipientList>
<onException id="_onException1" useOriginalMessage="true">
<exception>java.sql.SQLException</exception>
<exception>java.lang.Exception</exception>
<handled>
<constant>true</constant>
</handled>
<log id="_log3"
message="ESBUUID : ${exchangeId} Exception occurred in GenerateOTPAdapter : ${exception.stacktrace}" />
</onException>
</route>
</camelContext>
</beans>
I'm using dbcp2, pool2 and camel-sql components to connect to DB.
Any suggestions please?
Have found the issue, I had created my own table but missed to grant the delete permission, so it was not deleting the data in DB from camel components so I granted the permission for my table and it worked.

Wildfly 8.2.0.Final: Misconfiguration because of example datasource or persistence.xml?

I have three datasources configured in my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
<persistence-unit name="MyPersistenceTestUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="hibernate.connection.username" value="myuser" />
<property name="hibernate.connection.password" value="mypass" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
<persistence-unit name="MyLoggingUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="hibernate.connection.username" value="myuser" />
<property name="hibernate.connection.password" value="mypass" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
</persistence>
The first is the 'default' unit. It's used by the standard entity manager. The second is needed for running JUnit tests outside of a Java EE environment. The third one is used for logging to be independent from JTA transactions.
This works. But when I start Wildfly, I even get some of these errors (although I don't use H2 in my application):
21:32:33,748 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 51) HHH000319: Could not get database metadata: org.h2.jdbc.JdbcSQLException: Table "PG_CLASS" not found; SQL statement:
select relname from pg_class where relkind='S' [42102-173]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
It only happens when the application is deployed.
The idea was to disable or remove the ExampleDS by admin console or in standalone.xml, but when I do that, I get a startup with errors:
21:36:20,992 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "com.zonacroft.mycuisine.webserver-TRUNK.war")]) - failure description: {"JBAS014771: Services with missing/unavailable dependencies" => [
"jboss.persistenceunit.\"com.mydomain.myapp.webserver-TRUNK.war#MyLoggingUnit\".__FIRST_PHASE__ is missing [jboss.data-source.java:jboss/datasources/ExampleDS]",
"jboss.persistenceunit.\"com.mydomain.myapp.webserver-TRUNK.war#MyPersistenceTestUnit\".__FIRST_PHASE__ is missing [jboss.data-source.java:jboss/datasources/ExampleDS]"
What on earth is going on here? Why does this persistence.xml need the exampleDS? Are the RESOURCE_LOCAL persistence units maybe wrong configured? (But if so, why do they work then). So what's the problem here?
[UPDATE]
I figured out that I don't get startup errors (with exampleDS enabled) when I remove the hibernate.hbm2ddl.auto properties from the RESOURCE_LOCAL persistence units. But it annoys me that I have to let the exampleDS of Wildfly enabled. Otherwise Wildfly would not start with the application as described above. Why is this so. What's wrong here?
You only define a datasource for the first persistence unit:
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
That means WildFly uses the default datasource exampleDS for your other persistence units.
This causes the exception, since the configured PostgreSQL dialect does not work for the default H2 datasource.

Error on persistence.xml configuring HSQLDB with Hibernate

I am configuring one HSQLDB database inside the application folder, follow below the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="crmUnity" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.crm.model.entities.Cliente</class>
<class>br.com.crm.model.entities.Contato</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:file://database/crm;shutdown=true" />
<property name="hibernate.connection.user" value="sa" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
</properties>
</persistence-unit>
</persistence>
The JBOSS 7.1.3 while starting show the error on log:
18:40:10,477 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 170) HHH000319: Could not get database metadata: java.sql.SQLException: No suitable driver found for jdbc:hsqldb:file://database/crm;shutdown=true
Can anybody help me about configure the jdbc url to resolve this error?
Thank's, TarcĂ­sio.

How to Setup SSL for Camel Route using the embedded Active MQ Broker in a service mix?

I am trying to use blueprint XML for the setup
My Blueprint XML looks like this
<?xml version="1.0" encoding="UTF-8"?>
<blueprint
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="file:activemq/input"/>
<to uri="file:activemq/output"/>
<setBody>
<simple>
FileMovedEvent(file: ${file:name}, timestamp: ${date:now:hh:MM:ss.SSS})
</simple>
</setBody>
<to uri="activemqs:queue:Main.Queue.Vibhav"/>
</route>
</camelContext>
<bean id="activemqConnectionFactory" class="org.apache.activemq.ActiveMQSslConnectionFactory">
<property name="brokerURL" value="ssl://localhost:61616" />
<property name="keyStore" value="file:C:/Users/xxx/Desktop/apache-servicemix-4.5.3/etc/myclient.ks"/>
<property name="keyStorePassword" value="test123"/>
<property name="trustStore" value = "file:C:/Users/xxx/Desktop/apache-servicemix-4.5.3/etc/myclient.ts"/>
</bean>
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="8" />
<property name="connectionFactory" ref="activemqConnectionFactory" />
</bean>
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConnectionFactory"/>
<property name="transacted" value="false"/>
<property name="concurrentConsumers" value="10"/>
</bean>
<bean id="activemqs" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig"/>
</bean>
</blueprint>
Note: In the broker I have already added the ssl context and the ssl connectors, also the plugin which has ssl certificate with the jaas realm.
I fixed this issue by setting the trust store password in the connection factory properties.

Do I need to manually set authenticationManager in spring?

After loading ApplicationContext I got a warning like this:
_ INFO: No authentication manager set. Reauthentication of users when changing passwords will not be performed. _
My Context.XML file is like this:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.6.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- =============== Security =============== -->
<sec:method-security-metadata-source
id="method-security-metadata-source">
<sec:protect access="MyAccess"
method="springsecuritytest._00_base.AuthenticationTester.*" />
</sec:method-security-metadata-source>
<sec:global-method-security
access-decision-manager-ref="accessDecisionManager"
secured-annotations="enabled" pre-post-annotations="enabled"
proxy-target-class="true">
<sec:protect-pointcut
expression="execution(* springsecuritytest._00_base.AuthenticationTester.*(..))"
access="ROLE_USER_BASIC_099" />
<!-- <sec:protect-pointcut access="ROLE_USER_BASIC_099" expression="execution(*
springsecuritytest._00_base.AuthenticationTester.* (..))" /> -->
</sec:global-method-security>
<sec:authentication-manager alias="authenticationManager"
erase-credentials="true">
<sec:authentication-provider>
<sec:jdbc-user-service data-source-ref="dataSource" />
<!-- role-prefix="ROLE_" /> -->
</sec:authentication-provider>
</sec:authentication-manager>
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased">
<property name="decisionVoters">
<list>
<bean class="org.springframework.security.access.vote.RoleVoter" />
<!-- <bean class="org.springframework.security.access.vote.AuthenticatedVoter"/> -->
</list>
</property>
</bean>
<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/spring_security" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
any body can help me?
I found it, it seems to be caused by the bean definition model I used.
I too was experiencing this nebulous message in the log. I had to add a reference to my authentication manager in the http and UserDetailsManager in the xml configuration file. This will depend on how Spring security is configured, but hopefully it will help!
<security:http auto-config="true" authentication-manager-ref="authenticationManager" use-expressions="true">
<security:remember-me data-source-ref="dataSource" user-service-ref="userDetailsManagerDao" />
<security:intercept-url pattern="/" access="permitAll" />
<security:intercept-url pattern="/home" access="permitAll" />
<security:intercept-url pattern="/login" access="permitAll" />
<security:intercept-url pattern="/registration" access="permitAll" />
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<security:form-login login-page="/login" default-target-url="/default" login-processing-url="/login/authenticate"
username-parameter="username" password-parameter="password" authentication-failure-url="/login?error" />
<security:logout logout-url="/logout" logout-success-url="/login?logout" />
</security:http>
<bean id="userDetailsManagerDao" class="com.alphatek.tylt.repository.UserDetailsManagerJdbcDao">
<property name="dataSource" ref="dataSource" />
<property name="enableAuthorities" value="false" />
<property name="enableGroups" value="true" />
<property name="authenticationManager" ref="authenticationManager" />
</bean>
<security:authentication-manager id="authenticationManager">
<security:authentication-provider user-service-ref="userDetailsManagerDao">
<security:password-encoder ref="passwordEncoder" />
</security:authentication-provider>
</security:authentication-manager>