OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[WRKRND] Integrating OO UNO App with java persistence libs

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
jtsdata
General User
General User


Joined: 06 Feb 2009
Posts: 9
Location: Sydney

PostPosted: Fri Feb 06, 2009 10:37 pm    Post subject: [WRKRND] Integrating OO UNO App with java persistence libs Reply with quote

OpenOffice UNO Java Client Application can not integrate with TopLink and Hibernate persistence libraries.

Dear All,

I am facing a severe show stopper problem. Using the Netbeans template, I have implemented a very basic OpenOffice UNO Java Client Application which works fine, but all my attempts to instantiate EntityManager from any of the persistence libraries shipped with Netbeans 6.5 were not successful. I suspect the reason is with the the way the custom class loader from the package com.sun.star.lib.loader.Loader works - conflict with some of the UNO classes?
Any help input from you will be highly appreciated!

To reproduce this problem with Netbeans 6.5, TopLink and OpenOffice plugin installed, you may follow this steps:
(When using Hibernate, the results are identical.)

1. From menu: File-->New Project...-->Choose Project-->OpenOffice.org Client Application
Create OpenOffice UNO Java Client Application named 'OpenOfficeTopLinkApp'
2. From the menu: New->Entity Class from Database...
Create class 'Person' in package 'toplink' using connection to the TRAVEL database which ships with Netbeans 6.5
jdbc:derby://locallhost:1527/travel
3. When asked to 'Create Persistence Unit..." do so, selecting TopLink as a Persistence Library.
4. In the Mapping Options screen, select 'java.util.List' for the 'Collection Type'
5. Create the OpenOfficeTopLinkApp.java file - attached bellow.

This application can be started by either invoking directly the OpenOfficeTopLinkApp.main() method
(in which case only the access to the database should work) or by invoking the
com.sun.star.lib.loader.Loader.main() method. In the later case, the application
should connect to the database, display the contents of the TRAVEL.PERSON table and then should
connect to the OpenOffice. Instead it produces the following error output:

run:
ERROR: OpenOfficeTopLinkApp.main(): Could not connect to database.
java.lang.ClassCastException: oracle.toplink.essentials.PersistenceProvider cannot be cast to javax.persistence.spi.PersistenceProvider
at javax.persistence.Persistence.findAllProviders(Persistence.java:186)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at com.example.OpenOfficeTopLinkApp.main(OpenOfficeTopLinkApp.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.star.lib.loader.Loader.main(Loader.java:154)
TRACE: OpenOfficeTopLinkApp.main(): Successfully connected to a running Office ...

When invoking directly the OpenOfficeTopLinkApp.main() method, the output should be something similar to:

run:
[TopLink Info]: 2009.02.07 05:01:33.847--ServerSession(1762721320)--TopLink, version: Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))
[TopLink Info]: 2009.02.07 05:01:34.184--ServerSession(1762721320)--file:/DEV/PROJECTS/TEST/OpenOffice/OpenOfficeTopLinkApp/build/classes/-OpenOfficeTopLinkAppPU login successful
TRACE: OpenOfficeTopLinkApp.displayResult(): The return value for record 1 is: Able, Tony, CEO, 1, Tue Dec 16 22:00:40 EST 2008;
TRACE: OpenOfficeTopLinkApp.displayResult(): The return value for record 2 is: Black, John, CTO-CFO, 1, Wed Dec 03 16:08:01 EST 2008;
TRACE: OpenOfficeTopLinkApp.displayResult(): The return value for record 3 is: Kent, Richard, VP, 1, Wed Dec 03 10:49:29 EST 2008;
TRACE: OpenOfficeTopLinkApp.displayResult(): The return value for record 4 is: Chen, Larry, VP/CXO - SGMS, 0, null;
TRACE: OpenOfficeTopLinkApp.displayResult(): The return value for record 5 is: Donaldson, Sue, VP, 0, null;
TRACE: OpenOfficeTopLinkApp.displayResult(): The return value for record 6 is: Murrell, Tony, VP - SFE, 0, null;




com.sun.star.comp.helper.BootstrapException: no office executable found!
at com.sun.star.comp.helper.Bootstrap.bootstrap(Bootstrap.java:253)
at com.example.OpenOfficeTopLinkApp.main(OpenOfficeTopLinkApp.java:29)



Code:

/*
 * OpenOfficeTopLinkApp.java
 *
 * Created on 2009.02.06 - 17:11:56
 *
 */

package com.example;

import com.sun.star.uno.XComponentContext;
import com.sun.star.comp.helper.Bootstrap;
import java.util.List;
import java.util.Vector;
import toplink.Person;

/**
 *
 * @author jtsdata
 */
public class OpenOfficeTopLinkApp {
   
    /** Creates a new instance of OpenOfficeTopLinkApp */
    public OpenOfficeTopLinkApp() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            //TopLink connection TEST
            List resultList;
            javax.persistence.EntityManager entityManager;
            javax.persistence.Query query;

