OO-Snippets: Calling Java from OOBasic

Commons

Keywordscalling java class from basic, uno component
LanguageJava
ApplicationOffice
AuthorsTom Schindl (initial)
Supported Versions1.1.x  
Supported OSAll  
QuestionHow can I call a java class from OOBasic

I have written a java programm i want to run from OpenOffice using

OOBasic.

Answer

You have to wrap your Java-Class/Java-Application in an UNO-Component.

See the developer guide and the examples coming with it

to see how this is done using the make utility.

Code-Snippet-Listing (snippet-source)

package at.bestsolution.oeush;

import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.task.XJobExecutor;
import com.sun.star.uno.XComponentContext;

/* Usage in OOBasic:
 * 
 * Sub OracleReports
 *   Dim Service As Object
 *   Service = CreateUnoService("at.bestsolution.oeush.OracleReports")
 *   Service.trigger("")
 * End Sub
 * 
 * Registered using pkgchk
 */
public class OracleReports extends WeakBase implements XServiceInfo, XJobExecutor
{
    static final String __serviceName = "at.bestsolution.oeush.OracleReports";
    
    private XComponentContext _xComponentContext;
    
    public OracleReports(XComponentContext xComponentContext){
        _xComponentContext = xComponentContext;
    }
    
    // static component operations
    public static XSingleServiceFactory __getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey ) {
        XSingleServiceFactory xSingleServiceFactory = null;

        if ( implName.equals( OracleReports.class.getName() ) ) {
            xSingleServiceFactory = FactoryHelper.getServiceFactory( OracleReports.class, OracleReports.__serviceName, multiFactory, regKey );
        }        

        return xSingleServiceFactory;
    }

    public static boolean __writeRegistryServiceInfo(XRegistryKey  regKey ) {
        return FactoryHelper.writeRegistryServiceInfo(
        OracleReports.class.getName(), OracleReports.__serviceName, regKey );
    }

    
    /* 
     * com.sun.star.lang.XServiceInfo.getImplementationName
     */
    public String getImplementationName()
    {
        return getClass().getName();
    }

    /* 
     * com.sun.star.lang.XServiceInfo.getSupportedServiceNames
     */
    public String[] getSupportedServiceNames()
    {
        String[] retValue= new String[0];
        retValue[0] = __serviceName;
        return retValue;
    }

    /* 
     * com.sun.star.lang.XServiceInfo.supportsService
     */
    public boolean supportsService(String serviceName)
    {
        if ( serviceName.equals( __serviceName))
        {
            return true;
        }

        return false;
    }

    /* 
     * com.sun.star.task.XJobExecutor.trigger
     */
    public void trigger(String event)
    {
        XMultiComponentFactory xMultiComponentFactory = _xComponentContext.getServiceManager();
        // here you can call your java classes like you would without openoffice
    }
}

Changelog

DateUserModification
2005-07-18tomsonInitial version

and