MessageFactory.java 884 Bytes
package com.bsth.socket.protocol;

import com.bsth.util.ConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Hill
 */
public class MessageFactory {
	
	private final static Logger log = LoggerFactory.getLogger(MessageFactory.class);

	public static IMessageBody create(byte commandType, byte[] data) {
		String pkgName = MessageFactory.class.getPackage().getName();
		IMessageBody body = null;
		try {
			boolean cache = false;
			if (commandType == 0x03) {
				commandType = 0x02;
				cache = true;
			}
			Class<?> cls = Class.forName(pkgName + ".Message" + String.format("%02x", commandType));
			body = (IMessageBody)cls.newInstance();
			body.read(data);
			if (cache) {
				((Message02) body).setCache(cache);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("MessageFactory create", e);
		}

		return body;
	}
}