Difference between revisions of "Documentation/DevGuide/WritingUNO/Providing a Single Factory Using a Helper Method"
m (1 revision(s)) |
m |
||
Line 8: | Line 8: | ||
The component must be able to create single factories for each service implementation it contains and return them in the static component operation <code>__getServiceFactory()</code>. The {{PRODUCTNAME}} Java UNO environment provides a Java class <code>com.sun.star.comp.loader.FactoryHelper</code> that creates a default implementation of a single factory through its method <code>getServiceFactory()</code>. The following example could be written: | The component must be able to create single factories for each service implementation it contains and return them in the static component operation <code>__getServiceFactory()</code>. The {{PRODUCTNAME}} Java UNO environment provides a Java class <code>com.sun.star.comp.loader.FactoryHelper</code> that creates a default implementation of a single factory through its method <code>getServiceFactory()</code>. The following example could be written: | ||
<!--[SOURCE:Components/Thumbs/org/openoffice/comp/test/ImageShrink.java]--> | <!--[SOURCE:Components/Thumbs/org/openoffice/comp/test/ImageShrink.java]--> | ||
− | + | <source lang="java"> | |
package org.openoffice.comp.test; | package org.openoffice.comp.test; | ||
Line 37: | Line 37: | ||
} | } | ||
− | + | </source> | |
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>. | ||
Revision as of 21:09, 21 March 2008
- 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). |