UNO Components
This section will explain some very basic things about UNO components. You can
skip it if you already know it. First I will explain what a component is and
its main parts. Afterwards, I will explain what is needed to create a UNO
component to help you to fully understand the different tools and their use
during the tutorial.
Components anatomy
“A component is a system element offering a predefined service and able to
communicate with other components” explains Wikipedia (Fr)
(Even thought there is a definition
on the English wikipedia article, I prefer the French one because of its
completeness). In UNOs case a component will generally be delivered as a
package containing a library (share library in C++ or jar in Java). For UNO, a
component is made of two distinct parts: specifications and an implementation.
- Specification: definition in a common language of what the component should do. For UNO, this language is called UNO-IDL and is more or less similar to Corba IDL. The documented part that will be provided as an API to use the component is the specification. Thus, it should be complete and implementation independent.
- Implementation: is the code that will realize what is described in the component specifications. This programming language can either be C++, Java, Python or others. This is the internal part of the component and will not be accessible from another component.
In order that UNO knows which implementation corresponds to which
specification, the component will use registries. There are two of them: the
first one describes all the component specifications ( types.rdb
) and the
second translates the implementation into specifications ( services.rdb
)
and is generated from the implementation.
Components creation process
The following diagram will help you better understand what should be done to
get a component from its specification and sources. As shown by the diagram,
the specifications are compiled and merged into one types.rdb
registry. Class
files are generated from that file to translate the specifications
into a Java class definition file which is built with the implementation into
a class using the java
compiler. All the classes are delivered as a jar
file with a specific manifest as we will see further in this tutorial. This
doesn’t make a usable OpenOffice.org: the types.rdb
and jar
file will
have to be zipped into a .uno.pkg
file described with another manifest.
http://wiki.openoffice.org/wiki/JavaEclipseTuto
例子:
0. prepare environment
cd /usr/lib64/openoffice.org/basis3.1/sdk
./setsdkenv_unix
1. create interface
XHelloWorld.idl
HelloWorld.idl
用idl语言来描述你自己需要向外部暴露的接口
2. idlc
idlc -C -I$OO_SDK_HOME/idl XHelloWorld.idl
idlc -C -I$OO_SDK_HOME/idl HelloWorld.idl
3. /UCR key (UNO core reflection)
regmerge thumbs.rdb /UCR XHelloWorld.urd
regmerge thumbs.rdb /UCR HelloWorld.urd
#check it
regview thumbs.rdb
4.
javamaker -Gc -BUCR -O./classes $OO_SDK_URE_HOME/share/misc/types.rdb
thumbs.rdb
5. implements interface and compile
javac -classpath
./classes:$OO_SDK_URE_HOME/share/java/juh.jar:$OO_SDK_URE_HOME/share/java/jurt.jar:$OO_SDK_URE_HOME/share/java/ridl.jar:$OO_SDK_URE_HOME/share/java/unoloader.jar
-d ./classes
/home/jialiang/workspace_uno_test/uno_test/src/org/openoffice/test/comp/HelloWorldImpl.java
6. deploy
jar cfm HelloWorldImpl.jar HelloWorldImpl.mf -C ./classes .
regcomp -register -r thumbs.rdb -br $OO_SDK_URE_HOME/share/misc/services.rdb
-br $OO_SDK_URE_HOME/share/misc/types.rdb -br thumbs.rdb -l
com.sun.star.loader.Java2 -c
file:///$OO_SDK_HOME/../program/classes/HelloWorldImpl.jar
7. install to OO, and test it
cd $OO_SDK_HOME/../program
su root
vi fundamentalbasisrc
append your jar file to URE_MORE_JAVA_TYPES path
append your rdb file to URE_MORE_SERVICES and URE_MORE_TYPES path
Sub Main
oTestComp = createUnoService("org.openoffice.test.HelloWorld")
MsgBox oTestComp.dbg_methods
MsgBox oTestComp.dbg_properties
print oTestComp.getHelloWorld()
oTestComp.DestinationDirectory = "test"
print oTestComp.DestinationDirectory
end sub
XHelloWorld.idl:
#ifndef _org_openoffice_test_XImageShrink_idl_
#define _org_openoffice_test_XImageShrink_idl_
#include <com/sun/star/uno/XInterface.idl>
#include <com/sun/star/awt/Size.idl>
module org { module openoffice { module test {
interface XHelloWorld {
[attribute] string SourceDirectory;
[attribute] string DestinationDirectory;
string getHelloWorld();
void printHelloWorld();
};
}; }; };
#endif
HelloWorld.idl:
#ifndef __org_openoffice_test_hellworld_idl__
#define __org_openoffice_test_hellworld_idl__
#include <XHelloWorld.idl>
module org { module openoffice { module test {
service HelloWorld : org::openoffice::test::XHelloWorld {
};
}; }; };
#endif
HelloWorldImpl.java:
package org.openoffice.test.comp;
import org.openoffice.test.XHelloWorld;
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;
import com.sun.star.lib.uno.helper.WeakBase;
public class HelloWorldImpl extends WeakBase implements
com.sun.star.lang.XServiceInfo, org.openoffice.test.XHelloWorld {
com.sun.star.uno.XComponentContext xComponentContext = null;
// maintain a static implementation id for all instances of HelloWorld
// initialized by the first call to getImplementationId()
static byte[] _implementationId;
// hold the service name in a private static member variable of the class
protected static final String __serviceName = "org.openoffice.test.HelloWorld";
String destDir = "";
String sourceDir = "";
/** Creates a new instance of HelloWorld */
public HelloWorldImpl() {
}
// static __getServiceFactory() 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(HelloWorldImpl.class.getName()))
xSingleServiceFactory = FactoryHelper.getServiceFactory(
HelloWorldImpl.class, HelloWorldImpl.__serviceName, multiFactory,
regKey);
return xSingleServiceFactory;
}
public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
// System.out.println(HelloWorldImpl.class.getName());
return FactoryHelper.writeRegistryServiceInfo(
HelloWorldImpl.class.getName(), __serviceName, regKey);
}
@Override
public String getSourceDirectory() {
return sourceDir;
}
@Override
public void setDestinationDirectory(String str) {
destDir = str;
}
@Override
public void setSourceDirectory(String str) {
sourceDir = str;
}
// XServiceInfo implementation
@Override
public String getImplementationName() {
return getClass().getName();
}
@Override
public boolean supportsService(String serviceName) {
if (serviceName.equals(__serviceName))
return true;
return false;
}
@Override
public String[] getSupportedServiceNames() {
return new String[] { __serviceName };
}
@Override
public String getHelloWorld() {
return "Hello World";
}
@Override
public void printHelloWorld() {
System.out.println("Hello World");
}
@Override
public String getDestinationDirectory() {
return destDir;
}
}
HelloWorldImpl.mf:
Manifest-Version: 1.0
RegistrationClassName: org.openoffice.test..comp.HelloWorldImpl
注意:我的OO版本是3.1,不同的版本可能不一样。