1 16 package com.ibatis.sqlmap.engine.datasource; 17 18 import com.ibatis.sqlmap.client.SqlMapException; 19 20 import javax.naming.Context ; 21 import javax.naming.InitialContext ; 22 import javax.naming.NamingException ; 23 import javax.sql.DataSource ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 29 32 public class JndiDataSourceFactory implements DataSourceFactory { 33 34 private DataSource dataSource; 35 36 public void initialize(Map properties) { 37 try { 38 InitialContext initCtx = null; 39 Hashtable context = getContextProperties(properties); 40 41 if (context == null) { 42 initCtx = new InitialContext (); 43 } else { 44 initCtx = new InitialContext (context); 45 } 46 47 if (properties.containsKey("DataSource")) { 48 dataSource = (DataSource ) initCtx.lookup((String ) properties.get("DataSource")); 49 } else if (properties.containsKey("DBJndiContext")) { dataSource = (DataSource ) initCtx.lookup((String ) properties.get("DBJndiContext")); 51 } else if (properties.containsKey("DBFullJndiContext")) { dataSource = (DataSource ) initCtx.lookup((String ) properties.get("DBFullJndiContext")); 53 } else if (properties.containsKey("DBInitialContext") 54 && properties.containsKey("DBLookup")) { Context ctx = (Context ) initCtx.lookup((String ) properties.get("DBInitialContext")); 56 dataSource = (DataSource ) ctx.lookup((String ) properties.get("DBLookup")); 57 } 58 59 } catch (NamingException e) { 60 throw new SqlMapException("There was an error configuring JndiDataSourceDaoTransactionPool. Cause: " + e, e); 61 } 62 } 63 64 public DataSource getDataSource() { 65 return dataSource; 66 } 67 68 private static Hashtable getContextProperties(Map allProps) { 69 final String PREFIX = "context."; 70 Hashtable contextProperties = null; 71 Iterator keys = allProps.keySet().iterator(); 72 while (keys.hasNext()) { 73 String key = (String ) keys.next(); 74 String value = (String ) allProps.get(key); 75 if (key.startsWith(PREFIX)) { 76 if (contextProperties == null) { 77 contextProperties = new Properties (); 78 } 79 contextProperties.put(key.substring(PREFIX.length()), value); 80 } 81 } 82 return contextProperties; 83 } 84 85 } 86 87 | Popular Tags |