Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1959|回复: 0

面向接口(组件)编程方法

[复制链接]

388

主题

602

帖子

2218

积分

金牌会员

Rank: 6Rank: 6

积分
2218
发表于 2015-9-15 17:58:45 | 显示全部楼层 |阅读模式
本帖最后由 hechengjin 于 2015-9-15 18:01 编辑

XPConnect(JavaScript-XPCOM桥)
JavaXPCOM:在java里使用XULRunner组件和服务  XULRunner with Java: JavaXPCOM Tutorial 1
1. JavaXPCOM初始化
2.XPCOM组件和接口的基本概念
XPCOM系统里的所有组件都有一个唯一的标识。XPCOM组件实现许多接口,这些接口提供各种功能。
同一个组件可能实现不同的接口,同样,一个接口可能被不同的组件实现。因为接口必需有一个具体的实现才能使。
接口名总是以nsI开头。
MDC是一个与语言无关的文档,并不是Java的。在JavaXPCOM里没有XPCOM组件的代码。因为没有java代码,所有也没有JavaDoc。
组件使用URI来命名,例如,@mozilla.org/file/local;1。Mozilla的组件以@mozilla.org开头。这个URI是contractID,每个组件也有一个组件标识符(CID)。
同样,每一个接口也有一个接口标识符(IID)。IID总能使用一个static的final的属性来获取。组件可能存在一个对象实例或者单例的服务。通常,服务包含单词Service或者Manager.
3 使用JavaXPCOM
JavaScript调用XPCOM来创建一个本地文件组件
/ Get the component
var aFile = Components.classes["@mozilla.org/file/local;1"].createInstance();
// Get the part of the component corresponding to the interface that we want to use
var aLocalFile = aFile.QueryInterface(Components.interfaces.nsILocalFile);
// Call the function (one or more than one)
aLocalFile.initWithPath("/mozilla/testfile.txt");
上面需要两行JavaScript代码来创建组件和获得接口。可以简化的代码:
// Get the component and directly the part of the component corresponding to the interface that we want to use
var aLocalFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
// Call the function (one or more than one)
aLocalFile.initWithPath("/mozilla/testfile.txt");


JavaXPCOM调用XPCOM
方法一:使用组件ID(CID):
// Get the component manager
ComponentManager cm = Mozilla.getInstance().getComponentManager();
// Get the part of the component corresponding to the interface that we want to use
nsISupports obj = cm.createInstance("{6ddb050c-0d04-11d4-986e-00c04fa0cf4a}", null, nsISupports.NS_ISUPPORTS_IID);
// Get the part of the component corresponding to the interface that we want to use
nsIInputStreamChannel isc = obj.queryInterface(nsIInputStreamChannel.NS_IINPUTSTREAMCHANNEL_IID);
// Call the method (one or more than one)
System.out.println("Content-Length: " + isc.getContentLength());

简化一下:
// Get the component manager
ComponentManager cm = Mozilla.getInstance().getComponentManager();
// Get the part of the component corresponding to the interface that we want to use
nsIInputStreamChannel isc = (nsIInputStreamChannel)cm.createInstance("{6ddb050c-0d04-11d4-986e-00c04fa0cf4a}", null, nsIInputStreamChannel.NS_IINPUTSTREAMCHANNEL_IID);
// Call the method (one or more than one)
System.out.println("Content-Length: " + isc.getContentLength());


方法二:通过contractID来访问:
// Get the component manager
ComponentManager cm = Mozilla.getInstance().getComponentManager();
// Get the part of the component corresponding to the interface that we want to use
nsISupports obj = cm.createInstanceByContractID("@ mozilla.org/file/local;1", null, nsISupports.NS_ISUPPORTS_IID);
// Get the part of the component corresponding to the interface that we want to use
nsILocalFile file = obj.queryInterface(nsILocalFile.NS_ILOCALFILE_IID);
// Call the method (one or more than one)
file.initWithPath("/home/alpgarcia/xpcom-file-test.txt");简化形式:
// Get the component manager
ComponentManager cm = Mozilla.getInstance().getComponentManager();
// Get the part of the component corresponding to the interface that we want to use
nsILocalFile file = (nsILocalFile)cm.createInstanceByContractID("@ mozilla.org/file/local;1", null, nsILocalFile.NS_ILOCALFILE_IID);
// Call the method (one or more than one)
file.initWithPath("/home/alpgarcia/xpcom-file-test.txt");




检查一个组件是否实现了一个接口
在JavaScript里,我们通过调用instanceOf来检查
var aFile = Components.classes["@mozilla.org/file/local;1"].createInstance();
if (aFile instanceOf Components.interfaces.nsILocalFile){
   var aLocalFile = aFile.QueryInterface(Components.interfaces.nsILocalFile);
   ...
}


在java里,instanceof运算符是其它的含义。在java里只能通过QueryInterface来判断。如果调用失败,那么这个组件就没有实现特定的接口






JavaScript和Java的主要区别是,在JavaXPCOM里没有一个特定的对象与XPCOM组件对应。


使用services组,Service作为一个单例存在,换句话说,你只能初始化一次。
Services被用来get和set全局的数据或者对其它对象进行操作。创建Service与创建组件类似,把getInstance()改成getService()就行了。
在JavaXPCOM里,我们需要nsIServiceManager对象来初始化一个Service。然后你需要调用getService或者getServiceByContractID来得到service组件的引用。
在JavaScript里的代码:
// get the dns service component in JavaScript
var dnsservice = Components.classes["@mozilla.org/network/dns-service;1"].getService();
dnsservice.QueryInterface(Components.interfaces.nsIDNSService);

JavaXPCOM的代码:
//Now, we will try to use a service, for example, dns service
nsIServiceManager sm = mozilla.getServiceManager();                              
nsIDNSService dns = (nsIDNSService) sm.getServiceByContractID("@mozilla.org/network/dns-service;1", nsIDNSService.NS_IDNSSERVICE_IID);




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-3-29 05:04 , Processed in 0.059661 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表