国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

發現 Eclipse 中未解析的插件依賴性

2024-07-21 02:14:42
字體:
來源:轉載
供稿:網友
  試圖定位無法解析的插件依賴性是件緊張而又耗時的事情。激活每個插件都要依賴于很多其他插件,這些插件又會依賴于其他更多插件。如果 eclipse 無法加載這個長長的鏈條中的某個插件,那么手工查找出現問題的到底是哪個插件可能會比原計劃所花費的時間和精力都要多。如果您希望有一種方法可以自動實現這種插件依賴性的檢測,就請閱讀本文。

  碰到的問題

  假設我們希望在 eclipse 中使用一個特定的插件,并已經執行了所有必須的操作,將其包含到插件的 manifest 文件中,并將其聲明為一個依賴文件。但是系統并沒有加載這個插件,這樣我們就會被困在這里了,軟件開發就無法繼續進展下去了。

  聽起來非常熟悉嗎?如果是這樣,那么您可能早已花費了很多時間和努力來查看很多 plugin.xml 文件,從而查明 eclipse 可能沒有加載哪個特定的插件。還可能已經嘗試使用了 eclipse pde 項目提供的 plug-in dependencies 視圖,此時您會發現這個視圖的唯一工作不過是顯示已經成功加載的插件而已。不幸的是,有問題的插件很可能并不屬于成功加載的插件。

  要確定 eclipse 沒有找到或加載哪個特定的插件,我們應該做些什么呢?我們不用手工遍歷每個 plugin.xml 文件,而是考慮自動實現這種搜索功能。要自動進行搜索,我們需要了解 eclipse 是如何保存自己的插件的,以及如何發現到保存在磁盤上的其他插件的鏈接。基于這些知識,我們可能會希望編寫自己的代碼來創建一個插件依賴性遍歷程序,或者使用在本文中給出的這個通用的 dependency walker 插件。本文的 “下載” 一節給出了這個例子的源代碼。

  開始:理解插件依賴性和 eclipse 的插件鏈

  插件依賴性

  eclipse 插件是提供了其他插件可以使用的功能的軟件模塊。如果插件 a 需要插件 b 才能正常工作,那么我們就說 a 依賴于 b。這種依賴性還意味著,除非插件 b 已經成功加載了,否則插件 a 就不能正常工作。有時候,插件 b 可能還會依賴于插件 c、d、e,令人更不爽的是,這些插件每個都可能會依賴于其他插件。這種依賴鏈很容易形成數百個插件相互鏈接在一起。毫無疑問,如果這個鏈條中的任何一個插件不能成功加載,那么依賴它的插件就可能會出現問題。

  插件 manifest 文件 plugin.xml 描述了每個插件。這個 xml 文件中有一節聲明了對于其他插件的依賴性或需求。在清單 1 中,plugin.xml 文件中使用黑體表示的一節就聲明了這種依賴性。

  清單 1. plugin.xml 文件
 <?xml version="1.0" encoding="utf-8" ?> <?eclipse version="3.0"?> <plugin id="org.eclipse.draw2d" name="draw2d" version="3.0.0"  provider-name="eclipse.org"> <runtime>  <library name="draw2d.jar">   <export name="*" />   <packages prefixes="org.eclipse.draw2d" />  </library> </runtime> <requires>  <import plugin="org.eclipse.swt" export="true" />  <import plugin="org.eclipse.core.runtime" /> </requires> </plugin> 

  注意嵌入在 <requires> </requires> 節中的 <import plugin="plugin id"/> 聲明。清單 1 的例子說明這個插件 id org.eclipse.draw2d 依賴于 id 為 org.eclipse.swt 和 org.eclipse.core.runtime 的插件。

  插件鏈

  當我們在 eclipse 中使用 java? 技術平臺來開發軟件時,系統實際上根據所選擇的目標平臺對源代碼進行編譯。可以在 window > preferences > plug-in development > target platform 中指定目標平臺的位置。這個目標平臺在 <targetplatform>/eclipse 中有自己的一個 eclipse 副本。要為代碼解析這些依賴性,請從兩個地方查找是否存在所需要的插件:
  1. <targetplatform>/eclipse/plugins 文件夾中的 eclipse 插件
  2. <targetplatform>/eclipse/links 文件夾中 .link 文件所指向的鏈接插件

  程序員通常會將第二個地方稱為 links 文件夾。這個 links 文件夾中包含 0 個或多個文件,文件名通常都是以 “.link” 擴展名結尾。這些文件中包含了一些鏈接信息,可以使用這些信息定位在磁盤上哪些地方可以找到鏈接插件。

  每個 .link 文件都有一個關鍵字-值對,其格式為 path=location。(例如,links 文件夾 c:/eclipse/links 中就可能會有很多 .link 文件,其中一個文件的名字可能為 com.ibm.indiver.dependencywalker.link。這個文件中唯一的一行可能類似于 path=c:/myplugins/dependencywalker)。這個 .link 文件會將 eclipse 引導到指定的位置,并在 /eclipse/plugins 文件夾中尋找更多的可用插件。

  創建自己的 eclipse 插件依賴性遍歷程序

  編寫一個依賴性遍歷程序基本上分為兩個步驟:首先羅列出所有插件,其次羅列出用戶所選擇的插件的依賴性。

  第一個步驟要負責定位 eclipse 系統中出現的每個插件,并在一個簡單的用戶界面(ui)—— 例如表 —— 中為終端用戶提供所有插件的清單。這個 ui 還應該為用戶提供一些方法來選擇希望解析其依賴性的插件。

  第二個步驟則要對用戶選擇的插件的 plugin.xml 文件進行分析,并查找這個 plugin.xml 文件中嵌入的 <import plugin="plugin id"/> 聲明。這種努力顯然需要對每個插件的 manifest 文件進行遞歸搜索,從而查明依賴插件的整個鏈條。對于描述這個插件可能依賴于其他插件的父-兄-子關系,樹狀視圖是最合適的一種 ui。我們還應該可以直觀地看出某個 eclipse 插件注冊項是否真正加載了一個物理存在的插件。

  步驟 1:羅列 eclipse 系統中的所有插件

  在掌握了以下信息之后,就可以編寫一些代碼來羅列磁盤上物理存在的所有插件了:

  1. 插件主要在 <targetplatform>/eclipse/plugins 文件夾中。
  2. 在其他幾個 <somelinkedpath>/eclipse/plugins 文件夾中也可能會找到插件。
  3. 從 <targetplatform>/eclipse/links 文件夾中的 .link 文件中可以獲得到每個 <somelinkedpath> 的路徑。

  下面是羅列 eclipse 系統中所有插件的詳細步驟:

  1. 找到目標平臺的位置。
  2. 準備 links 文件夾的路徑。links 文件夾在 /eclipse 文件夾中。
  3. 獲得 /eclipse/links 文件夾中文件的清單。請參考源代碼中的 utilities.getlinkedpaths() 函數。
  4. 查看每個 .link 文件,獲取鏈接 eclipse 插件的路徑。
  5. 準備一個所有插件根文件夾的清單(即,<targetplatform>/eclipse/plugins 文件夾和所有可能的 <somelinkedpath>/eclipse/plugins 文件夾)。
  6. 對于每個根文件夾,進入每個插件目錄中,并獲取 plugin.xml 文件的路徑。
  7. 對 plugin.xml 文件進行分析,獲得插件 id 和插件版本,并將這些信息保存到一個數據結構中。
  8. 回到步驟 6,繼續處理下一個插件目錄。

  清單 2. 準備在 eclipse 系統下物理存在的所有插件的清單

 /**  *    * @return returns a vector containing plugindata objects.  * each plugindata object represents a plugin found under any of the following   * plugin directories  * a. the targetplatformlocation/eclipse/plugins directory,   * b. other plugin directories as specified by *.link files under   *    targetplatform/eclipse/links directory   **/  public static vector getpluginsintargetplatform(){  /**  //step1: get path of target platform.  //step2: prepare path of links folder.  //step3: get list of files in links folder.  //step4: parse each link file and get the path of linked eclipse folder.  //step5: prepare a list of all plugin root folders   //       (eclipse plugins and linked eclipse plugins).  //step6: 6a. for each plugin root folder,   //       6b. go to each plugin directory and get path of plugin.xml.  //step7: parse the plugin.xml file to get plugin id, plugin version,   //       and store in vectors, lists, etc.  //step8: go back to step 6 to continue with next plugin directory.  **/  //step1: get path of target platform.   //fall back to eclipse install location if targetplatform in not set.  url platformurl = platform.getinstalllocation().geturl();  location location = platform.getinstalllocation();  ipath eclipsepath = null ;    //get path of target platform against which the users of this tool   //will compile their code.   ipath targetplatformlocation = new path(gettargetplatformpath(true));  if(_usetargetplatform == false)   eclipsepath = new path(platformurl.getpath());  else   eclipsepath = targetplatformlocation;    showmessage("considering target platform to be: " +      eclipsepath.tostring());    //step2: prepare path of links folder.  //step3: get list of files in links folder.    //step4: parse each link file and get the path of linked eclipse folder.  ipath linkspath = new path( eclipsepath.tostring() ).append("/links");  string linkedpaths[] = getlinkedpaths(linkspath.tostring());  int linkedpathlength = 0;  if(null != linkedpaths){   linkedpathlength = linkedpaths.length;  }    //step5: prepare a list of all plugin root folders   //       (eclipse plugins and linked eclipse plugins).  ipath eclipsepluginrootfolders[] = new ipath[linkedpathlength + 1];  eclipsepluginrootfolders[0] =    new path( eclipsepath.tostring() ).append("/plugins");  if(null != linkedpaths){   for(int i=0; i<linkedpaths.length; i++){    eclipsepluginrootfolders[i+1] =     new path(linkedpaths[i]).append("/eclipse/plugins");   }  }  //step6: 6a. for each plugin root folder,   //       6b. go to each plugin directory and get path of plugin.xml.  //step7: parse the plugin.xml file to get plugin id, plugin version,   //       and store in vectors, lists, etc.  vector vectorsinthisvector = new vector();  for(int i=0; i<eclipsepluginrootfolders.length; i++){   system.out.println("/n========plugin ids and versions in " +    eclipsepluginrootfolders[i] + "========");   vector plugindataobjs  =    getplugindataforallplugins(    eclipsepluginrootfolders[i].tostring());   vectorsinthisvector.add(plugindataobjs);   system.out.println(plugindataobjs);   system.out.println("/n===========|||=== end ===|||===========");  }    vector plugindata = new vector();  iterator outeriterator = vectorsinthisvector.iterator();  while(outeriterator.hasnext()){   vector plugindataobjs = (vector)outeriterator.next();   iterator inneriterator = plugindataobjs.iterator();   while(inneriterator.hasnext()){    plugindata pd = (plugindata)inneriterator.next();    string pluginidkey = pd.getpluginid();    string versionvalue = pd.getpluginversion();    string pluginpath = pd.getpluginlocation();    plugindata.add(pd);   }  }    int breakpoint=0;  return plugindata; } 

  在掌握了所有的插件之后,我們就可以顯示插件的 id、版本、位置以及更多信息了,這些可以顯示在一個 standard widget toolkit(swt)表中,從而使這些信息更加直觀。我們也可以編寫一些代碼根據插件 id 列進行排序,就像是我們的樣例代碼一樣。還應該在一列中說明找到了多少個插件。結果應該如下所示:

  圖 1. target-platform 視圖中的所有插件


  步驟 2:對 plugin.xml 文件進行遞歸搜索,從而遍歷整個依賴鏈

  當用戶選擇希望解析依賴鏈的插件之后,我們就需要對用戶所選擇的插件的 plugin.xml 文件進行分析,從而查看它的依賴性。每個依賴性都會導致檢查另外一個 plugin.xml 文件,后者又有自己的依賴性。從用戶選擇的插件開始,這個依賴鏈可以迅速導致有很多個 plugin.xml 文件需要進行分析。我們可以編寫一個遞歸函數來遍歷這些依賴性,它可以查找某個插件的最新版本(針對在相同的系統中有重復插件的情況)以及它的所有依賴性。

  編寫這種遞歸函數需要執行的步驟如下所示,清單 3 給出了這個函數的源代碼。遞歸函數有時對資源的消耗量很大,而且在用戶失去耐心之前可能還沒有返回結果。另外一種選擇是編寫一個函數,只獲取用戶選擇的插件的直接依賴性清單。后一種方法請參看樣例代碼中的 loadimmediatedependencies() 函數。

  1. 獲得用戶選擇的插件的路徑。
  2. 檢查這個位置上是否存在 plugin.xml 或 fragment.xml 文件。
  3. 對 plugin.xml 或 fragment.xml 文件進行分析,從而獲得這個插件所需要的所有插件的清單。
  4. 對于這個清單中的每個插件 id,尋找對應的插件。
    1. 如果多個插件具有相同的 id,就只向用戶報告一次,并自動確定使用版本較新的插件。如何編程對插件版本進行比較并尋找一個版本較新的插件,請參看圖 4。
  5. 將(步驟 4 或 4a 中找到的)插件添加到一個樹視圖中,并遞歸地調用相同的函數,再次從步驟 1 開始重新執行。不過這次用戶不用再選擇插件了;步驟 4 或 4a 對應的代碼會負責選擇插件。

  清單 3. recursiveplugindependencywalker() 函數

 private vector alreadynotified = new vector(); private boolean firstcall = true; private treeparent root = null; private void recursiveplugindependencywalker(plugindata pdobject,       treeparent parentnode){  try {   string path = pdobject.getpluginlocation();   pluginparser pp = null;   file plugindotxmlfile = new file(path + "/plugin.xml");   if(plugindotxmlfile.exists()){    pp = new pluginparser(plugindotxmlfile);   }else{    file fragmentdotxmlfile = new file(path +      "/fragment.xml");    if(fragmentdotxmlfile.exists()){     pp = new pluginparser(fragmentdotxmlfile);    }else{     return;//no plugin.xml or fragment.xml found    }    }   string displayname = pdobject.getdisplayname();   system.out.println("/nplugin ["+ displayname + "]    requires" + "/n");   string requires[] = pp.getdependencylist();   if(0 != requires.length ){     for(int i=0; i<requires.length; i++){     system.out.println("/t" + requires[i] );     plugindata pd[] =      getplugindataobjectsfrompluginid(requires[i]);     plugindata nextplugin = null;     switch(pd.length){     case 0:      //great, we know there is       //something missing      nextplugin = null;      break;     case 1:      //best case, everything will be smooth      nextplugin = pd[0];      break;     default:      //worst case, there must be more       //than 1 plugin with the same id      //at different locations.      string msgline1 =       "plugin " + displayname +       " requires "  +       requires[i] + "/n";      string msgline2 =       "duplicate plug-ins found for id: /      " " + requires[i] +      "/"" +       "/n continuing with higher version...      " ;            //it is bad to give repeated        //reminders,        //so remind only once per plugin id.      if(! alreadynotified.contains(       new string(requires[i]))){      messagedialog.openinformation(null,      "dependency walker",           msgline1 +         msgline2);      alreadynotified.add(       new string(requires[i]));      }      //always take the better       //version anyway      nextplugin =        getbetterversionplugin(pd);      break;     }//end of switch     if( null != nextplugin ){      treeparent nextinline =       new treeparent(       nextplugin.getdisplayname(),       nextplugin.isplugin       loadedinregistry()       );      parentnode.addchild(nextinline);      recursiveplugindependencywalker(       nextplugin,        nextinline);     }else{      treeparent nextinline =       new treeparent(       requires[i] +        " [this plug-in is missing]       ",       false);      parentnode.addchild(nextinline);      //obviously we can't recurse       //into a missing plugin...     }    }//end of for   }else{    system.out.println("/t nothing:     no further dependency /n" );    //no further dependency   }  } catch (exception e) {   e.printstacktrace();  } }

  有時候,我們會碰到在不同位置存在具有相同 id 的插件的情況。例如,id 為 org.eclipse.xsd 的插件可能會在 <targetplatform>/eclipse/plugins 文件夾和 <somelinkedpath>/eclipse/plugins 文件夾中同時出現。

  在這種情況中,必須要確定從這兩個或更多個磁盤副本中選用哪個插件。顯然,我們所感興趣的應該是最新的插件,也就是說,版本較新的插件。我們可以利用現有的一些函數來對 eclipse 插件的版本進行比較,或者可以基于清單 4 所示的樣例代碼編寫一個簡單的函數來對插件版本進行比較。

  清單 4. 比較插件版本

 private plugindata getbetterversionplugin(plugindata pdo[]){  plugindata _pdobjs[] = pdo;  int len = pdo.length;  if(len==0)   return null;    arrays.sort(_pdobjs,new comparator() {   /**compares its two arguments for order.     * returns a negative integer, zero, or a positive integer     * as the first argument is less than, equal to, or greater than     * the second.     **/   public int compare(object leftobj, object riteobj) {        string leftpid = ((plugindata)leftobj).     getpluginversion().replace('.', ':');    string ritepid = ((plugindata)riteobj).     getpluginversion().replace('.', ':');    string leftid[] = leftpid.split(":");    string riteid[] = ritepid.split(":");    int maxlen = leftid.length > riteid.length ?       leftid.length : riteid.length;    for(int i=0; i<maxlen; i++){     int left = 0;     int rite = 0;     try {     left = new integer(leftid[i]).intvalue();     } catch (nullpointerexception e) { left = 0; }     try {     rite = new integer(riteid[i]).intvalue();     } catch (nullpointerexception e) { rite = 0; }          if(left==rite){      continue;     }else{     int bigger = left > rite ? left : rite;      if(bigger==left)       return 1;      if(bigger==rite)       return -1;     }         }    return 0;   }   public boolean equals(object arg0) {    return false;   }  });    return _pdobjs[len-1];   } 

  在代碼遍歷完整個鏈接依賴性鏈之后,我們就可以使用一個樹視圖來直觀地將其表示出來。還應該直觀地指出(請參看下圖中的紅圈)是哪一個插件導致了加載失敗。

  這個搜索的結果應該類似于下圖所示:

  圖 2. dependency walker tree view


  結束語

  如果我們希望定位一些無法解析的插件依賴性(缺少插件或 eclipse 由于某些原因未能加載它們),首先可以使用 eclipse pde plug-in dependencies 視圖來顯示插件的依賴性。如果 plug-in dependencies 視圖沒有顯示我們的插件,就可能希望使用本文中介紹這個工具對所有鏈接插件文件夾進行自動化搜索。如果您只對某個具體的插件感興趣,也可以對這段代碼進行修改來滿足您的要求。

  可以從下面的 “下載” 一節獲得這個工具的源代碼。要瀏覽源代碼,請展開源代碼包,并將這個插件作為一個 eclipse 項目打開。要使用這個工具,請將這個插件解壓到 /eclipse/plugins 文件夾中,并執行以下操作:

  1. 在 eclipse 中,切換到 window > show view > others > dependencywalker category 中,并選擇 all plugins in target-platform 視圖。
  2. 這個視圖會顯示在指定目標平臺中出現的所有插件。選擇一個插件并雙擊它。
  3. dependencywalkertreeview 會顯示您所選擇的插件的所有依賴性。完成之后,請關閉這個視圖。
中國最大的web開發資源網站及技術社區,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伽师县| 庄河市| 响水县| 阿拉尔市| 清原| 光泽县| 班玛县| 沙雅县| 宁夏| 浪卡子县| 晴隆县| 泸溪县| 咸丰县| 南昌市| 马山县| 景洪市| 湖州市| 娱乐| 卢氏县| 河南省| 金平| 嘉兴市| 股票| 佛教| 南木林县| 竹山县| 黑水县| 宜黄县| 瑞金市| 富民县| 乌什县| 神池县| 仁化县| 宝鸡市| 玉树县| 大渡口区| 肇庆市| 故城县| 改则县| 乐平市| 尼勒克县|