File: java_skel.c

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (175 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download
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
/*
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2009 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <jni.h>
#include <ksrvext.h>

static JavaVM *java_vm = NULL;
static dk_mutex_t *java_vm_mutex = NULL;

static caddr_t
java_vm_create (JNIEnv ** java_vm_env)
{
  JavaVMInitArgs vm_args;
  JavaVMOption options[5];
  jint res;
  caddr_t classpath_opt = NULL;
  int inx;
  char *classpath = NULL;

  if (!classpath)
    {
      classpath=getenv ("CLASSPATH");
    }
  if (!classpath)
    {
      classpath= ".";
    }

  classpath_opt = dk_alloc_box (strlen (classpath) + 20, DV_SHORT_STRING);
  sprintf (classpath_opt, "-Djava.class.path=%s", classpath);
  options[0].optionString = classpath_opt;
  vm_args.nOptions = 1;
  vm_args.version = JNI_VERSION_1_2;
  vm_args.options = options;
  vm_args.ignoreUnrecognized = JNI_FALSE;

  res = JNI_CreateJavaVM (&java_vm, (void **) java_vm_env, &vm_args);
  if (res < 0)
    return srv_make_new_error ("42000", "JV002", "Can't create the Java VM");
  else
    return NULL;
}


static caddr_t
java_vm_attach (JNIEnv ** env)
{
  caddr_t err = NULL;

  mutex_enter (java_vm_mutex);
  *env = NULL;
  if (!java_vm)
    {
      err = java_vm_create (env);
      if (err)
	{
	  mutex_leave (java_vm_mutex);
	  return err;
	}
    }
  else
    {
      if (JNI_OK != (*java_vm)->GetEnv (java_vm, (void **) env, JNI_VERSION_1_2))
	{
	  if (0 > (*java_vm)->AttachCurrentThread (java_vm, (void **) env,
		  NULL))
	    {
	      err =
		  srv_make_new_error ("42000", "JV003",
		  "Can't attach to the java VM");
	      mutex_leave (java_vm_mutex);
	      return err;
	    }
	}
    }
  mutex_leave (java_vm_mutex);
  return err;
}

static caddr_t
java_vm_detach (void)
{
  jint rc;

  mutex_enter (java_vm_mutex);
  rc = (*java_vm)->DetachCurrentThread (java_vm);
  mutex_leave (java_vm_mutex);
  if (rc < 0)
    return srv_make_new_error ("42000", "JV004",
	"Can't dettach from the java VM");
  else
    {
      log_debug ("Thread %p detached from the Java VM", thread_current());
      return NULL;
    }
}


static caddr_t
bif_do_something_bif (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  JNIEnv *env = NULL;
  jclass class_obj;
  jmethodID work_method_obj, cleanup_method_obj;
  jint ret;
  jvalue jargs[1];
  query_instance_t *qi = (query_instance_t *)qst;

  if (NULL != (*err_ret = java_vm_attach (&env)))
    goto finish;
  class_obj = (*env)->FindClass (env, "handler_class");

  work_method_obj =
    (*env)->GetStaticMethodID (env, class_obj, "do_bit_of_work", "()I");

  cleanup_method_obj =
    (*env)->GetStaticMethodID (env, class_obj, "cleanup_the_staff", "()V");

  while (0 < (ret = (*env)->CallStaticIntMethodA (env, class_obj, work_method_obj, jargs)))
    {
      if (qi_have_trx_error (qi))
	{
	  (*env)->CallStaticVoidMethodA (env, class_obj, cleanup_method_obj, jargs);
	  goto finish;
	}
    }
finish:
  if (env)
    {
      caddr_t err = java_vm_detach ();
      if (!*err_ret)
	*err_ret = err;
      else
	dk_free_tree (err);
    }
  qi_check_trx_error (qi, 1);
  return NULL;
}

void
bif_init_func_javavm (void)
{
  java_vm_mutex = mutex_allocate ();
  bif_define ("do_something_bif", bif_do_something_bif);
}

int
main (int argc, char *argv[])
{
  VirtuosoServerSetInitHook (bif_init_func_javavm);
  return VirtuosoServerMain (argc, argv);
}