Start an acceptor of TCP/IP socket or named pipe in order to read/write an office document or handle the instance via UNO from your external program.
Topics
About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice
The table of contents of this article
- Starting Context
- Target Context
- Orientation
- Main Body
- 1: Making the LibreOffice or Apache OpenOffice Instance a UNO Server
- 2: Starting the LibreOffice or Apache OpenOffice Instance Invisible
- 3: The Conclusion and Beyond
Starting Context
- The reader has knowledge on what what UNO is and how it is related to LibreOffice or Apache OpenOffice.
- The reader has built an environment for developing UNO programs in Linux or Windows, according to a previous article (here for Linux and here for Windows).
Target Context
- The reader will know how to make the LibreOffice or Apache OpenOffice instance a UNO server.
Here are Hypothesizer 7, Objector 9A, and Objector 9B in front of a computer.
Orientation
Hypothesizer 7
In this article, we will learn how to make the LibreOffice or Apache OpenOffice instance a UNO server.
Objector 9A
I don't want to do such a thing, but you insist that I have to . . .
Hypothesizer 7
If you want to read and/or write an office document file and/or otherwise handle the LibreOffice or Apache OpenOffice instance through UNO from your external program, you will have to make the LibreOffice or Apache OpenOffice instance a UNO server. That is inevitable as you will understand if you understand what UNO is.
Objector 9A
The instance will be a hindrance on the screen . . .
Hypothesizer 7
You can make it invisible, as we will learn in the main body.
Objector 9A
. . . It is cumbersome that I have to start an instance every time before I execute my program . . .
Hypothesizer 7
Sir, if you are certain to start the instance in the same computer with your program, you can just have your program start it automatically.
Objector 9A
Why didn't you say so? Or why didn't you have your sample do so?
Hypothesizer 7
Because we are not on the premise that we are going to start the instance in the same computer with our program.
Objector 9B
I heard that just calling 'com.sun.star.comp.helper.Bootstrap.Bootstrap.bootstrap ()' unnecessitates starting an instance.
Hypothesizer 7
It is more that that method automatically starts an instance, as I said you can have your program do, than that method unnecessitates starting an instance.
Objector 9B
Anyway, why don't you just use the method.
Hypothesizer 7
Because, as I have said, we are not on the premise that we are going to start the instance in the same computer with our program, and also because that way cannot detect connection cuts and does not develop to creating UNO servers.
Objector 9B
Whatever.
Main Body
1: Making the LibreOffice or Apache OpenOffice Instance a UNO Server
Hypothesizer 7
Before our external UNO program connects to the LibreOffice or Apache OpenOffice instance, the instance must be a UNO server.
Being a UNO server means having an acceptor started.
Objector 9B
"acceptor"?
Hypothesizer 7
Yes. As you can imagine from the name, it is something that accepts connections from UNO clients.
Objector 9B
Hmm.
Hypothesizer 7
There are two ways for making the LibreOffice or Apache OpenOffice instance a UNO server: adding a setting into a configuring file and starting the instance, or executing the LibreOffice or Apache OpenOffice execution command with a certain argument.
Objector 9B
Hmm.
Hypothesizer 7
Note that I said "executing the LibreOffice or Apache OpenOffice execution command", not "starting the instance", in the latter way because we do not have to start a new instance, but can turn the existing instance a UNO server.
Objector 9B
Huh?
Hypothesizer 7
The LibreOffice or Apache OpenOffice execution command starts a new instance only if there is no existing instance.
Objector 9B
Oh.
Hypothesizer 7
For the former way, the configuration file is 'share/registry/base.xcd' in the office installation directory, and we can set '<prop oor:name="ooSetupConnectionURL"><value>socket,host=localhost,port=2002;urp;</value></prop>' after '<node oor:name="Office">' in the '<oor:component-data ~ oor:name="Setup" ~>' ('~' means a ellipsis) tag, where '2002' is the socket port number, which we can change, and 'localhost' has to be changed to the host name (or the IP address) on which the UNO server listens, if the UNO server has to accept UNO connections from other computers.
For the latter way, the argument to be specified is '"--accept=socket,host=localhost,port=2002;urp;"', where '2002' is again the socket port number, which we can change, and 'localhost' has to be changed to the host name (or the IP address) on which the UNO server listens, if the UNO server has to accept UNO connections from other computers.
Objector 9B
Actually, I don't know what "the LibreOffice or Apache OpenOffice execution command" is.
Hypothesizer 7
Ah, it is 'program/soffice' ('program/soffice.exe' in Windows) in the office installation directory.
2: Starting the LibreOffice or Apache OpenOffice Instance Invisible
Hypothesizer 7
As we are starting the LibreOffice or Apache OpenOffice instance in the purpose of handling it from our program, the GUI of the instance may be a hindrance.
Objector 9A
Certainly, it is.
Hypothesizer 7
You can start the instance invisible by specifying the '--headless' argument for the LibreOffice or Apache OpenOffice execution command.
Objector 9A
That's good, but how can I stop the instance?
Hypothesizer 7
As the execution command does not return until the instance is stopped, you can stop the instance by 'Control-C' on the terminal, or by killing the process.
Objector 9A
Can I turn the existing instance invisible?
Hypothesizer 7
I does not seem that you can.
Objector 9A
I guess that I cannot turn the existing instance visible either.
Hypothesizer 7
I guess so too.
3: The Conclusion and Beyond
Hypothesizer 7
Now, we can make the LibreOffice or Apache OpenOffice instance a UNO server.
That is necessary for our external programs to connect to the LibreOffice or Apache OpenOffice instance, and we will see some sample programs that require that preparation, in future articles.