A working XPCom component made in Java

Murilo Camões Perrone <[email protected]>
Newsgroups gmane.comp.mozilla.devel.java
Message-ID <[email protected]>
Now I have a ready and working solution. An XPCom component implemented in Java, which externally uses a bit of JavaScript.
 
As described on my last message, I tried to instantiate the java classes by javaConnect´s ComponentManager.
Later, I discovered the problem was that XPIDL interface could not match exactly the java interfaces, so there should be a translation.
The differences are that java methods start with lower letters and the properties are accessed by get/set methods.
 
In my solution, I created one JavaScript module to wrap each Java class and relate each java method to the expected interface attribute/method, making the Java-to-XPIDL translation.
Maybe this translation could be done generically, by changing the JavaClassLoader package from Java Firefox Extension.
 
Any further suggestions?
 
----------------
Murilo Perrone
CertiSign
 
-----Mensagem original-----
De: [email protected] [mailto:[email protected]]Em nome de Murilo Camões Perrone
Enviada em: terça-feira, 10 de janeiro de 2006 15:44
Para: Mozilla-Java
Assunto: RES: JavaConnect and JavaScript
 
Javier Pedemonte,
 
Your example (below) is nice way of using the Java code from mozilla.
 
nsIServiceManager sm = Mozilla.getServiceManager();
nsIWindowWatcher watcher = (nsIWindowWatcher) sm
.getServiceByContractID(NS_WINDOWWATCHER_CONTRACTID,
nsIWindowWatcher.NS_IWINDOWWATCHER_IID);
watcher.setWindowCreate(WindowCreator);

Though, I need to entirelly use the XPIDL interface of my XPCom component from withing JavaScript code. 
Currently I am being able to instantiate Java objects from JavaScript as XPCom objects implementing XPIDL interfaces.
 
JavaScript test code:
 
    var certs = Components.classes[javaXPComComponentID].getService(Components.interfaces.ICertificates);
 
    alert( "certs = " + certs ); // certs = [xpconnect wrapped (nsISupports, ICertificates)]
    alert( "certs.Count = " + certs.Count ); // certs.Count = 0
    alert( "certs.Clear = " + certs.Clear ); // certs.Clear = function Clear() { [native code] }
    certs.Clear(); // exception is thrown with code NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED
 
So I could sucessfully access the attributes defined on XPIDL interface with no get/set prefix, but there is a problem when calling the methods (which are also described on the XPIDL interface).
 
I followed the steps in Java Firefox Extension example, except that I added a shortcut so that the java object will be returned directly from Mozilla´s Component Manager, and thus used by JavaScript as defined on the XPIDL interface (such as ICertificates.xpt).
 
In my JavaScript component, the factory´s createInstance method returns new Java object instances. First, it loads the class from a “package.classname” string, then instantiates it calling the constructor.
 
To avoid the NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED error, I will try to add an extra step in the process of loading the Java class.
I will use the JavaConnect´s XPCOM class instead of just calling the class constructor.
 
nsIComponentManager componentManager = XPCOM.getComponentManager();
componentManager.createInstanceByContractID( ICERTIFICATES_CONTRACTID, null, ICERTIFICATES_IID);
 
Then XPConnect AND JavaConnect would be both used together.
 
The two instructions above could be used in different ways:
*        from within Java code, returning an nsISupports object
*        from within JavaScript, loading XPCOM class
*        maybe even from C++ code (if that would be usefull), using JavaConnect´s DLL function entries
 
Makes sence?
 
Also, from were can I download updated JavaConnect sources?
 
The XulRunner nighty zip from the link below does not include it.
 
http://ftp.mozilla.org/pub/mozilla.org/xulrunner/nightly/latest-mozilla1.8/
 
----------------
Murilo Perrone
CertiSign
smime.p7s (application/x-pkcs7-signature, 3.9 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.