Providing a Single Factory Using Helper Method

From Apache OpenOffice Wiki
Jump to: navigation, search



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.

Documentation note.png In this case, the implementation name, which the default factory finds through Class.getName() is org.openoffice.comp.test.ImageShrink and the service name is org.openoffice.test.ImageShrink. The implementation name and the service name are used for the separate XServiceInfo implementation within the default factory. Not only do you support the XServiceInfo interface in your service implementation, but the single factory must implement this interface as well.

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).
Personal tools
In other languages