1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.avalon.cornerstone.services.datasource.DataSourceSelector; 21 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 22 import org.apache.avalon.framework.component.ComponentManager; 23 import org.apache.james.Constants; 24 import org.apache.james.util.JDBCUtil; 25 import org.apache.mailet.MailAddress; 26 import org.apache.mailet.MailetException; 27 28 import javax.mail.MessagingException ; 29 import javax.mail.internet.ParseException ; 30 import java.sql.*; 31 import java.util.Collection ; 32 import java.util.Locale ; 33 import java.util.Vector ; 34 35 52 public class JDBCListserv extends GenericListserv { 53 54 protected DataSourceComponent datasource; 55 protected String listservID = null; 56 protected String listservTable = null; 57 protected String membersTable = null; 58 protected boolean cacheSettings = true; 59 60 protected Collection members = null; 62 protected boolean membersOnly = true; 63 protected boolean attachmentsAllowed = true; 64 protected boolean replyToList = true; 65 protected MailAddress listservAddress = null; 66 protected String subjectPrefix = null; 67 68 protected String listservQuery = null; 70 protected String membersQuery = null; 71 72 75 private final JDBCUtil theJDBCUtil = 76 new JDBCUtil() { 77 protected void delegatedLog(String logString) { 78 log("JDBCListserv: " + logString); 79 } 80 }; 81 82 85 public void init() throws MessagingException { 86 if (getInitParameter("data_source") == null) { 87 throw new MailetException("data_source not specified for JDBCListserv"); 88 } 89 if (getInitParameter("listserv_id") == null) { 90 throw new MailetException("listserv_id not specified for JDBCListserv"); 91 } 92 if (getInitParameter("listserv_table") == null) { 93 throw new MailetException("listserv_table not specified for JDBCListserv"); 94 } 95 if (getInitParameter("members_table") == null) { 96 throw new MailetException("members_table not specified for JDBCListserv"); 97 } 98 99 String datasourceName = getInitParameter("data_source"); 100 listservID = getInitParameter("listserv_id"); 101 listservTable = getInitParameter("listserv_table"); 102 membersTable = getInitParameter("members_table"); 103 104 if (getInitParameter("cache_settings") != null) { 105 try { 106 cacheSettings = new Boolean (getInitParameter("cache_settings")).booleanValue(); 107 } catch (Exception e) { 108 } 110 } 111 112 Connection conn = null; 113 114 try { 115 ComponentManager componentManager = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 116 DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE); 118 datasource = (DataSourceComponent)datasources.select(datasourceName); 120 121 conn = datasource.getConnection(); 122 123 DatabaseMetaData dbMetaData = conn.getMetaData(); 125 if (!(theJDBCUtil.tableExists(dbMetaData, listservTable))) { 128 StringBuffer exceptionBuffer = 129 new StringBuffer (128) 130 .append("Could not find table '") 131 .append(listservTable) 132 .append("' in datasource '") 133 .append(datasourceName) 134 .append("'"); 135 throw new MailetException(exceptionBuffer.toString()); 136 } 137 138 if (!( theJDBCUtil.tableExists(dbMetaData, membersTable))) { 142 StringBuffer exceptionBuffer = 143 new StringBuffer (128) 144 .append("Could not find table '") 145 .append(membersTable) 146 .append("' in datasource '") 147 .append(datasourceName) 148 .append("'"); 149 throw new MailetException(exceptionBuffer.toString()); 150 } 151 152 StringBuffer queryBuffer = 153 new StringBuffer (256) 154 .append("SELECT members_only, attachments_allowed, reply_to_list, subject_prefix, list_address FROM ") 155 .append(listservTable) 156 .append(" WHERE listserv_id = ?"); 157 listservQuery = queryBuffer.toString(); 158 queryBuffer = 159 new StringBuffer (128) 160 .append("SELECT member FROM ") 161 .append(membersTable) 162 .append(" WHERE listserv_id = ?"); 163 membersQuery = queryBuffer.toString(); 164 165 loadSettings(); 167 } catch (MailetException me) { 168 throw me; 169 } catch (Exception e) { 170 throw new MessagingException ("Error initializing JDBCListserv", e); 171 } finally { 172 theJDBCUtil.closeJDBCConnection(conn); 173 } 174 } 175 176 179 public Collection getMembers() throws MessagingException { 180 if (!cacheSettings) { 181 loadSettings(); 182 } 183 184 return members; 185 } 186 187 190 public boolean isMembersOnly() throws MessagingException { 191 return membersOnly; 192 } 193 194 197 public boolean isAttachmentsAllowed() throws MessagingException { 198 return attachmentsAllowed; 199 } 200 201 206 public boolean isReplyToList() throws MessagingException { 207 return replyToList; 208 } 209 210 215 public MailAddress getListservAddress() throws MessagingException { 216 return listservAddress; 217 } 218 219 222 public String getSubjectPrefix() throws MessagingException { 223 return subjectPrefix; 224 } 225 226 232 protected void loadSettings() throws MessagingException { 233 Connection conn = null; 234 PreparedStatement stmt = null; 235 ResultSet rs = null; 236 237 try { 238 conn = datasource.getConnection(); 240 try { 241 stmt = conn.prepareStatement(membersQuery); 242 stmt.setString(1, listservID); 243 rs = stmt.executeQuery(); 244 Collection tmpMembers = new Vector (); 245 while (rs.next()) { 246 String address = rs.getString(1); 247 try { 248 MailAddress mailAddress = new MailAddress(address); 249 tmpMembers.add(mailAddress); 250 } catch (ParseException pe) { 251 StringBuffer exceptionBuffer = 253 new StringBuffer (64) 254 .append("error parsing address '") 255 .append(address) 256 .append("' in listserv '") 257 .append(listservID) 258 .append("'"); 259 log(exceptionBuffer.toString()); 260 } 261 } 262 members = tmpMembers; 263 } finally { 264 ResultSet localRS = rs; 265 rs = null; 267 theJDBCUtil.closeJDBCResultSet(localRS); 268 Statement localStmt = stmt; 269 stmt = null; 271 theJDBCUtil.closeJDBCStatement(localStmt); 272 } 273 274 stmt = conn.prepareStatement(listservQuery); 275 stmt.setString(1, listservID); 276 rs = stmt.executeQuery(); 277 if (!rs.next()) { 278 StringBuffer exceptionBuffer = 279 new StringBuffer (64) 280 .append("Could not find listserv record for '") 281 .append(listservID) 282 .append("'"); 283 throw new MailetException(exceptionBuffer.toString()); 284 } 285 membersOnly = rs.getBoolean("members_only"); 286 attachmentsAllowed = rs.getBoolean("attachments_allowed"); 287 replyToList = rs.getBoolean("reply_to_list"); 288 subjectPrefix = rs.getString("subject_prefix"); 289 String address = rs.getString("list_address"); 290 if (address == null) { 291 listservAddress = null; 292 } else { 293 try { 294 listservAddress = new MailAddress(address); 295 } catch (ParseException pe) { 296 StringBuffer logBuffer = 298 new StringBuffer (128) 299 .append("invalid listserv address '") 300 .append(listservAddress) 301 .append("' for listserv '") 302 .append(listservID) 303 .append("'"); 304 log(logBuffer.toString()); 305 listservAddress = null; 306 } 307 } 308 } catch (SQLException sqle) { 309 throw new MailetException("Problem loading settings", sqle); 310 } finally { 311 theJDBCUtil.closeJDBCResultSet(rs); 312 theJDBCUtil.closeJDBCStatement(stmt); 313 theJDBCUtil.closeJDBCConnection(conn); 314 } 315 } 316 317 322 public String getMailetInfo() { 323 return "JDBC listserv mailet"; 324 } 325 } 326 | Popular Tags |