1 53 54 106 107 package com.Yasna.forum.database; 108 109 import java.sql.Connection ; 110 import java.sql.SQLException ; 111 import javax.sql.DataSource ; 112 import java.util.Properties ; 113 import java.util.Enumeration ; 114 import javax.naming.Context ; 115 import javax.naming.InitialContext ; 116 import javax.naming.NamingException ; 117 import com.Yasna.forum.PropertyManager; 118 119 134 public class DataSourceConnectionProvider extends DbConnectionProvider { 135 136 private static final String NAME = "JNDI DataSource Connection Provider"; 137 private static final String DESCRIPTION = 138 "Connection Provider for Yazd to lookup pooled " 139 + "DataSource from JNDI location. Requires 'name' " 140 + "property with JNDI location. This can be set in " 141 + "the properties file as 'JNDIDataSource.name'"; 142 private static final String AUTHOR = "Joe Walnes - joe@truemesh.com"; 143 private static final int MAJOR_VERSION = 1; 144 private static final int MINOR_VERSION = 0; 145 private static final boolean POOLED = true; 146 147 private Properties properties; 148 private DataSource dataSource; 149 150 private static final boolean DEBUG = false; 151 152 155 private static final String [] jndiPropertyKeys = { 156 Context.APPLET , 157 Context.AUTHORITATIVE , 158 Context.BATCHSIZE , 159 Context.DNS_URL , 160 Context.INITIAL_CONTEXT_FACTORY , 161 Context.LANGUAGE , 162 Context.OBJECT_FACTORIES , 163 Context.PROVIDER_URL , 164 Context.REFERRAL , 165 Context.SECURITY_AUTHENTICATION , 166 Context.SECURITY_CREDENTIALS , 167 Context.SECURITY_PRINCIPAL , 168 Context.SECURITY_PROTOCOL , 169 Context.STATE_FACTORIES , 170 Context.URL_PKG_PREFIXES 171 }; 172 173 176 public DataSourceConnectionProvider() { 177 debug("constructor()"); 178 properties = new Properties (); 179 setProperty("name",PropertyManager.getProperty("JNDIDataSource.name")); 180 } 181 182 185 protected void start() { 186 debug("start()"); 187 String name = getProperty("name"); 188 if (name==null || name.length()==0) { 189 error("No name specified for DataSource JNDI lookup - 'name' " + 190 "Property should be set.", null); 191 return; 192 } 193 try { 194 Properties contextProperties = new Properties (); 195 for (int i=0; i<jndiPropertyKeys.length; i++) { 196 String k = jndiPropertyKeys[i]; 197 String v = PropertyManager.getProperty(k); 198 if (v != null) { 199 contextProperties.setProperty(k,v); 200 } 201 } 202 Context context = new InitialContext (contextProperties); 203 dataSource = (DataSource ) context.lookup( name ); 204 } 205 catch (Exception e) { 206 error("Could not lookup DataSource at '" + name + "'",e); 207 } 208 } 209 210 213 protected void restart() { 214 debug("restart()"); 215 destroy(); 216 start(); 217 } 218 219 222 protected void destroy() { 223 debug("destroy()"); 224 String name = getProperty("name"); 225 if (name!=null && name.length()>0) { 226 PropertyManager.setProperty("JNDIDataSource.name", name); 227 } 228 } 229 230 233 public Connection getConnection() { 234 debug("getConnection()"); 235 if (dataSource==null) { 236 error("DataSource has not yet been looked up",null); 237 return null; 238 } 239 try { 240 return dataSource.getConnection(); 241 } 242 catch (SQLException e) { 243 error("Could not retrieve Connection from DataSource",e); 244 return null; 245 } 246 } 247 248 public String getProperty(String name) { 249 debug("getProperty('"+name+"+')"); 250 return properties.getProperty(name); 251 } 252 253 public void setProperty(String name, String value) { 254 debug("setProperty('"+name+"+','"+value+"')"); 255 properties.setProperty(name,value); 256 } 257 258 public Enumeration propertyNames() { 259 debug("propertyNames()"); 260 return properties.propertyNames(); 261 } 262 263 public String getPropertyDescription(String name) { 264 debug("getPropertyDescription('"+name+"')"); 265 if (name.equals("name")) { 266 return "JNDI name to lookup. eg: java:comp/env/jdbc/MyDataSource"; 267 } 268 else { 269 return null; 270 } 271 } 272 273 279 private final void error(String msg, Exception e) { 280 System.err.println("Error: "+msg); 281 if (e!=null) { 282 e.printStackTrace(); 283 } 284 } 285 286 289 private final void debug(String msg) { 290 if (DEBUG) { 291 System.err.println("DEBUG: "+msg); 292 } 293 } 294 } 295 | Popular Tags |