mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
Code cleanup - move old iterator/while loops to for loops
This commit is contained in:
1 parent
0c4fc4eea3
commit
63e9f603ec
25 files changed
+360
-524
No files matched your search
@@ -1,7 +1,7 @@
|
||||
package com.avaje.ebeaninternal.server.lib.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
@@ -60,9 +60,7 @@ public class Dnode {
|
||||
sb = new StringBuilder();
|
||||
}
|
||||
sb.append("<").append(nodeName);
|
||||
Iterator<String> it = attributeNames();
|
||||
while (it.hasNext()) {
|
||||
String attr = it.next();
|
||||
for (String attr : attrList.keySet()) {
|
||||
Object attrValue = getAttribute(attr);
|
||||
sb.append(" ").append(attr).append("=\"");
|
||||
if (attrValue != null) {
|
||||
@@ -145,9 +143,7 @@ public class Dnode {
|
||||
if (children.remove(node)) {
|
||||
return true;
|
||||
}
|
||||
Iterator<Dnode> it = children.iterator();
|
||||
while (it.hasNext()) {
|
||||
Dnode child = it.next();
|
||||
for (Dnode child : children) {
|
||||
if (child.remove(node)) {
|
||||
return true;
|
||||
}
|
||||
@@ -286,8 +282,8 @@ public class Dnode {
|
||||
/**
|
||||
* The attribute names as strings.
|
||||
*/
|
||||
public Iterator<String> attributeNames() {
|
||||
return attrList.keySet().iterator();
|
||||
public Collection<String> attributeNames() {
|
||||
return attrList.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,8 +2,9 @@ package com.avaje.ebeaninternal.server.lib.util;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A simple test message that can be sent via smtp.
|
||||
@@ -84,8 +85,8 @@ public class MailMessage {
|
||||
/**
|
||||
* Return the recipient list.
|
||||
*/
|
||||
public Iterator<MailAddress> getRecipientList() {
|
||||
return recipientList.iterator();
|
||||
public List<MailAddress> getRecipientList() {
|
||||
return recipientList;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,15 +120,15 @@ public class MailMessage {
|
||||
/**
|
||||
* Return the body text.
|
||||
*/
|
||||
public Iterator<String> getBodyLines() {
|
||||
return bodylines.iterator();
|
||||
public List<String> getBodyLines() {
|
||||
return bodylines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the headers.
|
||||
*/
|
||||
public Iterator<String> getHeaderFields() {
|
||||
return header.keySet().iterator();
|
||||
public Collection<String> getHeaderFields() {
|
||||
return header.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,16 +141,13 @@ public class MailMessage {
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(100);
|
||||
sb.append("Sender: " + senderAddress + "\tRecipient: " + recipientList + "\n");
|
||||
Iterator<String> hi = header.keySet().iterator();
|
||||
while (hi.hasNext()) {
|
||||
String key = hi.next();
|
||||
for (String key : header.keySet()) {
|
||||
String hline = key + ": " + header.get(key) + "\n";
|
||||
sb.append(hline);
|
||||
}
|
||||
sb.append("\n");
|
||||
Iterator<String> e = bodylines.iterator();
|
||||
while (e.hasNext()) {
|
||||
sb.append(e.next()).append("\n");
|
||||
for (String line : bodylines) {
|
||||
sb.append(line).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
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;
|
||||
@@ -10,206 +7,200 @@ import java.io.OutputStreamWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Sends simple MailMessages via smtp.
|
||||
* Sends simple MailMessages via smtp.
|
||||
*/
|
||||
public class MailSender implements Runnable {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MailSender.class);
|
||||
|
||||
int traceLevel = 0;
|
||||
|
||||
Socket sserver;
|
||||
String server;
|
||||
|
||||
BufferedReader in;
|
||||
private static final Logger logger = LoggerFactory.getLogger(MailSender.class);
|
||||
|
||||
OutputStreamWriter out;
|
||||
|
||||
MailMessage message;
|
||||
int traceLevel = 0;
|
||||
|
||||
MailListener listener = null;
|
||||
Socket sserver;
|
||||
String server;
|
||||
|
||||
private static final int SMTP_PORT = 25;
|
||||
BufferedReader in;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
OutputStreamWriter out;
|
||||
|
||||
/**
|
||||
* Send the message.
|
||||
*/
|
||||
public void run() {
|
||||
send(message);
|
||||
}
|
||||
MailMessage message;
|
||||
|
||||
/**
|
||||
* Send the message in a background thread.
|
||||
*/
|
||||
public void sendInBackground(MailMessage message){
|
||||
this.message = message;
|
||||
Thread thread = new Thread(this);
|
||||
thread.start();
|
||||
}
|
||||
MailListener listener = null;
|
||||
|
||||
|
||||
/**
|
||||
* Send the message in the current thread.
|
||||
*/
|
||||
public void send(MailMessage message){
|
||||
try {
|
||||
Iterator<MailAddress> i = message.getRecipientList();
|
||||
while (i.hasNext()) {
|
||||
MailAddress recipientAddress = (MailAddress) i.next();
|
||||
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 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() + ">");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Iterator<String> hi = message.getHeaderFields();
|
||||
while (hi.hasNext()) {
|
||||
String key = (String) hi.next();
|
||||
writeln(key + ": " + message.getHeader(key));
|
||||
}
|
||||
writeln(""); // end of header;
|
||||
Iterator<String> e = message.getBodyLines();
|
||||
while (e.hasNext()) {
|
||||
String bline = (String) e.next();
|
||||
if (bline.startsWith(".")) {
|
||||
bline = "." + bline;
|
||||
}
|
||||
writeln(bline);
|
||||
}
|
||||
writeln(".");
|
||||
expect250();
|
||||
writeln("QUIT");
|
||||
// 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;
|
||||
}
|
||||
|
||||
private boolean expect250() throws IOException {
|
||||
String line = readln();
|
||||
if (!line.startsWith("2")) {
|
||||
logger.info("SmtpSender.expect250: " + line);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
writeln("EHLO " + localaddress);
|
||||
if (!expect250()) {
|
||||
return;
|
||||
}
|
||||
|
||||
private void writeln(String s) throws IOException {
|
||||
if (traceLevel > 2){
|
||||
logger.debug("From client: " + s);
|
||||
}
|
||||
out.write(s + "\r\n");
|
||||
out.flush();
|
||||
writeln("MAIL FROM:<" + sender.getEmailAddress() + ">");
|
||||
if (!expect250()) {
|
||||
return;
|
||||
}
|
||||
|
||||
private String readln() throws IOException {
|
||||
String line = in.readLine();
|
||||
if (traceLevel > 1){
|
||||
logger.debug("From server: " + line);
|
||||
}
|
||||
return line;
|
||||
writeln("RCPT TO:<" + recipientAddress.getEmailAddress() + ">");
|
||||
if (!expect250()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the trace level.
|
||||
*/
|
||||
public void setTraceLevel(int traceLevel){
|
||||
this.traceLevel = traceLevel;
|
||||
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");
|
||||
|
||||
/**
|
||||
* 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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