- 浏览: 87121 次
- 性别:
文章分类
最新评论
-
alexgreenbar:
function User(name) { this.n ...
javascript里的函数调用模式 -
alexgreenbar:


VM arguments to start runtime workbench -
robbin:
我也是第一次知道say命令,确实不错,哈哈
Macbookpro 初印象 -
alexgreenbar:
jnn 写道 欢迎成为Mac用户,记着下次喝咖啡的时候带上拉一 ...
Macbookpro 初印象 -
alexgreenbar:
plstryagain 写道欢迎交流mac使用心得。另外,大侠 ...
Macbookpro 初印象
Eclipse allow you to extend its functionalities by implementing its extension-point. We often write a Eclipse plugin which implement some of Eclipse's existing extension-points, e.g. if we want to contribute a popup menu for Eclipse, we need implement org.eclipse.ui.popupMenus extension-point, and follow the org.eclipse.ui.popupMenus's API contract which defined by org.eclipse.ui.popupMenus extension-point schema, then Eclipse will do things as we wish.
So what happened here? Why Eclipse know how to process your popup menu contribution? How to add a pretty new functionality to Eclipse, which can't find or defined by Eclipse's existing extension-point? The answer is: contribute a extension-point for Eclipse by yourself.
Here I will use a example to explain how to define a extension-point by yourself, suppose I want to write a view that will show services status which deploy in a container like tomcat, spring, websphere etc., and customer can add any unknown containers support by implement my new extension-point for this view.
1. The requirements
I want to get service from container, also I need support unknown container type, so a client will be needed, i.e. I can user that client to get what I want to show in my view. So here I will define a client extension-point and this client will follow the interface contract below:
The three methods at beginning will set connection information for client and create a client instance, then listServices() will get service back
2. Define client extension-point
You can use PDE extension-point schema editor do this, here's my client schema:
3. Extension-point handle classes
When my view need get services status back, I need load all contributed extension, and instance client which know how to get service status back, here's code:
4. A example client extension
5. Use client extension
In the view code:
6. Summary
So write a extension-point is not so hard? It's pretty easy actually! Could you imagine all the powerful functionalities of Eclipse are based on this extension mechanism?
- ClientsEntry, ClientsRegistry can be reused, they are similar with code in Eclipse itself that process extension-point
- the most important thing is design your extension-point API contract and select a suitable opportunity to load your extension, apply lazy loading when possible
So what happened here? Why Eclipse know how to process your popup menu contribution? How to add a pretty new functionality to Eclipse, which can't find or defined by Eclipse's existing extension-point? The answer is: contribute a extension-point for Eclipse by yourself.
Here I will use a example to explain how to define a extension-point by yourself, suppose I want to write a view that will show services status which deploy in a container like tomcat, spring, websphere etc., and customer can add any unknown containers support by implement my new extension-point for this view.
1. The requirements
I want to get service from container, also I need support unknown container type, so a client will be needed, i.e. I can user that client to get what I want to show in my view. So here I will define a client extension-point and this client will follow the interface contract below:
public interface IClient { public void setHost(String host); public void setPort(int port); public void createClient(); public List<IService> listServices(); }
The three methods at beginning will set connection information for client and create a client instance, then listServices() will get service back
2. Define client extension-point
You can use PDE extension-point schema editor do this, here's my client schema:
<?xml version='1.0' encoding='UTF-8'?> <!-- Schema file written by PDE --> <schema targetNamespace="com.example.services"> <annotation> <appInfo> <meta.schema plugin="com.example.services" id="clients" name="Clients"/> </appInfo> <documentation> this extension-point will be used to connect different container </documentation> </annotation> <element name="extension"> <complexType> <sequence minOccurs="1" maxOccurs="unbounded"> <element ref="client"/> </sequence> <attribute name="point" type="string" use="required"> <annotation> <documentation> </documentation> </annotation> </attribute> <attribute name="id" type="string"> <annotation> <documentation> </documentation> </annotation> </attribute> <attribute name="name" type="string"> <annotation> <documentation> </documentation> <appInfo> <meta.attribute translatable="true"/> </appInfo> </annotation> </attribute> </complexType> </element> <element name="client"> <complexType> <attribute name="id" type="string" use="required"> <annotation> <documentation> </documentation> </annotation> </attribute> <attribute name="name" type="string" use="required"> <annotation> <documentation> </documentation> </annotation> </attribute> <attribute name="clientType" type="string" use="required"> <annotation> <documentation> </documentation> </annotation> </attribute> <attribute name="class" type="string" use="required"> <annotation> <documentation> </documentation> <appInfo> <meta.attribute kind="java" basedOn="com.example.services.client.IClient"/> </appInfo> </annotation> </attribute> </complexType> </element> <annotation> <appInfo> <meta.section type="since"/> </appInfo> <documentation> 2007/09 </documentation> </annotation> <annotation> <appInfo> <meta.section type="examples"/> </appInfo> <documentation> <pre> <extension point="com.example.services.clients"> <client class="com.example.services.TomcatClient" clientType="tomcat" id="com.example.services.TomcatClient" name="Tomcat Client"/> </extension> </pre> </documentation> </annotation> <annotation> <appInfo> <meta.section type="apiInfo"/> </appInfo> <documentation> extension of this extension-point must implement <samp>com.example.services.client.IClient</samp> </documentation> </annotation> <annotation> <appInfo> <meta.section type="implementation"/> </appInfo> <documentation> see com.example.services plugin for a implementation example </documentation> </annotation> <annotation> <appInfo> <meta.section type="copyright"/> </appInfo> <documentation> alexgreenbar </documentation> </annotation> </schema>
3. Extension-point handle classes
When my view need get services status back, I need load all contributed extension, and instance client which know how to get service status back, here's code:
//describe every client contribution public class ClientsEntry { private final static String ATTR_TYPE = "clientType"; private final static String ATTR_CLAZZ = "class"; private IConfigurationElement element; private String type; public ClientsEntry(IConfigurationElement aElement) { element = aElement; type = element.getAttribute(ATTR_TYPE); } public String getType() { return type; } public IClient createClient() throws CoreException { return (IClient)element.createExecutableExtension(ATTR_CLAZZ); } }
//ClientsRegistry manage all client contribution, use singleton pattern public class ClientsRegistry { private final static Logger LOG = Logger.getLogger(ClientsRegistry.class.getName()); private final static String EXTENSION_POINT_ID = "com.example.services.clients"; private final static ClientsRegistry INSTANCE = new ClientsRegistry(); private List<ClientsEntry> entries = new ArrayList<ClientsEntry>(); private ClientsRegistry() { // } public static ClientsRegistry getInstance() { return INSTANCE; } private void load(){ entries.clear(); IExtensionRegistry registry = Platform.getExtensionRegistry(); IExtensionPoint point = registry.getExtensionPoint(EXTENSION_POINT_ID); for (IExtension extension : point.getExtensions()){ for (IConfigurationElement element : extension.getConfigurationElements()){ entries.add(new ClientsEntry(element)); } } } public List<ClientsEntry> getEntries() { load(); return entries; } public IClient getClient(String type) { IClient client = null; load(); for (ClientsEntry entry : entries) { if (entry.getType().equalsIgnoreCase(type)) { try { client = entry.createClient(); } catch (CoreException e) { LOG.log(Level.FINE, "can't instance client extension: ", e); client = null; continue; } break; } } return client; } }
4. A example client extension
<extension point="com.example.services.clients"> <client class="com.example.services.TomcatClient" clientType="tomcat" id="com.example.services.TomcatClient" name="Tomcat Client"/> </extension>
5. Use client extension
In the view code:
String newClientType = "tomcat" IClient client = ClientRegistry.getInstance().getClient(newClientType); client.setHost("localhost"); client.setPort(8080); client.createClient(); List<IService> allServices = client.listServices();
6. Summary
So write a extension-point is not so hard? It's pretty easy actually! Could you imagine all the powerful functionalities of Eclipse are based on this extension mechanism?
- ClientsEntry, ClientsRegistry can be reused, they are similar with code in Eclipse itself that process extension-point
- the most important thing is design your extension-point API contract and select a suitable opportunity to load your extension, apply lazy loading when possible
评论
1 楼
reverocean
2008-05-06
Good article。Thank you
I know how to develop an extension point for a plug-in.
I know how to develop an extension point for a plug-in.
发表评论
-
VM arguments to start runtime workbench
2010-09-01 10:26 915-Xms40m -Xmx512m -XX:MaxPermSiz ... -
The way of Buckminster (1)
2007-12-13 17:39 1146http://www.eclipse.org/buckmins ... -
A good article on Eclipse Job
2007-11-20 10:02 1287http://www.ibm.com/developerwor ... -
Model is king: EMF will play a main role in Eclipse
2007-10-22 14:01 1088If you have several years devel ... -
A good introduction for RPC and Create it quickly
2007-08-31 10:08 1061http://www.eclipsezone.com/eps/ ... -
Is RCP only suitable for application but IDE?
2007-08-24 15:57 1188Any insight? -
Eclipse lazy loading flag changed from time to time
2007-08-24 15:54 1024Due to embrace OSGi and make it ... -
good introduction on OSGi
2007-08-15 18:19 1262http://neilbartlett.name/blog/o ... -
A simple demo yet reveal core concept of OSGi
2007-08-15 16:18 1004http://www.eclipsezone.com/ecli ... -
profile your Eclipse plugins using TPTP
2007-08-13 13:39 2553At end of project development, ... -
load any resource from any installed plugin of Eclipse
2007-07-24 14:17 1173one common solution for this li ...
相关推荐
### 量化交易:如何构建自己的算法交易业务 #### 核心知识点概述 本文将深入探讨《量化交易:如何构建自己的算法交易业务》一书中的核心知识点。本书由Ernest P. Chan撰写,作为一位在量化交易领域有着丰富经验的...
Build Your Own AI - how to start
It begins with background on floating-point representation and rounding error, continues with a discussion of the IEEE floating-point standard, and concludes with numerous examples of how computer ...
how-to-improve-your-website-performance.html
How to Make Your Own Keylogger in C++ Programming Language 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Title:How Secure is Your Wireless Network? Safeguarding Your Wi-Fi LAN URL:http://www.amazon.com/exec/obidos/tg/detail/-/0131402064 ISBN:0131402064 Author:Lee Barken Publisher:Prentice Hall PTR Page:...
MIT.Press.-.How.to.Design.Programs.-.An.Introduction.to.Programming.and.Computing,.Revised.Edition.-.2003
This article shows how to add your own pages to Control Panel applets by writing a property sheet handler.(14KB)
You’ll learn how to create your own Pylons-driven web site and attain the mastery of advanced Pylons features. You’ll also learn how to stretch Pylons to its fullest ability, as well as share ...
Build Your Own 2D Game Engine and Create Great Web Games teaches you how to develop your own web-based game engine step-by-step, allowing you to create a wide variety of online videogames that can be ...
How to design programs -- An Introduction to Computing and Programming!从官网上抓取下来的网页,差不多整个www.htdp.org都抓取下来了,所以到这里共享给各位! 该书的中文译本是如何设计程序,很好的一本书。
How to Make Your Own Keylogger in C++ Programming Language 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
How to Make Your Own Keylogger in C++ Programming Language 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Click on button "Add..." to add an new repository, Enter "JD-Eclipse Update Site" and select the local site directory, Check "Java Decompiler Eclipse Plug-in", Next, next, next... and restart Eclipse.
The text introduces each major technique, explains what it is, explains how to do it, presents an example, and provides opportunities for students to practice before they do it for real in a project....