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 PLATFORM_H 27 #define PLATFORM_H 28 29 #include "OrderedMap.h" 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <memory.h> 34 #include <string> 35 #include <map> 36 #include <list> 37 #include <vector> 38 39 40 #ifdef WIN32 41 #ifndef WINDOWS 42 #define WINDOWS 43 #endif 44 #endif //WIN32 45 46 #ifdef __APPLE__ 47 #define MAC 48 #define POSIX 49 #endif //__APPLE__ 50 51 52 #ifdef __linux 53 #ifndef LINUX 54 #define LINUX 55 #endif 56 #endif //__linux 57 58 #ifdef LINUX 59 #define POSIX 60 #endif //LINUX 61 62 63 64 #ifdef WINDOWS 65 // Define Windows compatibility requirements XP or later 66 #define WINVER 0x0600 67 #define _WIN32_WINNT 0x0600 68 69 #include <Windows.h> 70 #include <tchar.h> 71 #include <shlobj.h> 72 #include <direct.h> 73 #include <process.h> 74 #include <malloc.h> 75 76 typedef std::wstring TString; 77 #define StringLength wcslen 78 79 #define TRAILING_PATHSEPARATOR '\\' 80 #define BAD_TRAILING_PATHSEPARATOR '/' 81 #define PATH_SEPARATOR ';' 82 #define BAD_PATH_SEPARATOR ':' 83 84 typedef ULONGLONG TPlatformNumber; 85 typedef DWORD TProcessID; 86 87 #endif //WINDOWS 88 89 90 #ifdef POSIX 91 #include <errno.h> 92 #include <unistd.h> 93 #include <sys/stat.h> 94 #include <dlfcn.h> 95 #include <libgen.h> 96 97 #define _T(x) x 98 99 typedef char TCHAR; 100 typedef std::string TString; 101 #define StringLength strlen 102 103 typedef unsigned long DWORD; 104 105 #define TRAILING_PATHSEPARATOR '/' 106 #define BAD_TRAILING_PATHSEPARATOR '\\' 107 #define PATH_SEPARATOR ':' 108 #define BAD_PATH_SEPARATOR ';' 109 #define MAX_PATH 1000 110 111 typedef long TPlatformNumber; 112 typedef pid_t TProcessID; 113 114 #define HMODULE void* 115 #endif //POSIX 116 117 118 // Config file sections 119 #define CONFIG_SECTION_APPLICATION _T("CONFIG_SECTION_APPLICATION") 120 #define CONFIG_SECTION_JVMOPTIONS _T("CONFIG_SECTION_JVMOPTIONS") 121 #define CONFIG_SECTION_APPCDSJVMOPTIONS _T("CONFIG_SECTION_APPCDSJVMOPTIONS") 122 #define CONFIG_SECTION_ARGOPTIONS _T("CONFIG_SECTION_ARGOPTIONS") 123 #define CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS \ 124 _T("CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS") 125 126 // Config file keys. 127 #define CONFIG_VERSION _T("CONFIG_VERSION") 128 #define CONFIG_MAINJAR_KEY _T("CONFIG_MAINJAR_KEY") 129 #define CONFIG_MAINMODULE_KEY _T("CONFIG_MAINMODULE_KEY") 130 #define CONFIG_MAINCLASSNAME_KEY _T("CONFIG_MAINCLASSNAME_KEY") 131 #define CONFIG_CLASSPATH_KEY _T("CONFIG_CLASSPATH_KEY") 132 #define CONFIG_MODULEPATH_KEY _T("CONFIG_MODULEPATH_KEY") 133 #define APP_NAME_KEY _T("APP_NAME_KEY") 134 #define CONFIG_SPLASH_KEY _T("CONFIG_SPLASH_KEY") 135 #define CONFIG_APP_ID_KEY _T("CONFIG_APP_ID_KEY") 136 #define CONFIG_APP_MEMORY _T("CONFIG_APP_MEMORY") 137 #define CONFIG_APP_DEBUG _T("CONFIG_APP_DEBUG") 138 #define CONFIG_APPLICATION_INSTANCE _T("CONFIG_APPLICATION_INSTANCE") 139 140 #define JVM_RUNTIME_KEY _T("JVM_RUNTIME_KEY") 141 #define JPACKAGE_APP_DATA_DIR _T("CONFIG_APP_IDENTIFIER") 142 143 144 145 typedef void* Module; 146 typedef void* Procedure; 147 148 149 template <typename ObjectType, typename ValueType, 150 ValueType (ObjectType::*getter)(void), 151 void (ObjectType::*setter)(ValueType)> 152 class Property { 153 private: 154 ObjectType* FObject; 155 156 public: 157 Property() { 158 FObject = NULL; 159 } 160 161 void SetInstance(ObjectType* Value) { 162 FObject = Value; 163 } 164 165 // To set the value using the set method. 166 ValueType operator =(const ValueType& Value) { 167 assert(FObject != NULL); 168 (FObject->*setter)(Value); 169 return Value; 170 } 171 172 // The Property class is treated as the internal type. 173 operator ValueType() { 174 assert(FObject != NULL); 175 return (FObject->*getter)(); 176 } 177 }; 178 179 template <typename ObjectType, typename ValueType, 180 ValueType (ObjectType::*getter)(void)> 181 class ReadProperty { 182 private: 183 ObjectType* FObject; 184 185 public: 186 ReadProperty() { 187 FObject = NULL; 188 } 189 190 void SetInstance(ObjectType* Value) { 191 FObject = Value; 192 } 193 194 // The Property class is treated as the internal type. 195 operator ValueType() { 196 assert(FObject != NULL); 197 return (FObject->*getter)(); 198 } 199 }; 200 201 template <typename ObjectType, typename ValueType, 202 void (ObjectType::*setter)(ValueType)> 203 class WriteProperty { 204 private: 205 ObjectType* FObject; 206 207 public: 208 WriteProperty() { 209 FObject = NULL; 210 } 211 212 void SetInstance(ObjectType* Value) { 213 FObject = Value; 214 } 215 216 // To set the value using the set method. 217 ValueType operator =(const ValueType& Value) { 218 assert(FObject != NULL); 219 (FObject->*setter)(Value); 220 return Value; 221 } 222 }; 223 224 template <typename ValueType, 225 ValueType (*getter)(void), void (*setter)(ValueType)> 226 class StaticProperty { 227 public: 228 StaticProperty() { 229 } 230 231 // To set the value using the set method. 232 ValueType operator =(const ValueType& Value) { 233 (*getter)(Value); 234 return Value; 235 } 236 237 // The Property class is treated as the internal type which is the getter. 238 operator ValueType() { 239 return (*setter)(); 240 } 241 }; 242 243 template <typename ValueType, ValueType (*getter)(void)> 244 class StaticReadProperty { 245 public: 246 StaticReadProperty() { 247 } 248 249 // The Property class is treated as the internal type which is the getter. 250 operator ValueType() { 251 return (*getter)(); 252 } 253 }; 254 255 template <typename ValueType, void (*setter)(ValueType)> 256 class StaticWriteProperty { 257 public: 258 StaticWriteProperty() { 259 } 260 261 // To set the value using the set method. 262 ValueType operator =(const ValueType& Value) { 263 (*setter)(Value); 264 return Value; 265 } 266 }; 267 268 269 class Process { 270 protected: 271 std::list<TString> FOutput; 272 273 public: 274 Process() { 275 Output.SetInstance(this); 276 Input.SetInstance(this); 277 } 278 279 virtual ~Process() {} 280 281 virtual bool IsRunning() = 0; 282 virtual bool Terminate() = 0; 283 virtual bool Execute(const TString Application, 284 const std::vector<TString> Arguments, bool AWait = false) = 0; 285 virtual bool Wait() = 0; 286 virtual TProcessID GetProcessID() = 0; 287 288 virtual std::list<TString> GetOutput() { return FOutput; } 318 } 319 320 T& operator* () const { 321 return *FObject; 322 } 323 324 T* operator->() const { 325 return FObject; 326 } 327 328 T** operator&() { 329 return &FObject; 330 } 331 332 T* operator=(const T * rhs) { 333 FObject = rhs; 334 return FObject; 335 } 336 }; 337 338 339 class IPropertyContainer { 340 public: 341 IPropertyContainer(void) {} 342 virtual ~IPropertyContainer(void) {} 343 344 virtual bool GetValue(const TString Key, TString& Value) = 0; 345 virtual size_t GetCount() = 0; 346 }; 347 348 class ISectionalPropertyContainer { 349 public: 350 ISectionalPropertyContainer(void) {} 351 virtual ~ISectionalPropertyContainer(void) {} 352 353 virtual bool GetValue(const TString SectionName, 354 const TString Key, TString& Value) = 0; 355 virtual bool ContainsSection(const TString SectionName) = 0; 356 virtual bool GetSection(const TString SectionName, 357 OrderedMap<TString, TString> &Data) = 0; 358 }; 359 360 class Environment { 361 private: 362 Environment() { 363 } 364 365 public: 366 static TString GetNewLine() { 367 #ifdef WINDOWS 368 return _T("\r\n"); 369 #endif //WINDOWS 370 #ifdef POSIX 371 return _T("\n"); 372 #endif //POSIX 373 } 374 375 static StaticReadProperty<TString, &Environment::GetNewLine> NewLine; 376 }; 377 378 379 enum DebugState {dsNone, dsNative, dsJava}; 380 enum MessageResponse {mrOK, mrCancel}; 381 enum AppCDSState {cdsUninitialized, cdsDisabled, 382 cdsEnabled, cdsAuto, cdsGenCache}; 383 384 class Platform { 385 private: 386 AppCDSState FAppCDSState; 387 388 protected: 389 Platform(void): FAppCDSState(cdsUninitialized) { 390 } 391 392 public: 393 AppCDSState GetAppCDSState() { return FAppCDSState; } 394 void SetAppCDSState(AppCDSState Value) { FAppCDSState = Value; } 395 396 static Platform& GetInstance(); 397 398 virtual ~Platform(void) {} 407 408 // Caller must free result using delete[]. 409 virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, 410 bool &release) = 0; 411 412 // Caller must free result using delete[]. 413 virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, 414 bool &release) = 0; 415 416 // Returns: 417 // Windows=C:\Users\<username>\AppData\Local 418 // Linux=~/.local 419 // Mac=~/Library/Application Support 420 virtual TString GetAppDataDirectory() = 0; 421 422 virtual TString GetPackageAppDirectory() = 0; 423 virtual TString GetPackageLauncherDirectory() = 0; 424 virtual TString GetPackageRuntimeBinDirectory() = 0; 425 virtual TString GetAppName() = 0; 426 427 virtual TString GetConfigFileName() = 0; 428 429 virtual TString GetBundledJVMLibraryFileName(TString RuntimePath) = 0; 430 431 // Caller must free result. 432 virtual ISectionalPropertyContainer* GetConfigFile(TString FileName) = 0; 433 434 virtual TString GetModuleFileName() = 0; 435 virtual TString GetPackageRootDirectory() = 0; 436 437 virtual Module LoadLibrary(TString FileName) = 0; 438 virtual void FreeLibrary(Module Module) = 0; 439 virtual Procedure GetProcAddress(Module Module, std::string MethodName) = 0; 440 virtual std::vector<TString> GetLibraryImports(const TString FileName) = 0; 441 virtual std::vector<TString> FilterOutRuntimeDependenciesForPlatform( 442 std::vector<TString> Imports) = 0; 443 444 // Caller must free result. 445 virtual Process* CreateProcess() = 0; 446 447 virtual bool IsMainThread() = 0; 448 449 // Returns megabytes. 450 virtual TPlatformNumber GetMemorySize() = 0; 451 452 virtual std::map<TString, TString> GetKeys() = 0; 453 454 virtual std::list<TString> LoadFromFile(TString FileName) = 0; 455 virtual void SaveToFile(TString FileName, 456 std::list<TString> Contents, bool ownerOnly) = 0; 457 458 virtual TString GetTempDirectory() = 0; 459 460 #ifdef DEBUG 461 virtual DebugState GetDebugState() = 0; 462 virtual int GetProcessID() = 0; 463 virtual bool IsNativeDebuggerPresent() = 0; 464 #endif //DEBUG 465 }; 466 467 468 class Library { 469 private: 470 std::vector<TString> *FDependentLibraryNames; 471 std::vector<Library*> *FDependenciesLibraries; 472 Module FModule; 473 std::string fname; 474 475 void Initialize(); 476 void InitializeDependencies(); 477 void LoadDependencies(); 478 void UnloadDependencies(); 479 480 public: 481 void* GetProcAddress(const std::string& MethodName) const; 482 483 public: 484 Library(); 485 Library(const TString &FileName); 486 ~Library(); 487 488 bool Load(const TString &FileName); 489 bool Unload(); 490 491 const std::string& GetName() const { 492 return fname; 493 } 494 495 void AddDependency(const TString &FileName); 496 void AddDependencies(const std::vector<TString> &Dependencies); 497 }; 498 499 500 class Exception: public std::exception { 501 private: 502 TString FMessage; 503 504 protected: 505 void SetMessage(const TString Message) { 506 FMessage = Message; 507 } 508 509 public: 510 explicit Exception() : exception() {} 511 explicit Exception(const TString Message) : exception() { 512 SetMessage(Message); 513 } 514 virtual ~Exception() throw() {} 515 516 TString GetMessage() { return FMessage; } 517 }; 518 519 class FileNotFoundException: public Exception { 520 public: 521 explicit FileNotFoundException(const TString Message) : Exception(Message) {} 522 }; 523 524 #endif // PLATFORM_H | 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 PLATFORM_H 27 #define PLATFORM_H 28 29 #include "PlatformDefs.h" 30 #include "Properties.h" 31 #include "OrderedMap.h" 32 #include "Library.h" 33 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <memory.h> 37 #include <string> 38 #include <map> 39 #include <list> 40 #include <vector> 41 #include <fstream> 42 43 using namespace std; 44 45 // Config file sections 46 #define CONFIG_SECTION_APPLICATION _T("CONFIG_SECTION_APPLICATION") 47 #define CONFIG_SECTION_JVMOPTIONS _T("CONFIG_SECTION_JVMOPTIONS") 48 #define CONFIG_SECTION_APPCDSJVMOPTIONS _T("CONFIG_SECTION_APPCDSJVMOPTIONS") 49 #define CONFIG_SECTION_ARGOPTIONS _T("CONFIG_SECTION_ARGOPTIONS") 50 #define CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS \ 51 _T("CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS") 52 53 // Config file keys. 54 #define CONFIG_VERSION _T("CONFIG_VERSION") 55 #define CONFIG_MAINJAR_KEY _T("CONFIG_MAINJAR_KEY") 56 #define CONFIG_MAINMODULE_KEY _T("CONFIG_MAINMODULE_KEY") 57 #define CONFIG_MAINCLASSNAME_KEY _T("CONFIG_MAINCLASSNAME_KEY") 58 #define CONFIG_CLASSPATH_KEY _T("CONFIG_CLASSPATH_KEY") 59 #define CONFIG_MODULEPATH_KEY _T("CONFIG_MODULEPATH_KEY") 60 #define APP_NAME_KEY _T("APP_NAME_KEY") 61 #define CONFIG_SPLASH_KEY _T("CONFIG_SPLASH_KEY") 62 #define CONFIG_APP_ID_KEY _T("CONFIG_APP_ID_KEY") 63 #define CONFIG_APP_MEMORY _T("CONFIG_APP_MEMORY") 64 #define CONFIG_APP_DEBUG _T("CONFIG_APP_DEBUG") 65 #define CONFIG_APPLICATION_INSTANCE _T("CONFIG_APPLICATION_INSTANCE") 66 67 #define JVM_RUNTIME_KEY _T("JVM_RUNTIME_KEY") 68 #define JPACKAGE_APP_DATA_DIR _T("CONFIG_APP_IDENTIFIER") 69 70 struct WideString { 71 size_t length; 72 wchar_t* data; 73 74 WideString() { length = 0; data = NULL; } 75 }; 76 77 struct MultibyteString { 78 size_t length; 79 char* data; 80 81 MultibyteString() { length = 0; data = NULL; } 82 }; 83 84 class Process { 85 protected: 86 std::list<TString> FOutput; 87 88 public: 89 Process() { 90 Output.SetInstance(this); 91 Input.SetInstance(this); 92 } 93 94 virtual ~Process() {} 95 96 virtual bool IsRunning() = 0; 97 virtual bool Terminate() = 0; 98 virtual bool Execute(const TString Application, 99 const std::vector<TString> Arguments, bool AWait = false) = 0; 100 virtual bool Wait() = 0; 101 virtual TProcessID GetProcessID() = 0; 102 103 virtual std::list<TString> GetOutput() { return FOutput; } 133 } 134 135 T& operator* () const { 136 return *FObject; 137 } 138 139 T* operator->() const { 140 return FObject; 141 } 142 143 T** operator&() { 144 return &FObject; 145 } 146 147 T* operator=(const T * rhs) { 148 FObject = rhs; 149 return FObject; 150 } 151 }; 152 153 enum DebugState {dsNone, dsNative, dsJava}; 154 enum MessageResponse {mrOK, mrCancel}; 155 enum AppCDSState {cdsUninitialized, cdsDisabled, 156 cdsEnabled, cdsAuto, cdsGenCache}; 157 158 class Platform { 159 private: 160 AppCDSState FAppCDSState; 161 162 protected: 163 Platform(void): FAppCDSState(cdsUninitialized) { 164 } 165 166 public: 167 AppCDSState GetAppCDSState() { return FAppCDSState; } 168 void SetAppCDSState(AppCDSState Value) { FAppCDSState = Value; } 169 170 static Platform& GetInstance(); 171 172 virtual ~Platform(void) {} 181 182 // Caller must free result using delete[]. 183 virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, 184 bool &release) = 0; 185 186 // Caller must free result using delete[]. 187 virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, 188 bool &release) = 0; 189 190 // Returns: 191 // Windows=C:\Users\<username>\AppData\Local 192 // Linux=~/.local 193 // Mac=~/Library/Application Support 194 virtual TString GetAppDataDirectory() = 0; 195 196 virtual TString GetPackageAppDirectory() = 0; 197 virtual TString GetPackageLauncherDirectory() = 0; 198 virtual TString GetPackageRuntimeBinDirectory() = 0; 199 virtual TString GetAppName() = 0; 200 201 virtual TString GetConfigFileName(); 202 203 virtual TString GetBundledJVMLibraryFileName(TString RuntimePath) = 0; 204 205 // Caller must free result. 206 virtual ISectionalPropertyContainer* GetConfigFile(TString FileName) = 0; 207 208 virtual TString GetModuleFileName() = 0; 209 virtual TString GetPackageRootDirectory() = 0; 210 211 virtual Module LoadLibrary(TString FileName) = 0; 212 virtual void FreeLibrary(Module Module) = 0; 213 virtual Procedure GetProcAddress(Module Module, std::string MethodName) = 0; 214 215 // Caller must free result. 216 virtual Process* CreateProcess() = 0; 217 218 virtual bool IsMainThread() = 0; 219 220 // Returns megabytes. 221 virtual TPlatformNumber GetMemorySize() = 0; 222 223 virtual std::map<TString, TString> GetKeys(); 224 225 virtual void InitStreamLocale(wios *stream) = 0; 226 virtual std::list<TString> LoadFromFile(TString FileName); 227 virtual void SaveToFile(TString FileName, 228 std::list<TString> Contents, bool ownerOnly); 229 230 virtual TString GetTempDirectory() = 0; 231 232 virtual void addPlatformDependencies(JavaLibrary *pJavaLibrary) = 0; 233 234 public: 235 // String helpers 236 // Caller must free result using delete[]. 237 static void CopyString(char *Destination, 238 size_t NumberOfElements, const char *Source); 239 240 // Caller must free result using delete[]. 241 static void CopyString(wchar_t *Destination, 242 size_t NumberOfElements, const wchar_t *Source); 243 244 static WideString MultibyteStringToWideString(const char* value); 245 static MultibyteString WideStringToMultibyteString(const wchar_t* value); 246 247 #ifdef DEBUG 248 virtual DebugState GetDebugState() = 0; 249 virtual int GetProcessID() = 0; 250 virtual bool IsNativeDebuggerPresent() = 0; 251 #endif //DEBUG 252 }; 253 254 class Exception: public std::exception { 255 private: 256 TString FMessage; 257 258 protected: 259 void SetMessage(const TString Message) { 260 FMessage = Message; 261 } 262 263 public: 264 explicit Exception() : exception() {} 265 explicit Exception(const TString Message) : exception() { 266 SetMessage(Message); 267 } 268 virtual ~Exception() throw() {} 269 270 TString GetMessage() { return FMessage; } 271 }; 272 273 #endif // PLATFORM_H |