00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00022 #include "../pch.h"
00023
00024 #include <jni.h>
00025 #include <errno.h>
00026 #include <sys/types.h>
00027
00028 #if !defined(_WIN32)
00029 #include <dirent.h>
00030 #endif
00031
00032 #include "../db/jsobj.h"
00033
00034 #include "engine.h"
00035
00036 namespace mongo {
00037
00038 void jasserted(const char *msg, const char *file, unsigned line);
00039 #define jassert(_Expression) if ( ! ( _Expression ) ){ jasserted(#_Expression, __FILE__, __LINE__); }
00040
00041 const char * findEd();
00042 const char * findEd(const char *);
00043 const string findJars();
00044
00045 class BSONObj;
00046
00047 class JavaJSImpl : public ScriptEngine {
00048 public:
00049 JavaJSImpl(const char * = 0);
00050 ~JavaJSImpl();
00051
00052 jlong scopeCreate();
00053 int scopeInit( jlong id , const BSONObj * obj );
00054 int scopeSetThis( jlong id , const BSONObj * obj );
00055 jboolean scopeReset( jlong id );
00056 void scopeFree( jlong id );
00057
00058 double scopeGetNumber( jlong id , const char * field );
00059 string scopeGetString( jlong id , const char * field );
00060 jboolean scopeGetBoolean( jlong id , const char * field );
00061 BSONObj scopeGetObject( jlong id , const char * field );
00062 char scopeGetType( jlong id , const char * field );
00063
00064 int scopeSetNumber( jlong id , const char * field , double val );
00065 int scopeSetString( jlong id , const char * field , const char * val );
00066 int scopeSetObject( jlong id , const char * field , const BSONObj * obj );
00067 int scopeSetBoolean( jlong id , const char * field , jboolean val );
00068
00069 jlong functionCreate( const char * code );
00070
00071
00072
00073
00074
00075
00076
00077 int invoke( jlong scope , jlong function );
00078
00079 void printException();
00080
00081 void run( const char * js );
00082
00083 void detach( JNIEnv * env ) {
00084 _jvm->DetachCurrentThread();
00085 }
00086
00087 Scope * createScope();
00088
00089 void runTest();
00090 private:
00091
00092 jobject create( const char * name ) {
00093 jclass c = findClass( name );
00094 if ( ! c )
00095 return 0;
00096
00097 jmethodID cons = _getEnv()->GetMethodID( c , "<init>" , "()V" );
00098 if ( ! cons )
00099 return 0;
00100
00101 return _getEnv()->NewObject( c , cons );
00102 }
00103
00104 jclass findClass( const char * name ) {
00105 return _getEnv()->FindClass( name );
00106 }
00107
00108
00109 private:
00110
00111 JNIEnv * _getEnv();
00112
00113 JavaVM * _jvm;
00114 JNIEnv * _mainEnv;
00115 JavaVMInitArgs * _vmArgs;
00116
00117 boost::thread_specific_ptr<JNIEnv> * _envs;
00118
00119 jclass _dbhook;
00120 jclass _dbjni;
00121
00122 jmethodID _scopeCreate;
00123 jmethodID _scopeInit;
00124 jmethodID _scopeSetThis;
00125 jmethodID _scopeReset;
00126 jmethodID _scopeFree;
00127
00128 jmethodID _scopeGetNumber;
00129 jmethodID _scopeGetString;
00130 jmethodID _scopeGetObject;
00131 jmethodID _scopeGetBoolean;
00132 jmethodID _scopeGuessObjectSize;
00133 jmethodID _scopeGetType;
00134
00135 jmethodID _scopeSetNumber;
00136 jmethodID _scopeSetString;
00137 jmethodID _scopeSetObject;
00138 jmethodID _scopeSetBoolean;
00139
00140 jmethodID _functionCreate;
00141
00142 jmethodID _invoke;
00143
00144 };
00145
00146 extern JavaJSImpl *JavaJS;
00147
00148
00149 class JavaScope : public Scope {
00150 public:
00151 JavaScope() {
00152 s = JavaJS->scopeCreate();
00153 }
00154 virtual ~JavaScope() {
00155 JavaJS->scopeFree(s);
00156 s = 0;
00157 }
00158 void reset() {
00159 JavaJS->scopeReset(s);
00160 }
00161
00162 void init( BSONObj * o ) {
00163 JavaJS->scopeInit( s , o );
00164 }
00165
00166 void localConnect( const char * dbName ) {
00167 setString("$client", dbName );
00168 }
00169
00170 double getNumber(const char *field) {
00171 return JavaJS->scopeGetNumber(s,field);
00172 }
00173 string getString(const char *field) {
00174 return JavaJS->scopeGetString(s,field);
00175 }
00176 bool getBoolean(const char *field) {
00177 return JavaJS->scopeGetBoolean(s,field);
00178 }
00179 BSONObj getObject(const char *field ) {
00180 return JavaJS->scopeGetObject(s,field);
00181 }
00182 int type(const char *field ) {
00183 return JavaJS->scopeGetType(s,field);
00184 }
00185
00186 void setThis( const BSONObj * obj ) {
00187 JavaJS->scopeSetThis( s , obj );
00188 }
00189
00190 void setNumber(const char *field, double val ) {
00191 JavaJS->scopeSetNumber(s,field,val);
00192 }
00193 void setString(const char *field, const char * val ) {
00194 JavaJS->scopeSetString(s,field,val);
00195 }
00196 void setObject(const char *field, const BSONObj& obj , bool readOnly ) {
00197 uassert( 10211 , "only readOnly setObject supported in java" , readOnly );
00198 JavaJS->scopeSetObject(s,field,&obj);
00199 }
00200 void setBoolean(const char *field, bool val ) {
00201 JavaJS->scopeSetBoolean(s,field,val);
00202 }
00203
00204 ScriptingFunction createFunction( const char * code ) {
00205 return JavaJS->functionCreate( code );
00206 }
00207
00208 int invoke( ScriptingFunction function , const BSONObj& args ) {
00209 setObject( "args" , args , true );
00210 return JavaJS->invoke(s,function);
00211 }
00212
00213 string getError() {
00214 return getString( "error" );
00215 }
00216
00217 jlong s;
00218 };
00219
00220 JNIEXPORT void JNICALL java_native_say(JNIEnv *, jclass, jobject outBuffer );
00221 JNIEXPORT jint JNICALL java_native_call(JNIEnv *, jclass, jobject outBuffer , jobject inBuffer );
00222
00223 }