1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.transport.mailets;
23
24 import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector;
25 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.james.Constants;
28 import org.apache.james.impl.vut.VirtualUserTableUtil;
29 import org.apache.james.util.sql.JDBCUtil;
30 import org.apache.mailet.MailAddress;
31 import org.apache.mailet.MailetException;
32
33 import javax.mail.MessagingException;
34
35 import java.sql.Connection;
36 import java.sql.DatabaseMetaData;
37 import java.sql.PreparedStatement;
38 import java.sql.ResultSet;
39 import java.sql.SQLException;
40 import java.util.Collection;
41 import java.util.Iterator;
42 import java.util.Map;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public class JDBCVirtualUserTable extends AbstractVirtualUserTable
95 {
96 protected DataSourceComponent datasource;
97
98
99
100
101 protected String query = null;
102
103
104
105
106 private final JDBCUtil theJDBCUtil = new JDBCUtil() {
107 protected void delegatedLog(String logString) {
108 log("JDBCVirtualUserTable: " + logString);
109 }
110 };
111
112
113
114
115 public void init() throws MessagingException {
116 if (getInitParameter("table") == null) {
117 throw new MailetException("Table location not specified for JDBCVirtualUserTable");
118 }
119
120 String tableURL = getInitParameter("table");
121
122 String datasourceName = tableURL.substring(5);
123 int pos = datasourceName.indexOf("/");
124 String tableName = datasourceName.substring(pos + 1);
125 datasourceName = datasourceName.substring(0, pos);
126 Connection conn = null;
127
128 try {
129 ServiceManager componentManager = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
130
131 DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE);
132
133 datasource = (DataSourceComponent)datasources.select(datasourceName);
134
135 conn = datasource.getConnection();
136
137
138 DatabaseMetaData dbMetaData = conn.getMetaData();
139
140
141 if (!(theJDBCUtil.tableExists(dbMetaData, tableName))) {
142 StringBuffer exceptionBuffer =
143 new StringBuffer(128)
144 .append("Could not find table '")
145 .append(tableName)
146 .append("' in datasource '")
147 .append(datasourceName)
148 .append("'");
149 throw new MailetException(exceptionBuffer.toString());
150 }
151
152
153 query = getInitParameter("sqlquery",VirtualUserTableUtil.QUERY);
154 } catch (MailetException me) {
155 throw me;
156 } catch (Exception e) {
157 throw new MessagingException("Error initializing JDBCVirtualUserTable", e);
158 } finally {
159 theJDBCUtil.closeJDBCConnection(conn);
160 }
161 }
162
163
164
165
166
167
168
169 protected void mapRecipients(Map recipientsMap) throws MessagingException {
170 Connection conn = null;
171 PreparedStatement mappingStmt = null;
172
173 Collection recipients = recipientsMap.keySet();
174
175 try {
176 conn = datasource.getConnection();
177 mappingStmt = conn.prepareStatement(query);
178
179 for (Iterator i = recipients.iterator(); i.hasNext(); ) {
180 ResultSet mappingRS = null;
181 try {
182 MailAddress source = (MailAddress)i.next();
183 mappingStmt.setString(1, source.getUser());
184 mappingStmt.setString(2, source.getHost());
185 mappingStmt.setString(3, source.getHost());
186 mappingRS = mappingStmt.executeQuery();
187 if (mappingRS.next()) {
188 String targetString = mappingRS.getString(1);
189 recipientsMap.put(source, targetString);
190 }
191 } finally {
192 theJDBCUtil.closeJDBCResultSet(mappingRS);
193 }
194 }
195 } catch (SQLException sqle) {
196 throw new MessagingException("Error accessing database", sqle);
197 } finally {
198 theJDBCUtil.closeJDBCStatement(mappingStmt);
199 theJDBCUtil.closeJDBCConnection(conn);
200 }
201 }
202
203 public String getMailetInfo() {
204 return "JDBC Virtual User Table mailet";
205 }
206 }