* NOT PART OF TBOX BUILDS *

Fixed bug with [out] parametrs in java.
Improved component registration.
Added test for accessing ComponentManager from java


git-svn-id: svn://10.0.0.236/trunk@79594 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
idk%eng.sun.com
2000-09-20 01:32:12 +00:00
parent bd843ec86f
commit 3ae85a8a7a
17 changed files with 337 additions and 49 deletions

View File

@@ -51,10 +51,9 @@ public class ComponentLoader {
//nb
return null;
}
Object object = loader.loadClass(componentClassName).newInstance();
if (object instanceof nsISupports) {
InterfaceRegistry.register((nsISupports)object);
}
Class component = loader.loadClass(componentClassName);
InterfaceRegistry.register(component);
Object object = component.newInstance();
return object;
} catch (Exception e) {
e.printStackTrace();

View File

@@ -25,7 +25,7 @@ package org.mozilla.xpcom;
import java.util.Hashtable;
import java.lang.reflect.*;
class InterfaceRegistry {
public class InterfaceRegistry {
private static String IID_STRING = "IID";
private static Hashtable interfaces = null;
@@ -39,13 +39,20 @@ class InterfaceRegistry {
if (obj == null) {
return;
}
Class cl = obj.getClass();
register(cl);
}
public static void register(Class cl) {
if (cl == null) {
return;
}
if (interfaces == null) {
interfaces = new Hashtable();
}
if (iMethods == null) {
iMethods = new Hashtable();
}
Class cl = obj.getClass();
if (!cl.isInterface()) {
Class[] ifaces = cl.getInterfaces();
for (int i = 0; i < ifaces.length; i++) {
@@ -56,12 +63,11 @@ class InterfaceRegistry {
}
}
public static void registerInterfaces(Class cl) {
private static void registerInterfaces(Class cl) {
try {
Object iidStr = cl.getField(IID_STRING).get(cl);
if (iidStr instanceof String) {
IID iid = new IID((String)iidStr);
ProxyFactory.registerInterfaceForIID(cl,iid);
String[] methodNames = Utilities.getInterfaceMethodNames((String)iidStr);
if (methodNames != null) {
Method[] rmethods = new Method[methodNames.length];
@@ -128,7 +134,7 @@ class InterfaceRegistry {
}
public static int getIndexByMethod(Method method, IID iid) {
int result = 0;
int result = -1;
MethodArray m = (MethodArray)iMethods.get(iid);
if (m != null && m.methods !=null) {
for (int i = 0; i < m.methods.length; i++) {

View File

@@ -47,28 +47,9 @@ class ProxyKey {
}
public class ProxyFactory {
public static void registerInterfaceForIID(Class inter, IID iid) {
System.out.println("--[java] ProxyFactory.registerInterfaceForIID "+iid);
if (interfaces == null) {
interfaces = new Hashtable();
}
interfaces.put(iid, inter); //nb who is gonna remove object from cache?
}
public static Class getInterface(IID iid) {
System.out.println("--[java] ProxyFactory.getInterface "+iid);
Object obj = null;
if (interfaces != null) {
obj = interfaces.get(iid);
if (obj == null) {
System.out.println("--[java] ProxyFactory.getInterface interface== null");
return null;
}
}
if (!(obj instanceof Class)) {
System.out.println("--[java] ProxyFactory.getInterface !(obj instanceof Class"+obj);
return null;
}
return (Class)obj;
return InterfaceRegistry.getInterface(iid);
}
public static Object getProxy(long oid, IID iid, long orb) {
@@ -104,7 +85,6 @@ public class ProxyFactory {
return null;
}
protected static Hashtable proxies = null;
private static Hashtable interfaces = null;
}

View File

@@ -33,7 +33,8 @@ class ProxyHandler implements InvocationHandler {
Method method,
Object[] args) throws Throwable {
System.out.println("--[java]ProxyHandler.invoke "+method);
if ("toString".equals(method.getName())) {
if ("toString".equals(method.getName()) &&
(method.getParameterTypes()).length == 0) { //it is String toString() method
return "ProxyObject@{oid = "+oid+" iid = "+iid+"}";
}
return Utilities.callMethod(oid, method, iid, orb, args);

View File

@@ -46,7 +46,7 @@ public class Utilities {
static Object callMethod(long oid, Method method, IID iid, long orb , Object[] args) {
System.out.println("--[java]Utilities.callMethod "+method);
int mid = InterfaceRegistry.getIndexByMethod(method, iid);
if (mid <= 0) {
if (mid < 0) {
System.out.println("--[java]Utilities.callMethod we do not have implementation for "+method);
return null;
}

View File

@@ -0,0 +1,96 @@
/*
* ************* DO NOT EDIT THIS FILE ***********
*
* This file was automatically generated from nsIComponentManager.idl.
*/
package org.mozilla.xpcom;
/**
* Interface nsIComponentManager
*
* IID: 0x8458a740-d5dc-11d2-92fb-00e09805570f
*/
public interface nsIComponentManager extends nsISupports
{
public static final String IID =
"8458a740-d5dc-11d2-92fb-00e09805570f";
/* nsIFactory findFactory (in nsCIDRef aClass); */
public void findFactory(IID aClass, nsIFactory[] _retval);
/* string CLSIDToContractID (in nsCIDRef aClass, out string aClassName); */
public void cLSIDToContractID(IID aClass, String[] aClassName, String[] _retval);
/* string registryLocationForSpec (in nsIFile aSpec); */
public void registryLocationForSpec(nsISupports aSpec, String[] _retval);
/* nsIFile specForRegistryLocation (in string aLocation); */
public void specForRegistryLocation(String aLocation, nsISupports[] _retval);
/* void registerFactory (in nsCIDRef aClass, in string aClassName, in string aContractID, in nsIFactory aFactory, in boolean aReplace); */
public void registerFactory(IID aClass, String aClassName, String aContractID, nsIFactory aFactory, boolean aReplace);
/* void registerComponent (in nsCIDRef aClass, in string aClassName, in string aContractID, in string aLocation, in boolean aReplace, in boolean aPersist); */
public void registerComponent(IID aClass, String aClassName, String aContractID, String aLocation, boolean aReplace, boolean aPersist);
/* void registerComponentWithType (in nsCIDRef aClass, in string aClassName, in string aContractID, in nsIFile aSpec, in string aLocation, in boolean aReplace, in boolean aPersist, in string aType); */
public void registerComponentWithType(IID aClass, String aClassName, String aContractID, nsISupports aSpec, String aLocation, boolean aReplace, boolean aPersist, String aType);
/* void registerComponentSpec (in nsCIDRef aClass, in string aClassName, in string aContractID, in nsIFile aLibrary, in boolean aReplace, in boolean aPersist); */
public void registerComponentSpec(IID aClass, String aClassName, String aContractID, nsISupports aLibrary, boolean aReplace, boolean aPersist);
/* void registerComponentLib (in nsCIDRef aClass, in string aClassName, in string aContractID, in string aDllName, in boolean aReplace, in boolean aPersist); */
public void registerComponentLib(IID aClass, String aClassName, String aContractID, String aDllName, boolean aReplace, boolean aPersist);
/* void unregisterFactory (in nsCIDRef aClass, in nsIFactory aFactory); */
public void unregisterFactory(IID aClass, nsIFactory aFactory);
/* void unregisterComponent (in nsCIDRef aClass, in string aLocation); */
public void unregisterComponent(IID aClass, String aLocation);
/* void unregisterComponentSpec (in nsCIDRef aClass, in nsIFile aLibrarySpec); */
public void unregisterComponentSpec(IID aClass, nsISupports aLibrarySpec);
/* void freeLibraries (); */
public void freeLibraries();
/* const long NS_Startup = 0; */
public static final int NS_Startup = 0;
/* const long NS_Script = 1; */
public static final int NS_Script = 1;
/* const long NS_Timer = 2; */
public static final int NS_Timer = 2;
/* const long NS_Shutdown = 3; */
public static final int NS_Shutdown = 3;
/* void autoRegister (in long when, in nsIFile directory); */
public void autoRegister(int when, nsISupports directory);
/* void autoRegisterComponent (in long when, in nsIFile aFileLocation); */
public void autoRegisterComponent(int when, nsISupports aFileLocation);
/* void autoUnregisterComponent (in long when, in nsIFile aFileLocation); */
public void autoUnregisterComponent(int when, nsISupports aFileLocation);
/* boolean isRegistered (in nsCIDRef aClass); */
public void isRegistered(IID aClass, boolean[] _retval);
/* nsIEnumerator enumerateCLSIDs (); */
public void enumerateCLSIDs(nsIEnumerator[] _retval);
/* nsIEnumerator enumerateContractIDs (); */
public void enumerateContractIDs(nsIEnumerator[] _retval);
}
/*
* end
*/

View File

@@ -0,0 +1,39 @@
/*
* ************* DO NOT EDIT THIS FILE ***********
*
* This file was automatically generated from nsIEnumerator.idl.
*/
package org.mozilla.xpcom;
/**
* Interface nsIEnumerator
*
* IID: 0xad385286-cbc4-11d2-8cca-0060b0fc14a3
*/
public interface nsIEnumerator extends nsISupports
{
public static final String IID =
"ad385286-cbc4-11d2-8cca-0060b0fc14a3";
/* void first (); */
public void first();
/* void next (); */
public void next();
/* nsISupports currentItem (); */
public void currentItem(nsISupports[] _retval);
/* void isDone (); */
public void isDone();
}
/*
* end
*/

View File

@@ -0,0 +1,33 @@
/*
* ************* DO NOT EDIT THIS FILE ***********
*
* This file was automatically generated from nsIFactory.idl.
*/
package org.mozilla.xpcom;
/**
* Interface nsIFactory
*
* IID: 0x00000001-0000-0000-c000-000000000046
*/
public interface nsIFactory extends nsISupports
{
public static final String IID =
"00000001-0000-0000-c000-000000000046";
/* void createInstance (in nsISupports aOuter, in nsIIDRef iid, [iid_is (iid), retval] out nsQIResult result); */
public void createInstance(nsISupports aOuter, IID iid, Object[] result);
/* void lockFactory (in PRBool lock); */
public void lockFactory(boolean lock);
}
/*
* end
*/

View File

@@ -21,7 +21,7 @@ public interface nsISupports
/* void QueryInterface (in nsIIDRef uuid, [iid_is (uuid), retval] out nsQIResult result); */
public void queryInterface(IID uuid, Object[] obj);
public void queryInterface(IID uuid, Object[] result);
}

View File

@@ -0,0 +1,30 @@
/*
* ************* DO NOT EDIT THIS FILE ***********
*
* This file was automatically generated from nsISupportsString.idl.
*/
package org.mozilla.xpcom;
/**
* Interface nsISupportsString
*
* IID: 0xd65ff270-4a1c-11d3-9890-006008962422
*/
public interface nsISupportsString extends nsISupports
{
public static final String IID =
"d65ff270-4a1c-11d3-9890-006008962422";
/* string toString (); */
public void toString(String[] _retval);
}
/*
* end
*/

View File

@@ -155,6 +155,9 @@ nsresult bcJavaMarshalToolkit::UnMarshal(bcIUnMarshaler *um) {
}
continue;
}
if (isOut) {
value = env->GetObjectArrayElement(args,i);
}
UnMarshalElement(&value, i, um, isOut, &param, XPTType2bcXPType(type.TagPart()),allocator);
env->SetObjectArrayElement(args,i,value);
}
@@ -355,7 +358,7 @@ bcJavaMarshalToolkit::MarshalElement(bcIMarshaler *m, jobject value, PRBool isO
*value = env->NewObject(_type_##Class,_type_##InitMID,data); \
} else if (isOut && (modifier == array)) { \
*value = env->NewObjectArray(1, _type_##ArrayClass, NULL); \
} else if (isOut \
} else if ( (isOut && callSide == onServer) \
|| (modifier == array)) { \
int arraySize; \
arraySize = (modifier == array) ? ind : 1; \
@@ -434,7 +437,7 @@ bcJavaMarshalToolkit::UnMarshalElement(jobject *value, uint8 ind, bcIUnMarshaler
*value = data;
} else if (isOut && (modifier == array)) {
*value = env->NewObjectArray(1, stringArrayClass, NULL);
} else if (isOut
} else if ( (isOut && callSide == onServer)
|| (modifier == array)) {
int arraySize;
arraySize = (modifier == array) ? ind : 1;
@@ -462,7 +465,7 @@ bcJavaMarshalToolkit::UnMarshalElement(jobject *value, uint8 ind, bcIUnMarshaler
*value = data;
} else if (isOut && (modifier == array)) {
*value = env->NewObjectArray(1, iidArrayClass, NULL);
} else if (isOut
} else if ( (isOut && callSide == onServer)
|| (modifier == array)) {
int arraySize;
arraySize = (modifier == array) ? ind : 1;
@@ -504,12 +507,12 @@ bcJavaMarshalToolkit::UnMarshalElement(jobject *value, uint8 ind, bcIUnMarshaler
if ( ! isOut
&& (modifier == none) ) {
*value = data;
} else if (isOut && (modifier == array)) { //how to create type[][] ?
} else if ( isOut && (modifier == array)) { //we are creating type[][]
jobject arrayObject;
arrayObject = env->NewObjectArray(1,clazz,NULL);
jclass arrayClass = (jclass) env->CallObjectMethod(arrayObject,getClassMID); //nb how to to it better ?
*value = env->NewObjectArray(1, arrayClass, NULL);
} else if (isOut
} else if ( (isOut && callSide == onServer)
|| (modifier == array)) {
int arraySize;
arraySize = (modifier == array) ? ind : 1;
@@ -531,7 +534,7 @@ bcJavaMarshalToolkit::UnMarshalElement(jobject *value, uint8 ind, bcIUnMarshaler
return NS_ERROR_FAILURE;
}
bcXPType type = XPTType2bcXPType(datumType.TagPart());
if (isOut) {
if (isOut && callSide == onServer) {
UnMarshalElement(value,ind,NULL,isOut,param,type,allocator,array);
}
if (um != NULL) {

View File

@@ -128,6 +128,7 @@ void bcJavaStubsAndProxies::Init(void) {
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
componentLoader = 0;
printf("--Did you set CLASSPATH correctly\n");
return;
}
componentLoader = (jclass)env->NewGlobalRef(componentLoader);

View File

@@ -41,7 +41,7 @@ bcJavaSample.jar.comp: manifest bcIJavaSample.class bcJavaSample.class
$(JDKHOME)/bin/jar cvfm bcJavaSample.jar.comp manifest *.class
.java.class:
$(JDKHOME)/bin/javac -classpath .:../classes $<
install-component: bcJavaSample.jar.comp bcJavaSample.jar.info
install-component: bcJavaSample.jar.comp bcJavaSample.jar.info
cp bcJavaSample.jar.comp bcJavaSample.jar.info $(DEPTH)/dist/bin/components/
clobber-java:

View File

@@ -1,4 +1,5 @@
#include "nsISupports.idl"
#include "nsIComponentManager.idl"
[scriptable, uuid(ca1e2656-1dd1-11b2-9c4e-f49ea557abde)]
interface bcIJavaSample : nsISupports
@@ -8,5 +9,5 @@ interface bcIJavaSample : nsISupports
void test2(in bcIJavaSample o);
void test3(in PRUint32 count,[array, size_is(count)] in long valueArray);
void test4(in PRUint32 count,[array, size_is(count)] inout string valueArray);
void test5(in nsIComponentManager cm);
};

View File

@@ -1,19 +1,44 @@
/*
* ************* DO NOT EDIT THIS FILE ***********
*
* This file was automatically generated from bcIJavaSample.idl.
*/
import org.mozilla.xpcom.*;
/**
* Interface nsISample
* Interface bcIJavaSample
*
* IID: 0xca1e2656-1dd1-11b2-9c4e-f49ea557abde
*/
import org.mozilla.xpcom.*;
public interface bcIJavaSample extends nsISupports
{
public static final String IID =
"ca1e2656-1dd1-11b2-9c4e-f49ea557abde";
void queryInterface(IID iid, Object[] result);
void test0();
void test1(int l);
void test2(bcIJavaSample o);
void test3(int count, int[] valueArray);
void test4(int count, String[][] valueArray);
/* void test0 (); */
public void test0();
/* void test1 (in long l); */
public void test1(int l);
/* void test2 (in bcIJavaSample o); */
public void test2(bcIJavaSample o);
/* void test3 (in PRUint32 count, [array, size_is (count)] in long valueArray); */
public void test3(int count, int[] valueArray);
/* void test4 (in PRUint32 count, [array, size_is (count)] inout string valueArray); */
public void test4(int count, String[][] valueArray);
/* void test5 (in nsIComponentManager cm); */
public void test5(nsIComponentManager cm);
}
/*
* end
*/

View File

@@ -23,6 +23,7 @@
#include "nsIGenericFactory.h"
#include "nsIModule.h"
#include "nsIEnumerator.h"
#include "stdlib.h"
#define BC_JAVA_SAMPLE_CID \
@@ -93,6 +94,12 @@ NS_IMETHODIMP bcJavaSample::Test4(PRUint32 count, char ***valueArray) {
*valueArray = array;
return NS_OK;
}
/* void test5 (in nsIComponentManager cm); */
NS_IMETHODIMP bcJavaSample::Test5(nsIComponentManager *cm) {
return NS_OK;
}
void test() {
printf("--BlackConnect test start\n");
nsresult r;
@@ -130,6 +137,20 @@ void test() {
printf("valueArray2[%d]=%s\n",i,(*valueArray2)[i]);
}
}
{
nsIComponentManager* cm;
nsresult rv = NS_GetGlobalComponentManager(&cm);
printf("--[c++] bcJavaSample before test->Test5(cm)\n");
test->Test5(cm);
nsIEnumerator *enumerator;
rv = cm->EnumerateCLSIDs(&enumerator);
if (NS_FAILED(rv)) {
printf("--[c++] can get enumerator\n");
}
printf("--[c++] bcJavaSample after test->Test5(cm)\n");
}
printf("--BlackConnect test end\n");
}

View File

@@ -73,8 +73,61 @@ public class bcJavaSample implements bcIJavaSample {
String[] returnArray = {"4","3","2","1"};
valueArray[0] = returnArray;
}
/* void test5 (in nsIComponentManager cm); */
public void test5(nsIComponentManager cm) {
System.out.println("--[java]bcJavaSample.test5");
try {
nsIEnumerator [] retval = new nsIEnumerator[1];
cm.enumerateContractIDs(retval);
nsIEnumerator enumerator = retval[0];
System.out.println("--[java] before calling enumerator.firts() "+
"enumerator==null "+(enumerator==null));
enumerator.first();
int counter = 0;
nsISupports obj[] = new nsISupports[1];
String str[] = new String[1];
nsISupportsString strObj[] = new nsISupportsString[1];
while (true) {
enumerator.currentItem(obj);
if (obj[0] == null ||
counter > 10) {
break;
}
obj[0].queryInterface(nsISupportsStringIID,strObj);
strObj[0].toString(str);
System.out.println("--[java] bcJavaSample.Test5 string "+str[0]);
enumerator.next(); counter++;
}
} catch (Exception e) {
System.out.println(e);
}
}
static IID bcIJavaSampleIID = new IID(bcIJavaSample.IID);
static IID nsISupportsIID = new IID(nsISupports.IID);
static IID nsIComponenstManagerIID = new IID(nsIComponentManager.IID);
static IID nsISupportsStringIID = new IID(nsISupportsString.IID);
static {
try {
Class nsIComponentManagerClass =
Class.forName("org.mozilla.xpcom.nsIComponentManager");
Class nsIEnumeratorClass =
Class.forName("org.mozilla.xpcom.nsIEnumerator");
Class nsISupportsStringClass =
Class.forName("org.mozilla.xpcom.nsISupportsString");
InterfaceRegistry.register(nsIComponentManagerClass);
InterfaceRegistry.register(nsIEnumeratorClass);
InterfaceRegistry.register(nsISupportsStringClass);
} catch (Exception e) {
System.out.println(e);
}
}
};