1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997, 1998
* Sleepycat Software. All rights reserved.
*/
#include "config.h"
#ifndef lint
static const char sccsid[] = "@(#)java_util.cpp 10.13 (Sleepycat) 10/28/98";
#endif /* not lint */
#include "java_util.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#define sys_errlist _sys_errlist
#define sys_nerr _sys_nerr
#endif
/****************************************************************
*
* Utility functions used by "glue" functions.
*
*/
/* Use our own malloc for any objects allocated via DB_DBT_MALLOC,
* since we must free them in the same library address space.
*/
extern "C"
void * java_db_malloc(size_t size)
{
return malloc(size);
}
/* Get the private data from a Db* object as a (64 bit) java long.
*/
jlong get_private_long_info(JNIEnv *jnienv, const char *classname,
jobject obj)
{
if (!obj)
return 0;
jclass dbClass = get_class(jnienv, classname);
jfieldID id = jnienv->GetFieldID(dbClass, "private_info_", "J");
return jnienv->GetLongField(obj, id);
}
/* Get the private data from a Db* object.
* The private data is stored in the object as a Java long (64 bits),
* which is long enough to store a pointer on current architectures.
*/
void *get_private_info(JNIEnv *jnienv, const char *classname,
jobject obj)
{
long_to_ptr lp;
if (!obj)
return 0;
lp.java_long = get_private_long_info(jnienv, classname, obj);
return lp.ptr;
}
/* Set the private data in a Db* object as a (64 bit) java long.
*/
void set_private_long_info(JNIEnv *jnienv, const char *classname,
jobject obj, jlong value)
{
jclass dbClass = get_class(jnienv, classname);
jfieldID id = jnienv->GetFieldID(dbClass, "private_info_", "J");
jnienv->SetLongField(obj, id, value);
}
/* Set the private data in a Db* object.
* The private data is stored in the object as a Java long (64 bits),
* which is long enough to store a pointer on current architectures.
*/
void set_private_info(JNIEnv *jnienv, const char *classname,
jobject obj, void *value)
{
long_to_ptr lp;
lp.java_long = 0;
lp.ptr = value;
set_private_long_info(jnienv, classname, obj, lp.java_long);
}
/*
* Given a non-qualified name (e.g. "foo"), get the class handl
* for the fully qualified name (e.g. "com.sleepycat.db.foo")
*/
jclass get_class(JNIEnv *jnienv, const char *classname)
{
// TODO: cache these (remember to use NewGlobalRef)
char fullname[128] = DB_PACKAGE_NAME;
strcat(fullname, classname);
return jnienv->FindClass(fullname);
}
/* Set an individual field in a Db* object.
* The field must be a DB object type.
*/
void set_object_field(JNIEnv *jnienv, jclass class_of_this,
jobject jthis, const char *object_classname,
const char *name_of_field, jobject obj)
{
char signature[512];
strcpy(signature, "L");
strcat(signature, DB_PACKAGE_NAME);
strcat(signature, object_classname);
strcat(signature, ";");
jfieldID id = jnienv->GetFieldID(class_of_this, name_of_field, signature);
jnienv->SetObjectField(jthis, id, obj);
}
/* Set an individual field in a Db* object.
* The field must be an integer type.
*/
void set_int_field(JNIEnv *jnienv, jclass class_of_this,
jobject jthis, const char *name_of_field, jint value)
{
jfieldID id = jnienv->GetFieldID(class_of_this, name_of_field, "I");
jnienv->SetIntField(jthis, id, value);
}
/* Set an individual field in a Db* object.
* The field must be an integer type.
*/
void set_long_field(JNIEnv *jnienv, jclass class_of_this,
jobject jthis, const char *name_of_field, jlong value)
{
jfieldID id = jnienv->GetFieldID(class_of_this, name_of_field, "J");
jnienv->SetLongField(jthis, id, value);
}
/* Set an individual field in a Db* object.
* The field must be an integer type.
*/
void set_lsn_field(JNIEnv *jnienv, jclass class_of_this,
jobject jthis, const char *name_of_field, DB_LSN value)
{
set_object_field(jnienv, class_of_this, jthis, name_DB_LSN,
name_of_field, get_DbLsn(jnienv, value));
}
/* Report an exception back to the java side.
*/
void report_exception(JNIEnv *jnienv, const char *text, int err)
{
jstring textString = get_java_string(jnienv, text);
jclass dbexcept;
if (err == DB_RUNRECOVERY) {
dbexcept = get_class(jnienv, name_DB_RUNRECOVERY);
}
else {
dbexcept = get_class(jnienv, name_DB_EXCEPTION);
}
jmethodID constructId = jnienv->GetMethodID(dbexcept, "<init>",
"(Ljava/lang/String;I)V");
jthrowable obj = (jthrowable)jnienv->AllocObject(dbexcept);
jnienv->CallVoidMethod(obj, constructId, textString, err);
jnienv->Throw(obj);
}
/* If the object is null, report an exception and return false (0),
* otherwise return true (1).
*/
int verify_non_null(JNIEnv *jnienv, void *obj)
{
if (obj == NULL) {
report_exception(jnienv, "null object", EINVAL);
return 0;
}
return 1;
}
/* If the DB_ENV* is null or has already been initialized (via appinit),
* report an exception and return false (0), otherwise return true (1).
* Setting a field in the environment after appinit has been called
* will never have any effect, so we raise the error to alert the
* user to a potential configuration bug.
*/
int verify_dbenv(JNIEnv *jnienv, DB_ENV *env)
{
if (verify_non_null(jnienv, env)) {
if ((env->flags & DB_ENV_APPINIT) != 0) {
report_exception(jnienv, "DbEnv.appinit already called", EINVAL);
return 0;
}
}
return 1;
}
/* If the error code is non-zero, report an exception and return false (0),
* otherwise return true (1).
*/
int verify_return(JNIEnv *jnienv, int err)
{
char *errstring;
if (err != 0) {
if (err > 0 && (errstring = strerror(err)) != NULL) {
report_exception(jnienv, errstring, err);
}
else {
char buffer[64];
sprintf(buffer, "error %d", err);
report_exception(jnienv, buffer, err);
}
return 0;
}
return 1;
}
/* Create an object of the given class, calling its default constructor.
*/
jobject create_default_object(JNIEnv *jnienv, const char *class_name)
{
jclass dbclass = get_class(jnienv, class_name);
jmethodID id = jnienv->GetMethodID(dbclass, "<init>", "()V");
jobject object = jnienv->NewObject(dbclass, id);
return object;
}
/* Convert an DB object to a Java encapsulation of that object.
* Note: This implementation creates a new Java object on each call,
* so it is generally useful when a new DB object has just been created.
*/
jobject convert_object(JNIEnv *jnienv, const char *class_name, void *dbobj)
{
if (!dbobj)
return 0;
jobject jo = create_default_object(jnienv, class_name);
set_private_info(jnienv, class_name, jo, dbobj);
return jo;
}
/* Create a copy of the string
*/
char *dup_string(const char *str)
{
char *retval = NEW_ARRAY(char, strlen(str)+1);
strcpy(retval, str);
return retval;
}
/* Create a java string from the given string
*/
jstring get_java_string(JNIEnv *jnienv, const char* string)
{
if (string == 0)
return 0;
return jnienv->NewStringUTF(string);
}
/* Storage allocator
*/
void *allocMemory(size_t n)
{
return malloc(n);
}
void freeMemory(void *p)
{
free(p);
}
/* Convert a java object to the various C pointers they represent.
*/
DB *get_DB(JNIEnv *jnienv, jobject obj)
{
return (DB *)get_private_info(jnienv, name_DB, obj);
}
DB_BTREE_STAT *get_DB_BTREE_STAT(JNIEnv *jnienv, jobject obj)
{
return (DB_BTREE_STAT *)get_private_info(jnienv, name_DB_BTREE_STAT, obj);
}
DBC *get_DBC(JNIEnv *jnienv, jobject obj)
{
return (DBC *)get_private_info(jnienv, name_DBC, obj);
}
DB_ENV *get_DB_ENV(JNIEnv *jnienv, jobject obj)
{
return (DB_ENV *)get_private_info(jnienv, name_DB_ENV, obj);
}
DB_INFO *get_DB_INFO(JNIEnv *jnienv, jobject obj)
{
return (DB_INFO *)get_private_info(jnienv, name_DB_INFO, obj);
}
// A DB_LOCK is just a size_t, so no need for indirection.
DB_LOCK get_DB_LOCK(JNIEnv *jnienv, jobject obj)
{
return (DB_LOCK)get_private_long_info(jnienv, name_DB_LOCK, obj);
}
DB_LOCKTAB *get_DB_LOCKTAB(JNIEnv *jnienv, jobject obj)
{
return (DB_LOCKTAB *)get_private_info(jnienv, name_DB_LOCKTAB, obj);
}
DB_LOG *get_DB_LOG(JNIEnv *jnienv, jobject obj)
{
return (DB_LOG *)get_private_info(jnienv, name_DB_LOG, obj);
}
DB_LOG_STAT *get_DB_LOG_STAT(JNIEnv *jnienv, jobject obj)
{
return (DB_LOG_STAT *)get_private_info(jnienv, name_DB_LOG_STAT, obj);
}
DB_LSN *get_DB_LSN(JNIEnv *jnienv, jobject obj)
{
return (DB_LSN *)get_private_info(jnienv, name_DB_LSN, obj);
}
DB_MPOOL *get_DB_MPOOL(JNIEnv *jnienv, jobject obj)
{
return (DB_MPOOL *)get_private_info(jnienv, name_DB_MPOOL, obj);
}
DB_MPOOL_FSTAT *get_DB_MPOOL_FSTAT(JNIEnv *jnienv, jobject obj)
{
return (DB_MPOOL_FSTAT *)get_private_info(jnienv, name_DB_MPOOL_FSTAT, obj);
}
DB_MPOOL_STAT *get_DB_MPOOL_STAT(JNIEnv *jnienv, jobject obj)
{
return (DB_MPOOL_STAT *)get_private_info(jnienv, name_DB_MPOOL_STAT, obj);
}
DB_TXN *get_DB_TXN(JNIEnv *jnienv, jobject obj)
{
return (DB_TXN *)get_private_info(jnienv, name_DB_TXN, obj);
}
DB_TXNMGR *get_DB_TXNMGR(JNIEnv *jnienv, jobject obj)
{
return (DB_TXNMGR *)get_private_info(jnienv, name_DB_TXNMGR, obj);
}
DB_TXN_STAT *get_DB_TXN_STAT(JNIEnv *jnienv, jobject obj)
{
return (DB_TXN_STAT *)get_private_info(jnienv, name_DB_TXN_STAT, obj);
}
DBT_info *get_DBT(JNIEnv *jnienv, jobject obj)
{
return (DBT_info *)get_private_info(jnienv, name_DBT, obj);
}
/* Convert a C pointer to the various Java objects they represent.
*/
jobject get_DbBtreeStat(JNIEnv *jnienv, DB_BTREE_STAT *dbobj)
{
return convert_object(jnienv, name_DB_BTREE_STAT, dbobj);
}
jobject get_Dbc(JNIEnv *jnienv, DBC *dbobj)
{
return convert_object(jnienv, name_DBC, dbobj);
}
jobject get_DbLockTab(JNIEnv *jnienv, DB_LOCKTAB *dbobj)
{
return convert_object(jnienv, name_DB_LOCKTAB, dbobj);
}
jobject get_DbLog(JNIEnv *jnienv, DB_LOG *dbobj)
{
return convert_object(jnienv, name_DB_LOG, dbobj);
}
jobject get_DbLogStat(JNIEnv *jnienv, DB_LOG_STAT *dbobj)
{
return convert_object(jnienv, name_DB_LOG_STAT, dbobj);
}
// LSNs are different since they are really normally
// treated as by-value objects. We actually create
// a pointer to the LSN and store that, deleting it
// when the LSN is GC'd.
//
jobject get_DbLsn(JNIEnv *jnienv, DB_LSN dbobj)
{
DB_LSN *lsnp = NEW(DB_LSN);
*lsnp = dbobj;
return convert_object(jnienv, name_DB_LSN, lsnp);
}
jobject get_DbMpool(JNIEnv *jnienv, DB_MPOOL *dbobj)
{
return convert_object(jnienv, name_DB_MPOOL, dbobj);
}
jobject get_DbMpoolFStat(JNIEnv *jnienv, DB_MPOOL_FSTAT *dbobj)
{
return convert_object(jnienv, name_DB_MPOOL_FSTAT, dbobj);
}
jobject get_DbMpoolStat(JNIEnv *jnienv, DB_MPOOL_STAT *dbobj)
{
return convert_object(jnienv, name_DB_MPOOL_STAT, dbobj);
}
jobject get_DbTxn(JNIEnv *jnienv, DB_TXN *dbobj)
{
return convert_object(jnienv, name_DB_TXN, dbobj);
}
jobject get_DbTxnMgr(JNIEnv *jnienv, DB_TXNMGR *dbobj)
{
return convert_object(jnienv, name_DB_TXNMGR, dbobj);
}
jobject get_DbTxnStat(JNIEnv *jnienv, DB_TXN_STAT *dbobj)
{
return convert_object(jnienv, name_DB_TXN_STAT, dbobj);
}
/****************************************************************
*
* Implementation of class DBT_info
*
*/
DBT_info::DBT_info()
: array_(0)
, offset_(0)
{
memset((DBT*)this, 0, sizeof(DBT));
}
void DBT_info::release(JNIEnv *jnienv)
{
if (array_ != 0) {
jnienv->DeleteGlobalRef(array_);
}
}
DBT_info::~DBT_info()
{
}
/****************************************************************
*
* Implementation of class LockedDBT
*
*/
LockedDBT::LockedDBT(JNIEnv *jnienv, jobject obj, OpKind kind)
: env_(jnienv)
, obj_(obj)
, has_error_(0)
, kind_(kind)
, java_array_len_(0)
, java_data_(0)
/* dbt initialized below */
{
dbt = (DBT_info *)get_private_info(jnienv, name_DBT, obj);
if (!verify_non_null(jnienv, dbt)) {
has_error_ = 1;
return;
}
if (kind == outOp &&
(dbt->flags & (DB_DBT_USERMEM | DB_DBT_MALLOC)) == 0) {
report_exception(jnienv,
"Dbt.flags must set DB_DBT_USERMEM or DB_DBT_MALLOC",
0);
has_error_ = 1;
return;
}
if ((dbt->flags & DB_DBT_USERMEM) || kind != outOp) {
// If writing with DB_DBT_USERMEM or it's a set (or get/set)
// operation, then the data should point to a java array.
// Note that outOp means data is coming out of the database
// (it's a get). inOp means data is going into the database
// (either a put, or a key input).
//
if (!dbt->array_) {
report_exception(jnienv, "Dbt.data is null", 0);
has_error_ = 1;
return;
}
// Verify other parameters
//
java_array_len_ = jnienv->GetArrayLength(dbt->array_);
if (dbt->offset_ < 0 ) {
report_exception(jnienv, "Dbt.offset illegal", 0);
has_error_ = 1;
return;
}
if (dbt->ulen + dbt->offset_ > java_array_len_) {
report_exception(jnienv,
"Dbt.ulen + Dbt.offset greater than array length", 0);
has_error_ = 1;
return;
}
java_data_ = jnienv->GetByteArrayElements(dbt->array_, NULL);
dbt->data = java_data_ + dbt->offset_;
}
else {
// If writing with DB_DBT_MALLOC, then the data is
// allocated by DB.
//
dbt->data = 0;
}
}
// The LockedDBT destructor is called when the java handler returns
// to the user, since that's when the LockedDBT objects go out of scope.
// Since it is thus called after any call to the underlying database,
// it copies any information from temporary structures back to user
// accessible arrays, and of course must free memory and remove references.
//
LockedDBT::~LockedDBT()
{
// If there was an error in the constructor,
// everything is already cleaned up.
//
if (has_error_)
return;
if ((dbt->flags & DB_DBT_USERMEM) || kind_ == inOp) {
// If writing with DB_DBT_USERMEM or it's a set (or get/set)
// operation, then the data may be already in the java array,
// in which case, we just need to release it.
// If DB didn't put it in the array (indicated by the
// dbt->data changing), we need to do that
//
jbyte *data = (jbyte *)dbt->data;
data -= dbt->offset_;
if (data != java_data_) {
env_->SetByteArrayRegion(dbt->array_, dbt->offset_, dbt->ulen, data);
}
env_->ReleaseByteArrayElements(dbt->array_, java_data_, 0);
dbt->data = 0;
}
if ((dbt->flags & DB_DBT_MALLOC) && kind_ != inOp) {
// If writing with DB_DBT_MALLOC, then the data was allocated
// by DB. If dbt->data is zero, it means an error occurred
// (and should have been already reported).
//
if (dbt->data) {
// Release any old references.
//
dbt->release(env_);
dbt->array_ = (jbyteArray)
env_->NewGlobalRef(env_->NewByteArray(dbt->size));
dbt->offset_ = 0;
env_->SetByteArrayRegion(dbt->array_, 0, dbt->size, (jbyte *)dbt->data);
free(dbt->data);
dbt->data = 0;
}
}
}
/****************************************************************
*
* Implementation of class LockedString
*
*/
LockedString::LockedString(JNIEnv *jnienv, jstring jstr)
: env_(jnienv)
, jstr_(jstr)
{
if (jstr == 0)
string = 0;
else
string = jnienv->GetStringUTFChars(jstr, NULL);
}
LockedString::~LockedString()
{
if (jstr_)
env_->ReleaseStringUTFChars(jstr_, string);
}
/****************************************************************
*
* Implementation of class LockedStringArray
*
*/
LockedStringArray::LockedStringArray(JNIEnv *jnienv, jobjectArray arr)
: env_(jnienv)
, arr_(arr)
, string_array(0)
{
typedef const char *conststr;
if (arr != 0) {
int count = jnienv->GetArrayLength(arr);
string_array = NEW_ARRAY(conststr, count+1);
for (int i=0; i<count; i++) {
jstring jstr = (jstring)jnienv->GetObjectArrayElement(arr, i);
if (jstr == 0) {
//
// An embedded null in the string array is treated
// as an endpoint.
//
string_array[i] = 0;
break;
}
else {
string_array[i] = jnienv->GetStringUTFChars(jstr, NULL);
}
}
string_array[count] = 0;
}
}
LockedStringArray::~LockedStringArray()
{
if (arr_) {
int count = env_->GetArrayLength(arr_);
for (int i=0; i<count; i++) {
if (string_array[i] == 0)
break;
jstring jstr = (jstring)env_->GetObjectArrayElement(arr_, i);
env_->ReleaseStringUTFChars(jstr, string_array[i]);
}
DELETE(string_array);
}
}
|