1 /*2 * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com3 * 4 * This program is free software; you can redistribute it and/or modify5 * it under the terms of the GNU General Public License as published by6 * the Free Software Foundation; either version 2 of the License, or7 * (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 License14 * 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-130720 */21 22 package com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl;23 24 import javax.naming.Context ;25 import javax.naming.InitialContext ;26 import javax.naming.NamingException ;27 import javax.sql.DataSource ;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 swood41 *42 */43 public class JndiJdbcReportDataSourceServiceFactory implements ReportDataSourceServiceFactory {44 45 private static final Log log = LogFactory.getLog(JndiJdbcReportDataSourceServiceFactory.class);46 47 private Context ctx = null;48 49 public JndiJdbcReportDataSourceServiceFactory() {50 try {51 52 // Set the context here, as it is a heavyweight constructor53 54 ctx = new InitialContext ();55 } catch (NamingException 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 jndiName = jndiDataSource.getJndiName();72 73 DataSource ds = (DataSource ) ctx.lookup("java:comp/env/" + jndiName);74 return new JdbcDataSourceService(ds);75 } catch (NamingException e) {76 log.error(e, e);77 throw new JSExceptionWrapper(e);78 }79 }80 81 }82