Jump to content
Changes to the Jaspersoft community edition download ×

My Custom Bean DataSource does not have method createDataSourceService


dkey

Recommended Posts

Hello, sorry for my english., i'm from Indonesia.

my JasperServer version 7.1 community .

I've following this "tutorial" for creating a dynamic datasource switch. based on this link

switching-datasource-based-user

thanks for mr. oscar16071987

my knowledge on java programming is zero, so after i has applied and creating Bean DataSource, getting this error on logs/jasperserver.log

Unexpected error occurs com.jaspersoft.jasperserver.api.JSException: The bean with the name JSCustomDS does not have method createDataSourceService

This is my code,


file JSCustomDS.java

package com.jasperserver.custom.ds;

import java.util.Map;
import java.sql.SQLException;
import net.sf.jasperreports.engine.JRParameter;
//import org.springframework.context.i18n.LocaleContextHolder;
 
import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceService;
import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
import com.jaspersoft.jasperserver.api.common.service.BeanForInterfaceImplementationFactory;
import com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JdbcDataSourceService;
import com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JdbcReportDataSourceServiceFactory;
import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
import com.jaspersoft.jasperserver.api.metadata.user.domain.impl.client.MetadataUserDetails;
import org.springframework.security.core.context.SecurityContextHolder;

public class JSCustomDS implements ReportDataSourceService {
    private JdbcDataSourceService connection;
    private RepositoryService repositoryService;
    private BeanForInterfaceImplementationFactory dataSourceService;

    public JSCustomDS(RepositoryService repositoryService,
            BeanForInterfaceImplementationFactory dsServiceFactory) {
        super();
        this.repositoryService = repositoryService;
        this.dataSourceService = dsServiceFactory;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void setReportParameterValues(Map parametersValue) {
        MetadataUserDetails userID = (MetadataUserDetails)
                    SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        String userName = userID.getUsername();
        // obtain a connection based on the username.
        String ds_uri = "/datasources/dscloud01";
        if (userName.equalsIgnoreCase("jasperadmin")) {
            ds_uri = "/datasources/dslocal01";
        }
        
        connection = getRepositoryDatasource(ds_uri);
        try {
            parametersValue.put(JRParameter.REPORT_CONNECTION,
                connection.getDataSource().getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public JdbcDataSourceService getRepositoryDatasource(String repositoryURI) {
        //ExecutionContext context = JasperServerUtil.getExecutionContext(LocaleContextHolder.getLocale());
        ExecutionContext context = JasperServerUtil.getExecutionContext();
        ReportDataSource datasource = (ReportDataSource) repositoryService.getResource(context, repositoryURI);
 
        JdbcReportDataSourceServiceFactory factory = (JdbcReportDataSourceServiceFactory)
            dataSourceService.getBean(datasource.getClass());
 
        JdbcDataSourceService DSservice = (JdbcDataSourceService)
            factory.createService(datasource);
        
        return DSservice;
    }
    
    @Override
    public void closeConnection() {
        if(connection != null) {
            connection.closeConnection();
        }
    }
}


file JSCustomDSFactory.java

package com.jasperserver.custom.ds;

import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceService;
import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceServiceFactory;
import com.jaspersoft.jasperserver.api.common.service.BeanForInterfaceImplementationFactory;
 
public class JSCustomDSFactory implements ReportDataSourceServiceFactory {
    private RepositoryService repositoryService;
    private BeanForInterfaceImplementationFactory dataSourceService;
 
    public JSCustomDSFactory() {
    }
   
    /* Factory methods */
    public JSCustomDS createDataSourceService() {
        return new JSCustomDS(repositoryService, dataSourceService);
    }

 
    @Override
    public ReportDataSourceService createService(ReportDataSource dataSource) {
        return new JSCustomDS(repositoryService, dataSourceService);
    }

    /* getters */
    public RepositoryService getRepositoryService() {
        return repositoryService;
    }
 
    public BeanForInterfaceImplementationFactory getDataSourceServiceFactory() {
        return dataSourceService;
    }
 
    /* Setters */
    public void setRepositoryService(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }
 
    public void setDataSourceServiceFactory(BeanForInterfaceImplementationFactory dataSourceService) {
        this.dataSourceService = dataSourceService;
    }
    
}


file applicationContext-JSCustomDS.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.1.xsd">
    <bean id="JSCustomDS" class="com.jasperserver.custom.ds.JSCustomDSFactory">

    <property name="repositoryService">
        <ref bean="repositoryService"/>
    </property>
    <property name="dataSourceServiceFactory">
        <ref bean="dataSourceServiceFactories"/>
    </property>


    </bean>
</beans>


both file JSCustomDS.class and JSCustomDSFactory.class apply on apache-tomcat/webapps/jasperserver/WEB-INF/classes/com/jasperserver/custom/ds,

and i apply too JSCustomDS.jar on apache-tomcat/webapps/jasperserver/WEB-INF/lib

 

Thanks for your help.

Best regards!

Link to comment
Share on other sites

  • 9 months later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...