1 16 package org.pentaho.core.util; 17 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import javax.naming.InitialContext ; 22 import javax.naming.NamingException ; 23 import javax.sql.DataSource ; 24 25 import org.pentaho.messages.Messages; 26 27 public class DatasourceHelper { 28 29 private static Map FoundDS = Collections.synchronizedMap(new HashMap ()); 30 31 36 public static void clearCache() { 37 if (FoundDS != null) { 38 FoundDS.clear(); 39 } 40 } 41 42 53 public static DataSource getDataSourceFromJndi(String dsName) throws NamingException { 54 Object foundDs = FoundDS.get(dsName); 55 if (foundDs != null) { 56 return (DataSource ) foundDs; 57 } 58 InitialContext ctx = new InitialContext (); 59 Object lkup = null; 60 DataSource rtn = null; 61 NamingException firstNe = null; 62 try { 64 lkup = ctx.lookup(dsName); 65 if (lkup != null) { 66 rtn = (DataSource ) lkup; 67 FoundDS.put(dsName, rtn); 68 return rtn; 69 } 70 } catch (NamingException ignored) { 71 firstNe = ignored; 72 } 73 try { 74 lkup = ctx.lookup("java:" + dsName); if (lkup != null) { 77 rtn = (DataSource ) lkup; 78 FoundDS.put(dsName, rtn); 79 return rtn; 80 } 81 } catch (NamingException ignored) { 82 } 83 try { 84 lkup = ctx.lookup("java:comp/env/jdbc/" + dsName); if (lkup != null) { 87 rtn = (DataSource ) lkup; 88 FoundDS.put(dsName, rtn); 89 return rtn; 90 } 91 } catch (NamingException ignored) { 92 } 93 try { 94 lkup = ctx.lookup("jdbc/" + dsName); if (lkup != null) { 97 rtn = (DataSource ) lkup; 98 FoundDS.put(dsName, rtn); 99 return rtn; 100 } 101 } catch (NamingException ignored) { 102 } 103 if (firstNe != null) { 104 throw firstNe; 105 } 106 throw new NamingException (Messages.getErrorString("DatasourceHelper.ERROR_0001_INVALID_DATASOURCE", dsName)); } 108 109 120 public static String getDSBoundName(String dsName) throws NamingException { 121 InitialContext ctx = new InitialContext (); 122 Object lkup = null; 123 NamingException firstNe = null; 124 String rtn = dsName; 125 try { 127 lkup = ctx.lookup(rtn); 128 if (lkup != null) { 129 return rtn; 130 } 131 } catch (NamingException ignored) { 132 firstNe = ignored; 133 } 134 try { 135 rtn = "java:" + dsName; lkup = ctx.lookup(rtn); 138 if (lkup != null) { 139 return rtn; 140 } 141 } catch (NamingException ignored) { 142 } 143 try { 144 rtn = "java:comp/env/jdbc/" + dsName; lkup = ctx.lookup(rtn); 147 if (lkup != null) { 148 return rtn; 149 } 150 } catch (NamingException ignored) { 151 } 152 try { 153 rtn = "jdbc/" + dsName; lkup = ctx.lookup(rtn); 156 if (lkup != null) { 157 return rtn; 158 } 159 } catch (NamingException ignored) { 160 } 161 if (firstNe != null) { 162 throw firstNe; 163 } 164 throw new NamingException (Messages.getErrorString("DatasourceHelper.ERROR_0001_INVALID_DATASOURCE", dsName)); 166 } 167 } 168 | Popular Tags |