1 /*
   2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef JAVAVIRTUALMACHINE_H
  27 #define JAVAVIRTUALMACHINE_H
  28 
  29 
  30 #include "jni.h"
  31 #include "Platform.h"
  32 
  33 
  34 enum JvmLaunchType {
  35     USER_APP_LAUNCH,
  36     SINGLE_INSTANCE_NOTIFICATION_LAUNCH,
  37     JVM_LAUNCH_TYPES_NUM
  38 };
  39 
  40 struct JavaOptionItem {
  41     TString name;
  42     TString value;
  43     void* extraInfo;
  44 };
  45 
  46 class JavaOptions {
  47 private:
  48     std::list<JavaOptionItem> FItems;
  49     JavaVMOption* FOptions;
  50 
  51 public:
  52     JavaOptions();
  53     ~JavaOptions();
  54 
  55     void AppendValue(const TString Key, TString Value, void* Extra);
  56     void AppendValue(const TString Key, TString Value);
  57     void AppendValue(const TString Key);
  58     void AppendValues(OrderedMap<TString, TString> Values);
  59     void ReplaceValue(const TString Key, TString Value);
  60     std::list<TString> ToList();
  61     size_t GetCount();
  62 };
  63 
  64 // Private typedef for function pointer casting
  65 #define LAUNCH_FUNC "JLI_Launch"
  66 
  67 typedef int (JNICALL *JVM_CREATE)(int argc, char ** argv,
  68                                   int jargc, const char** jargv,
  69                                   int appclassc, const char** appclassv,
  70                                   const char* fullversion,
  71                                   const char* dotversion,
  72                                   const char* pname,
  73                                   const char* lname,
  74                                   jboolean javaargs,
  75                                   jboolean cpwildcard,
  76                                   jboolean javaw,
  77                                   jint ergo);
  78 
  79 class JavaLibrary : public Library {
  80     JVM_CREATE FCreateProc;
  81     JavaLibrary(const TString &FileName);
  82 public:
  83     JavaLibrary();
  84     bool JavaVMCreate(size_t argc, char *argv[]);
  85 };
  86 
  87 class JavaVirtualMachine {
  88 private:
  89     JavaLibrary javaLibrary;
  90 
  91     void configureLibrary();
  92     bool launchVM(JavaOptions& options, std::list<TString>& vmargs,
  93             bool addSiProcessId);
  94 public:
  95     JavaVirtualMachine();
  96     ~JavaVirtualMachine(void);
  97 
  98     bool StartJVM();
  99     bool NotifySingleInstance();
 100 };
 101 
 102 bool RunVM(JvmLaunchType type);
 103 
 104 #endif // JAVAVIRTUALMACHINE_H