Difference between revisions of "Zh/Documentation/DevGuide/ProUNO/Java/Getting a Service Manager"
From Apache OpenOffice Wiki
< Zh | Documentation
m |
|||
Line 70: | Line 70: | ||
− | {{ | + | {{Tip|本地组件上下文的服务管理器可以创建其他组件,但是,只有在运行时向服务管理器提供了各自工厂的情况下,这才会成为可能。可以在项目 javaunohelper 中的 Bootstrap 类实现中找到说明这种情况的示例。 |
此外,还有使用注册数据库来定位服务的服务管理器。该服务管理器是通过项目 javaunohelper 中的类 <tt>com.sun.star.comp.helper.RegistryServiceFactory</tt> 实现的。但是,该实现使用一个本机注册服务管理器,而不是提供完全的 Java 实现。}} | 此外,还有使用注册数据库来定位服务的服务管理器。该服务管理器是通过项目 javaunohelper 中的类 <tt>com.sun.star.comp.helper.RegistryServiceFactory</tt> 实现的。但是,该实现使用一个本机注册服务管理器,而不是提供完全的 Java 实现。}} |
Latest revision as of 20:09, 14 July 2018
编写访问办公软件的客户机应用程序时,需要可提供所需功能的办公软件对象。所有这些对象的根都是服务管理器组件,因此客户机需要实例化该组件。服务管理器在办公软件进程中执行,因此,使用由办公软件实例化的 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). |