以前写的一个画流程图的Serlvet,发出来和大家共享
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.xpath.DefaultXPath;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springmodules.workflow.jbpm31.JbpmCallback;
import org.springmodules.workflow.jbpm31.JbpmTemplate;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class JbpmImageServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
//
private WebApplicationContext wac = null;
//
private Token currentToken = null;
private List<Token> childrens = null;
private String message = "无流程图型信息";
// 图像参数
final static Color RunningColor = new Color(0,0,255);
final static Color RunningBackColor = new Color(153,153,255);//#9999FF
final static Color SuspenedColor = new Color(170,102,0); //#AA6600
final static Color SuspenedBackColor = new Color(255,204,153);
final static Color LockedColor = new Color(170,102,0); //#AA6600
final static Color LockedBackColor = new Color(255,204,153);//#FFAA99
final static Color EndedColor = new Color(204,0,0); //#CC0000
final static Color EndedBackColor = new Color(102,0,0);//#66000
final static int BorderWidth = 1;
final static int BoderBackWidth = 2;
final static int FontSize = 10;
final static int TitleHeight = 12;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(wac == null) {
System.out.println("初始化Spring上下文");
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
}
// 设置该类型,可以使浏览器识别出它是一张图片
response.setContentType("image/jpeg");
// 显示token状态图
String tokenId = request.getParameter("tokenId");
if(tokenId != null) {
childrens = new ArrayList<Token>();
showTokenImage(Long.parseLong(tokenId), response.getOutputStream());
return;
}
// 显示task实例状态图
String taskId = request.getParameter("taskId");
if(taskId != null) {
showTaskImage(Long.parseLong(taskId), response.getOutputStream());
return;
}
// 显示流程定义图
String processDefinitionId = request.getParameter("definitionId");
if(processDefinitionId != null) {
showProcessDefinitionImage(Long.parseLong(processDefinitionId), response.getOutputStream());
return;
}
}
/**
* 输出token状态图
*
* @param id tokenid
* @param out 输出流
* @throws ImageFormatException
* @throws IOException
*/
public void showTokenImage(long id, OutputStream out) throws ImageFormatException, IOException {
byte [] imageBytes = getProcessDefinitionImageBytesByTokenId(id);
// 流程定义图(背景)
BufferedImage bi = getProcessDefinitionImage(imageBytes);
if(imageBytes != null) {
byte [] gpdBytes = getGpdBytesByTokenId(id);
try {
Element rootDiagramElement = DocumentHelper.parseText(new String(gpdBytes)).getRootElement();
// 当前token所在座标
int[] boxConstraint = extractBoxConstraint(rootDiagramElement, currentToken);
int x = boxConstraint[0] - BorderWidth + 1;
int y = boxConstraint[1] - TitleHeight - BorderWidth + 1;
int w = boxConstraint[2] - 1;
int h = boxConstraint[3] + TitleHeight - 1;
String title = "";
Font font = new Font("verdana,sans-serif", Font.PLAIN, FontSize);
Stroke stroke = new BasicStroke(BorderWidth);
Color color = null;
Color bColor = null;
if(currentToken.isSuspended()) {
title = "Suspended";
color = SuspenedColor;
bColor = SuspenedBackColor;
} else if(currentToken.isLocked()) {
title = "Locked";
color = LockedColor;
bColor = LockedBackColor;
} else if(currentToken.hasEnded()) {
title = "Ended";
color = EndedColor;
bColor = EndedBackColor;
} else {
title = "Running";
color = RunningColor;
bColor = RunningBackColor;
}
if(currentToken.getName() != null) {
title += " \"" + currentToken.getName() + "\"";
}
Graphics2D g = bi.createGraphics();
g.setFont(font);
g.setColor(color);
g.setStroke(stroke);
// 画边框
g.drawRect(x, y, w, h);
// 画Title
g.fillRect(x, y, w, TitleHeight + BorderWidth);
g.setColor(bColor);
// 画竖阴影
g.fillRect(x + w + BorderWidth - BorderWidth/2, y + BoderBackWidth, BoderBackWidth, h + BorderWidth - BorderWidth/2);
// 画横阴影
g.fillRect(x + BoderBackWidth, y + h + BorderWidth - BorderWidth/2, w + BorderWidth - BorderWidth/2, BoderBackWidth);
g.setColor(Color.WHITE);
// 字符串要离开边框3个宽度.下同
g.drawString(title, x + BorderWidth + 3, y + TitleHeight/2 + FontSize/2);
for(Token token : childrens) {
// 子token所在座标
boxConstraint = extractBoxConstraint(rootDiagramElement, token);
x = boxConstraint[0] - BorderWidth + 1;
y = boxConstraint[1] - TitleHeight - BorderWidth + 1;
w = boxConstraint[2] - 1;
h = boxConstraint[3] + TitleHeight - 1;
if(token.isSuspended()) {
title = "Suspended";
color = SuspenedColor;
bColor = SuspenedBackColor;
} else if(token.isLocked()) {
title = "Locked";
color = LockedColor;
bColor = LockedBackColor;
} else if(token.hasEnded()) {
title = "Ended";
color = EndedColor;
bColor = EndedBackColor;
} else {
title = "Running";
color = RunningColor;
bColor = RunningBackColor;
}
g.setColor(color);
// 画边框
g.drawRect(x, y, w, h);
// 画Title
g.fillRect(x, y, w, TitleHeight + BorderWidth);
g.setColor(bColor);
// 画竖阴影
g.fillRect(x + w + BorderWidth - BorderWidth/2, y + BoderBackWidth, BoderBackWidth, h + BorderWidth - BorderWidth/2);
// 画横阴影
g.fillRect(x + BoderBackWidth, y + h + BorderWidth - BorderWidth/2, w + BorderWidth - BorderWidth/2, BoderBackWidth);
g.setColor(Color.WHITE);
if(token.getName() != null) {
title += " \"" + token.getName() + "\"";
}
g.drawString(title, x + BorderWidth + 3, y + TitleHeight/2 + FontSize/2);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
}
/**
* 输出task状态图
*
* @param id taskid
* @param out 输出流
* @throws ImageFormatException
* @throws IOException
*/
public void showTaskImage(long id, OutputStream out) throws ImageFormatException, IOException {
byte [] imageBytes = getProcessDefinitionImageBytesByTaskId(id);
BufferedImage bi = getProcessDefinitionImage(imageBytes);
if(imageBytes != null) {
byte [] gpdBytes = getGpdBytesByTaskId(id);
try {
Element rootDiagramElement = DocumentHelper.parseText(new String(gpdBytes)).getRootElement();
int[] boxConstraint = extractBoxConstraint(rootDiagramElement, currentToken);
Graphics2D g = bi.createGraphics();
g.setColor(RunningColor);
Stroke stroke = new BasicStroke(BorderWidth);
g.setStroke(stroke);
g.drawRect(boxConstraint[0]-BorderWidth, boxConstraint[1]-BorderWidth, boxConstraint[2]+BorderWidth*2, boxConstraint[3]+BorderWidth*2);
} catch (DocumentException e) {
e.printStackTrace();
}
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
}
/**
* 输出流程定义图
*
* @param id processdefinitionid
* @param out 输出流
* @throws ImageFormatException
* @throws IOException
*/
public void showProcessDefinitionImage(long id, OutputStream out) throws ImageFormatException, IOException {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(getProcessDefinitionImage(getProcessDefinitionImageBytes(id)));
out.flush();
}
/**
* 生成流程定义图
*
* @param bytes 流程定义图字节码
* @return
*/
private BufferedImage getProcessDefinitionImage(final byte[] bytes) {
// 图型缓存
BufferedImage bi = null;
// 原始图型
Image img = null;
// MediaTracker 类是一个跟踪多种媒体对象状态的实用工具类。
// 媒体对象可以包括音频剪辑和图像,但目前仅支持图像。
MediaTracker tracker = null;
// 图像默认宽度
int w = 400;
// 图像默认高度
int h = 400;
if(bytes == null) {
bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
} else {
img = Toolkit.getDefaultToolkit().createImage(bytes);
// 接收Image信息通知的异步更新接口
Container p = new Container();
tracker = new MediaTracker(p);
tracker.addImage(img, 0);
try {
// 至关重要:没有它,图片的高宽无法得到
// 它将等待图像信息加载完毕
tracker.waitForAll();
// tracker.waitForID(0);
w = img.getWidth(p);
h = img.getHeight(p);
} catch (InterruptedException e) {
e.printStackTrace();
message = e.getLocalizedMessage();
} catch (Exception e) {
e.printStackTrace();
message = e.getLocalizedMessage();
}
bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
}
Graphics2D g = bi.createGraphics();
// 如果不设背景色,默认为黑色
g.setBackground(Color.WHITE);
// 留1个宽度的边
g.clearRect(1, 1, w-2, h-2);
if(tracker != null && MediaTracker.COMPLETE == tracker.statusAll(false)) {
g.drawImage(img, 0, 0, null);
} else {
g.setColor(Color.BLACK);
g.setFont(new Font("黑体", Font.PLAIN, 20));
// 用于获取字符宽度
FontMetrics fm = g.getFontMetrics();
g.drawString(message, w/2 - fm.stringWidth(message)/2, h/2);
}
//g.dispose();
return bi;
}
/**
* 根据taskid获得流程定义图字节码
*
* @param id taskid
* @return
*/
private byte[] getProcessDefinitionImageBytesByTaskId(final long id) {
JbpmTemplate jbpmTemplate = (JbpmTemplate) wac.getBean("jbpmTemplate");
return (byte[])jbpmTemplate.execute(new JbpmCallback() {
public Object doInJbpm(JbpmContext context) {
try {
TaskInstance taskInstance = context.getTaskMgmtSession().loadTaskInstance(id);
currentToken = taskInstance.getToken();
// 这里currentToken有可能为null
ProcessDefinition pd = currentToken.getProcessInstance().getProcessDefinition();
if(pd.getFileDefinition() == null) {
message = "没有流程图";
return null;
} else {
return pd.getFileDefinition().getBytes("processimage.jpg");
}
} catch(Exception e) {
e.printStackTrace();
message = "No TaskInstance:" + id;
return null;
}
}
});
}
/**
* 根据tokenid获得流程定义图字节码
*
* @param id tokenid
* @return
*/
private byte[] getProcessDefinitionImageBytesByTokenId(final long id) {
JbpmTemplate jbpmTemplate = (JbpmTemplate) wac.getBean("jbpmTemplate");
return (byte[])jbpmTemplate.execute(new JbpmCallback() {
public Object doInJbpm(JbpmContext context) {
try {
currentToken = context.loadToken(id);
Collection childTokens = currentToken.getChildren().values();
for (Iterator iterator = childTokens.iterator(); iterator.hasNext();) {
Token child = (Token) iterator.next();
childrens.add(child);
}
// 这里token可能没有processInstanceId 即getProcessInstance为null.
ProcessDefinition pd = currentToken.getProcessInstance().getProcessDefinition();
if(pd.getFileDefinition() == null) {
message = "没有流程图";
return null;
} else {
return pd.getFileDefinition().getBytes("processimage.jpg");
}
} catch(Exception e) {
e.printStackTrace();
message = "No Token:" + id;
return null;
}
}
});
}
/**
* 根据processdefinitionid获得流程定义图字节码
*
* @param id processdefinitionid
* @return
*/
private byte[] getProcessDefinitionImageBytes(final long id) {
JbpmTemplate jbpmTemplate = (JbpmTemplate) wac.getBean("jbpmTemplate");
return (byte[])jbpmTemplate.execute(new JbpmCallback() {
public Object doInJbpm(JbpmContext context) {
try {
ProcessDefinition pd = context.getGraphSession().loadProcessDefinition(id);
if(pd.getFileDefinition() == null) {
message = "没有流程图";
return null;
} else {
return pd.getFileDefinition().getBytes("processimage.jpg");
}
} catch(Exception e) {
e.printStackTrace();
message = "No ProcessDefinition:" + id;
return null;
}
}
});
}
private byte[] getGpdBytesByTaskId(final long id) {
JbpmTemplate jbpmTemplate = (JbpmTemplate) wac.getBean("jbpmTemplate");
return (byte[])jbpmTemplate.execute(new JbpmCallback() {
public Object doInJbpm(JbpmContext context) {
try {
TaskInstance taskInstance = context.getTaskMgmtSession().loadTaskInstance(id);
currentToken = taskInstance.getToken();
ProcessDefinition pd = currentToken.getProcessInstance().getProcessDefinition();
if(pd.getFileDefinition() == null) {
message = "没有流程图";
return null;
} else {
return pd.getFileDefinition().getBytes("gpd.xml");
}
} catch(Exception e) {
e.printStackTrace();
message = "No TaskInstance:" + id;
return null;
}
}
});
}
private byte[] getGpdBytesByTokenId(final long id) {
JbpmTemplate jbpmTemplate = (JbpmTemplate) wac.getBean("jbpmTemplate");
return (byte[])jbpmTemplate.execute(new JbpmCallback() {
public Object doInJbpm(JbpmContext context) {
try {
currentToken = context.loadToken(id);
ProcessDefinition pd = currentToken.getProcessInstance().getProcessDefinition();
if(pd.getFileDefinition() == null) {
message = "没有流程图";
return null;
} else {
return pd.getFileDefinition().getBytes("gpd.xml");
}
} catch(Exception e) {
e.printStackTrace();
message = "No Token:" + id;
return null;
}
}
});
}
/**
* 提取座标及宽高
*
* @param root 根节点
* @param token
* @return
*/
private int[] extractBoxConstraint(Element root, Token token) {
int[] result = new int[4];
String nodeName = token.getNode().getName();
XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
Element node = (Element) xPath.selectSingleNode(root);
result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
result[2] = Integer.valueOf(node.attribute("width").getValue())
.intValue();
result[3] = Integer.valueOf(node.attribute("height").getValue())
.intValue();
return result;
}
}