1 19 20 package org.efs.openreports.providers.impl; 21 22 import java.sql.Connection ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.sql.DataSource ; 30 31 import org.apache.log4j.Logger; 32 import org.efs.openreports.objects.ReportDataSource; 33 import org.efs.openreports.providers.DataSourceProvider; 34 import org.efs.openreports.providers.ProviderException; 35 import org.efs.openreports.providers.persistence.DataSourcePersistenceProvider; 36 import org.efs.openreports.util.LocalStrings; 37 38 public class DataSourceProviderImpl implements DataSourceProvider 39 { 40 protected static Logger log = 41 Logger.getLogger(DataSourceProviderImpl.class.getName()); 42 43 private Hashtable dataSources = new Hashtable (); 44 45 private DataSourcePersistenceProvider dataSourcePersistenceProvider; 46 47 public DataSourceProviderImpl() throws ProviderException 48 { 49 50 dataSourcePersistenceProvider = new DataSourcePersistenceProvider(); 51 52 loadDataSources(); 53 54 log.info("DataSourceProviderImpl"); 55 } 56 57 protected void loadDataSources() throws ProviderException 58 { 59 Iterator iterator = 60 dataSourcePersistenceProvider.getDataSources().iterator(); 61 62 while (iterator.hasNext()) 63 { 64 ReportDataSource dataSource = (ReportDataSource) iterator.next(); 65 dataSources.put(dataSource.getId(), dataSource); 66 } 67 } 68 69 public Connection getConnection(Integer id) throws ProviderException 70 { 71 ReportDataSource dataSource = (ReportDataSource) dataSources.get(id); 72 73 try 74 { 75 if (dataSource.isJndi()) 76 { 77 Context initCtx = new InitialContext (); 78 79 DataSource jndiDataSource = 80 (DataSource ) initCtx.lookup(dataSource.getUrl()); 81 82 return jndiDataSource.getConnection(); 83 } 84 else 85 { 86 return dataSource.getConnection(); 87 } 88 } 89 catch (Exception e) 90 { 91 log.error(e.toString()); 92 throw new ProviderException(e.getMessage()); 93 } 94 } 95 96 public boolean isValidDataSource(Integer id) 97 { 98 if (dataSources.containsKey(id)) 99 return true; 100 101 return false; 102 } 103 104 public List getDataSources() throws ProviderException 105 { 106 return dataSourcePersistenceProvider.getDataSources(); 107 } 108 109 public ReportDataSource getDataSource(Integer id) throws ProviderException 110 { 111 return dataSourcePersistenceProvider.getDataSource(id); 112 } 113 114 public ReportDataSource getDataSource(String name) throws ProviderException 115 { 116 return dataSourcePersistenceProvider.getDataSource(name); 117 } 118 119 public ReportDataSource insertDataSource(ReportDataSource dataSource) 120 throws ProviderException 121 { 122 testDataSource(dataSource); 123 124 dataSource = dataSourcePersistenceProvider.insertDataSource(dataSource); 125 dataSources.put(dataSource.getId(), dataSource); 126 127 return dataSource; 128 } 129 130 public void updateDataSource(ReportDataSource dataSource) 131 throws ProviderException 132 { 133 testDataSource(dataSource); 134 135 dataSourcePersistenceProvider.updateDataSource(dataSource); 136 dataSources.put(dataSource.getId(), dataSource); 137 } 138 139 public void deleteDataSource(ReportDataSource dataSource) 140 throws ProviderException 141 { 142 dataSourcePersistenceProvider.deleteDataSource(dataSource); 143 dataSources.remove(dataSource.getId()); 144 } 145 146 public void testDataSource(ReportDataSource dataSource) 147 throws ProviderException 148 { 149 Connection conn = null; 150 151 try 152 { 153 if (dataSource.isJndi()) 154 { 155 Context initCtx = new InitialContext (); 156 157 DataSource jndiDataSource = 158 (DataSource ) initCtx.lookup(dataSource.getUrl()); 159 160 conn = jndiDataSource.getConnection(); 161 } 162 else 163 { 164 conn = dataSource.getConnection(); 165 } 166 } 167 catch (Exception e) 168 { 169 throw new ProviderException(LocalStrings 170 .getString(LocalStrings.ERROR_TESTING_CONNECTION) 171 + ": " + e.getMessage()); 172 } 173 finally 174 { 175 try 176 { 177 if (conn != null) 178 conn.close(); 179 } 180 catch (Exception e) 181 { 182 log.error(e.toString()); 183 } 184 } 185 } 186 187 } | Popular Tags |