« 开发一个调试JSP的Eclipse插件3(收藏)« »开发一个调试JSP的Eclipse插件5(收藏) »
开发一个调试JSP的Eclipse插件4(收藏)
开发一个调试JSPEclipse插件1(收藏)    
开发一个调试JSPEclipse插件2(收藏) 
开发一个调试JSPEclipse插件3(收藏)
开发一个调试JSPEclipse插件4(收藏)
开发一个调试JSPEclipse插件5(收藏)
开发一个调试JSPEclipse插件6(收藏)
  本文通过开发一个JSP 编辑器插件的示例,介绍了 Eclipse 中设置 JSP 断点的方法,以及如何远程调试 JSP。作为基础知识,本文的前两部分描述了 JAVA Debug JSR-45 的基本原理。
  环境要求: 本文的代码是在 Eclipse3.0.0JDK1.4.2 Tomcat5.0.5 上测试过的
开发一个JSP编辑器
  Eclipse 提供了 TextEditor,作为文本编辑器的父类。由于 Editor 的开发不是本文的重点,不做具体论述。我们可以利用 Eclipse 的 Plugin 项目向导,生成一个简单的 JSP 编辑器:
  (1)点击 File 菜单,New -> Project -> Plug-in Project ;
  (2)输入项目名称 JSP_DEBUG,下一步;
  (3)输入 plugin ID : com.jsp.debug
    Plugin Class name : com.jsp.debug.JSP_DebugPlugin
  (4)选择用模板创建
  使用 Plug-in with editor,输入
  Java Package Name :com.jsp.editors
  Editor Class Name :JSPEditor
  File extension :jsp
  一个 jsp editor 就产生了。
  运行这个Plugin,新建一个JAVA项目,新建一个 Hello.jsp 和 greeting.jsp,在 Navigator 视图双击 jsp,这个editor就打开了。
  在JSP编辑器中设置断点
  在编辑器中添加断点的操作方式有两种,一种是在编辑器左侧垂直标尺上双击,另一种是在左侧垂直标尺上点击鼠标右键,选择菜单"添加/删除断点"。
  在 Eclipse 的实现中,添加断点实际上就是为 IFile 添加一个marker ,类型是IBreakpoint.BREAKPOINT_MARKER,然后将断点注册到 BreakpointManager。
  BreakpointManager 将产生一个 BreakpointRequest,通知正在运行的JVM Target,如果此时还没有启动 JVM,会在 JVM 启动的时候,将所有断点一起通知 JVM Target。
  添加断点使用一个 AbstractRulerActionDelegate,重载 createAction 方法,返回一个 IAction ManageBreakpointRulerAction动作:

public class ManageBreakpointRulerActionDelegate extends AbstractRulerActionDelegate{
 protected IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo) {
  return new ManageBreakpointRulerAction(rulerInfo, editor);
 }
}
  为了将 ManageBreakpointRulerActionDelegate 添加到文本编辑器左侧标尺的鼠标右键菜单,并且能够处理左侧标尺的鼠标双击事件,在 plugin.xml 中加入定义。
  处理双击事件:
<extension  point="org.eclipse.ui.editorActions">
 <editorContribution
      targetID="com.jiaoly.editors.JSPEditor"
      id="com.jiaoly.debug.ManageBreakpointRulerActionDelegate">
   <action
         label="添加/删除断点"
         class="com.jiaoly.debug.ManageBreakpointRulerActionDelegate"
         actionID="RulerDoubleClick"
         id="com.jiaoly.debug.ManageBreakpointRulerActionDelegate">
   </action>
 </editorContribution>
</extension>
添加右键菜单:

<extension point="org.eclipse.ui.popupMenus">
 <viewerContribution
     targetID="#TextRulerContext"
     id="com.jiaoly.debug.ManageBreakpointRulerActionDelegate">
     <action
       label="添加/删除断点"
       class="com.jiaoly.debug.ManageBreakpointRulerActionDelegate"
       menubarPath="addition"
       id="com.jiaoly.debug.ManageBreakpointRulerActionDelegate">
    </action>
 </viewerContribution>
</extension>
  ManageBreakpointRulerAction 是实际添加断点的Action,实现了 IUpdate 接口,这个Action的工作,就是判断当前选中行是否存在断点类型的 Marker,如果不存在创建一个,如果存在,将它删除。
public class ManageBreakpointRulerAction extends Action implements IUpdate{
     
     private IVerticalRulerInfo rulerInfo;
     private ITextEditor textEditor;
    
     private String BPmarkerType ;     //当点Marker的类型
     private List allMarkers;       //当前鼠标点击行所有的Marker
     private String addBP;   //Action 的显示名称
    
public ManageBreakpointRulerAction(IVerticalRulerInfo ruler, ITextEditor editor){
     this.rulerInfo = ruler;
     this.textEditor = editor;
     BPmarkerType = IBreakpoint.BREAKPOINT_MARKER;
     addBP = "添加/删除断点"; //$NON-NLS-1$
     setText(this.addBP);
}
    
public void update() {
  this.allMarkers = this.fetchBPMarkerList(); 
}
    
public void run(){
if(this.allMarkers.isEmpty())
   this.addMarker();
else
   this.removeMarkers(this.allMarkers);
}
}
  update 方法会在点击时首先调用,这时就可以收集当前选中行是否有marker了(调用fetchBPMarkerList方法),如果有,就保存在 变量allMarkers 中。由于ManageBreakpointRulerAction每一次都产生一个新的实例,因此不会产生冲突。
  下面是update的调用栈,可以看出,update方法是在鼠标点击事件中被调用的:
