论坛首页 Java企业应用论坛

无API侵入的JIOPi模块化编程风格达成

浏览 2167 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-06-17   最后修改:2010-06-17

无API侵入的JIOPi模块化编程风格达成

——POJO风格实现简单邮件发送API调用

 

API侵入是任何框架都很难避免的问题,而被侵入框架API的程序也很容易被该框架绑定,很难脱离框架运行环境。JIOPi作为一种IOP编程和模块化编程风格(而非框架),一直努力减少额外API的引入,非运行时标注让JIOPi模块完全与POJO兼容,通过IoC框架整合,避免调用代码中再引入额外API。然而IoC框架整合也只是将JIOPiAPI从用户的代码中转移到了IoC框架配置文件中,并且需要引入IoC框架。JIOPi 0.4引入了全新的类重定义代码风格,完全避免了使用JIOPi模块而需要引入JIOPi API的问题,真正将JIOPi从框架变为编程风格,将编程风格的实现变为透明实现,你要做的,只是将一个不到50k的Jar包放在lib目录中,然后在应用程序启动后尽早调用JIOPi的初始化程序函数。

 

 注:JDK1.5无法实现上下文环境(ContextClassLoader)中的类重定义,这个特性需要JDK1.6的支持。

 

下面就以调用一个简单邮件发送API为例来说明JIOPi v0.4带来的无API侵入风格的模块化编程。相关源码请下载JIOPi-iBean-0.4.0.0-src.zip源码包

http://code.google.com/p/ibean/downloads/list

 

邮件发送API定义如下:

MailSender.java

package org.jiopi.blueprint.mailsender;

import org.jiopi.framework.annotation.blueprint.RegisterControlPanel;

@Version
@RegisterControlPanel("jiopi.MailSender")
public interface MailSender {

	@RegisterControlPanel("sendMail")
	public String sendMail(String to,String subject,String message) throws SendMailException;

	public String sendMail(String to,String subject,String html,String text) throws SendMailException;
	
	@RegisterControlPanel("createNewTextMail")
	public Mail createNewTextMail();
	
	@RegisterControlPanel("createNewHtmlMail")
	public Mail createNewHtmlMail();
	
	public String getHostName();
	
	public String getAuthenticationUserName();
	
	public String getAuthenticationPassword();
	
	public String getFrom();
	
	public String getCharset();
}

 

Mail.java

package org.jiopi.blueprint.mailsender;

import java.io.File;
import java.net.URL;

import org.jiopi.framework.annotation.blueprint.RegisterControlPanel;

@Version
@RegisterControlPanel("jiopi.Mail")
public interface Mail {
	
	public final int TEXT_MAIL = 1;
	public final int HTML_MAIL = 2;
	
	public void addTo(String to);
	
	public void addTo(String email,String name);
	
	public void setFrom(String from);
	
	public void setFrom(String email,String name);
	
	public void setSubject(String subject);
	
	public void setTextMsg(String text);
	
	public void setHtmlMsg(String html);
	
	public void changeMailType(int mailType);
	
	public String embed(URL url);
	
	public String embed(File file);
	
	public void attach(MailAttachment attachment);
	
	public String send() throws SendMailException;
	
}

 

MailAttachment.java

package org.jiopi.blueprint.mailsender;

import java.io.File;
import java.net.URL;

@Version
public final class MailAttachment implements java.io.Serializable{
	
	private static final long serialVersionUID = 3366988524797658258L;
	
	public final File file;
	public final URL url;
	
	private String name;
	private String description;
	
	public MailAttachment(File file){
		this.file =  file;
		this.url = null;
	}
	
	public MailAttachment(URL url){
		this.url = url;
		this.file = null;
	}

	public String getName() {
		return name;
	}

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

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
}

 

以上是主要的三个类,还有一个Version.java定义版本信息,SendMailException.java定义了一个异常

 

如果只想发送简单邮件,可直接调用MailSender的sendMail方法,如果想发送带附件的邮件,则需要使用Mail对象,Mail对象可以从MailSender中生成,而在JIOPi v0.4中,则也可以直接new出来。并且可以将本不是静态方法的sendMail方法转换为静态方法,实现如下代码的简单调用:

MyMailSender.sendMyMail("姓名<to@yourmail.com>", "hello", "content test");

 

下面我们就来一步一步实现基于JIOPi模块化编程的简单邮件发送API的调用。
首先需要知道 jiopi.module.mailsender 模块实现了该邮件发送蓝图。这是一个基于Apache Commons Email和JavaMail的实现,因此邮件发送不是异步的,如果你希望实现异步发送,则可以重新做一个模块来实现,而对于使用代码来说,只需要修改一下模块名即可。
为了不引入额外API,则需要生成自己的实现类,无需填写任何实现代码,因为会被重定义,代码如下:

 

MyMailSender.java

package org.jiopi.ibean.show.mailsender;

import org.jiopi.blueprint.mailsender.Mail;
import org.jiopi.blueprint.mailsender.MailSender;
import org.jiopi.blueprint.mailsender.SendMailException;

