-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathPgObject.c
More file actions
190 lines (167 loc) · 4.85 KB
/
PgObject.c
File metadata and controls
190 lines (167 loc) · 4.85 KB
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
/*
* Copyright (c) 2004-2023 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
* which accompanies this distribution, and is available at
* http://opensource.org/licenses/BSD-3-Clause
*
* Contributors:
* Tada AB - Thomas Hallgren
* Chapman Flack
*/
#include <postgres.h>
#include <executor/spi.h>
#include "pljava/PgObject_priv.h"
#include "pljava/type/String.h"
/**
*/
static bool s_loopLock = false;
static jclass s_Class_class = 0;
static jmethodID s_Class_getName = 0;
/* effectiveModulePath is set at initialization time (in Backend.c)
*/
const char* effectiveModulePath;
void PgObject_free(PgObject object)
{
Finalizer finalizer = object->m_class->finalize;
if(finalizer != 0)
finalizer(object);
pfree(object);
}
PgObject PgObjectClass_allocInstance(PgObjectClass clazz, MemoryContext ctx)
{
Size sz = clazz->instanceSize;
PgObject infant = (PgObject)MemoryContextAlloc(ctx, sz);
memset(infant, 0, sz);
infant->m_class = clazz;
return infant;
}
void PgObjectClass_init(PgObjectClass clazz, const char* name, Size instanceSize, Finalizer finalizer)
{
clazz->name = name;
clazz->instanceSize = instanceSize;
clazz->finalize = finalizer;
}
PgObjectClass PgObjectClass_create(const char* name, Size instanceSize, Finalizer finalizer)
{
PgObjectClass self = (PgObjectClass)MemoryContextAlloc(TopMemoryContext, sizeof(struct PgObjectClass_));
memset(self, 0, sizeof(struct PgObjectClass_));
PgObjectClass_init(self, name, instanceSize, finalizer);
return self;
}
PgObjectClass PgObject_getClass(PgObject self)
{
return self->m_class;
}
const char* PgObjectClass_getName(PgObjectClass self)
{
return self->name;
}
char* PgObject_getClassName(jclass cls)
{
jstring jstr;
char* tmp;
if(s_Class_getName == 0)
{
if(s_loopLock)
return "<exception while obtaining Class.getName()>";
s_loopLock = true;
s_Class_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/Class"));
s_Class_getName = PgObject_getJavaMethod(s_Class_class, "getName", "()Ljava/lang/String;");
s_loopLock = false;
}
jstr = (jstring)JNI_callObjectMethod(cls, s_Class_getName);
tmp = String_createNTS(jstr);
JNI_deleteLocalRef(jstr);
return tmp;
}
void PgObject_throwMemberError(jclass cls, const char* memberName, const char* signature, bool isMethod, bool isStatic)
{
JNI_exceptionDescribe();
JNI_exceptionClear();
ereport(ERROR, (
errmsg("Unable to find%s %s %s.%s with signature %s",
(isStatic ? " static" : ""),
(isMethod ? "method" : "field"),
PgObject_getClassName(cls),
memberName,
signature)));
}
jclass PgObject_getJavaClass(const char* className)
{
jclass cls = JNI_findClass(className);
if(cls == 0)
{
if(JNI_exceptionCheck())
{
JNI_exceptionDescribe();
JNI_exceptionClear();
}
ereport(ERROR, (
errmsg("Unable to load class %s using module path '%s'",
className, effectiveModulePath == 0 ? "null" :
effectiveModulePath)));
}
return cls;
}
void PgObject_registerNatives(const char* className, JNINativeMethod* methods)
{
jclass cls = PgObject_getJavaClass(className);
PgObject_registerNatives2(cls, methods);
JNI_deleteLocalRef(cls);
}
void PgObject_registerNatives2(jclass cls, JNINativeMethod* methods)
{
#ifndef GCJ
jint nMethods = 0;
JNINativeMethod* m = methods;
while(m->name != 0)
{
m++;
nMethods++;
}
if(JNI_registerNatives(cls, methods, nMethods) != 0)
{
JNI_exceptionDescribe();
JNI_exceptionClear();
ereport(ERROR, (
errmsg("Unable to register native methods")));
}
#endif
}
jmethodID PgObject_getJavaMethod(jclass cls, const char* methodName, const char* signature)
{
jmethodID m = JNI_getMethodID(cls, methodName, signature);
if(m == 0)
PgObject_throwMemberError(cls, methodName, signature, true, false);
return m;
}
jmethodID PgObject_getStaticJavaMethod(jclass cls, const char* methodName, const char* signature)
{
jmethodID m = JNI_getStaticMethodID(cls, methodName, signature);
if(m == 0)
PgObject_throwMemberError(cls, methodName, signature, true, true);
return m;
}
jfieldID PgObject_getJavaField(jclass cls, const char* fieldName, const char* signature)
{
jfieldID m = JNI_getFieldID(cls, fieldName, signature);
if(m == 0)
PgObject_throwMemberError(cls, fieldName, signature, false, false);
return m;
}
jfieldID PgObject_getStaticJavaField(jclass cls, const char* fieldName, const char* signature)
{
jfieldID m = JNI_getStaticFieldID(cls, fieldName, signature);
if(m == 0)
PgObject_throwMemberError(cls, fieldName, signature, false, true);
return m;
}
HeapTuple PgObject_getValidTuple(int cacheId, Oid tupleId, const char* tupleType)
{
HeapTuple tuple = SearchSysCache(cacheId, ObjectIdGetDatum(tupleId), 0, 0, 0);
if(!HeapTupleIsValid(tuple))
ereport(ERROR, (errmsg("cache lookup failed for %s %u", tupleType, tupleId)));
return tuple;
}