1 21 22 package com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 27 import org.springframework.beans.BeansException; 28 import org.springframework.context.ApplicationContext; 29 import org.springframework.context.ApplicationContextAware; 30 31 import com.jaspersoft.jasperserver.api.JSException; 32 import com.jaspersoft.jasperserver.api.JSExceptionWrapper; 33 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.BeanReportDataSource; 34 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource; 35 import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceService; 36 import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceServiceFactory; 37 38 42 public class BeanReportDataSourceServiceFactory implements ReportDataSourceServiceFactory, ApplicationContextAware { 43 44 ApplicationContext ctx; 45 46 49 public BeanReportDataSourceServiceFactory() { 50 super(); 51 } 52 53 56 public void setApplicationContext(ApplicationContext arg0) throws BeansException { 57 ctx = arg0; 58 } 59 60 63 public ReportDataSourceService createService(ReportDataSource reportDataSource) { 64 if (!(reportDataSource instanceof BeanReportDataSource)) { 65 throw new JSException("Invalid bean data source. Was: " + reportDataSource.getClass()); 66 } 67 BeanReportDataSource beanDataSource = (BeanReportDataSource) reportDataSource; 68 69 Object bean = ctx.getBean(beanDataSource.getBeanName()); 70 71 if (bean == null) { 72 throw new JSException("No bean with name: " + beanDataSource.getBeanName()); 73 } 74 75 if (beanDataSource.getBeanMethod() == null) { 76 if (!(bean instanceof ReportDataSourceService)) { 78 throw new JSException("Bean with name: " + beanDataSource.getBeanName() + " is not a ReportDataSourceService"); 79 } else { 80 return (ReportDataSourceService) bean; 81 } 82 } else { 83 Method serviceMethod; 85 try { 86 serviceMethod = bean.getClass().getMethod(beanDataSource.getBeanMethod(), null); 87 return (ReportDataSourceService) serviceMethod.invoke(bean, null); 88 } catch (SecurityException e) { 89 throw new JSExceptionWrapper(e); 90 } catch (NoSuchMethodException e) { 91 throw new JSException("Bean with name: " + beanDataSource.getBeanName() + " does not have method: " + beanDataSource.getBeanMethod()); 92 } catch (IllegalArgumentException e) { 93 throw new JSExceptionWrapper(e); 94 } catch (IllegalAccessException e) { 95 throw new JSExceptionWrapper(e); 96 } catch (InvocationTargetException e) { 97 throw new JSExceptionWrapper(e); 98 } 99 } 100 } 101 102 } 103 | Popular Tags |