1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.lang3; 19 20 import java.util.function.Supplier; 21 22 /** 23 * Accesses current system property names and values. 24 * 25 * @since 3.13.0 26 */ 27 public final class SystemProperties { 28 29 private static final Supplier<String> NULL_SUPPLIER = () -> null; 30 31 /** 32 * The System property name {@value}. 33 */ 34 public static final String AWT_TOOLKIT = "awt.toolkit"; 35 36 /** 37 * The System property name {@value}. 38 */ 39 public static final String FILE_ENCODING = "file.encoding"; 40 41 /** 42 * The System property name {@value}. 43 */ 44 public static final String FILE_SEPARATOR = "file.separator"; 45 46 /** 47 * The System property name {@value}. 48 */ 49 public static final String JAVA_AWT_FONTS = "java.awt.fonts"; 50 51 /** 52 * The System property name {@value}. 53 */ 54 public static final String JAVA_AWT_GRAPHICSENV = "java.awt.graphicsenv"; 55 56 /** 57 * The System property name {@value}. 58 */ 59 public static final String JAVA_AWT_HEADLESS = "java.awt.headless"; 60 61 /** 62 * The System property name {@value}. 63 */ 64 public static final String JAVA_AWT_PRINTERJOB = "java.awt.printerjob"; 65 66 /** 67 * The System property name {@value}. 68 */ 69 public static final String JAVA_CLASS_PATH = "java.class.path"; 70 71 /** 72 * The System property name {@value}. 73 */ 74 public static final String JAVA_CLASS_VERSION = "java.class.version"; 75 76 /** 77 * The System property name {@value}. 78 */ 79 public static final String JAVA_COMPILER = "java.compiler"; 80 81 /** 82 * The System property name {@value}. 83 */ 84 public static final String JAVA_ENDORSED_DIRS = "java.endorsed.dirs"; 85 86 /** 87 * The System property name {@value}. 88 */ 89 public static final String JAVA_EXT_DIRS = "java.ext.dirs"; 90 91 /** 92 * The System property name {@value}. 93 */ 94 public static final String JAVA_HOME = "java.home"; 95 96 /** 97 * The System property name {@value}. 98 */ 99 public static final String JAVA_IO_TMPDIR = "java.io.tmpdir"; 100 101 /** 102 * The System property name {@value}. 103 */ 104 public static final String JAVA_LIBRARY_PATH = "java.library.path"; 105 106 /** 107 * The System property name {@value}. 108 */ 109 public static final String JAVA_RUNTIME_NAME = "java.runtime.name"; 110 111 /** 112 * The System property name {@value}. 113 */ 114 public static final String JAVA_RUNTIME_VERSION = "java.runtime.version"; 115 116 /** 117 * The System property name {@value}. 118 */ 119 public static final String JAVA_SPECIFICATION_NAME = "java.specification.name"; 120 121 /** 122 * The System property name {@value}. 123 */ 124 public static final String JAVA_SPECIFICATION_VENDOR = "java.specification.vendor"; 125 126 /** 127 * The System property name {@value}. 128 */ 129 public static final String JAVA_SPECIFICATION_VERSION = "java.specification.version"; 130 131 /** 132 * The System property name {@value}. 133 */ 134 public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = "java.util.prefs.PreferencesFactory"; 135 136 /** 137 * The System property name {@value}. 138 */ 139 public static final String JAVA_VENDOR = "java.vendor"; 140 141 /** 142 * The System property name {@value}. 143 */ 144 public static final String JAVA_VENDOR_URL = "java.vendor.url"; 145 146 /** 147 * The System property name {@value}. 148 */ 149 public static final String JAVA_VERSION = "java.version"; 150 151 /** 152 * The System property name {@value}. 153 */ 154 public static final String JAVA_VM_INFO = "java.vm.info"; 155 156 /** 157 * The System property name {@value}. 158 */ 159 public static final String JAVA_VM_NAME = "java.vm.name"; 160 161 /** 162 * The System property name {@value}. 163 */ 164 public static final String JAVA_VM_SPECIFICATION_NAME = "java.vm.specification.name"; 165 166 /** 167 * The System property name {@value}. 168 */ 169 public static final String JAVA_VM_SPECIFICATION_VENDOR = "java.vm.specification.vendor"; 170 171 /** 172 * The System property name {@value}. 173 */ 174 public static final String JAVA_VM_SPECIFICATION_VERSION = "java.vm.specification.version"; 175 176 /** 177 * The System property name {@value}. 178 */ 179 public static final String JAVA_VM_VENDOR = "java.vm.vendor"; 180 181 /** 182 * The System property name {@value}. 183 */ 184 public static final String JAVA_VM_VERSION = "java.vm.version"; 185 186 /** 187 * The System property name {@value}. 188 */ 189 public static final String LINE_SEPARATOR = "line.separator"; 190 191 /** 192 * The System property name {@value}. 193 */ 194 public static final String OS_ARCH = "os.arch"; 195 196 /** 197 * The System property name {@value}. 198 */ 199 public static final String OS_NAME = "os.name"; 200 201 /** 202 * The System property name {@value}. 203 */ 204 public static final String OS_VERSION = "os.version"; 205 206 /** 207 * The System property name {@value}. 208 */ 209 public static final String PATH_SEPARATOR = "path.separator"; 210 211 /** 212 * The System property name {@value}. 213 */ 214 public static final String USER_COUNTRY = "user.country"; 215 216 /** 217 * The System property name {@value}. 218 */ 219 public static final String USER_DIR = "user.dir"; 220 221 /** 222 * The System property name {@value}. 223 */ 224 public static final String USER_HOME = "user.home"; 225 226 /** 227 * The System property name {@value}. 228 */ 229 public static final String USER_LANGUAGE = "user.language"; 230 231 /** 232 * The System property name {@value}. 233 */ 234 public static final String USER_NAME = "user.name"; 235 236 /** 237 * The System property name {@value}. 238 */ 239 public static final String USER_REGION = "user.region"; 240 241 /** 242 * The System property name {@value}. 243 */ 244 public static final String USER_TIMEZONE = "user.timezone"; 245 246 /** 247 * Gets the current value from the system properties map. 248 * <p> 249 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 250 * </p> 251 * 252 * @return the current value from the system properties map. 253 */ getAwtToolkit()254 public static String getAwtToolkit() { 255 return getProperty(AWT_TOOLKIT); 256 } 257 258 /** 259 * Gets the current value from the system properties map. 260 * <p> 261 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 262 * </p> 263 * <p> 264 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 265 * </p> 266 * 267 * @return the current value from the system properties map. 268 */ getFileEncoding()269 public static String getFileEncoding() { 270 return getProperty(FILE_ENCODING); 271 } 272 273 /** 274 * Gets the current value from the system properties map. 275 * <p> 276 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 277 * </p> 278 * 279 * @return the current value from the system properties map. 280 */ getFileSeparator()281 public static String getFileSeparator() { 282 return getProperty(FILE_SEPARATOR); 283 } 284 285 /** 286 * Gets the current value from the system properties map. 287 * <p> 288 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 289 * </p> 290 * 291 * @return the current value from the system properties map. 292 */ getJavaAwtFonts()293 public static String getJavaAwtFonts() { 294 return getProperty(JAVA_AWT_FONTS); 295 } 296 297 /** 298 * Gets the current value from the system properties map. 299 * <p> 300 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 301 * </p> 302 * 303 * @return the current value from the system properties map. 304 */ getJavaAwtGraphicsenv()305 public static String getJavaAwtGraphicsenv() { 306 return getProperty(JAVA_AWT_GRAPHICSENV); 307 } 308 309 /** 310 * Gets the current value from the system properties map. 311 * <p> 312 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 313 * </p> 314 * 315 * @return the current value from the system properties map. 316 */ getJavaAwtHeadless()317 public static String getJavaAwtHeadless() { 318 return getProperty(JAVA_AWT_HEADLESS); 319 } 320 321 /** 322 * Gets the current value from the system properties map. 323 * <p> 324 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 325 * </p> 326 * 327 * @return the current value from the system properties map. 328 */ getJavaAwtPrinterjob()329 public static String getJavaAwtPrinterjob() { 330 return getProperty(JAVA_AWT_PRINTERJOB); 331 } 332 333 /** 334 * Gets the current value from the system properties map. 335 * <p> 336 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 337 * </p> 338 * 339 * @return the current value from the system properties map. 340 */ getJavaClassPath()341 public static String getJavaClassPath() { 342 return getProperty(JAVA_CLASS_PATH); 343 } 344 345 /** 346 * Gets the current value from the system properties map. 347 * <p> 348 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 349 * </p> 350 * 351 * @return the current value from the system properties map. 352 */ getJavaClassVersion()353 public static String getJavaClassVersion() { 354 return getProperty(JAVA_CLASS_VERSION); 355 } 356 357 /** 358 * Gets the current value from the system properties map. 359 * <p> 360 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 361 * </p> 362 * 363 * @return the current value from the system properties map. 364 */ getJavaCompiler()365 public static String getJavaCompiler() { 366 return getProperty(JAVA_COMPILER); 367 } 368 369 /** 370 * Gets the current value from the system properties map. 371 * <p> 372 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 373 * </p> 374 * 375 * @return the current value from the system properties map. 376 */ getJavaEndorsedDirs()377 public static String getJavaEndorsedDirs() { 378 return getProperty(JAVA_ENDORSED_DIRS); 379 } 380 381 /** 382 * Gets the current value from the system properties map. 383 * <p> 384 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 385 * </p> 386 * 387 * @return the current value from the system properties map. 388 */ getJavaExtDirs()389 public static String getJavaExtDirs() { 390 return getProperty(JAVA_EXT_DIRS); 391 } 392 393 /** 394 * Gets the current value from the system properties map. 395 * <p> 396 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 397 * </p> 398 * 399 * @return the current value from the system properties map. 400 */ getJavaHome()401 public static String getJavaHome() { 402 return getProperty(JAVA_HOME); 403 } 404 405 /** 406 * Gets the current value from the system properties map. 407 * <p> 408 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 409 * </p> 410 * 411 * @return the current value from the system properties map. 412 */ getJavaIoTmpdir()413 public static String getJavaIoTmpdir() { 414 return getProperty(JAVA_IO_TMPDIR); 415 } 416 417 /** 418 * Gets the current value from the system properties map. 419 * <p> 420 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 421 * </p> 422 * 423 * @return the current value from the system properties map. 424 */ getJavaLibraryPath()425 public static String getJavaLibraryPath() { 426 return getProperty(JAVA_LIBRARY_PATH); 427 } 428 429 /** 430 * Gets the current value from the system properties map. 431 * <p> 432 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 433 * </p> 434 * 435 * @return the current value from the system properties map. 436 */ getJavaRuntimeName()437 public static String getJavaRuntimeName() { 438 return getProperty(JAVA_RUNTIME_NAME); 439 } 440 441 /** 442 * Gets the current value from the system properties map. 443 * <p> 444 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 445 * </p> 446 * 447 * @return the current value from the system properties map. 448 */ getJavaRuntimeVersion()449 public static String getJavaRuntimeVersion() { 450 return getProperty(JAVA_RUNTIME_VERSION); 451 } 452 453 /** 454 * Gets the current value from the system properties map. 455 * <p> 456 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 457 * </p> 458 * 459 * @return the current value from the system properties map. 460 */ getJavaSpecificationName()461 public static String getJavaSpecificationName() { 462 return getProperty(JAVA_SPECIFICATION_NAME); 463 } 464 465 /** 466 * Gets the current value from the system properties map. 467 * <p> 468 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 469 * </p> 470 * 471 * @return the current value from the system properties map. 472 */ getJavaSpecificationVendor()473 public static String getJavaSpecificationVendor() { 474 return getProperty(JAVA_SPECIFICATION_VENDOR); 475 } 476 477 /** 478 * Gets the current value from the system properties map. 479 * <p> 480 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 481 * </p> 482 * 483 * @return the current value from the system properties map. 484 */ getJavaSpecificationVersion()485 public static String getJavaSpecificationVersion() { 486 return getProperty(JAVA_SPECIFICATION_VERSION); 487 } 488 489 /** 490 * Gets the current value from the system properties map. 491 * <p> 492 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 493 * </p> 494 * 495 * @return the current value from the system properties map. 496 */ getJavaUtilPrefsPreferencesFactory()497 public static String getJavaUtilPrefsPreferencesFactory() { 498 return getProperty(JAVA_UTIL_PREFS_PREFERENCES_FACTORY); 499 } 500 501 /** 502 * Gets the current value from the system properties map. 503 * <p> 504 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 505 * </p> 506 * 507 * @return the current value from the system properties map. 508 */ getJavaVendor()509 public static String getJavaVendor() { 510 return getProperty(JAVA_VENDOR); 511 } 512 513 /** 514 * Gets the current value from the system properties map. 515 * <p> 516 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 517 * </p> 518 * 519 * @return the current value from the system properties map. 520 */ getJavaVendorUrl()521 public static String getJavaVendorUrl() { 522 return getProperty(JAVA_VENDOR_URL); 523 } 524 525 /** 526 * Gets the current value from the system properties map. 527 * <p> 528 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 529 * </p> 530 * 531 * @return the current value from the system properties map. 532 */ getJavaVersion()533 public static String getJavaVersion() { 534 return getProperty(JAVA_VERSION); 535 } 536 537 /** 538 * Gets the current value from the system properties map. 539 * <p> 540 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 541 * </p> 542 * 543 * @return the current value from the system properties map. 544 */ getJavaVmInfo()545 public static String getJavaVmInfo() { 546 return getProperty(JAVA_VM_INFO); 547 } 548 549 /** 550 * Gets the current value from the system properties map. 551 * <p> 552 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 553 * </p> 554 * 555 * @return the current value from the system properties map. 556 */ getJavaVmName()557 public static String getJavaVmName() { 558 return getProperty(JAVA_VM_NAME); 559 } 560 561 /** 562 * Gets the current value from the system properties map. 563 * <p> 564 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 565 * </p> 566 * 567 * @return the current value from the system properties map. 568 */ getJavaVmSpecificationName()569 public static String getJavaVmSpecificationName() { 570 return getProperty(JAVA_VM_SPECIFICATION_NAME); 571 } 572 573 /** 574 * Gets the current value from the system properties map. 575 * <p> 576 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 577 * </p> 578 * 579 * @return the current value from the system properties map. 580 */ getJavaVmSpecificationVendor()581 public static String getJavaVmSpecificationVendor() { 582 return getProperty(JAVA_VM_SPECIFICATION_VENDOR); 583 } 584 585 /** 586 * Gets the current value from the system properties map. 587 * <p> 588 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 589 * </p> 590 * 591 * @return the current value from the system properties map. 592 */ getJavaVmSpecificationVersion()593 public static String getJavaVmSpecificationVersion() { 594 return getProperty(JAVA_VM_SPECIFICATION_VERSION); 595 } 596 597 /** 598 * Gets the current value from the system properties map. 599 * <p> 600 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 601 * </p> 602 * 603 * @return the current value from the system properties map. 604 */ getJavaVmVendor()605 public static String getJavaVmVendor() { 606 return getProperty(JAVA_VM_VENDOR); 607 } 608 609 /** 610 * Gets the current value from the system properties map. 611 * <p> 612 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 613 * </p> 614 * 615 * @return the current value from the system properties map. 616 */ getJavaVmVersion()617 public static String getJavaVmVersion() { 618 return getProperty(JAVA_VM_VERSION); 619 } 620 621 /** 622 * Gets the current value from the system properties map. 623 * <p> 624 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 625 * </p> 626 * 627 * @return the current value from the system properties map. 628 */ getLineSeparator()629 public static String getLineSeparator() { 630 return getProperty(LINE_SEPARATOR); 631 } 632 633 /** 634 * Gets the current value from the system properties map. 635 * <p> 636 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 637 * </p> 638 * 639 * @return the current value from the system properties map. 640 */ getOsArch()641 public static String getOsArch() { 642 return getProperty(OS_ARCH); 643 } 644 645 /** 646 * Gets the current value from the system properties map. 647 * <p> 648 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 649 * </p> 650 * 651 * @return the current value from the system properties map. 652 */ getOsName()653 public static String getOsName() { 654 return getProperty(OS_NAME); 655 } 656 657 /** 658 * Gets the current value from the system properties map. 659 * <p> 660 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 661 * </p> 662 * 663 * @return the current value from the system properties map. 664 */ getOsVersion()665 public static String getOsVersion() { 666 return getProperty(OS_VERSION); 667 } 668 669 /** 670 * Gets the current value from the system properties map. 671 * <p> 672 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 673 * </p> 674 * 675 * @return the current value from the system properties map. 676 */ getPathSeparator()677 public static String getPathSeparator() { 678 return getProperty(PATH_SEPARATOR); 679 } 680 681 /** 682 * Gets a System property, defaulting to {@code null} if the property cannot be read. 683 * <p> 684 * If a {@link SecurityException} is caught, the return value is {@code null}. 685 * </p> 686 * 687 * @param property the system property name 688 * @return the system property value or {@code null} if a security problem occurs 689 */ getProperty(final String property)690 public static String getProperty(final String property) { 691 return getProperty(property, NULL_SUPPLIER); 692 } 693 694 /** 695 * Gets a System property, defaulting to {@code null} if the property cannot be read. 696 * <p> 697 * If a {@link SecurityException} is caught, the return value is {@code null}. 698 * </p> 699 * 700 * @param property the system property name. 701 * @param defaultValue get this Supplier when the property is empty or throws SecurityException. 702 * @return the system property value or {@code null} if a security problem occurs 703 */ getProperty(final String property, final Supplier<String> defaultValue)704 static String getProperty(final String property, final Supplier<String> defaultValue) { 705 try { 706 if (StringUtils.isEmpty(property)) { 707 return defaultValue.get(); 708 } 709 final String value = System.getProperty(property); 710 return StringUtils.getIfEmpty(value, defaultValue); 711 } catch (final SecurityException ignore) { 712 // We are not allowed to look at this property. 713 // 714 // System.err.println("Caught a SecurityException reading the system property '" + property 715 // + "'; the SystemUtils property value will default to null."); 716 return defaultValue.get(); 717 } 718 } 719 720 /** 721 * Gets the current value from the system properties map. 722 * <p> 723 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 724 * </p> 725 * 726 * @return the current value from the system properties map. 727 */ getUserCountry()728 public static String getUserCountry() { 729 return getProperty(USER_COUNTRY); 730 } 731 732 /** 733 * Gets the current value from the system properties map. 734 * <p> 735 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 736 * </p> 737 * 738 * @return the current value from the system properties map. 739 */ getUserDir()740 public static String getUserDir() { 741 return getProperty(USER_DIR); 742 } 743 744 /** 745 * Gets the current value from the system properties map. 746 * <p> 747 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 748 * </p> 749 * 750 * @return the current value from the system properties map. 751 */ getUserHome()752 public static String getUserHome() { 753 return getProperty(USER_HOME); 754 } 755 756 /** 757 * Gets the current value from the system properties map. 758 * <p> 759 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 760 * </p> 761 * 762 * @return the current value from the system properties map. 763 */ getUserLanguage()764 public static String getUserLanguage() { 765 return getProperty(USER_LANGUAGE); 766 } 767 768 /** 769 * Gets the current value from the system properties map. 770 * <p> 771 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 772 * </p> 773 * 774 * @return the current value from the system properties map. 775 */ getUserName()776 public static String getUserName() { 777 return getProperty(USER_NAME); 778 } 779 780 /** 781 * Gets the current value from the system properties map. 782 * <p> 783 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 784 * </p> 785 * 786 * @return the current value from the system properties map. 787 */ getUserTimezone()788 public static String getUserTimezone() { 789 return getProperty(USER_TIMEZONE); 790 } 791 } 792