1 11 12 package org.jivesoftware.database; 13 14 import org.jivesoftware.util.JiveGlobals; 15 import org.jivesoftware.util.Log; 16 17 import javax.naming.Context ; 18 import javax.naming.InitialContext ; 19 import javax.sql.DataSource ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 import java.util.*; 23 24 37 public class JNDIDataSourceProvider implements ConnectionProvider { 38 39 private String dataSourceName; 40 private DataSource dataSource; 41 42 45 private static final String [] jndiPropertyKeys = { 46 Context.APPLET, 47 Context.AUTHORITATIVE, 48 Context.BATCHSIZE, 49 Context.DNS_URL, 50 Context.INITIAL_CONTEXT_FACTORY, 51 Context.LANGUAGE, 52 Context.OBJECT_FACTORIES, 53 Context.PROVIDER_URL, 54 Context.REFERRAL, 55 Context.SECURITY_AUTHENTICATION, 56 Context.SECURITY_CREDENTIALS, 57 Context.SECURITY_PRINCIPAL, 58 Context.SECURITY_PROTOCOL, 59 Context.STATE_FACTORIES, 60 Context.URL_PKG_PREFIXES 61 }; 62 63 66 public JNDIDataSourceProvider() { 67 dataSourceName = JiveGlobals.getXMLProperty("database.JNDIProvider.name"); 68 } 69 70 public String getName() { 71 return "JNDI DataSource Connection Provider"; 72 } 73 74 public String getDescription() { 75 return "Connection Provider for Jive to lookup pooled " 76 + "DataSource from JNDI location. Requires 'name' " 77 + "property with JNDI location. This can be set in " 78 + "the properties file as 'JNDIDataSource.name'"; 79 } 80 81 public String getAuthor() { 82 return "Joe Walnes - joe@truemesh.com"; 83 } 84 85 public int getMajorVersion() { 86 return 2; 87 } 88 89 public int getMinorVersion() { 90 return 1; 91 } 92 93 public boolean isPooled() { 94 return true; 95 } 96 97 public void start() { 98 if (dataSourceName == null || dataSourceName.equals("")) { 99 error("No name specified for DataSource. JNDI lookup will fail", null); 100 return; 101 } 102 try { 103 Properties contextProperties = new Properties(); 104 for (int i = 0; i < jndiPropertyKeys.length; i++) { 105 String k = jndiPropertyKeys[i]; 106 String v = JiveGlobals.getXMLProperty(k); 107 if (v != null) { 108 contextProperties.setProperty(k, v); 109 } 110 } 111 Context context = null; 112 if (contextProperties.size() > 0) { 113 context = new InitialContext (contextProperties); 114 } 115 else { 116 context = new InitialContext (); 117 } 118 dataSource = (DataSource )context.lookup(dataSourceName); 119 } 120 catch (Exception e) { 121 error("Could not lookup DataSource at '" + dataSourceName + "'", e); 122 } 123 } 124 125 public void restart() { 126 destroy(); 127 start(); 128 } 129 130 public void destroy() { 131 132 } 133 134 public Connection getConnection() { 135 if (dataSource == null) { 136 error("DataSource has not been initialized.", null); 137 return null; 138 } 139 try { 140 return dataSource.getConnection(); 141 } 142 catch (SQLException e) { 143 error("Could not retrieve Connection from DataSource", e); 144 return null; 145 } 146 } 147 148 public String getProperty(String name) { 149 if ("name".equals(name)) { 150 return dataSourceName; 151 } 152 else { 153 return null; 154 } 155 } 156 157 public void setProperty(String name, String value) { 158 if ("name".equals(name)) { 159 this.dataSourceName = value; 160 JiveGlobals.setXMLProperty("database.JNDIProvider.name", value); 161 } 162 } 163 164 public Iterator propertyNames() { 165 List list = new ArrayList(); 166 list.add("name"); 167 return Collections.unmodifiableList(list).iterator(); 168 } 169 170 public String getPropertyDescription(String name) { 171 if ("name".equals(name)) { 172 return "JNDI name to lookup. eg: java:comp/env/jdbc/MyDataSource"; 173 } 174 else { 175 return null; 176 } 177 } 178 179 185 private final void error(String msg, Exception e) { 186 Log.error(msg, e); 187 } 188 } | Popular Tags |