KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > impl > DataSourceProviderImpl


1 /*
2  * Copyright (C) 2002 Erik Swenson - eswenson@opensourcesoft.net
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.providers.impl;
21
22 import java.sql.Connection JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.sql.DataSource JavaDoc;
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 JavaDoc dataSources = new Hashtable JavaDoc();
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 JavaDoc 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 JavaDoc getConnection(Integer JavaDoc id) throws ProviderException
70     {
71         ReportDataSource dataSource = (ReportDataSource) dataSources.get(id);
72
73         try
74         {
75             if (dataSource.isJndi())
76             {
77                 Context JavaDoc initCtx = new InitialContext JavaDoc();
78
79                 DataSource JavaDoc jndiDataSource =
80                     (DataSource JavaDoc) initCtx.lookup(dataSource.getUrl());
81
82                 return jndiDataSource.getConnection();
83             }
84             else
85             {
86                 return dataSource.getConnection();
87             }
88         }
89         catch (Exception JavaDoc e)
90         {
91             log.error(e.toString());
92             throw new ProviderException(e.getMessage());
93         }
94     }
95
96     public boolean isValidDataSource(Integer JavaDoc id)
97     {
98         if (dataSources.containsKey(id))
99             return true;
100
101         return false;
102     }
103
104     public List JavaDoc getDataSources() throws ProviderException
105     {
106         return dataSourcePersistenceProvider.getDataSources();
107     }
108
109     public ReportDataSource getDataSource(Integer JavaDoc id) throws ProviderException
110     {
111         return dataSourcePersistenceProvider.getDataSource(id);
112     }
113     
114     public ReportDataSource getDataSource(String JavaDoc 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 JavaDoc conn = null;
150
151         try
152         {
153             if (dataSource.isJndi())
154             {
155                 Context JavaDoc initCtx = new InitialContext JavaDoc();
156                 
157                 DataSource JavaDoc jndiDataSource =
158                     (DataSource JavaDoc) initCtx.lookup(dataSource.getUrl());
159                 
160                 conn = jndiDataSource.getConnection();
161             }
162             else
163             {
164                 conn = dataSource.getConnection();
165             }
166         }
167         catch (Exception JavaDoc 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 JavaDoc e)
181             {
182                 log.error(e.toString());
183             }
184         }
185     }
186
187 }
Popular Tags