Add Registry class for storing information about java-c++

objects binding
This commit is contained in:
idk%eng.sun.com 1999-09-25 02:53:56 +00:00
parent 0e1d8c6019
commit dd3ecbe58a
7 changed files with 173 additions and 5 deletions

View File

@ -0,0 +1,45 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of the Original Code is Sun Microsystems,
* Inc. Portions created by Sun are Copyright (C) 1999 Sun Microsystems,
* Inc. All Rights Reserved.
*/
package org.mozilla.pluglet;
import java.util.*;
public class Registry {
static Hashtable table = null;
public static void setPeer(Object key,long peer) {
if (table == null) {
table = new Hashtable(10);
}
table.put(key, new Long(peer));
}
public static long getPeer(Object key) {
if (table == null) {
return 0;
}
Object obj = table.get(key);
if (obj == null) {
return 0;
} else {
return ((Long)obj).longValue();
}
}
public static void remove(Object key) {
if (table != null) {
table.remove(key);
}
}
}

View File

@ -49,7 +49,7 @@ nsresult Pluglet::CreatePluginInstance(const char* aPluginMIMEType, void **aResu
nsresult Pluglet::Initialize(void) {
JNIEnv *env = PlugletEngine::GetJNIEnv();
if(!env) {
return NS_ERROR_FAILURE;
return NS_ERROR_FAILURE;
}
if (!initializeMID) {
jclass clazz = env->FindClass("org/mozilla/pluglet/Pluglet");

View File

@ -1,4 +1,4 @@
/*
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
@ -17,7 +17,7 @@
#include "PlugletEngine.h"
#include "PlugletStreamListener.h"
#include "PlugletInstancePeer.h"
#include "Registry.h"
jmethodID PlugletInstance::initializeMID = NULL;
jmethodID PlugletInstance::startMID = NULL;
@ -37,9 +37,11 @@ PlugletInstance::PlugletInstance(jobject object) {
//nb check for null
peer = NULL;
view = new PlugletInstanceView();
Registry::SetPeer(jthis,(jlong)this);
}
PlugletInstance::~PlugletInstance() {
Registry::Remove(jthis);
PlugletEngine::GetJNIEnv()->DeleteGlobalRef(jthis);
NS_RELEASE(peer);
}

View File

@ -15,8 +15,8 @@
*/
#ifndef __PlugletInstance_h__
#define __PlugletInstance_h__
#include "jni.h"
#include "nsplugin.h"
#include "jni.h"
#include "PlugletInstanceView.h"
class PlugletInstance : public nsIPluginInstance {

View File

@ -0,0 +1,87 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of the Original Code is Sun Microsystems,
* Inc. Portions created by Sun are Copyright (C) 1999 Sun Microsystems,
* Inc. All Rights Reserved.
*/
#include "PlugletEngine.h"
#include "Registry.h"
jclass Registry::clazz = NULL;
jmethodID Registry::setPeerMID = NULL;
jmethodID Registry::removeMID = NULL;
void Registry::SetPeer(jobject key, jlong peer) {
if (!clazz) {
Initialize();
if(!clazz) {
return;
}
}
JNIEnv *env = PlugletEngine::GetJNIEnv();
env->CallStaticVoidMethod(clazz,setPeerMID,key,peer);
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
return;
}
}
void Registry::Remove(jobject key) {
if (!clazz) { // it is impossible
Initialize();
if(!clazz) {
return;
}
}
JNIEnv *env = PlugletEngine::GetJNIEnv();
env->CallStaticVoidMethod(clazz,removeMID,key);
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
return;
}
}
void Registry::Initialize() {
JNIEnv *env = PlugletEngine::GetJNIEnv();
if(!env) {
return;
}
clazz = env->FindClass("org/mozilla/pluglet/Registry");
if(!clazz) {
env->ExceptionDescribe();
return;
}
setPeerMID = env->GetStaticMethodID(clazz,"setPeer","(Ljava/lang/Object;J)V");
if (!setPeerMID) {
env->ExceptionDescribe();
clazz = NULL;
return;
}
removeMID = env->GetStaticMethodID(clazz,"remove","(Ljava/lang/Object;)V");
if (!removeMID) {
env->ExceptionDescribe();
clazz = NULL;
return;
}
}

View File

@ -0,0 +1,33 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of the Original Code is Sun Microsystems,
* Inc. Portions created by Sun are Copyright (C) 1999 Sun Microsystems,
* Inc. All Rights Reserved.
*/
#ifndef __Registry_h__
#define __Registry_h__
#include "jni.h"
class Registry {
public:
static void SetPeer(jobject key, jlong peer);
static void Remove(jobject key);
private:
static void Initialize();
static jclass clazz;
static jmethodID setPeerMID;
static jmethodID removeMID;
};
#endif /* __Registry_h__ */

View File

@ -57,7 +57,8 @@ OBJS = \
.\$(OBJDIR)\PlugletInstancePeer.obj \
.\$(OBJDIR)\PlugletManager.obj \
.\$(OBJDIR)\PlugletInputStream.obj \
.\$(OBJDIR)\PlugletInstanceView.obj
.\$(OBJDIR)\PlugletInstanceView.obj \
.\$(OBJDIR)\Registry.obj
MAKE_OBJ_TYPE = DLL