For Undo, use a dispatcher call as shown in listing 4.3 of Andrew Pitonyak's macro document.
For OnStartApp, I did not find a lot of documentation. Create a file called Events.xcu
with code like the following:
<node oor:name="ApplicationEvents">
<node oor:name="Bindings">
<node oor:name="OnStartApp" oor:op="replace">
<prop oor:name="BindingURL" oor:type="xs:string">
<value>vnd.sun.star.script:events.py$OnStartApp?language=Python&location=application</value>
Change the value of vnd.sun.star.script
for use with Java, according to this documentation.
Then add this to manifest.xml:
<manifest:file-entry
manifest:media-type="application/vnd.sun.star.configuration-data"
manifest:full-path="Events.xcu" />
The CTLO project at GitHub is an example of using Events.xcu and manifest.xml in this way.
EDIT:
Here is an example of a dispatcher call in Java:
PropertyValue[] printProperties = new PropertyValue[1];
printProperties[0] = new PropertyValue();
printProperties[0].Name = "Print";
printProperties[0].Value = new Boolean(true);
XDispatchProvider xDispatchProvider = (XDispatchProvider)
UnoRuntime.queryInterface (XDispatchProvider.class, xDesktop);
dispatcher.executeDispatch(
xDispatchProvider, ".uno:Print","_self", 0, printProperties);