1 25 26 27 package org.nemesis.forum.util.jdbc; 28 29 import java.sql.Connection ; 30 import java.sql.SQLException ; 31 import java.util.Properties ; 32 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.sql.DataSource ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.nemesis.forum.config.ConfigLoader; 40 41 42 43 58 public class DataSourceConnectionProvider extends DbConnectionProvider { 59 60 static protected Log log =LogFactory.getLog(DataSourceConnectionProvider.class); 61 62 63 private static final boolean POOLED = true; 64 65 private DataSource dataSource; 66 67 68 71 89 92 public DataSourceConnectionProvider() { 93 94 97 } 98 99 102 protected void start() { 103 String name = ConfigLoader.getInstance().getConfig().getJDBCProviderProperties().getProperty("JNDIDataSource"); 105 if (name==null || name.length()==0) { 106 log.error("No name specified for DataSource JNDI lookup - 'name' " + 107 "Property should be set."); 108 return; 109 } 110 try { 111 Properties contextProperties = new Properties (); 112 contextProperties.setProperty("name",name); 113 114 121 Context context = new InitialContext (contextProperties); 122 dataSource = (DataSource ) context.lookup( name ); 123 124 128 } 129 catch (Exception e) { 130 log.error("Could not lookup DataSource at '" + name + "'"); 131 } 132 } 133 134 137 protected void restart() { 138 139 destroy(); 140 start(); 141 } 142 143 146 protected void destroy() { 147 148 return; 149 } 150 151 154 public Connection getConnection() { 155 156 if (dataSource==null) { 157 log.error("DataSource has not yet been looked up",null); 158 return null; 159 } 160 try { 161 return dataSource.getConnection(); 162 } 163 catch (SQLException e) { 164 log.error("Could not retrieve Connection from DataSource",e); 165 return null; 166 } 167 } 168 169 170 171 172 173 174 } 175 | Popular Tags |