3
/* Copyright 2009 10gen Inc.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
/* this file contains code to call into java (into the 10gen sandbox) from inside the database */
22
#include "../stdafx.h"
25
#include <boost/thread/tss.hpp>
27
#include <sys/types.h>
33
#include "../db/jsobj.h"
39
void jasserted(const char *msg, const char *file, unsigned line);
40
#define jassert(_Expression) if ( ! ( _Expression ) ){ jasserted(#_Expression, __FILE__, __LINE__); }
42
const char * findEd();
43
const char * findEd(const char *);
44
const string findJars();
48
class JavaJSImpl : public ScriptEngine {
50
JavaJSImpl(const char * = 0);
54
int scopeInit( jlong id , const BSONObj * obj );
55
int scopeSetThis( jlong id , const BSONObj * obj );
56
jboolean scopeReset( jlong id );
57
void scopeFree( jlong id );
59
double scopeGetNumber( jlong id , const char * field );
60
string scopeGetString( jlong id , const char * field );
61
jboolean scopeGetBoolean( jlong id , const char * field );
62
BSONObj scopeGetObject( jlong id , const char * field );
63
char scopeGetType( jlong id , const char * field );
65
int scopeSetNumber( jlong id , const char * field , double val );
66
int scopeSetString( jlong id , const char * field , const char * val );
67
int scopeSetObject( jlong id , const char * field , const BSONObj * obj );
68
int scopeSetBoolean( jlong id , const char * field , jboolean val );
70
jlong functionCreate( const char * code );
73
public static final int NO_SCOPE = -1;
74
public static final int NO_FUNCTION = -2;
75
public static final int INVOKE_ERROR = -3;
76
public static final int INVOKE_SUCCESS = 0;
78
int invoke( jlong scope , jlong function );
80
void printException();
82
void run( const char * js );
84
void detach( JNIEnv * env ) {
85
_jvm->DetachCurrentThread();
88
Scope * createScope();
93
jobject create( const char * name ) {
94
jclass c = findClass( name );
98
jmethodID cons = _getEnv()->GetMethodID( c , "<init>" , "()V" );
102
return _getEnv()->NewObject( c , cons );
105
jclass findClass( const char * name ) {
106
return _getEnv()->FindClass( name );
116
JavaVMInitArgs * _vmArgs;
118
boost::thread_specific_ptr<JNIEnv> * _envs;
123
jmethodID _scopeCreate;
124
jmethodID _scopeInit;
125
jmethodID _scopeSetThis;
126
jmethodID _scopeReset;
127
jmethodID _scopeFree;
129
jmethodID _scopeGetNumber;
130
jmethodID _scopeGetString;
131
jmethodID _scopeGetObject;
132
jmethodID _scopeGetBoolean;
133
jmethodID _scopeGuessObjectSize;
134
jmethodID _scopeGetType;
136
jmethodID _scopeSetNumber;
137
jmethodID _scopeSetString;
138
jmethodID _scopeSetObject;
139
jmethodID _scopeSetBoolean;
141
jmethodID _functionCreate;
147
extern JavaJSImpl *JavaJS;
149
// a javascript "scope"
150
class JavaScope : public Scope {
153
s = JavaJS->scopeCreate();
155
virtual ~JavaScope() {
156
JavaJS->scopeFree(s);
160
JavaJS->scopeReset(s);
163
void init( BSONObj * o ) {
164
JavaJS->scopeInit( s , o );
167
void localConnect( const char * dbName ){
168
setString("$client", dbName );
171
double getNumber(const char *field) {
172
return JavaJS->scopeGetNumber(s,field);
174
string getString(const char *field) {
175
return JavaJS->scopeGetString(s,field);
177
bool getBoolean(const char *field) {
178
return JavaJS->scopeGetBoolean(s,field);
180
BSONObj getObject(const char *field ) {
181
return JavaJS->scopeGetObject(s,field);
183
int type(const char *field ) {
184
return JavaJS->scopeGetType(s,field);
187
void setThis( const BSONObj * obj ){
188
JavaJS->scopeSetThis( s , obj );
191
void setNumber(const char *field, double val ) {
192
JavaJS->scopeSetNumber(s,field,val);
194
void setString(const char *field, const char * val ) {
195
JavaJS->scopeSetString(s,field,val);
197
void setObject(const char *field, const BSONObj& obj , bool readOnly ) {
198
uassert( 10211 , "only readOnly setObject supported in java" , readOnly );
199
JavaJS->scopeSetObject(s,field,&obj);
201
void setBoolean(const char *field, bool val ) {
202
JavaJS->scopeSetBoolean(s,field,val);
205
ScriptingFunction createFunction( const char * code ){
206
return JavaJS->functionCreate( code );
209
int invoke( ScriptingFunction function , const BSONObj& args ){
210
setObject( "args" , args , true );
211
return JavaJS->invoke(s,function);
215
return getString( "error" );
221
JNIEXPORT void JNICALL java_native_say(JNIEnv *, jclass, jobject outBuffer );
222
JNIEXPORT jint JNICALL java_native_call(JNIEnv *, jclass, jobject outBuffer , jobject inBuffer );