grim/gplugin
Clone
Summary
Browse
Changes
Graph
abandoning this branch
feature/java
2021-04-28, Gary Kramlich
01fbef6b01cb
abandoning this branch
/*
* Copyright (C) 2011-2013 Gary Kramlich <grim@reaperworld.com>
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include
"gplugin-java-loader.h"
//#include "gplugin-java-plugin.h"
#include
<glib/gi18n.h>
#include
<jni.h>
/******************************************************************************
* Globals
*****************************************************************************/
static
GObjectClass
*
parent_class
=
NULL
;
static
GType
type_real
=
0
;
static
GModule
*
jsig_mod
=
NULL
;
static
GModule
*
jvm_mod
=
NULL
;
static
JavaVM
*
jvm
=
NULL
;
static
JNIEnv
*
env
=
NULL
;
/******************************************************************************
* Helpers
*****************************************************************************/
/******************************************************************************
* Java Stuff
*****************************************************************************/
static
const
gchar
*
gplugin_java_loader_get_arch_name
(
void
)
{
#if defined(__x86_64__)
return
"amd64"
;
#elif defined(__i386__)
return
"i386"
;
#else
return
NULL
;
#endif
}
static
gchar
*
gplugin_java_loader_find_java
(
const
gchar
*
java_path
,
GError
**
error
)
{
gchar
*
path
=
g_strdup
(
java_path
);
gchar
*
tmp
=
NULL
;
while
(
g_file_test
(
path
,
G_FILE_TEST_IS_SYMLINK
))
{
tmp
=
path
;
path
=
g_file_read_link
(
tmp
,
error
);
g_free
(
tmp
);
if
(
*
error
)
{
g_free
(
path
);
path
=
NULL
;
break
;
}
}
/* remove java from the path */
tmp
=
g_path_get_dirname
(
path
);
g_free
(
path
);
/* remove bin from the path */
path
=
tmp
;
tmp
=
g_path_get_dirname
(
path
);
g_free
(
path
);
/* now return the adjusted path */
return
tmp
;
}
static
gchar
*
gplugin_java_loader_get_library_path
(
const
gchar
*
jdk_path
,
const
gchar
*
library
)
{
const
gchar
*
arch
=
gplugin_java_loader_get_arch_name
();
gchar
*
path
=
NULL
,
*
module_path
=
NULL
;
path
=
g_build_filename
(
jdk_path
,
"lib"
,
arch
,
"server"
,
NULL
);
module_path
=
g_module_build_path
(
path
,
library
);
g_free
(
path
);
return
module_path
;
}
static
gboolean
gplugin_java_loader_load_library
(
const
gchar
*
jdk_path
,
const
gchar
*
name
,
GModule
**
module
,
GError
**
error
)
{
gboolean
ret
=
TRUE
;
gchar
*
path
=
gplugin_java_loader_get_library_path
(
jdk_path
,
name
);
*
module
=
g_module_open
(
path
,
0
);
if
(
*
module
==
NULL
)
{
if
(
error
)
{
*
error
=
g_error_new
(
GPLUGIN_DOMAIN
,
0
,
"Failed to open %s: %s"
,
path
,
g_module_error
());
}
ret
=
FALSE
;
}
g_free
(
path
);
return
ret
;
}
static
void
gplugin_java_loader_load_libraries
(
void
)
{
GError
*
error
=
NULL
;
gchar
*
path
=
gplugin_java_loader_find_java
(
"/usr/bin/java"
,
&
error
);
}
static
void
gplugin_java_loader_init_jvm
(
void
)
{
JavaVMInitArgs
vm_args
;
gplugin_java_loader_load_libraries
();
vm_args
.
version
=
0x00010001
;
JNI_GetDefaultJavaVMInitArgs
(
&
vm_args
);
JNI_CreateJavaVM
(
&
jvm
,
(
void
**
)
&
env
,
&
vm_args
);
}
/******************************************************************************
* GPluginLoaderInterface API
*****************************************************************************/
static
GSList
*
gplugin_java_loader_class_supported_extensions
(
GPLUGIN_UNUSED
const
GPluginLoaderClass
*
klass
)
{
return
g_slist_append
(
NULL
,
"class"
);
}
static
GPluginPlugin
*
gplugin_java_loader_query
(
GPLUGIN_UNUSED
GPluginLoader
*
loader
,
GPLUGIN_UNUSED
const
gchar
*
filename
,
GPLUGIN_UNUSED
GError
**
error
)
{
return
NULL
;
}
static
gboolean
gplugin_java_loader_load
(
GPLUGIN_UNUSED
GPluginLoader
*
loader
,
GPLUGIN_UNUSED
GPluginPlugin
*
plugin
,
GPLUGIN_UNUSED
GError
**
error
)
{
return
FALSE
;
}
static
gboolean
gplugin_java_loader_unload
(
GPLUGIN_UNUSED
GPluginLoader
*
loader
,
GPLUGIN_UNUSED
GPluginPlugin
*
plugin
,
GPLUGIN_UNUSED
GError
**
error
)
{
return
FALSE
;
}
/******************************************************************************
* GObject Stuff
*****************************************************************************/
static
void
gplugin_java_loader_class_init
(
GPluginJavaLoaderClass
*
klass
)
{
GPluginLoaderClass
*
loader_class
=
GPLUGIN_LOADER_CLASS
(
klass
);
parent_class
=
g_type_class_peek_parent
(
klass
);
loader_class
->
supported_extensions
=
gplugin_java_loader_class_supported_extensions
;
loader_class
->
query
=
gplugin_java_loader_query
;
loader_class
->
load
=
gplugin_java_loader_load
;
loader_class
->
unload
=
gplugin_java_loader_unload
;
}
/******************************************************************************
* API
*****************************************************************************/
void
gplugin_java_loader_register
(
GPluginNativePlugin
*
plugin
)
{
if
(
g_once_init_enter
(
&
type_real
))
{
GType
type
=
0
;
static
const
GTypeInfo
info
=
{
.
class_size
=
sizeof
(
GPluginJavaLoaderClass
),
.
class_init
=
(
GClassInitFunc
)
gplugin_java_loader_class_init
,
.
instance_size
=
sizeof
(
GPluginJavaLoader
),
};
type
=
gplugin_native_plugin_register_type
(
plugin
,
GPLUGIN_TYPE_LOADER
,
"GPluginJavaLoader"
,
&
info
,
0
);
//gplugin_java_loader_init_jvm();
g_once_init_leave
(
&
type_real
,
type
);
}
}
GType
gplugin_java_loader_get_type
(
void
)
{
if
(
G_UNLIKELY
(
type_real
==
0
))
{
g_warning
(
"gplugin_java_loader_get_type was called before "
"the type was registered!
\n
"
);
}
return
type_real
;
}