public class MyMailSender implements MailSender {
	
	private static MailSender instance = new MyMailSender();

	public Mail createNewHtmlMail() {
		// TODO Auto-generated method stub
		return null;
	}

	public Mail createNewTextMail() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getAuthenticationPassword() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getAuthenticationUserName() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getCharset() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getFrom() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getHostName() {
		// TODO Auto-generated method stub
		return null;
	}

	public String sendMail(String arg0, String arg1, String arg2)
			throws SendMailException {
		// TODO Auto-generated method stub
		return null;
	}

	public String sendMail(String arg0, String arg1, String arg2, String arg3)
			throws SendMailException {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static Mail createMyNewHtmlMail() {
		return instance.createNewHtmlMail();
	}
	
	public static Mail createMyNewTextMail() {
		return instance.createNewTextMail();
	}
	
	public static String sendMyMail(String arg0, String arg1, String arg2)
			throws SendMailException {
		return instance.sendMail(arg0, arg1, arg2);
	}

	public static String sendMyMail(String arg0, String arg1, String arg2,
			String arg3) throws SendMailException {
		return instance.sendMail(arg0, arg1, arg2, arg3);
	}

}

 

MyMail.java

package org.jiopi.ibean.show.mailsender;

import java.io.File;
import java.net.URL;

import org.jiopi.blueprint.mailsender.Mail;
import org.jiopi.blueprint.mailsender.MailAttachment;
import org.jiopi.blueprint.mailsender.SendMailException;

public class MyMail implements Mail {

	public void addTo(String arg0) {
		// TODO Auto-generated method stub

	}

	public void addTo(String arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	public void attach(MailAttachment arg0) {
		// TODO Auto-generated method stub

	}

	public void changeMailType(int arg0) {
		// TODO Auto-generated method stub

	}

	public String embed(URL arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public String embed(File arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public String send() throws SendMailException {
		// TODO Auto-generated method stub
		return null;
	}

	public void setFrom(String arg0) {
		// TODO Auto-generated method stub

	}

	public void setFrom(String arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	public void setHtmlMsg(String arg0) {
		// TODO Auto-generated method stub

	}

	public void setSubject(String arg0) {
		// TODO Auto-generated method stub

	}

	public void setTextMsg(String arg0) {
		// TODO Auto-generated method stub

	}

}

 

 

其中MyMailSender中,又额外添加了几个静态方法,这是为了可以更加方便调用。

 

下面需要配置 jiopi.module.mailsender 模块,配置发送邮件所需的设置,以及类重定义设置:
jiopi-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://www.jiopi.org/2010/JIOPISchema" xmlns:jiopi="http://www.jiopi.org/2010/JIOPISchema-configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jiopi.org/2010/JIOPISchema configuration/jiopi-config-configuration.xsd ">
    <module name="jiopi.module.mailsender" version="0.1">
        <controlpanel name="jiopi.MailSender" id="mymail">
            <properties>
                <property name="hostname"><value>smtp.yourmail.com</value></property>
                <property name="username"><value>yourname@yourmail.com</value></property>
                <property name="password"><value>yourpassword</value></property>
                <property name="charset"><value>UTF-8</value></property>
                <property name="from"><value>姓名 &lt;yourname@yourmail.com&gt;</value></property>
            </properties>
        </controlpanel>
        <controlpanel name="jiopi.Mail" id="mymail">
            <inner-accessory id="mailSender" module="this" configuration="mymail"/>
        </controlpanel>
    </module>

    
    <redefine class="org.jiopi.ibean.show.mailsender.MyMailSender" module="jiopi.module.mailsender" version="0.1" controlpanel="jiopi.MailSender" configuration="mymail"/>
    <redefine class="org.jiopi.ibean.show.mailsender.MyMail" module="jiopi.module.mailsender" version="0.1" controlpanel="jiopi.Mail" configuration="mymail"/>
    
</configuration>

 

控制面板配置中,使用了指定配置ID,通过配置不同的ID,你可以创建多个实现类,以使用不同的邮件配置。

redefine标签则定义了将用哪个模块的哪个控制面板来重定义指定类,从而避免使用额外的API来获取实现模块,通过类重定义,你可以直接new出来而不是通过额外的API来get。
完成这些配置,便可以这样调用了:

 

MyMailSender.sendMyMail("姓名<to@yourmail.com>", "hello", "content test");
Mail myMail = new MyMail();
		myMail.addTo("to@yourname.com","姓名");
		myMail.setSubject("测试邮件");
		myMail.setTextMsg("测试内容");
		
		MailAttachment attachment = new MailAttachment(new URL("http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"));

		myMail.attach(attachment);
		myMail.send();

 

当然,你需要先调用一下JIOPi的初始化函数

FrameworkInitializer.initialize();

 

纯POJO+非运行时Annotation(蓝图和模块中)+XML装配文件=Java模块化系统

 

这便是JIOPi v0.4带来的Java模块化编程风格。

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics