001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Aug 30, 2005
014: * @author mbatchel
015: */
016: package org.pentaho.core.util;
017:
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.Map;
021: import javax.naming.InitialContext;
022: import javax.naming.NamingException;
023: import javax.sql.DataSource;
024:
025: import org.pentaho.messages.Messages;
026:
027: public class DatasourceHelper {
028:
029: private static Map FoundDS = Collections
030: .synchronizedMap(new HashMap());
031:
032: /**
033: * This method clears the JNDI DS cache. The need exists because after a JNDI
034: * connection edit the old DS must be removed from the cache.
035: *
036: */
037: public static void clearCache() {
038: if (FoundDS != null) {
039: FoundDS.clear();
040: }
041: }
042:
043: /**
044: * This method clears the JNDI DS cache. The need exists because after a JNDI
045: * connection edit the old DS must be removed from the cache.
046: *
047: */
048: public static void clearDataSource(String dsName) {
049: if (FoundDS != null) {
050: FoundDS.remove(dsName);
051: }
052: }
053:
054: /**
055: * Since JNDI is supported different ways in different app servers, it's
056: * nearly impossible to have a ubiquitous way to look up a datasource. This
057: * method is intended to hide all the lookups that may be required to find a
058: * jndi name.
059: *
060: * @param dsName
061: * The Datasource name
062: * @return DataSource if there is one bound in JNDI
063: * @throws NamingException
064: */
065: public static DataSource getDataSourceFromJndi(String dsName)
066: throws NamingException {
067: Object foundDs = FoundDS.get(dsName);
068: if (foundDs != null) {
069: return (DataSource) foundDs;
070: }
071: InitialContext ctx = new InitialContext();
072: Object lkup = null;
073: DataSource rtn = null;
074: NamingException firstNe = null;
075: // First, try what they ask for...
076: try {
077: lkup = ctx.lookup(dsName);
078: if (lkup != null) {
079: rtn = (DataSource) lkup;
080: FoundDS.put(dsName, rtn);
081: return rtn;
082: }
083: } catch (NamingException ignored) {
084: firstNe = ignored;
085: }
086: try {
087: // Needed this for Jboss
088: lkup = ctx.lookup("java:" + dsName); //$NON-NLS-1$
089: if (lkup != null) {
090: rtn = (DataSource) lkup;
091: FoundDS.put(dsName, rtn);
092: return rtn;
093: }
094: } catch (NamingException ignored) {
095: }
096: try {
097: // Tomcat
098: lkup = ctx.lookup("java:comp/env/jdbc/" + dsName); //$NON-NLS-1$
099: if (lkup != null) {
100: rtn = (DataSource) lkup;
101: FoundDS.put(dsName, rtn);
102: return rtn;
103: }
104: } catch (NamingException ignored) {
105: }
106: try {
107: // Others?
108: lkup = ctx.lookup("jdbc/" + dsName); //$NON-NLS-1$
109: if (lkup != null) {
110: rtn = (DataSource) lkup;
111: FoundDS.put(dsName, rtn);
112: return rtn;
113: }
114: } catch (NamingException ignored) {
115: }
116: if (firstNe != null) {
117: throw firstNe;
118: }
119: throw new NamingException(
120: Messages
121: .getErrorString(
122: "DatasourceHelper.ERROR_0001_INVALID_DATASOURCE", dsName)); //$NON-NLS-1$
123: }
124:
125: /**
126: * Since JNDI is supported different ways in different app servers, it's
127: * nearly impossible to have a ubiquitous way to look up a datasource. This
128: * method is intended to hide all the lookups that may be required to find a
129: * jndi name, and return the actual bound name.
130: *
131: * @param dsName
132: * The Datasource name (like SampleData)
133: * @return The bound DS name if it is bound in JNDI (like "jdbc/SampleData")
134: * @throws NamingException
135: */
136: public static String getDSBoundName(String dsName)
137: throws NamingException {
138: InitialContext ctx = new InitialContext();
139: Object lkup = null;
140: NamingException firstNe = null;
141: String rtn = dsName;
142: // First, try what they ask for...
143: try {
144: lkup = ctx.lookup(rtn);
145: if (lkup != null) {
146: return rtn;
147: }
148: } catch (NamingException ignored) {
149: firstNe = ignored;
150: }
151: try {
152: // Needed this for Jboss
153: rtn = "java:" + dsName; //$NON-NLS-1$
154: lkup = ctx.lookup(rtn);
155: if (lkup != null) {
156: return rtn;
157: }
158: } catch (NamingException ignored) {
159: }
160: try {
161: // Tomcat
162: rtn = "java:comp/env/jdbc/" + dsName; //$NON-NLS-1$
163: lkup = ctx.lookup(rtn);
164: if (lkup != null) {
165: return rtn;
166: }
167: } catch (NamingException ignored) {
168: }
169: try {
170: // Others?
171: rtn = "jdbc/" + dsName; //$NON-NLS-1$
172: lkup = ctx.lookup(rtn);
173: if (lkup != null) {
174: return rtn;
175: }
176: } catch (NamingException ignored) {
177: }
178: if (firstNe != null) {
179: throw firstNe;
180: }
181: throw new NamingException(
182: Messages
183: .getErrorString(
184: "DatasourceHelper.ERROR_0001_INVALID_DATASOURCE", dsName)); //$NON-NLS-1$
185:
186: }
187: }
|