~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to scripting/engine_java.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// engine_java.h
 
2
 
 
3
/*    Copyright 2009 10gen Inc.
 
4
 *
 
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
 
8
 *
 
9
 *    http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 */
 
17
 
 
18
/* this file contains code to call into java (into the 10gen sandbox) from inside the database */
 
19
 
 
20
#pragma once
 
21
 
 
22
#include "../stdafx.h"
 
23
 
 
24
#include <jni.h>
 
25
#include <boost/thread/tss.hpp>
 
26
#include <errno.h>
 
27
#include <sys/types.h>
 
28
 
 
29
#if !defined(_WIN32)
 
30
#include <dirent.h>
 
31
#endif
 
32
 
 
33
#include "../db/jsobj.h"
 
34
 
 
35
#include "engine.h"
 
36
 
 
37
namespace mongo {
 
38
 
 
39
    void jasserted(const char *msg, const char *file, unsigned line);
 
40
#define jassert(_Expression) if ( ! ( _Expression ) ){ jasserted(#_Expression, __FILE__, __LINE__); }
 
41
 
 
42
    const char * findEd();
 
43
    const char * findEd(const char *);
 
44
    const string findJars();
 
45
 
 
46
    class BSONObj;
 
47
 
 
48
    class JavaJSImpl : public ScriptEngine {
 
49
    public:
 
50
        JavaJSImpl(const char * = 0);
 
51
        ~JavaJSImpl();
 
52
 
 
53
        jlong scopeCreate();
 
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 );
 
58
 
 
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 );
 
64
 
 
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 );
 
69
 
 
70
        jlong functionCreate( const char * code );
 
71
 
 
72
        /* return values:
 
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;
 
77
          */
 
78
        int invoke( jlong scope , jlong function );
 
79
 
 
80
        void printException();
 
81
 
 
82
        void run( const char * js );
 
83
 
 
84
        void detach( JNIEnv * env ) {
 
85
            _jvm->DetachCurrentThread();
 
86
        }
 
87
 
 
88
        Scope * createScope();
 
89
 
 
90
        void runTest();
 
91
    private:
 
92
 
 
93
        jobject create( const char * name ) {
 
94
            jclass c = findClass( name );
 
95
            if ( ! c )
 
96
                return 0;
 
97
 
 
98
            jmethodID cons = _getEnv()->GetMethodID( c , "<init>" , "()V" );
 
99
            if ( ! cons )
 
100
                return 0;
 
101
 
 
102
            return _getEnv()->NewObject( c , cons );
 
103
        }
 
104
 
 
105
        jclass findClass( const char * name ) {
 
106
            return _getEnv()->FindClass( name );
 
107
        }
 
108
 
 
109
 
 
110
    private:
 
111
 
 
112
        JNIEnv * _getEnv();
 
113
 
 
114
        JavaVM * _jvm;
 
115
        JNIEnv * _mainEnv;
 
116
        JavaVMInitArgs * _vmArgs;
 
117
 
 
118
        boost::thread_specific_ptr<JNIEnv> * _envs;
 
119
 
 
120
        jclass _dbhook;
 
121
        jclass _dbjni;
 
122
 
 
123
        jmethodID _scopeCreate;
 
124
        jmethodID _scopeInit;
 
125
        jmethodID _scopeSetThis;
 
126
        jmethodID _scopeReset;
 
127
        jmethodID _scopeFree;
 
128
 
 
129
        jmethodID _scopeGetNumber;
 
130
        jmethodID _scopeGetString;
 
131
        jmethodID _scopeGetObject;
 
132
        jmethodID _scopeGetBoolean;
 
133
        jmethodID _scopeGuessObjectSize;
 
134
        jmethodID _scopeGetType;
 
135
 
 
136
        jmethodID _scopeSetNumber;
 
137
        jmethodID _scopeSetString;
 
138
        jmethodID _scopeSetObject;
 
139
        jmethodID _scopeSetBoolean;
 
140
 
 
141
        jmethodID _functionCreate;
 
142
 
 
143
        jmethodID _invoke;
 
144
 
 
145
    };
 
146
 
 
147
    extern JavaJSImpl *JavaJS;
 
148
 
 
149
// a javascript "scope"
 
150
    class JavaScope : public Scope {
 
151
    public:
 
152
        JavaScope() {
 
153
            s = JavaJS->scopeCreate();
 
154
        }
 
155
        virtual ~JavaScope() {
 
156
            JavaJS->scopeFree(s);
 
157
            s = 0;
 
158
        }
 
159
        void reset() {
 
160
            JavaJS->scopeReset(s);
 
161
        }
 
162
 
 
163
        void init( BSONObj * o ) {
 
164
            JavaJS->scopeInit( s , o );
 
165
        }
 
166
 
 
167
        void localConnect( const char * dbName ){
 
168
            setString("$client", dbName );
 
169
        }
 
170
        
 
171
        double getNumber(const char *field) {
 
172
            return JavaJS->scopeGetNumber(s,field);
 
173
        }
 
174
        string getString(const char *field) {
 
175
            return JavaJS->scopeGetString(s,field);
 
176
        }
 
177
        bool getBoolean(const char *field) {
 
178
            return JavaJS->scopeGetBoolean(s,field);
 
179
        }
 
180
        BSONObj getObject(const char *field ) {
 
181
            return JavaJS->scopeGetObject(s,field);
 
182
        }
 
183
        int type(const char *field ) {
 
184
            return JavaJS->scopeGetType(s,field);
 
185
        }
 
186
 
 
187
        void setThis( const BSONObj * obj ){
 
188
            JavaJS->scopeSetThis( s , obj );
 
189
        }
 
190
 
 
191
        void setNumber(const char *field, double val ) {
 
192
            JavaJS->scopeSetNumber(s,field,val);
 
193
        }
 
194
        void setString(const char *field, const char * val ) {
 
195
            JavaJS->scopeSetString(s,field,val);
 
196
        }
 
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);
 
200
        }
 
201
        void setBoolean(const char *field, bool val ) {
 
202
            JavaJS->scopeSetBoolean(s,field,val);
 
203
        }
 
204
        
 
205
        ScriptingFunction createFunction( const char * code ){
 
206
            return JavaJS->functionCreate( code );
 
207
        }
 
208
 
 
209
        int invoke( ScriptingFunction function , const BSONObj& args ){
 
210
            setObject( "args" , args , true );
 
211
            return JavaJS->invoke(s,function);
 
212
        }
 
213
        
 
214
        string getError(){
 
215
            return getString( "error" );
 
216
        }
 
217
 
 
218
        jlong s;
 
219
    };
 
220
 
 
221
    JNIEXPORT void JNICALL java_native_say(JNIEnv *, jclass, jobject outBuffer );
 
222
    JNIEXPORT jint JNICALL java_native_call(JNIEnv *, jclass, jobject outBuffer , jobject inBuffer );
 
223
 
 
224
} // namespace mongo