⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notifybean.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
/**
 * 
 * NotifyBean.java
 * 
 * Created on 2003-8-15 12:22:23
 * 
 */
package com.liuyang.jboss.sessionbean.notify.ejb;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.liuyang.jboss.sessionbean.product.ejb.Product;
import com.liuyang.jboss.sessionbean.product.ejb.ProductHome;

/**
 * @author
 * @ejb.bean	description="NotifyBean"
 *	          	display-name="NotifyBean"
 *  	        local-jndi-name = "sessionbean/notify"
 *      	    name="NotifyBean"
 *          	type="Stateless"
 *	    	    view-type="local"
 *  			transaction-type = "Container"
 */
public class NotifyBean implements SessionBean {

	private SessionContext ctx;
	public String SMTPHost = "";
	public String Username = "";
	public String Password = "";
	private ProductHome home = null;	
	/**
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public boolean sendNotify(InternetAddress address){
		String subject = "产品信息";
		String content = "";
		if(home == null){
			InitialContext initContext = null;
			try {
				initContext = new InitialContext();
				Object ref  = initContext.lookup("sessionbean/product");
				home = (ProductHome)ref;				
			} catch (NamingException e2) {
				e2.printStackTrace();
			}
		}	
		Collection products = null;
		try {
			products = home.findAll();
		} catch (FinderException e2) {
			e2.printStackTrace();
		}
		Element root = DocumentHelper.createElement("products");
		Document document = DocumentHelper.createDocument(root);
		if(products != null){
			if(!products.isEmpty()){
				Iterator it = products.iterator();
				while(it.hasNext()){
					Product product = (Product) it.next();
					String pname = product.getName();
					String pdescript = product.getDescript(); 
					Element pelement = DocumentHelper.createElement(pname);
					pelement.setText(pdescript);
					root.add(pelement);
				}
			}
		}else{
			return false;
		}
		content = document.asXML();
		if(SMTPHost.equalsIgnoreCase("")||Username.equalsIgnoreCase("")||Password.equalsIgnoreCase(""))
		return false;	
		boolean success = true;
		Properties props = System.getProperties();	
		props.put("mail.smtp.host",SMTPHost);	
		props.put("mail.smtp.auth", "true");
		PAAuthenticator pa = new PAAuthenticator(Username,Password);
		Session sess = Session.getInstance(props,pa);
		sess.setDebug(false);
		MimeMessage msg = new MimeMessage(sess);
		try {
			msg.setFrom(new InternetAddress(this.Username+"@"+this.SMTPHost));
			if(address == null)return false;
			msg.setRecipient(Message.RecipientType.TO,address);
			msg.setSubject(subject);
			msg.setText(content);
			Transport.send(msg);			
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		} 
		return success;	
	}
	/**
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public void setHost(String host){
		SMTPHost = host;
	}
	/**
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public void setUser(String user,String pass){
		Username = user;
		Password = pass; 
	}	
	/**
	 * @ejb.create-method 
	 */
	public void ejbCreate () {
	}	
	public void ejbActivate() throws EJBException, RemoteException {

	}
	public void ejbPassivate() throws EJBException, RemoteException {

	}
	public void ejbRemove() throws EJBException, RemoteException {

	}
	public void setSessionContext(SessionContext sc)
		throws EJBException, RemoteException {
		ctx = sc;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -