KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > jasperreports > service > impl > BeanReportDataSourceServiceFactory


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
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 /**
39  * @author swood
40  *
41  */

42 public class BeanReportDataSourceServiceFactory implements ReportDataSourceServiceFactory, ApplicationContextAware {
43
44     ApplicationContext ctx;
45     
46     /**
47      *
48      */

49     public BeanReportDataSourceServiceFactory() {
50         super();
51     }
52
53     /* (non-Javadoc)
54      * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
55      */

56     public void setApplicationContext(ApplicationContext arg0) throws BeansException {
57         ctx = arg0;
58     }
59
60     /* (non-Javadoc)
61      * @see com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceServiceFactory#createService(com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource)
62      */

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 JavaDoc 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             // The bean had better be a ReportDataSourceService
77
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             // The method on this bean returns a ReportDataSourceService
84
Method JavaDoc serviceMethod;
85             try {
86                 serviceMethod = bean.getClass().getMethod(beanDataSource.getBeanMethod(), null);
87                 return (ReportDataSourceService) serviceMethod.invoke(bean, null);
88             } catch (SecurityException JavaDoc e) {
89                 throw new JSExceptionWrapper(e);
90             } catch (NoSuchMethodException JavaDoc e) {
91                 throw new JSException("Bean with name: " + beanDataSource.getBeanName() + " does not have method: " + beanDataSource.getBeanMethod());
92             } catch (IllegalArgumentException JavaDoc e) {
93                 throw new JSExceptionWrapper(e);
94             } catch (IllegalAccessException JavaDoc e) {
95                 throw new JSExceptionWrapper(e);
96             } catch (InvocationTargetException JavaDoc e) {
97                 throw new JSExceptionWrapper(e);
98             }
99         }
100     }
101
102 }
103
Popular Tags