corda/src/embedded-loader.cpp

111 lines
2.8 KiB
C++
Raw Normal View History

/* Copyright (c) 2008-2013, Avian Contributors
2012-11-13 08:11:49 +00:00
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
2012-12-05 00:37:02 +00:00
#include <windows.h>
2012-11-13 08:11:49 +00:00
#include <tchar.h>
#include <stdint.h>
#include "avian/embed.h"
2012-11-13 08:11:49 +00:00
#include "jni.h"
#if (defined __MINGW32__) || (defined _MSC_VER)
# define EXPORT __declspec(dllexport)
2012-12-05 22:30:49 +00:00
# ifdef _MSC_VER
# define not !
# endif
2012-11-13 08:11:49 +00:00
#else
# define EXPORT __attribute__ ((visibility("default"))) \
__attribute__ ((used))
#endif
extern "C" {
2012-12-05 22:30:49 +00:00
// since we aren't linking against libstdc++, we must implement this
// ourselves:
void __cxa_pure_virtual(void) { abort(); }
2012-11-13 08:11:49 +00:00
EXPORT const uint8_t*
bootJar(unsigned* size)
{
2012-12-05 22:30:49 +00:00
if(HRSRC hResInfo = FindResourceW(NULL, RESID_BOOT_JAR, reinterpret_cast<LPCWSTR>(RT_RCDATA)))
2012-11-13 08:11:49 +00:00
{
if(HGLOBAL hRes = LoadResource(NULL, hResInfo))
{
*size = SizeofResource(NULL, hResInfo);
return (const uint8_t*)LockResource(hRes);
}
}
fprintf(stderr, "boot.jar resource not found\n");
*size = 0;
return NULL;
}
} // extern "C"
2012-12-05 22:30:49 +00:00
static void getMainClass(char* pName, int maxLen)
2012-11-13 08:11:49 +00:00
{
if(0 == LoadString(NULL, RESID_MAIN_CLASS, pName, maxLen))
{
fprintf(stderr, "Main class not specified\n");
strcpy(pName, "Main");
}
}
int
main(int ac, const char** av)
{
JavaVMInitArgs vmArgs;
vmArgs.version = JNI_VERSION_1_2;
vmArgs.nOptions = 1;
vmArgs.ignoreUnrecognized = JNI_TRUE;
2012-12-05 22:30:49 +00:00
JavaVMOption options[1];
2012-11-13 08:11:49 +00:00
vmArgs.options = options;
options[0].optionString = const_cast<char*>("-Xbootclasspath:[bootJar]");
JavaVM* vm;
void* env;
JNI_CreateJavaVM(&vm, &env, &vmArgs);
JNIEnv* e = static_cast<JNIEnv*>(env);
char mainClass[256];
getMainClass(mainClass, sizeof(mainClass));
jclass c = e->FindClass(mainClass);
if (not e->ExceptionCheck()) {
jmethodID m = e->GetStaticMethodID(c, "main", "([Ljava/lang/String;)V");
if (not e->ExceptionCheck()) {
jclass stringClass = e->FindClass("java/lang/String");
if (not e->ExceptionCheck()) {
jobjectArray a = e->NewObjectArray(ac-1, stringClass, 0);
if (not e->ExceptionCheck()) {
for (int i = 1; i < ac; ++i) {
e->SetObjectArrayElement(a, i-1, e->NewStringUTF(av[i]));
}
e->CallStaticVoidMethod(c, m, a);
} else fprintf(stderr, "Couldn't create array\n");
} else fprintf(stderr, "java.lang.String not found\n");
} else fprintf(stderr, "main method not found\n");
} else fprintf(stderr, "Main class not found\n");
int exitCode = 0;
if(e->ExceptionCheck()) {
exitCode = -1;
e->ExceptionDescribe();
e->ExceptionClear();
}
vm->DestroyJavaVM();
return exitCode;
2012-12-05 00:37:02 +00:00
}