Standalone Use Case

From Apache OpenOffice Wiki
Jump to: navigation, search



Simply typing uno gives the following usage screen :

 uno (-c ComponentImplementationName -l LocationUrl | -s ServiceName) 
    [-ro ReadOnlyRegistry1] [-ro ReadOnlyRegistry2] ... [-rw ReadWriteRegistry] 
 [-u uno:(socket[,host=HostName][,port=nnn]|pipe[,name=PipeName]);urp;Name [--singleaccept] [--singleinstance]] 
 [-- Argument1 Argument2 ...]


Choosing the implementation to be instantiated

Using the option -s servicename gives the name of the service which shall be instantiated. The uno executable then tries to instantiate a service by this name, using the registries as listed below.
Alternatively, the -l and -c options can be used. The -l gives an url to the location of the shared library or .jar file, and -c the name of the desired service implementation inside the component. Remember that a component may contain more than one implementation.

Choosing the registries for the component context (optional)

With the option -ro, give a file url to a registry file containing component's registration information and/or type libraries. The -ro option can be given multiple times. The -rw option can only be given once and must be the name of a registry with read/write access. It will be used when the instantiated component tries to register components at runtime. This option is rarely needed.
Note that the uno tool ignores bootstrap variables, such as UNO_TYPES and UNO_SERVICES.

The UNO URL (optional)

Giving a UNO URL causes the uno tool to start in server mode, then it accepts on the connection part of the UNO URL. In case another process connects to the resource (tcp/ip socket or named pipe), it establishes a UNO interprocess bridge on top of the connection (see also UNO Interprocess Connections). Note that urp should always be used as protocol. An instance of the component is instantiated when the client requests a named object using the name, which was given in the last part of the UNO URL.

Option --singleaccept

Only meaningful when a UNO URL is given. It tells the uno executable to accept only one connection, thus blocking any further connection attempts.

Option --singleinstance

Only meaningful when a UNO URL is given. It tells the uno executable to always return the same (first) instance of the component, thus multiple processes communicate to the same instance of the implementation. If the option is not given, every getInstance() call at the com.sun.star.bridge.XBridge interface instantiates a new object.

Option -- (double dash)

Everything following -- is interpreted as an option for the component itself. The arguments are passed to the component through the initialize() call of com.sun.star.lang.XInitialization interface.
Documentation note.png The uno executable currently does not support the bootstrap variable concept as introduced by C++ Language Binding. The uno registries must be given explicitly given by command line.

The following example shows how to implement a Java component suitable for the uno executable.

  import com.sun.star.uno.XComponentContext;
  import com.sun.star.comp.loader.FactoryHelper;
  import com.sun.star.lang.XSingleServiceFactory;
  import com.sun.star.lang.XMultiServiceFactory;
  import com.sun.star.registry.XRegistryKey;
  
  public class UnoExeMain implements com.sun.star.lang.XMain
  {
      final static String __serviceName = "MyMain";
      XComponentContext _ctx;
  
      public UnoExeMain( XComponentContext ctx )
      {
          // in case we would need the component context !
          _ctx = ctx;
      }
  
      public int run( /*IN*/String[] aArguments )
      {
          System.out.println( "Hello world !" );
          return 0;
      }
  
      public static XSingleServiceFactory __getServiceFactory(
          String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
      {
          XSingleServiceFactory xSingleServiceFactory = null;
    
          if (implName.equals(UnoExeMain.class.getName()))
          {
              xSingleServiceFactory =
                  FactoryHelper.getServiceFactory(
                      UnoExeMain.class, UnoExeMain.__serviceName, multiFactory, regKey);
          }
          return xSingleServiceFactory;
      }
      
      public static boolean __writeRegistryServiceInfo(XRegistryKey regKey)
      {
          boolean b = FactoryHelper.writeRegistryServiceInfo(
              UnoExeMain.class.getName(),
              UnoExeMain.__serviceName, regKey);
          return b;
      }
  }

The class itself inherits from com.sun.star.lang.XMain. It implements a constructor with the com.sun.star.uno.XComponentContext interface and stores the component context for future use. Within its run() method, it prints 'Hello World'. The last two mandatory functions are responsible for instantiating the component and writing component information into a registry. Refer to Simple Component in Java for further information.

The code needs to be compiled and put into a .jar file with an appropriate manifest file:

 RegistrationClassName: UnoExeMain

These commands create the jar:

 javac UnoExeMain
 jar -cvfm UnoExeMain.jar Manifest UnoExeMain.class

To be able to use it, register it with the following command line into a separate registry file (here test.rdb). The <OfficePath>/program directory needs to be the current directory, and the regcomp and uno tools must have been copied into this directory.

 regcomp -register \ 
         -br <officepath>/program/services.rdb \ 
         -r test.rdb \ 
         -c file:///c:/devmanual/Develop/samples/unoexe/UnoExeMain.jar \ 
         -l com.sun.star.loader.Java2

The \ means command line continuation.

The component can now be run:

 uno -s MyMain -ro types.rdb -ro services.rdb -ro test.rdb 

This command should give the output "hello world !"

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages