简体   繁体   中英

ClassCastException when I'm getting a BasicDataSource in Apache Karaf 3.0.4

I'm working in Apache Karaf 3.0.4 and my problem is that I'm getting an error when I try to get the properties from a datasource (I have a list of datasources), I'm using this code

    BundleContext bundleContext = FrameworkUtil.getBundle(MyController.class).getBundleContext(); 
    ServiceReference[] serviceReferences =bundleContext.getAllServiceReferences("javax.sql.DataSource", null); 

     //and in this part 

     for (ServiceReference serviceReference : serviceReferences) { 
            Object jndi = serviceReference.getProperty("osgi.jndi.service.name"); 
            if (jndi != null) { 
                serviceReferenceDataSources.add(serviceReference); 
                Object service = bundleContext.getService(serviceReference); 
                //here the code fails 
                BasicDataSource basicDataSource = (BasicDataSource) service; 

             } 
      } 

This is the stack trace

java.lang.ClassCastException: Proxyfcb4dd22_6103_4978_b41b_0bacfb118a66 cannot be cast to org.apache.commons.dbcp2.BasicDataSource 

I need the BasicDataSource object to get their properties (maxTotal, username, url, validationQuery, initialSize, maxWaitMillis)

Finally when I try to cast to "javax.sql.DataSource" this code works, but I need this object BasicDataSource.

EDIT 1

This is my Blueprint

<?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:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
     xsi:schemaLocation="
        http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

  <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
      <property name="initialSize" value="${jdbc.initialSize}" />
      <property name="maxTotal" value="${jdbc.maxTotal}" />
      <property name="maxWaitMillis" value="${jdbc.maxWaitMillis}" />
      <property name="validationQuery" value="${jdbc.validationQuery}"/>
      <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
      <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
  </bean>

  <service interface="javax.sql.DataSource" ref="dataSource">
    <service-properties>
        <entry key="osgi.jndi.service.name" value="jdbc/ejemplodb"/>
     <entry key="datasource" value="ejemplodb"/>
    </service-properties>
  </service>

If I print in console this code

  ...
    Object service = bundleContext.getService(serviceReference);
    logger.debug("Service getClass() ", service.getClass());
    System.out.println("Service in println" + service);
  ...

I get

  Service getClass() Proxy79d23ceb_cee2_4031_bdc3_13fd572cdf8c
  Service in println org.apache.commons.dbcp2.BasicDataSource@7f753504

I hope that you can help me, Thanks.

As by the request you made, the returned services will be of type javax.sql.DataSource. Casting to apache's Basic data source is invalid there. But as long as you only need DB connections you should be okay with javax.sql.Datasource?

javax.sql.DataSource basicDataSource = (javax.sql.DataSource) service; 

If you need the service to be of type BasicDataSource, you'd probably end up changing the bundleContext.getAllServiceReferences("javax.sql.DataSource", null); to look for BasicDatSource instead and change the declaration for your BasicDataSource to be of Type BasicDataSource and not of type javax.sql.DataSource .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM