1 17 18 package org.apache.james.mailrepository; 19 20 import org.apache.avalon.cornerstone.services.store.StreamRepository; 21 import org.apache.james.core.MimeMessageSource; 22 import org.apache.james.util.JDBCUtil; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.SequenceInputStream ; 28 import java.sql.Blob ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 35 39 public class MimeMessageJDBCSource extends MimeMessageSource { 40 41 44 private static final boolean DEEP_DEBUG = false; 45 46 JDBCMailRepository repository = null; 48 String key = null; 49 StreamRepository sr = null; 50 51 private long size = -1; 52 53 56 String retrieveMessageBodySQL = null; 57 58 61 String retrieveMessageBodySizeSQL = null; 62 63 66 private static final JDBCUtil theJDBCUtil = 67 new JDBCUtil() { 68 protected void delegatedLog(String logString) { 69 } 72 }; 73 74 78 public MimeMessageJDBCSource(JDBCMailRepository repository, 79 String key, StreamRepository sr) throws IOException { 80 if (repository == null) { 81 throw new IOException ("Repository is null"); 82 } 83 if (key == null) { 84 throw new IOException ("Message name (key) was not defined"); 85 } 86 this.repository = repository; 87 this.key = key; 88 this.sr = sr; 89 90 retrieveMessageBodySQL = 91 repository.sqlQueries.getSqlString("retrieveMessageBodySQL", true); 92 retrieveMessageBodySizeSQL = 94 repository.sqlQueries.getSqlString("retrieveMessageBodySizeSQL"); 95 } 96 97 104 public String getSourceId() { 105 StringBuffer sourceIdBuffer = 106 new StringBuffer (128) 107 .append(repository.repositoryName) 108 .append("/") 109 .append(key); 110 return sourceIdBuffer.toString(); 111 } 112 113 118 public synchronized InputStream getInputStream() throws IOException { 119 Connection conn = null; 120 PreparedStatement retrieveMessageStream = null; 121 ResultSet rsRetrieveMessageStream = null; 122 try { 123 conn = repository.getConnection(); 124 125 byte[] headers = null; 126 127 long start = 0; 128 if (DEEP_DEBUG) { 129 start = System.currentTimeMillis(); 130 System.out.println("starting"); 131 } 132 retrieveMessageStream = conn.prepareStatement(retrieveMessageBodySQL); 133 retrieveMessageStream.setString(1, key); 134 retrieveMessageStream.setString(2, repository.repositoryName); 135 rsRetrieveMessageStream = retrieveMessageStream.executeQuery(); 136 137 if (!rsRetrieveMessageStream.next()) { 138 throw new IOException ("Could not find message"); 139 } 140 141 String getBodyOption = repository.sqlQueries.getDbOption("getBody"); 142 if (getBodyOption != null && getBodyOption.equalsIgnoreCase("useBlob")) { 143 Blob b = rsRetrieveMessageStream.getBlob(1); 144 headers = b.getBytes(1, (int)b.length()); 145 } else { 146 headers = rsRetrieveMessageStream.getBytes(1); 147 } 148 if (DEEP_DEBUG) { 149 System.err.println("stopping"); 150 System.err.println(System.currentTimeMillis() - start); 151 } 152 153 InputStream in = new ByteArrayInputStream (headers); 154 try { 155 if (sr != null) { 156 in = new SequenceInputStream (in, sr.get(key)); 157 } 158 } catch (Exception e) { 159 } 162 return in; 163 } catch (SQLException sqle) { 164 throw new IOException (sqle.toString()); 165 } finally { 166 theJDBCUtil.closeJDBCResultSet(rsRetrieveMessageStream); 167 theJDBCUtil.closeJDBCStatement(retrieveMessageStream); 168 theJDBCUtil.closeJDBCConnection(conn); 169 } 170 } 171 172 175 public synchronized long getMessageSize() throws IOException { 176 if (size != -1) return size; 177 if (retrieveMessageBodySizeSQL == null) { 178 System.err.println("no SQL statement to find size"); 180 return size = super.getMessageSize(); 181 } 182 Connection conn = null; 183 PreparedStatement retrieveMessageSize = null; 184 ResultSet rsRetrieveMessageSize = null; 185 try { 186 conn = repository.getConnection(); 187 188 retrieveMessageSize = conn.prepareStatement(retrieveMessageBodySizeSQL); 189 retrieveMessageSize.setString(1, key); 190 retrieveMessageSize.setString(2, repository.repositoryName); 191 rsRetrieveMessageSize = retrieveMessageSize.executeQuery(); 192 193 if (!rsRetrieveMessageSize.next()) { 194 throw new IOException ("Could not find message"); 195 } 196 197 size = rsRetrieveMessageSize.getLong(1); 198 199 InputStream in = null; 200 try { 201 if (sr != null) { 202 if (sr instanceof org.apache.james.mailrepository.filepair.File_Persistent_Stream_Repository) { 203 size += ((org.apache.james.mailrepository.filepair.File_Persistent_Stream_Repository) sr).getSize(key); 204 } else { 205 in = sr.get(key); 206 int len = 0; 207 byte[] block = new byte[1024]; 208 while ((len = in.read(block)) > -1) { 209 size += len; 210 } 211 } 212 } 213 } catch (Exception e) { 214 } finally { 217 try { 218 if (in != null) { 219 in.close(); 220 } 221 } catch (IOException ioe) { 222 } 224 } 225 226 return size; 227 } catch (SQLException sqle) { 228 throw new IOException (sqle.toString()); 229 } finally { 230 theJDBCUtil.closeJDBCResultSet(rsRetrieveMessageSize); 231 theJDBCUtil.closeJDBCStatement(retrieveMessageSize); 232 theJDBCUtil.closeJDBCConnection(conn); 233 } 234 } 235 236 239 public boolean equals(Object obj) { 240 if (obj instanceof MimeMessageJDBCSource) { 241 MimeMessageJDBCSource source = (MimeMessageJDBCSource)obj; 244 return ((source.key == key) || ((source.key != null) && source.key.equals(key))) && 245 ((source.repository == repository) || ((source.repository != null) && source.repository.equals(repository))); 246 } 247 return false; 248 } 249 250 255 public int hashCode() { 256 int result = 17; 257 if (key != null) { 258 result = 37 * key.hashCode(); 259 } 260 if (repository != null) { 261 result = 37 * repository.hashCode(); 262 } 263 return result; 264 } 265 } 266 | Popular Tags |