            entityManager = javax.persistence.Persistence.createEntityManagerFactory("OpenOfficeTopLinkAppPU").createEntityManager();

            query = entityManager.createQuery("SELECT p FROM Person p");
            resultList = query.getResultList();
            displayResult(resultList);
        } catch (Throwable t){
            System.err.println("ERROR: OpenOfficeTopLinkApp.main(): Could not connect to database.");
            t.printStackTrace();
        }
        try {
            // get the remote office component context
            XComponentContext xContext = Bootstrap.bootstrap();
            if (xContext == null) {
                System.err.println("ERROR: OpenOfficeTopLinkApp.main(): Could not bootstrap default Office.");
            } else {
                System.out.println("TRACE: OpenOfficeTopLinkApp.main(): Successfully connected to a running Office ...");
            }
        } catch (Throwable t){
            t.printStackTrace();
        }
        finally {
            System.exit( 0 );
        }
    } // main()

    private static void displayResult(List resultList) {
        Vector tableData = new Vector();
        try {
            for(Object o : resultList) {
                Person person = (Person)o;
                StringBuffer sbOneRow = new StringBuffer();
                sbOneRow.append(person.getPersonid()+" is: ");
                sbOneRow.append(person.getName()+", ");
                sbOneRow.append(person.getJobtitle()+", ");
                sbOneRow.append(person.getFrequentflyer()+", ");
                sbOneRow.append(person.getLastupdated()+";");
                tableData.add(sbOneRow);
                System.out.println("TRACE: OpenOfficeTopLinkApp.displayResult(): The return value for record "+sbOneRow.toString());
            }
            System.out.println("\n");
        } catch (Throwable t){
            System.out.println("ERROR: OpenOfficeTopLinkApp.displayResult(): "+t);
        }
    } // displayResult()

} // class OpenOfficeTopLinkApp


Last edited by jtsdata on Sat Feb 14, 2009 9:28 pm; edited 3 times in total
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Sat Feb 07, 2009 5:39 pm    Post subject: Reply with quote

Hi,

at least for BeanShell, there was a regression in the classloader between OO 2.0 and OO 2.4. ( http://qa.openoffice.org/issues/show_bug.cgi?id=89978 ).

I am just guessing that this could be the same problem. A workaround is to add
Code:
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
to each function as the first statement.

Good luck,

ms777
Back to top
View user's profile Send private message
jtsdata
General User
General User


Joined: 06 Feb 2009
Posts: 9
Location: Sydney

PostPosted: Sun Feb 08, 2009 1:45 am    Post subject: Integrating OO UNO App with java persistence libraries Reply with quote

ms777, tank you very much for yor prompt response!

The workaround suggested by you worked with some minor modification. I just added the following line in the body of the main() method of the Loader.java class generated by the Netbeans 6.5 OpenOffice.org Client Application plugin:

Code:

package com.sun.star.lib.loader;

public final class Loader {
.....
    public static void main( String[] arguments ) throws Exception {
.....
        // load the class with the customized class loader and
        // invoke the main method
        if ( className != null ) {
            ClassLoader cl = getCustomLoader();
       Thread.currentThread().setContextClassLoader(cl); // WORKAROUND: http://www.oooforum.org/forum/viewtopic.phtml?p=313538#313538
            Class c = cl.loadClass( className );
            Method m = c.getMethod( "main", new Class[] { String[].class } );
            m.invoke( null, new Object[] { args } );
        }
    }
.....
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Sun Feb 08, 2009 8:57 am    Post subject: Reply with quote

Good to hear !

Can you create an own QA issue or vote for mine with the comment that a similar error occurs also for 'normal' (=non-beanshell) java usage ?

The current target 3.X means that they will never take care.
Back to top
View user's profile Send private message
jtsdata
General User
General User


Joined: 06 Feb 2009
Posts: 9
Location: Sydney

PostPosted: Sun Feb 08, 2009 3:07 pm    Post subject: Reply with quote

Where can I vote? Please send me a link - I can't find it.
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Sun Feb 08, 2009 3:16 pm    Post subject: Reply with quote

go to http://qa.openoffice.org/issues/show_bug.cgi?id=89978 - in the upper right you find a button to register yourself. After registering, you should be able to vote
Back to top
View user's profile Send private message
jtsdata
General User
General User


Joined: 06 Feb 2009
Posts: 9
Location: Sydney

PostPosted: Sun Feb 08, 2009 3:38 pm    Post subject: Reply with quote

The help page on voting says: "Click on the "Vote for this issue" link just above the "Additional Comments" field. (If no such link appears, then voting may not be allowed for this issue's project/component.)"

I did register, but still the "Vote for this issue" link does NOT appear on your issue page...
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Mon Mar 02, 2009 3:06 pm    Post subject: Reply with quote

... then register as cc for that issue and post an medium unfriendly comment ...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group