ManageBreakpointRulerAction.update() line: 55
ManageBreakpointRulerActionDelegate(AbstractRulerActionDelegate).update() line: 114
ManageBreakpointRulerActionDelegate(AbstractRulerActionDelegate).mouseDown(MouseEvent) line: 139
  updae被调用后,会执行 run 方法,就可以根据 allMarkers.isEmpty() 确定要删除还是添加 marker 了。
  添加断点的时候,首先利用 IVerticalRulerInfo,获取鼠标点击的行号,根据行号,从 Document 模型中取得该行的描述IRegion,得到开始字符位置和结束字符位置,创建一个 JSP 断点。
protected void addMarker() {
  IEditorInput editorInput= this.getTextEditor().getEditorInput();
  
  IDocument document= this.getDocument();
  //the line number of the last mouse button activity
  int rulerLine= this.getRulerInfo().getLineOfLastMouseButtonActivity();
  try{
   int lineNum = rulerLine + 1;
   if(lineNum > 0){
       //Returns a description of the specified line
    IRegion iregion = document.getLineInformation(lineNum - 1);
    int charStart = iregion.getOffset();
    int charEnd = (charStart + iregion.getLength()) - 1;
    JSPDebugUtility.createJspLineBreakpoint(this.getResource(), 
             lineNum, charStart, charEnd);
   }
  }catch(CoreException coreexception){
   coreexception.printStackTrace();
  }
  catch(BadLocationException badlocationexception){
   badlocationexception.printStackTrace();
  }  
}
  注册 JSP 断点为支持 JSR-45 规范,Eclipse 中提供了 JavaStratumLineBreakpoint。不过它目前是一个 internal 的实现,在以后的版本中不能保证不作修改。这里为了简单起见,直接从 JavaStratumLineBreakpoint 继承。
  public class JSPBreakpoint extends JavaStratumLineBreakpoint {
        public JSPBreakpoint(IResource resource, String stratum, String sourceName,
                String sourcePath, String classNamePattern, int lineNumber,
                int charStart, int charEnd, int hitCount, boolean register,
                Map attributes) throws DebugException {
            super(resource, stratum, sourceName, sourcePath, classNamePattern,
                    lineNumber, charStart, charEnd, hitCount, register, attributes);
        }
    }
  查看 JavaStratumLineBreakpoint 的源代码可以知道,创建 JavaStratumLineBreakpoint 的时候做了两件事情:
  (1) 创建断点类型的 marker, 并且设置了marker的属性resource.createMarker(markerType);
  (2) 将断点注册到断点管理器
  DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(this); 断点管理器负责产生一个 BreakpointRequest,通知正在运行的JVM Target 如果此时还没有启动 JVM,会在 JVM 启动的时候,将所有断点一起通知 JVM Target。
  下面是 JavaStratumLineBreakpoint 构造函数中的代码:
    IWorkspaceRunnable wr= new IWorkspaceRunnable() {
   public void run(IProgressMonitor monitor) throws CoreException {
    // create the marker
    setMarker(resource.createMarker(markerType));    
    // modify pattern
    String pattern = classNamePattern;
    if (pattern != null && pattern.length() == 0) {
     pattern = null;
    }
    // add attributes
    addLineBreakpointAttributes(attributes, getModelIdentifier(), true, 
          lineNumber, charStart, charEnd);
    addStratumPatternAndHitCount(attributes, stratum, sourceName, 
sourcePath, pattern, hitCount);
    // set attributes
    ensureMarker().setAttributes(attributes);
    
    register(register);
   }
  };
  run(null, wr);
    
    protected void register(boolean register) throws CoreException {
  if (register) {
   DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(this);
  } else {
   setRegistered(false);
  }
}
    移除断点的时候,根据 marker 找到相应的 IBreakpoint,从 BreakpointManager 中移除 BreakpointManager 会自动删除 marker,通知 JVM Target。

breakpointManager  = DebugPlugin.getDefault().getBreakpointManager();
IBreakpoint breakpoint = breakpointManager.getBreakpoint(IMarker);
breakpointManager.removeBreakpoint(breakpoint, true);
 


Tags: java基础  

原创文章如转载,请注明:转载自:飞扬部落编程仓库 : http://www.busfly.net/csdn/

本文链接地址:http://www.busfly.net/csdn/post/jsp_eclipse_debug_4.html

如果你喜欢本文,请顶一下,支持我,你的支持是我继续发好文章的最大动力。谢谢。
好东西需要分享,快把本文发给你的朋友吧~!~

     
相关文章:




◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
Feed订阅集
网站分类
勤劳致富^.^
最近发表
最新评论及回复
最近留言
随机推荐文章
Powered By Z-Blog   STYLE by busfly . FatMouse
Copyright © 2007 巴士飞扬技术博客. . 沪ICP备07027972号. 会员群1(VS为主):3769186.