KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.sql.DataSource JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import com.jaspersoft.jasperserver.api.JSException;
33 import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
34 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JndiJdbcReportDataSource;
35 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
36 import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceService;
37 import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceServiceFactory;
38
39 /**
40  * @author swood
41  *
42  */

43 public class JndiJdbcReportDataSourceServiceFactory implements ReportDataSourceServiceFactory {
44
45     private static final Log log = LogFactory.getLog(JndiJdbcReportDataSourceServiceFactory.class);
46     
47     private Context JavaDoc ctx = null;
48     
49     public JndiJdbcReportDataSourceServiceFactory() {
50         try {
51             
52             // Set the context here, as it is a heavyweight constructor
53

54             ctx = new InitialContext JavaDoc();
55         } catch (NamingException JavaDoc e) {
56             log.error(e);
57             throw new JSException(e);
58         }
59     }
60
61     /* (non-Javadoc)
62      * @see com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceServiceFactory#createService(com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource)
63      */

64     public ReportDataSourceService createService(ReportDataSource dataSource) {
65         try {
66             if (!(dataSource instanceof JndiJdbcReportDataSource)) {
67                 throw new JSException("Invalid JNDI JDBC data source. Was: " + dataSource.getClass());
68             }
69             JndiJdbcReportDataSource jndiDataSource = (JndiJdbcReportDataSource) dataSource;
70             
71             String JavaDoc jndiName = jndiDataSource.getJndiName();
72             
73             DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("java:comp/env/" + jndiName);
74             return new JdbcDataSourceService(ds);
75         } catch (NamingException JavaDoc e) {
76             log.error(e, e);
77             throw new JSExceptionWrapper(e);
78         }
79     }
80
81 }
82
Popular Tags