Friday, January 30, 2009

Sample of Java Pattern -- Template Method

EmailTemplate.java
package templatemethod;

import javax.mail.*;
import javax.mail.internet.*;

public abstract class EmailTemplate {
public void send(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd, String[] recipients, String subject,String message, String from){
MimeMessage msg = connect(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd);
try {
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
} catch (MessagingException e) {
System.out.println("Could not send this email, title=" + subject + " addressTo=" + recipients[0] + ", please check the email server connection.");
}
}

protected abstract MimeMessage connect(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd);
}


GeneralEmailService.java
package templatemethod;

import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

import templatemethod.GoogleEmailService.SMTPAuthenticator;

public class GeneralEmailService extends EmailTemplate{
private String smtpAuthUser;
private String smtpAuthPwd;
protected MimeMessage connect(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd){

this.smtpAuthUser = smtpAuthUser;
this.smtpAuthPwd = smtpAuthPwd;

boolean debug = false;

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", smtpHostName);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.quitwait", "false");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

MimeMessage msg = new MimeMessage(session);

return msg;

}

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = smtpAuthUser;
String password = smtpAuthPwd;
return new PasswordAuthentication(username, password);
}
}


}


GoogleEmailService.java
package templatemethod;

import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

public class GoogleEmailService extends EmailTemplate{
private String smtpAuthUser;
private String smtpAuthPwd;
protected MimeMessage connect(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd){

this.smtpAuthUser = smtpAuthUser;
this.smtpAuthPwd = smtpAuthPwd;

boolean debug = false;

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", smtpHostName);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.quitwait", "false");

props.put("mail.smtp.socketFactory.port", smtpPort);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

MimeMessage msg = new MimeMessage(session);

return msg;

}

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = smtpAuthUser;
String password = smtpAuthPwd;
return new PasswordAuthentication(username, password);
}
}

}


TemplateMethodTest.java
package templatemethod;

public class TemplateMethodTest {

public static void main(String[] args) {
String smtpHostName = "";
String smtpPort = "";
String smtpAuthUser = "";
String smtpAuthPwd = "";
String[] recipients = {""};
String subject = "";
String message = "";
String from = "";
EmailTemplate googleEmailService = new GoogleEmailService();
googleEmailService.send(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd, recipients, subject, message, from);
EmailTemplate generalEmailService = new GeneralEmailService();
generalEmailService.send(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd, recipients, subject, message, from);
}

}


you may download the source code.

Saturday, January 24, 2009

Sample of Java Pattern -- Façade

Customer.java
package facade;

public class Customer {
private String name;
private String address;
private Order order;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Order getOrder() {
return order;
}

public void setOrder(Order order) {
this.order = order;
}

public void save(){

}

}


Order.java
package facade;

public class Order {
private String sku;

public String getSku() {
return sku;
}

public void setSku(String sku) {
this.sku = sku;
}

public void save(){

}
}


CustomerFacade.java
package facade;

public class CustomerFacade {
public void placeOrder(String customerName, String customerAddress, String sku){
Customer customer = new Customer();
customer.setName(customerName);
customer.setAddress(customerAddress);
Order order = new Order();
order.setSku(sku);
customer.setOrder(order);
customer.save();
order.save();
}
}


FacadeTest.java
package facade;

public class FacadeTest {

public static void main(String[] args) {
CustomerFacade customerFacade = new CustomerFacade();
customerFacade.placeOrder("customerName", "customerAddress", "sku");
}

}


you may download the source code.

Thursday, January 22, 2009

Debug Tomcat and JBoss Web-application in Eclipse

1. configure tomcat to allow remote debugging
ensure the startup.bat includes:
set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket

or startup.sh includes:
export JPDA_ADDRESS=8000
export JPDA_TRANSPORT=dt_socket

and update the execute line to inlcude(the first line for startup.bat, the second line for startup.sh):
call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%
exec "$PRGDIR"/"$EXECUTABLE" jpda start "$@"


2. configure jboss to allow remote debugging
ensure the JAVA_OPTS in your jboss run.sh or run.bat includes:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y


3. configure eclipse to remote debug
go into 'debug' in eclipse and new a remote debug application, remember apply 'port number' to web application server debug port, in this case, this port number is 8787, not 8080.

Friday, January 16, 2009

Sample of Java Pattern -- Adapter

LegacyModel.java
package adapter;

public class LegacyModel {
public void ins(String sku){
System.out.println("call ins method.");
}
}


ModernModel.java
package adapter;

public interface ModernModel {
public void save(Order order);
}


ModernModelAdapter.java
package adapter;

public class ModernModelAdapter implements ModernModel{
private LegacyModel legacyModel;

public ModernModelAdapter(LegacyModel legacyModel) {
super();
this.legacyModel = legacyModel;
}

public void save(Order order){
String sku = order.getSku();
legacyModel = new LegacyModel();
legacyModel.ins(sku);
}
}


Order.java
package adapter;

public class Order {
private String sku;

public String getSku() {
return sku;
}

public void setSku(String sku) {
this.sku = sku;
}

}


AdapterTest.java
package adapter;

public class AdapterTest {
public static void main(String[] args) {
LegacyModel legacy = new LegacyModel();
Order order = new Order();
order.setSku("sku");
ModernModelAdapter adapter = new ModernModelAdapter(legacy);
adapter.save(order);
}
}


you may download the source code.

Monday, January 12, 2009

Sample of Java Pattern -- Command

Command.java
package command;

public interface Command {
public void execute();
}


ConfirmCommand.java
package command;

public class ConfirmCommand implements Command {

private EmailService emailService;

public ConfirmCommand(){}

public void execute() {
emailService = new EmailService();
emailService.confirmMeeting();
}

}


CancelCommand.java
package command;

public class CancelCommand implements Command {

private EmailService emailService;

public CancelCommand(){}

public void execute() {
emailService = new EmailService();
emailService.cancelMeeting();
}

}


EmailService.java
package command;

public class EmailService {

public EmailService(){}

public void cancelMeeting(){
System.out.println("send cacellation email");
}

public void confirmMeeting(){
System.out.println("send confirmation email");
}

}


Invoker.java
package command;

public class Invoker {
private Command command;

public Invoker(){
}

public void setCommand(Command command){
this.command = command;
}

public void executeCommand(){
this.command.execute();
}
}


CommandTest.java
package command;

public class CommandTest {

public static void main(String[] args) {
CancelCommand cancelCommand = new CancelCommand();
ConfirmCommand confirmCommand = new ConfirmCommand();
Invoker invoker = new Invoker();
invoker.setCommand(confirmCommand);
invoker.executeCommand();
invoker.setCommand(cancelCommand);
invoker.executeCommand();
}

}
you can download the source code .

Monday, January 5, 2009

Sample of Java Pattern -- Singleton

Utils.java
package singleton;

public class Utils {

private Utils(){}

private static Utils instance = new Utils();

public static Utils getInstance() {
return instance;
}

public void println(String str){
System.out.println(str);
}

public static void main(String[] args) {
Utils.getInstance().println("Hello");
}
}


you may download the source code.