Difference between revisions of "Documentation/DevGuide/WritingUNO/Providing a Single Factory Using a Helper Method"
OOoWikiBot (Talk | contribs) m (FINAL VERSION FOR L10N) |
|||
Line 41: | Line 41: | ||
The <code>FactoryHelper</code> is contained in the ''jurt'' jar file. The <code>getServiceFactory()</code> method takes as a first argument a <code>Class</code> object. When <code>createInstance()</code> is called on the default factory, it creates an instance of that <code>Class</code> using <code>newInstance()</code> on it and retrieves the implementation name through <code>getName()</code>. The second argument is the service name. The <code>multiFactory</code> and <code>regKey</code> arguments were received in <code>__getServiceFactory()</code> and are passed to the <code>FactoryHelper</code>. | The <code>FactoryHelper</code> is contained in the ''jurt'' jar file. The <code>getServiceFactory()</code> method takes as a first argument a <code>Class</code> object. When <code>createInstance()</code> is called on the default factory, it creates an instance of that <code>Class</code> using <code>newInstance()</code> on it and retrieves the implementation name through <code>getName()</code>. The second argument is the service name. The <code>multiFactory</code> and <code>regKey</code> arguments were received in <code>__getServiceFactory()</code> and are passed to the <code>FactoryHelper</code>. | ||
− | {{ | + | {{Note|In this case, the implementation name, which the default factory finds through <tt>Class.getName()</tt> is <tt>org.openoffice.comp.test.ImageShrink</tt> and the service name is <tt>org.openoffice.test.ImageShrink</tt>. The implementation name and the service name are used for the separate <tt>XServiceInfo</tt> implementation within the default factory. Not only do you support the <tt>XServiceInfo</tt> interface in your service implementation, but the single factory must implement this interface as well.}} |
The default factory created by the <code>FactoryHelper</code> expects a public constructor in the implementation class of the service and calls it when it instantiates the service implementation. The constructor can be a default constructor, or it can take a <idl>com.sun.star.uno.XComponentContext</idl> or a <idl>com.sun.star.lang.XMultiServiceFactory</idl> as an argument. Refer to [[Documentation/DevGuide/WritingUNO/Create Instance with Arguments|Create Instance with Arguments]] for other arguments that are possible. | The default factory created by the <code>FactoryHelper</code> expects a public constructor in the implementation class of the service and calls it when it instantiates the service implementation. The constructor can be a default constructor, or it can take a <idl>com.sun.star.uno.XComponentContext</idl> or a <idl>com.sun.star.lang.XMultiServiceFactory</idl> as an argument. Refer to [[Documentation/DevGuide/WritingUNO/Create Instance with Arguments|Create Instance with Arguments]] for other arguments that are possible. |
Revision as of 14:55, 4 July 2018
- Class Definition with Helper Class
- Implementing Your Own Interfaces
- Providing a Single Factory Using a Helper Method
- Write Registration Info Using a Helper Method
- Implementing without Helpers
- Storing the Service Manager for Further Use
- Create Instance with Arguments
- Possible Structures for Java Components
- Running and Debugging Java Components
The component must be able to create single factories for each service implementation it contains and return them in the static component operation __getServiceFactory()
. The OpenOffice.org Java UNO environment provides a Java class com.sun.star.comp.loader.FactoryHelper
that creates a default implementation of a single factory through its method getServiceFactory()
. The following example could be written:
package org.openoffice.comp.test; import com.sun.star.lang.XSingleServiceFactory; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.registry.XRegistryKey; import com.sun.star.comp.loader.FactoryHelper; public class ImageShrink ... { ... // static __getServiceFactory() implementation // static member __serviceName was introduced above for XServiceInfo implementation public static XSingleServiceFactory __getServiceFactory(String implName, XMultiServiceFactory multiFactory, com.sun.star.registry.XRegistryKey regKey) { com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null; if (implName.equals( ImageShrink.class.getName()) ) xSingleServiceFactory = FactoryHelper.getServiceFactory(ImageShrink.class, ImageShrink.__serviceName, multiFactory, regKey); return xSingleServiceFactory; } ... }
The FactoryHelper
is contained in the jurt jar file. The getServiceFactory()
method takes as a first argument a Class
object. When createInstance()
is called on the default factory, it creates an instance of that Class
using newInstance()
on it and retrieves the implementation name through getName()
. The second argument is the service name. The multiFactory
and regKey
arguments were received in __getServiceFactory()
and are passed to the FactoryHelper
.
The default factory created by the FactoryHelper
expects a public constructor in the implementation class of the service and calls it when it instantiates the service implementation. The constructor can be a default constructor, or it can take a com.sun.star.uno.XComponentContext or a com.sun.star.lang.XMultiServiceFactory as an argument. Refer to Create Instance with Arguments for other arguments that are possible.
Java components are housed in jar files. When a component has been registered, the registry contains the name of the jar file, so that the service manager can find it. However, because a jar file can contain several class files, the service manager must be told which one contains the __getServiceFactory()
method. That information has to be put into the jar's Manifest file, for example:
RegistrationClassName: org.openoffice.comp.test.ImageShrink
Content on this page is licensed under the Public Documentation License (PDL). |