MULE2.0升级(一)程序修改

MULE2.0升级(一)程序修改

MULE首页
http://mule.mulesource.org/display/MULE/Home

下载文件
mule-2.0.2.zip

解开压缩后配置:
MULE_HOME=D:\tool\mule-2.0.2
在PATH中加入bin

MULE1.4和MULE2.0.2的接口差别对照表

1.x 2.x
org.mule.umo.* org.mule.api.* in most cases
org.mule.providers.* org.mule.transport.*. For details on migrating 1.x transports to 2.x, click here.

org.mule.extras.* org.mule.module.NAME.*
package names are singular/plural package names are singular
MuleClient.receive(...) MuleClient.request(...)
MuleManager utility class MuleContext instance available through life cycle methods or MuleAwareContext implementation.
QuickConfigurationBuilder No longer supported
MuleXMLConfigurationBuilder DefaultMuleContextFactory
UMOMessage MuleMessage
MuleMessage DefaultMuleMessage
UMOEndpoint InboundEndpoint or OutboundEndpoint
UMOEndpointURI EndpointURI
MuleEndpoint.getOrCreateEndpointForUri(..., UMOEndpoint.ENDPOINT_TYPE_SENDER) new DefaultEndpointFactory().getOutboundEndpoint(...)
MuleEndpoint.getOrCreateEndpointForUri(..., UMOEndpoint.ENDPOINT_TYPE_RECEIVER) new DefaultEndpointFactory().getInboundEndpoint(...)
doPostFunctionalSetUp() suitePreSetUp() and doSetUp()


在MULE1.4的基础上程序修改如下
传递信息的BEAN没有做修改 MessageBean.java:
package com.megaeyes.sms.gateway.databean;

import java.io.Serializable;

public class MessageBean implements Serializable {
private static final long serialVersionUID = -1353347933934679190L;
private String topic;
private String msgSrcID;
private String msgTypeID;
private String deviceName;
private String deviceTypeName;
private String deviceIP;
private String msgTypeName;
private String msgID;
private String severityName;
private String faultTime;
private String content;
private String targetNumber;
}

将邮件的xml内容转化为MessageBean的转化类EmailMessageToMessageBean.java
mule的底层接口有变动,做了调整

package com.megaeyes.transforms;

import java.io.IOException;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

import com.megaeyes.sms.gateway.databean.MessageBean;
import com.megaeyes.utils.StringUtil;
import com.megaeyes.utils.XmlFileUtil;

