mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#618 - Refactor - remove DataSourcePool implementation as separate dependency
This commit is contained in:
1 parent
83d0d6320c
commit
c296127bab
38 files changed
+69
-6275
No files matched your search
@@ -1,43 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.util;
|
||||
|
||||
/**
|
||||
* An Email address with an associated alias.
|
||||
*/
|
||||
public class MailAddress {
|
||||
|
||||
|
||||
final String alias;
|
||||
|
||||
final String emailAddress;
|
||||
|
||||
/**
|
||||
* Create an address with an optional alias.
|
||||
*/
|
||||
public MailAddress(String alias, String emailAddress) {
|
||||
this.alias = alias;
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the alias.
|
||||
* If the alias is null this returns an empty string.
|
||||
*/
|
||||
public String getAlias() {
|
||||
if (alias == null) {
|
||||
return "";
|
||||
}
|
||||
return alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the email address.
|
||||
*/
|
||||
public String getEmailAddress() {
|
||||
return emailAddress;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getAlias() + " " + "<" + getEmailAddress() + ">";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.util;
|
||||
|
||||
/**
|
||||
* Represents the success or failure of a mail send.
|
||||
*/
|
||||
public class MailEvent {
|
||||
|
||||
|
||||
/**
|
||||
* The error indicating a send failure.
|
||||
*/
|
||||
final Throwable error;
|
||||
|
||||
/**
|
||||
* The message that was sent.
|
||||
*/
|
||||
final MailMessage message;
|
||||
|
||||
|
||||
/**
|
||||
* The message send failed with an error.
|
||||
*/
|
||||
public MailEvent(MailMessage message, Throwable error) {
|
||||
this.message = message;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
/**
|
||||
* The message that we attempted to send.
|
||||
*/
|
||||
public MailMessage getMailMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the message was sent successfully.
|
||||
*/
|
||||
public boolean wasSuccessful() {
|
||||
return (error == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The error indicating the send failed.
|
||||
*/
|
||||
public Throwable getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.util;
|
||||
|
||||
/**
|
||||
* Listens to see if the message was successfully sent.
|
||||
*/
|
||||
public interface MailListener {
|
||||
|
||||
/**
|
||||
* Handle the message event.
|
||||
*/
|
||||
void handleEvent(MailEvent event);
|
||||
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.util;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A simple test message that can be sent via smtp.
|
||||
*/
|
||||
public class MailMessage {
|
||||
|
||||
/**
|
||||
* The body content.
|
||||
*/
|
||||
final ArrayList<String> bodylines;
|
||||
|
||||
/**
|
||||
* The sender email address.
|
||||
*/
|
||||
MailAddress senderAddress;
|
||||
|
||||
/**
|
||||
* The headers.
|
||||
*/
|
||||
final HashMap<String, String> header = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* the recipient of the email.
|
||||
*/
|
||||
MailAddress currentRecipient;
|
||||
|
||||
/**
|
||||
* The list of recipients.
|
||||
*/
|
||||
final ArrayList<MailAddress> recipientList = new ArrayList<MailAddress>();
|
||||
|
||||
/**
|
||||
* Create the message.
|
||||
*/
|
||||
public MailMessage() {
|
||||
bodylines = new ArrayList<String>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current recipient.
|
||||
*/
|
||||
public void setCurrentRecipient(MailAddress currentRecipient) {
|
||||
this.currentRecipient = currentRecipient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current recipient.
|
||||
*/
|
||||
public MailAddress getCurrentRecipient() {
|
||||
return currentRecipient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a recipient.
|
||||
*/
|
||||
public void addRecipient(String alias, String emailAddress) {
|
||||
recipientList.add(new MailAddress(alias, emailAddress));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sender details.
|
||||
*/
|
||||
public void setSender(String alias, String senderEmail) {
|
||||
this.senderAddress = new MailAddress(alias, senderEmail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the sender address.
|
||||
*/
|
||||
public MailAddress getSender() {
|
||||
return senderAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the recipient list.
|
||||
*/
|
||||
public List<MailAddress> getRecipientList() {
|
||||
return recipientList;
|
||||
}
|
||||
|
||||
/**
|
||||
* add a header to the message.
|
||||
*/
|
||||
public void addHeader(String key, String val) {
|
||||
header.put(key, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the subject text.
|
||||
*/
|
||||
public void setSubject(String subject) {
|
||||
addHeader("Subject", subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the subject text.
|
||||
*/
|
||||
public String getSubject() {
|
||||
return getHeader("Subject");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add text to the body.
|
||||
*/
|
||||
public void addBodyLine(String line) {
|
||||
bodylines.add(line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the body text.
|
||||
*/
|
||||
public List<String> getBodyLines() {
|
||||
return bodylines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the headers.
|
||||
*/
|
||||
public Collection<String> getHeaderFields() {
|
||||
return header.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a given header.
|
||||
*/
|
||||
public String getHeader(String key) {
|
||||
return header.get(key);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(100);
|
||||
sb.append("Sender: ").append(senderAddress).append("\tRecipient: ").append(recipientList).append("\n");
|
||||
for (String key : header.keySet()) {
|
||||
String hline = key + ": " + header.get(key) + "\n";
|
||||
sb.append(hline);
|
||||
}
|
||||
sb.append("\n");
|
||||
for (String line : bodylines) {
|
||||
sb.append(line).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,206 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* Sends simple MailMessages via smtp.
|
||||
*/
|
||||
public class MailSender implements Runnable {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MailSender.class);
|
||||
|
||||
int traceLevel = 0;
|
||||
|
||||
Socket sserver;
|
||||
final String server;
|
||||
|
||||
BufferedReader in;
|
||||
|
||||
OutputStreamWriter out;
|
||||
|
||||
MailMessage message;
|
||||
|
||||
MailListener listener = null;
|
||||
|
||||
private static final int SMTP_PORT = 25;
|
||||
|
||||
/**
|
||||
* Create for a given mail server.
|
||||
*/
|
||||
public MailSender(String server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the listener to handle MessageEvents.
|
||||
*/
|
||||
public void setMailListener(MailListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the message.
|
||||
*/
|
||||
public void run() {
|
||||
send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the message in a background thread.
|
||||
*/
|
||||
public void sendInBackground(MailMessage message) {
|
||||
this.message = message;
|
||||
Thread thread = new Thread(this);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the message in the current thread.
|
||||
*/
|
||||
public void send(MailMessage message) {
|
||||
try {
|
||||
for (MailAddress recipientAddress : message.getRecipientList()) {
|
||||
sserver = new Socket(server, SMTP_PORT);
|
||||
send(message, sserver, recipientAddress);
|
||||
sserver.close();
|
||||
|
||||
if (listener != null) {
|
||||
MailEvent event = new MailEvent(message, null);
|
||||
listener.handleEvent(event);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
if (listener != null) {
|
||||
MailEvent event = new MailEvent(message, ex);
|
||||
listener.handleEvent(event);
|
||||
} else {
|
||||
logger.error(null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void send(MailMessage message, Socket sserver, MailAddress recipientAddress) throws IOException {
|
||||
|
||||
// A bit convoluted, but doesn't depend on DNS in any way...
|
||||
InetAddress localhost = sserver.getLocalAddress();
|
||||
String localaddress = localhost.getHostAddress();
|
||||
MailAddress sender = message.getSender();
|
||||
message.setCurrentRecipient(recipientAddress);
|
||||
|
||||
// Mandatory header fields, Date and From
|
||||
if (message.getHeader("Date") == null) {
|
||||
message.addHeader("Date", new java.util.Date().toString());
|
||||
}
|
||||
if (message.getHeader("From") == null) {
|
||||
message.addHeader("From", sender.getAlias() + " <" + sender.getEmailAddress() + ">");
|
||||
}
|
||||
|
||||
// if (message.getHeader("From") == null){
|
||||
message.addHeader("To", recipientAddress.getAlias() + " <" + recipientAddress.getEmailAddress() + ">");
|
||||
// }
|
||||
|
||||
out = new OutputStreamWriter(sserver.getOutputStream());
|
||||
in = new BufferedReader(new InputStreamReader(sserver.getInputStream()));
|
||||
String sintro = readln();
|
||||
if (!sintro.startsWith("220")) { // 220
|
||||
logger.debug("SmtpSender: intro==" + sintro);
|
||||
return;
|
||||
}
|
||||
|
||||
writeln("EHLO " + localaddress);
|
||||
if (!expect250()) {
|
||||
return;
|
||||
}
|
||||
|
||||
writeln("MAIL FROM:<" + sender.getEmailAddress() + ">");
|
||||
if (!expect250()) {
|
||||
return;
|
||||
}
|
||||
writeln("RCPT TO:<" + recipientAddress.getEmailAddress() + ">");
|
||||
if (!expect250()) {
|
||||
return;
|
||||
}
|
||||
writeln("DATA");
|
||||
while (true) { // may be multiple 250 replies pending from server
|
||||
String line = readln();
|
||||
if (line.startsWith("3"))
|
||||
break; // ready to send
|
||||
if (!line.startsWith("2")) {
|
||||
logger.debug("SmtpSender.send reponse to DATA: " + line);
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (String key : message.getHeaderFields()) {
|
||||
writeln(key + ": " + message.getHeader(key));
|
||||
}
|
||||
writeln(""); // end of header;
|
||||
for (String bline : message.getBodyLines()) {
|
||||
if (bline.startsWith(".")) {
|
||||
bline = "." + bline;
|
||||
}
|
||||
writeln(bline);
|
||||
}
|
||||
writeln(".");
|
||||
expect250();
|
||||
writeln("QUIT");
|
||||
|
||||
}
|
||||
|
||||
private boolean expect250() throws IOException {
|
||||
String line = readln();
|
||||
if (!line.startsWith("2")) {
|
||||
logger.info("SmtpSender.expect250: " + line);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void writeln(String s) throws IOException {
|
||||
if (traceLevel > 2) {
|
||||
logger.debug("From client: " + s);
|
||||
}
|
||||
out.write(s + "\r\n");
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private String readln() throws IOException {
|
||||
String line = in.readLine();
|
||||
if (traceLevel > 1) {
|
||||
logger.debug("From server: " + line);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the trace level.
|
||||
*/
|
||||
public void setTraceLevel(int traceLevel) {
|
||||
this.traceLevel = traceLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the hostname of the local machine.
|
||||
*/
|
||||
public String getLocalHostName() {
|
||||
try {
|
||||
InetAddress ipaddress = InetAddress.getLocalHost();
|
||||
String localHost = ipaddress.getHostName();
|
||||
if (localHost == null) {
|
||||
return "localhost";
|
||||
} else {
|
||||
return localHost;
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
return "localhost";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user