Providing a Single Factory Using Helper Method
- 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 Apache OpenOffice 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). |