R
Ramon F Herrera
Please consider the justification for an acknowledged "dirty hack" and
its controversial code below.
My actual question will be in my next message.
-Ramon
---------
Working with bootstraps.
The OpenOffice.org Java API has its own methods for bootstrapping
OpenOffice.org. By "bootstrapping" OpenOffice.org, we simply mean
"starting up" or "launching" the OpenOffice.org application launcher.
This is done by finding the location of the juh.jar library and
looking for the soffice(.exe) executable in that location, or in one
directory above that location. This requires the juh.jar library to be
put on the CLASSPATH, enabling the application that you create in this
tutorial to find it. However, here we want to ship our own juh.jar
file (and others) with the application. In this case, this approach to
the bootstrapping mechanism doesn't work.
In order to solve this issue, there are two possibilities. First, it
is possible to make sure that Java can find the soffice(.exe)
executable every time. This can be done by putting the directory
containing the executable on the PATH in Windows, or on
LD_LIBRARY_PATH in Mac, Unix, and Linux. This requires action from
potential users and we want to prevent that.
So we choose the second possibility, which involves working with
access modifiers. In the Sun JDK, the system ClassLoader is an
instance of the class URLClassLoader. This class has a private method
called addURL, which is called when Java starts and which will add all
JARs and other resources needed. Using Reflection, we request an
instance of the URLClassLoader, make the addURL method accessible to
us, and add the directory containing the soffice(.exe) executable to
the stack of URLs in the URLClassLoader. It is a dirty hack, but it
works.
-----------------
and this is the questioned code:
public Object simpleBootstrap
(String pathToExecutable) throws Exception {
//Get the executable from the incoming String:
String ooBaseDirectory = pathToExecutable.replaceAll("soffice(.exe)
{0,1}$","");
System.out.println("Your ooBaseDir is: " + ooBaseDirectory);
ClassLoader loader = ClassLoader.getSystemClassLoader();
if (loader instanceof URLClassLoader) {
URLClassLoader cl = (URLClassLoader)loader;
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", new
Class[]{URL.class});
method.setAccessible(true);
method.invoke(cl, new Object[]{new
File(ooBaseDirectory).toURL()});
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system
classloader");
}
} else {
System.out.println("Error occured, URLClassLoader expected but
" +
loader.getClass() + " received. Could not continue.");
}
(If you are inside an IDE, you don't need any of the above "dirty
hack", you only need the line below):
//Get the office component context:
XComponentContext xContext = Bootstrap.bootstrap();
its controversial code below.
My actual question will be in my next message.
-Ramon
---------
Working with bootstraps.
The OpenOffice.org Java API has its own methods for bootstrapping
OpenOffice.org. By "bootstrapping" OpenOffice.org, we simply mean
"starting up" or "launching" the OpenOffice.org application launcher.
This is done by finding the location of the juh.jar library and
looking for the soffice(.exe) executable in that location, or in one
directory above that location. This requires the juh.jar library to be
put on the CLASSPATH, enabling the application that you create in this
tutorial to find it. However, here we want to ship our own juh.jar
file (and others) with the application. In this case, this approach to
the bootstrapping mechanism doesn't work.
In order to solve this issue, there are two possibilities. First, it
is possible to make sure that Java can find the soffice(.exe)
executable every time. This can be done by putting the directory
containing the executable on the PATH in Windows, or on
LD_LIBRARY_PATH in Mac, Unix, and Linux. This requires action from
potential users and we want to prevent that.
So we choose the second possibility, which involves working with
access modifiers. In the Sun JDK, the system ClassLoader is an
instance of the class URLClassLoader. This class has a private method
called addURL, which is called when Java starts and which will add all
JARs and other resources needed. Using Reflection, we request an
instance of the URLClassLoader, make the addURL method accessible to
us, and add the directory containing the soffice(.exe) executable to
the stack of URLs in the URLClassLoader. It is a dirty hack, but it
works.
-----------------
and this is the questioned code:
public Object simpleBootstrap
(String pathToExecutable) throws Exception {
//Get the executable from the incoming String:
String ooBaseDirectory = pathToExecutable.replaceAll("soffice(.exe)
{0,1}$","");
System.out.println("Your ooBaseDir is: " + ooBaseDirectory);
ClassLoader loader = ClassLoader.getSystemClassLoader();
if (loader instanceof URLClassLoader) {
URLClassLoader cl = (URLClassLoader)loader;
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", new
Class[]{URL.class});
method.setAccessible(true);
method.invoke(cl, new Object[]{new
File(ooBaseDirectory).toURL()});
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system
classloader");
}
} else {
System.out.println("Error occured, URLClassLoader expected but
" +
loader.getClass() + " received. Could not continue.");
}
(If you are inside an IDE, you don't need any of the above "dirty
hack", you only need the line below):
//Get the office component context:
XComponentContext xContext = Bootstrap.bootstrap();