KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > util > DatasourceHelper


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Aug 30, 2005
14  * @author mbatchel
15  */

16 package org.pentaho.core.util;
17
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.sql.DataSource JavaDoc;
24
25 import org.pentaho.messages.Messages;
26
27 public class DatasourceHelper {
28
29     private static Map JavaDoc FoundDS = Collections.synchronizedMap(new HashMap JavaDoc());
30
31     /**
32      * This method clears the JNDI DS cache. The need exists because after a JNDI
33      * connection edit the old DS must be removed from the cache.
34      *
35      */

36     public static void clearCache() {
37       if (FoundDS != null) {
38         FoundDS.clear();
39       }
40     }
41     
42     /**
43      * Since JNDI is supported different ways in different app servers, it's
44      * nearly impossible to have a ubiquitous way to look up a datasource. This
45      * method is intended to hide all the lookups that may be required to find a
46      * jndi name.
47      *
48      * @param dsName
49      * The Datasource name
50      * @return DataSource if there is one bound in JNDI
51      * @throws NamingException
52      */

53     public static DataSource JavaDoc getDataSourceFromJndi(String JavaDoc dsName) throws NamingException JavaDoc {
54         Object JavaDoc foundDs = FoundDS.get(dsName);
55         if (foundDs != null) {
56             return (DataSource JavaDoc) foundDs;
57         }
58         InitialContext JavaDoc ctx = new InitialContext JavaDoc();
59         Object JavaDoc lkup = null;
60         DataSource JavaDoc rtn = null;
61         NamingException JavaDoc firstNe = null;
62         // First, try what they ask for...
63
try {
64             lkup = ctx.lookup(dsName);
65             if (lkup != null) {
66                 rtn = (DataSource JavaDoc) lkup;
67                 FoundDS.put(dsName, rtn);
68                 return rtn;
69             }
70         } catch (NamingException JavaDoc ignored) {
71             firstNe = ignored;
72         }
73         try {
74             // Needed this for Jboss
75
lkup = ctx.lookup("java:" + dsName); //$NON-NLS-1$
76
if (lkup != null) {
77                 rtn = (DataSource JavaDoc) lkup;
78                 FoundDS.put(dsName, rtn);
79                 return rtn;
80             }
81         } catch (NamingException JavaDoc ignored) {
82         }
83         try {
84             // Tomcat
85
lkup = ctx.lookup("java:comp/env/jdbc/" + dsName); //$NON-NLS-1$
86
if (lkup != null) {
87                 rtn = (DataSource JavaDoc) lkup;
88                 FoundDS.put(dsName, rtn);
89                 return rtn;
90             }
91         } catch (NamingException JavaDoc ignored) {
92         }
93         try {
94             // Others?
95
lkup = ctx.lookup("jdbc/" + dsName); //$NON-NLS-1$
96
if (lkup != null) {
97                 rtn = (DataSource JavaDoc) lkup;
98                 FoundDS.put(dsName, rtn);
99                 return rtn;
100             }
101         } catch (NamingException JavaDoc ignored) {
102         }
103         if (firstNe != null) {
104             throw firstNe;
105         }
106         throw new NamingException JavaDoc(Messages.getErrorString("DatasourceHelper.ERROR_0001_INVALID_DATASOURCE", dsName)); //$NON-NLS-1$
107
}
108
109     /**
110      * Since JNDI is supported different ways in different app servers, it's
111      * nearly impossible to have a ubiquitous way to look up a datasource. This
112      * method is intended to hide all the lookups that may be required to find a
113      * jndi name, and return the actual bound name.
114      *
115      * @param dsName
116      * The Datasource name (like SampleData)
117      * @return The bound DS name if it is bound in JNDI (like "jdbc/SampleData")
118      * @throws NamingException
119      */

120     public static String JavaDoc getDSBoundName(String JavaDoc dsName) throws NamingException JavaDoc {
121         InitialContext JavaDoc ctx = new InitialContext JavaDoc();
122         Object JavaDoc lkup = null;
123         NamingException JavaDoc firstNe = null;
124         String JavaDoc rtn = dsName;
125         // First, try what they ask for...
126
try {
127             lkup = ctx.lookup(rtn);
128             if (lkup != null) {
129                 return rtn;
130             }
131         } catch (NamingException JavaDoc ignored) {
132             firstNe = ignored;
133         }
134         try {
135             // Needed this for Jboss
136
rtn = "java:" + dsName; //$NON-NLS-1$
137
lkup = ctx.lookup(rtn);
138             if (lkup != null) {
139                 return rtn;
140             }
141         } catch (NamingException JavaDoc ignored) {
142         }
143         try {
144             // Tomcat
145
rtn = "java:comp/env/jdbc/" + dsName; //$NON-NLS-1$
146
lkup = ctx.lookup(rtn);
147             if (lkup != null) {
148                 return rtn;
149             }
150         } catch (NamingException JavaDoc ignored) {
151         }
152         try {
153             // Others?
154
rtn = "jdbc/" + dsName; //$NON-NLS-1$
155
lkup = ctx.lookup(rtn);
156             if (lkup != null) {
157                 return rtn;
158             }
159         } catch (NamingException JavaDoc ignored) {
160         }
161         if (firstNe != null) {
162             throw firstNe;
163         }
164         throw new NamingException JavaDoc(Messages.getErrorString("DatasourceHelper.ERROR_0001_INVALID_DATASOURCE", dsName)); //$NON-NLS-1$
165

166     }
167 }
168
Popular Tags