获取服务管理器
From Apache OpenOffice Wiki
< Zh | Documentation
编写访问办公软件的客户机应用程序时,需要可提供所需功能的办公软件对象。所有这些对象的根都是服务管理器组件,因此客户机需要实例化该组件。服务管理器在办公软件进程中执行,因此,使用由办公软件实例化的 Java 组件时必须首先执行办公软件。在客户机/服务器方案中,必须直接启动办公软件。进程间通信使用一种远程协议,可以将该协议作为一个命令行参数提供给办公软件:
soffice -accept=socket,host=localhost,port=8100;urp
客户机使用本地 com.sun.star.bridge.UnoUrlResolver 获取对办公软件的全局服务管理器(服务器)的引用。办公软件的全局服务管理器用于从桥的另一端获取对象。在本示例中,获取 com.sun.star.frame.Desktop 的一个实例:
import com.sun.star.uno.XComponentContext;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.bridge.UnoUrlResolver;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.beans.XPropertySet
import com.sun.star.uno.UnoRuntime;
XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);
// create a connector, so that it can contact the office
XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext);
Object initialObject = urlResolver.resolve(
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(
XMultiComponentFactory.class, initialObject);
// retrieve the component context as property (it is not yet exported from the office)
// Query for the XPropertySet interface.
XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xOfficeFactory);
// Get the default context from the office server.
Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");
// Query for the interface XComponentContext.
XComponentContext xOfficeComponentContext = (XComponentContext) UnoRuntime.queryInterface(
XComponentContext.class, oDefaultContext);
// now create the desktop service
// NOTE: use the office component context here!
Object oDesktop = xOfficeFactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", xOfficeComponentContext);
如本例所示,本地组件上下文是通过 com.sun.star.comp.helper.Bootstrap 类(一个 Java UNO 运行时类)创建的。其实现提供了一个服务管理器,该服务管理器对可以创建的服务数目进行了限制。这些服务的名称为:
- com.sun.star.lang.ServiceManager
- com.sun.star.lang.MultiServiceFactory
- com.sun.star.loader.Java
- com.sun.star.loader.Java2
- com.sun.star.bridge.UnoUrlResolver
- com.sun.star.bridge.BridgeFactory
- com.sun.star.connection.Connector
- com.sun.star.connection.Acceptor
它们足以建立一个远程连接并获取办公软件提供的功能全面的服务管理器。
| Content on this page is licensed under the Public Documentation License (PDL). |