/**
*
* @author sillycat.luo
*
*/
public class EmailMessageToMessageBean extends AbstractTransformer {

protected Log log = LogFactory.getLog(EmailMessageToMessageBean.class);

public EmailMessageToMessageBean() {
   registerSourceType(MimeBodyPart.class);
   registerSourceType(Message.class);
   registerSourceType(String.class);
   setReturnClass(MessageBean.class);
}

protected Object doTransform(Object src, String encoding)
    throws TransformerException {
   MimeBodyPart mail = null;
   MimeMessage mimeMes = null;
   String returnString = "";
   MessageBean message = null;
   if (src instanceof MimeBodyPart) {
    mail = (MimeBodyPart) src;
    try {
     Object o = mail.getContent();
     if (o instanceof String) {
      returnString = (String) o;
     } else if (o instanceof MimeMultipart) {
      MimeMultipart mime = (MimeMultipart) o;
      if (mime != null) {
       BodyPart body = mime.getBodyPart(0);
       if (body != null) {
        returnString = (String) body.getContent();
       }
      }
     } else {
      log.error("Unknow part from the MimebodyPart.getContent");
     }
    } catch (IOException e) {
     log.error(e);
    } catch (MessagingException e) {
     log.error(e);
    }
   } else if (src instanceof MimeMessage) {
    mimeMes = (MimeMessage) src;
    try {
     Object o = mimeMes.getContent();
     if (o instanceof String) {
      returnString = (String) o;
     } else {
      log.error("Unknow part from the mimeMessage.getContent");
     }
    } catch (IOException e) {
     e.printStackTrace();
    } catch (MessagingException e) {
     e.printStackTrace();
    }
   } else {
    log
      .error("src type is :" + src.toString()
        + " tranfor can't read!");
   }
   if (validateXml(returnString)) {
    // if validate pass
    // transform string 2 object
    message = transFromString2Message(returnString);
   } else {
    // xml format is wrong
    log.error("message is error,message is : " + returnString);
   }
   return message;
}

private boolean validateXml(String xml) {
   boolean flag = false;
   if (StringUtil.isBlank(xml)) {
    return flag;
   }
   if (xml.length() <= 6) {
    return flag;
   }
   if ("<root>".equalsIgnoreCase(xml.substring(0, 6))) {
    flag = true;
   }
   return flag;
}

/**
*
* @param src
*            String like <root><name>sillycat.luo</name></root>
* @return MessageBean Object
*/
private MessageBean transFromString2Message(String src) {
   MessageBean message = null;
   if (StringUtil.isBlank(src)) {
    log.error("src 2 transfer can't be null");
    return message;
   }
   Document doc = XmlFileUtil.getDocumentSaxByXmlStr(src);
   if (doc != null) {
    message = new MessageBean();
    message.setMsgSrcID(XmlFileUtil.getStringValue("MsgSrcID", doc));
    message.setMsgTypeID(XmlFileUtil.getStringValue("FaultType", doc));
    message
      .setDeviceName(XmlFileUtil
        .getStringValue("DeviceName", doc));
    message.setDeviceTypeName(XmlFileUtil.getStringValue("DeviceType",
      doc));
    message.setDeviceIP(XmlFileUtil.getStringValue("DeviceIp", doc));
    message.setMsgTypeName(XmlFileUtil.getStringValue("FaultTypeName",
      doc));
    message.setMsgID(XmlFileUtil.getStringValue("FaultID", doc));
    message
      .setSeverityName(XmlFileUtil
        .getStringValue("Severity", doc));
    message.setFaultTime(XmlFileUtil.getStringValue("FaultTime", doc));
    message.setTargetNumber(XmlFileUtil.getStringValue("TargetNumber",
      doc));
   } else {
    log.error("xml doc string dom4j parse error!");
   }
   return message;
}
}

解析XML工具类,没有做修改XmlFileUtil.java
package com.megaeyes.utils;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
* xml operate class using dom4j to parse xml document
*
* @author sillycat.luo
*
*/
public class XmlFileUtil {

protected static Log log = LogFactory.getLog(XmlFileUtil.class);

// document root element mark
public static final String ROOT_ELEMENT = "//root";

/**
* get xml document from String of xml type
*
* @param xmlStr
*            xml type String
* @return dom4j Document
*/
public static Document getDocumentSaxByXmlStr(String xmlStr) {
   Document doc = null;
   SAXReader reader = new SAXReader();
   if (null == xmlStr) {
    log.error("xmlString is null when parse xml document!");
    throw new IllegalArgumentException("xmlStr is null");
   } else {
    try {
     doc = reader.read(new ByteArrayInputStream(xmlStr
       .getBytes("UTF-8")));
    } catch (UnsupportedEncodingException e) {
     log.error(e);
    } catch (DocumentException e) {
     log.error(e);
    }
   }
   return doc;
}

/**
* get the element value by element label name
*
* @param title
*            :label name
* @param doc
*            :the xml source dom4j document
* @return :String value of the element
*/
public static String getStringValue(String title, Document doc) {
   String returnString = "";
   if (StringUtil.isBlank(title)) {
    log.warn("label name can't be null when get element name by label");
    return returnString;
   }
   Node node = doc.selectSingleNode(ROOT_ELEMENT + "/" + title);
   if (node != null) {
    returnString = node.getText();
   } else {
    log.warn("node can't find in the source, label name = " + title);
   }
   return returnString;
}
}

邮件转化为MessageBean的方法类EmailManagerImpl.java
package com.megaeyes.service.impl;

import org.mule.api.MuleEventContext;
import org.mule.component.simple.LogComponent;

import com.megaeyes.service.EmailManager;

/**
*
* @author sillycat.luo
*
*/
public class EmailManagerImpl extends LogComponent implements EmailManager {

public Object onCall(MuleEventContext context) throws Exception {
   Object o = context.transformMessage();
   return o;
}

}

你可能感兴趣的:(apache,bean,xml)