Author: mturk
Date: Sat Sep 12 17:24:31 2009
New Revision: 814204
URL: http://svn.apache.org/viewvc?rev=814204&view=rev
Log:
Add skeleton java.util.Observer support
Added:
commons/sandbox/runtime/trunk/src/main/native/include/acr_observer.h
(with props)
commons/sandbox/runtime/trunk/src/main/native/shared/observer.c (with
props)
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestObserver.java
(with props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/Makefile.in
commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h
commons/sandbox/runtime/trunk/src/main/native/shared/callback.c
commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=814204&r1=814203&r2=814204&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Sat Sep 12
17:24:31 2009
@@ -99,6 +99,7 @@
$(SRCDIR)/shared/native.$(OBJ) \
$(SRCDIR)/shared/nbb.$(OBJ) \
$(SRCDIR)/shared/pointer.$(OBJ) \
+ $(SRCDIR)/shared/observer.$(OBJ) \
$(SRCDIR)/shared/object.$(OBJ) \
$(SRCDIR)/shared/base64.$(OBJ) \
$(SRCDIR)/shared/md5.$(OBJ) \
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=814204&r1=814203&r2=814204&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Sat Sep 12
17:24:31 2009
@@ -89,6 +89,7 @@
$(SRCDIR)/shared/native.$(OBJ) \
$(SRCDIR)/shared/nbb.$(OBJ) \
$(SRCDIR)/shared/pointer.$(OBJ) \
+ $(SRCDIR)/shared/observer.$(OBJ) \
$(SRCDIR)/shared/object.$(OBJ) \
$(SRCDIR)/shared/base64.$(OBJ) \
$(SRCDIR)/shared/md5.$(OBJ) \
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h?rev=814204&r1=814203&r2=814204&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h Sat
Sep 12 17:24:31 2009
@@ -63,7 +63,7 @@
* The handler function must check for data validity.
* Consecutive invocations will always contain NULL data.
*/
-typedef int (acr_descriptor_handler_fn_t)(JNIEnv *, void *,
+typedef int (acr_descriptor_handler_fn_t)(JNIEnv *, jobject,
acr_descriptor_cb_type_e,
acr_descriptor_cb_t *);
Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_observer.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_observer.h?rev=814204&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_observer.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_observer.h Sat
Sep 12 17:24:31 2009
@@ -0,0 +1,50 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACR_OBSERVER_H
+#define _ACR_OBSERVER_H
+
+#include "acr.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @file acr_observer.h
+ * @brief
+ *
+ * ACR java.util.Observer support.
+ *
+ */
+
+/**
+ * Run the callback.
+ * @param env Current JNI environment
+ * @param thiz Instance of the java.util.Observer object.
+ * @param o Instance of the java.util.Observable object.
+ * @param arg Argument passed to the notifyObservers method.
+ * @return If the update callback throws exception ACR_EGENERAL is
+ * returned.
+*/
+ACR_DECLARE(int) ACR_ObserverUpdate(JNIEnv *env, jobject thiz,
+ jobject o, jobject arg);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ACR_OBSERVER_H */
Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr_observer.h
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/callback.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/callback.c?rev=814204&r1=814203&r2=814204&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/callback.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/callback.c Sat Sep 12
17:24:31 2009
@@ -25,9 +25,7 @@
#include "acr_clazz.h"
#include "acr_memory.h"
#include "acr_error.h"
-#include "acr_descriptor.h"
#include "acr_object.h"
-#include "acr_pointer.h"
#include "acr_callback.h"
#include "acr_tlsd.h"
#include "acr_vm.h"
@@ -141,6 +139,8 @@
/* Get JNIEnv only if needed
*/
_E = ACR_GetJNIEnv();
+ if (IS_INVALID_HANDLE(_E))
+ return ACR_ENOJNIENV;
}
if (cb->handler) {
/* This is native callback
@@ -159,9 +159,10 @@
rc = ACR_SUCCESS;
}
if (cb->thiz == NULL) {
+ /* Either invalid or already handled */
return rc;
}
- if (IS_VALID_HANDLE(_E)) {
+ else {
jobject d = NULL;
jobject o = (*_E)->NewLocalRef(_E, cb->thiz);
if (o == NULL) {
@@ -195,140 +196,4 @@
return 0;
}
- else {
- /* Failed obtaining JNIEnv.
- */
- return ACR_ENOJNIENV;
- }
-}
-
-#ifdef ACR_ENABLE_TEST
-ACR_JNI_EXPORT_DECLARE(int, TestCallback, test00)(ACR_JNISTDARGS, jobject o)
-{
- acr_callback_t *cb;
-
- if ((cb = ACR_CallbackAttach(_E, o, NULL, 0, ACR_CALLBACK_SYNC, NULL))) {
- int i;
- fputc('\n', stdout);
- for (i = 0; i < 10; i++) {
- int rv;
- int rc;
-
- rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
- if (rc) {
- fprintf(stderr, "CallbackRun failed with %d\n", rc);
- fflush(stderr);
- break;
- }
- else {
- fprintf(stdout, "CallbackRun (%d) return %d\n", i, rv);
- fflush(stdout);
- if (rv < 0)
- break;
- }
- }
- return ACR_CallbackFree(NULL, cb);
- }
- return 1;
}
-
-static int test_handler(JNIEnv *_E, acr_callback_t *cb,
- void **ctx, int val)
-{
- if (val) {
- char buf[32];
- sprintf(buf, "Native object %d", val);
- *ctx = (*_E)->NewStringUTF(_E, buf);
- }
- return 0;
-}
-
-ACR_JNI_EXPORT_DECLARE(int, TestCallback, test01)(ACR_JNISTDARGS, jint d)
-{
- acr_callback_t *cb;
-
- if ((cb = ACR_CallbackCreate(_E, NULL, 0, ACR_CALLBACK_SYNC,
test_handler))) {
- int i;
- fputc('\n', stdout);
- for (i = 0; i < 5; i++) {
- int rv;
- int rc;
-
- rc = ACR_CallbackRun(NULL, cb, NULL, 0, &rv);
- if (rc) {
- fprintf(stderr, "CallbackRun01 failed with %d\n", rc);
- fflush(stderr);
- break;
- }
- else {
- fprintf(stdout, "[native] CallbackRun01 (%d) return %d\n", i,
rv);
- fflush(stdout);
- if (rv < 0)
- break;
- }
- }
- return ACR_CallbackFree(NULL, cb);
- }
- return 1;
-}
-
-ACR_JNI_EXPORT_DECLARE(int, TestCallback, test02)(ACR_JNISTDARGS, jobject o,
- jobject d)
-{
- acr_callback_t *cb;
-
- if ((cb = ACR_CallbackAttach(_E, o, d, 0, ACR_CALLBACK_SYNC, NULL))) {
- int i;
- fputc('\n', stdout);
- for (i = 0; i < 10; i++) {
- int rv;
- int rc;
-
- rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
- if (rc) {
- fprintf(stderr, "CallbackRun02 failed with %d\n", rc);
- fflush(stderr);
- break;
- }
- else {
- fprintf(stdout, "[native] CallbackRun02 (%d) return %d\n", i,
rv);
- fflush(stdout);
- if (rv < 0)
- break;
- }
- }
- return ACR_CallbackFree(NULL, cb);
- }
- return 1;
-}
-
-ACR_JNI_EXPORT_DECLARE(int, TestCallback, test03)(ACR_JNISTDARGS, jobject o)
-{
- acr_callback_t *cb;
-
- if ((cb = ACR_CallbackAttach(_E, o, NULL, 0, ACR_CALLBACK_SYNC,
test_handler))) {
- int i;
- fputc('\n', stdout);
- for (i = 0; i < 10; i++) {
- int rv;
- int rc;
-
- rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
- if (rc) {
- fprintf(stderr, "CallbackRun03 failed with %d\n", rc);
- fflush(stderr);
- break;
- }
- else {
- fprintf(stdout, "[native] CallbackRun03 (%d) return %d\n", i,
rv);
- fflush(stdout);
- if (rv < 0)
- break;
- }
- }
- return ACR_CallbackFree(NULL, cb);
- }
- return 1;
-}
-
-#endif
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=814204&r1=814203&r2=814204&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Sat Sep 12
17:24:31 2009
@@ -187,6 +187,7 @@
}
/* Core classes forward declarations */
+ACR_CLASS_LDEF(Observer);
ACR_CLASS_LDEF(Object);
ACR_CLASS_LDEF(String);
@@ -224,6 +225,7 @@
ACR_CLASS_LRUN(Object);
ACR_CLASS_LRUN(Class);
ACR_CLASS_LRUN(String);
+ ACR_CLASS_LRUN(Observer);
return ACR_SUCCESS;
}
Added: commons/sandbox/runtime/trunk/src/main/native/shared/observer.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/observer.c?rev=814204&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/observer.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/observer.c Sat Sep 12
17:24:31 2009
@@ -0,0 +1,73 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ *
+ * @author Mladen Turk
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_clazz.h"
+#include "acr_error.h"
+#include "acr_object.h"
+#include "acr_observer.h"
+#include "acr_vm.h"
+
+J_DECLARE_CLAZZ = {
+ NULL,
+ NULL,
+ "java/util/Observer"
+};
+
+J_DECLARE_M_ID(0000) = {
+ NULL,
+ "update",
+ "(Ljava/util/Observable;Ljava/lang/Object;)V"
+};
+
+ACR_CLASS_LDEF(Observer)
+{
+ int rv;
+
+ if ((rv = ACR_LoadClass(_E, &_clazzn, 0)) != ACR_SUCCESS)
+ return rv;
+ J_LOAD_METHOD(0000);
+
+ return ACR_SUCCESS;
+}
+
+ACR_CLASS_UDEF(Observer)
+{
+ ACR_UnloadClass(_E, &_clazzn);
+}
+
+ACR_DECLARE(int) ACR_ObserverUpdate(ACR_JNISTDARGS, jobject o, jobject arg)
+{
+ if (_O == NULL)
+ return ACR_EINVAL;
+ if (_E == NULL)
+ _E = ACR_GetJNIEnv();
+ if (IS_INVALID_HANDLE(_E))
+ return ACR_ENOJNIENV;
+ CALL_VMETHOD2(0000, _O, o, arg);
+ if ((*_E)->ExceptionCheck(_E) == JNI_TRUE) {
+ (*_E)->ExceptionClear(_E);
+ return ACR_EGENERAL;
+ }
+ return ACR_SUCCESS;
+}
Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/observer.c
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=814204&r1=814203&r2=814204&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Sat Sep 12
17:24:31 2009
@@ -32,6 +32,8 @@
#include "acr_shm.h"
#include "acr_procmutex.h"
#include "acr_semaphore.h"
+#include "acr_callback.h"
+#include "acr_observer.h"
#if defined (WIN32)
#include <io.h>
@@ -911,3 +913,145 @@
return ACR_SemaphoreRemove(_E, sem_name);
}
+ACR_JNI_EXPORT_DECLARE(int, TestCallback, test00)(ACR_JNISTDARGS, jobject o)
+{
+ acr_callback_t *cb;
+
+ if ((cb = ACR_CallbackAttach(_E, o, NULL, 0, ACR_CALLBACK_SYNC, NULL))) {
+ int i;
+ fputc('\n', stdout);
+ for (i = 0; i < 10; i++) {
+ int rv;
+ int rc;
+
+ rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
+ if (rc) {
+ fprintf(stderr, "CallbackRun failed with %d\n", rc);
+ fflush(stderr);
+ break;
+ }
+ else {
+ fprintf(stdout, "CallbackRun (%d) return %d\n", i, rv);
+ fflush(stdout);
+ if (rv < 0)
+ break;
+ }
+ }
+ return ACR_CallbackFree(NULL, cb);
+ }
+ return 1;
+}
+
+static int test_handler(JNIEnv *_E, acr_callback_t *cb,
+ void **ctx, int val)
+{
+ if (val) {
+ char buf[32];
+ sprintf(buf, "Native object %d", val);
+ *ctx = (*_E)->NewStringUTF(_E, buf);
+ }
+ return 0;
+}
+
+ACR_JNI_EXPORT_DECLARE(int, TestCallback, test01)(ACR_JNISTDARGS, jint d)
+{
+ acr_callback_t *cb;
+
+ if ((cb = ACR_CallbackCreate(_E, NULL, 0, ACR_CALLBACK_SYNC,
test_handler))) {
+ int i;
+ fputc('\n', stdout);
+ for (i = 0; i < 5; i++) {
+ int rv;
+ int rc;
+
+ rc = ACR_CallbackRun(NULL, cb, NULL, 0, &rv);
+ if (rc) {
+ fprintf(stderr, "CallbackRun01 failed with %d\n", rc);
+ fflush(stderr);
+ break;
+ }
+ else {
+ fprintf(stdout, "[native] CallbackRun01 (%d) return %d\n", i,
rv);
+ fflush(stdout);
+ if (rv < 0)
+ break;
+ }
+ }
+ return ACR_CallbackFree(NULL, cb);
+ }
+ return 1;
+}
+
+ACR_JNI_EXPORT_DECLARE(int, TestCallback, test02)(ACR_JNISTDARGS, jobject o,
+ jobject d)
+{
+ acr_callback_t *cb;
+
+ if ((cb = ACR_CallbackAttach(_E, o, d, 0, ACR_CALLBACK_SYNC, NULL))) {
+ int i;
+ fputc('\n', stdout);
+ for (i = 0; i < 10; i++) {
+ int rv;
+ int rc;
+
+ rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
+ if (rc) {
+ fprintf(stderr, "CallbackRun02 failed with %d\n", rc);
+ fflush(stderr);
+ break;
+ }
+ else {
+ fprintf(stdout, "[native] CallbackRun02 (%d) return %d\n", i,
rv);
+ fflush(stdout);
+ if (rv < 0)
+ break;
+ }
+ }
+ return ACR_CallbackFree(NULL, cb);
+ }
+ return 1;
+}
+
+ACR_JNI_EXPORT_DECLARE(int, TestCallback, test03)(ACR_JNISTDARGS, jobject o)
+{
+ acr_callback_t *cb;
+
+ if ((cb = ACR_CallbackAttach(_E, o, NULL, 0, ACR_CALLBACK_SYNC,
test_handler))) {
+ int i;
+ fputc('\n', stdout);
+ for (i = 0; i < 10; i++) {
+ int rv;
+ int rc;
+
+ rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
+ if (rc) {
+ fprintf(stderr, "CallbackRun03 failed with %d\n", rc);
+ fflush(stderr);
+ break;
+ }
+ else {
+ fprintf(stdout, "[native] CallbackRun03 (%d) return %d\n", i,
rv);
+ fflush(stdout);
+ if (rv < 0)
+ break;
+ }
+ }
+ return ACR_CallbackFree(NULL, cb);
+ }
+ return 1;
+}
+
+ACR_JNI_EXPORT_DECLARE(int, TestObserver, test00)(ACR_JNISTDARGS, jobject o)
+{
+ int i;
+ for (i = 0; i < 10; i++) {
+ int rc;
+ rc = ACR_ObserverUpdate(_E, o, NULL, NULL);
+ if (rc) {
+ fprintf(stderr, "CallbackRun failed with %d\n", rc);
+ fflush(stderr);
+ return 1;
+ }
+ }
+ return 0;
+}
Modified:
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=814204&r1=814203&r2=814204&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
(original)
+++
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
Sat Sep 12 17:24:31 2009
@@ -48,6 +48,7 @@
suite.addTest(TestSharedMemory.suite());
suite.addTest(TestMemoryMap.suite());
suite.addTest(TestCallback.suite());
+ suite.addTest(TestObserver.suite());
return suite;
}
Added:
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestObserver.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestObserver.java?rev=814204&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestObserver.java
(added)
+++
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestObserver.java
Sat Sep 12 17:24:31 2009
@@ -0,0 +1,65 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import org.apache.commons.runtime.exception.*;
+import junit.framework.*;
+import java.util.Observer;
+import java.util.Observable;
+
+/**
+ * Callback tests
+ */
+public class TestObserver extends TestCase
+{
+
+ private static native int test00(Object o);
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(TestObserver.class);
+ return suite;
+ }
+
+ class MyObserver implements Observer {
+ public int val;
+ public MyObserver()
+ {
+ val = 0;
+ }
+ public void update(Observable o, Object arg)
+ {
+ val++;
+ }
+ }
+
+ protected void setUp()
+ throws Exception
+ {
+ System.loadLibrary("acr");
+ }
+
+ public void testSimpleUpdate()
+ throws Exception
+ {
+ MyObserver o = new MyObserver();
+ int rc = test00(o);
+ assertEquals("Update result", 0, rc);
+ assertEquals("Update called", 10, o.val);
+ }
+
+}
+
Propchange:
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestObserver.java
------------------------------------------------------------------------------
svn:eol-style = native