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.BooleanSupplier; 21 import java.util.function.IntSupplier; 22 import java.util.function.LongSupplier; 23 import java.util.function.Supplier; 24 25 import org.apache.commons.lang3.function.Suppliers; 26 27 /** 28 * Accesses current system property names and values. 29 * 30 * @since 3.13.0 31 */ 32 public final class SystemProperties { 33 34 /** 35 * The System property name {@value}. 36 */ 37 public static final String AWT_TOOLKIT = "awt.toolkit"; 38 39 /** 40 * The System property name {@value}. 41 */ 42 public static final String FILE_ENCODING = "file.encoding"; 43 44 /** 45 * The System property name {@value}. 46 */ 47 public static final String FILE_SEPARATOR = "file.separator"; 48 49 /** 50 * The System property name {@value}. 51 */ 52 public static final String JAVA_AWT_FONTS = "java.awt.fonts"; 53 54 /** 55 * The System property name {@value}. 56 */ 57 public static final String JAVA_AWT_GRAPHICSENV = "java.awt.graphicsenv"; 58 59 /** 60 * The System property name {@value}. 61 */ 62 public static final String JAVA_AWT_HEADLESS = "java.awt.headless"; 63 64 /** 65 * The System property name {@value}. 66 */ 67 public static final String JAVA_AWT_PRINTERJOB = "java.awt.printerjob"; 68 69 /** 70 * The System property name {@value}. 71 */ 72 public static final String JAVA_CLASS_PATH = "java.class.path"; 73 74 /** 75 * The System property name {@value}. 76 */ 77 public static final String JAVA_CLASS_VERSION = "java.class.version"; 78 79 /** 80 * The System property name {@value}. 81 */ 82 public static final String JAVA_COMPILER = "java.compiler"; 83 84 /** 85 * The System property name {@value}. 86 */ 87 public static final String JAVA_ENDORSED_DIRS = "java.endorsed.dirs"; 88 89 /** 90 * The System property name {@value}. 91 */ 92 public static final String JAVA_EXT_DIRS = "java.ext.dirs"; 93 94 /** 95 * The System property name {@value}. 96 */ 97 public static final String JAVA_HOME = "java.home"; 98 99 /** 100 * The System property name {@value}. 101 */ 102 public static final String JAVA_IO_TMPDIR = "java.io.tmpdir"; 103 104 /** 105 * The System property name {@value}. 106 */ 107 public static final String JAVA_LIBRARY_PATH = "java.library.path"; 108 109 /** 110 * The System property name {@value}. 111 */ 112 public static final String JAVA_LOCALE_PROVIDERS = "java.locale.providers"; 113 114 /** 115 * The System property name {@value}. 116 */ 117 public static final String JAVA_RUNTIME_NAME = "java.runtime.name"; 118 119 /** 120 * The System property name {@value}. 121 */ 122 public static final String JAVA_RUNTIME_VERSION = "java.runtime.version"; 123 124 /** 125 * The System property name {@value}. 126 */ 127 public static final String JAVA_SPECIFICATION_NAME = "java.specification.name"; 128 129 /** 130 * The System property name {@value}. 131 */ 132 public static final String JAVA_SPECIFICATION_VENDOR = "java.specification.vendor"; 133 134 /** 135 * The System property name {@value}. 136 */ 137 public static final String JAVA_SPECIFICATION_VERSION = "java.specification.version"; 138 139 /** 140 * The System property name {@value}. 141 */ 142 public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = "java.util.prefs.PreferencesFactory"; 143 144 /** 145 * The System property name {@value}. 146 */ 147 public static final String JAVA_VENDOR = "java.vendor"; 148 149 /** 150 * The System property name {@value}. 151 */ 152 public static final String JAVA_VENDOR_URL = "java.vendor.url"; 153 154 /** 155 * The System property name {@value}. 156 */ 157 public static final String JAVA_VERSION = "java.version"; 158 159 /** 160 * The System property name {@value}. 161 */ 162 public static final String JAVA_VM_INFO = "java.vm.info"; 163 164 /** 165 * The System property name {@value}. 166 */ 167 public static final String JAVA_VM_NAME = "java.vm.name"; 168 169 /** 170 * The System property name {@value}. 171 */ 172 public static final String JAVA_VM_SPECIFICATION_NAME = "java.vm.specification.name"; 173 174 /** 175 * The System property name {@value}. 176 */ 177 public static final String JAVA_VM_SPECIFICATION_VENDOR = "java.vm.specification.vendor"; 178 179 /** 180 * The System property name {@value}. 181 */ 182 public static final String JAVA_VM_SPECIFICATION_VERSION = "java.vm.specification.version"; 183 184 /** 185 * The System property name {@value}. 186 */ 187 public static final String JAVA_VM_VENDOR = "java.vm.vendor"; 188 189 /** 190 * The System property name {@value}. 191 */ 192 public static final String JAVA_VM_VERSION = "java.vm.version"; 193 194 /** 195 * The System property name {@value}. 196 */ 197 public static final String LINE_SEPARATOR = "line.separator"; 198 199 /** 200 * The System property name {@value}. 201 */ 202 public static final String OS_ARCH = "os.arch"; 203 204 /** 205 * The System property name {@value}. 206 */ 207 public static final String OS_NAME = "os.name"; 208 209 /** 210 * The System property name {@value}. 211 */ 212 public static final String OS_VERSION = "os.version"; 213 214 /** 215 * The System property name {@value}. 216 */ 217 public static final String PATH_SEPARATOR = "path.separator"; 218 219 /** 220 * The System property name {@value}. 221 */ 222 public static final String USER_COUNTRY = "user.country"; 223 224 /** 225 * The System property name {@value}. 226 */ 227 public static final String USER_DIR = "user.dir"; 228 229 /** 230 * The System property name {@value}. 231 */ 232 public static final String USER_HOME = "user.home"; 233 234 /** 235 * The System property name {@value}. 236 */ 237 public static final String USER_LANGUAGE = "user.language"; 238 239 /** 240 * The System property name {@value}. 241 */ 242 public static final String USER_NAME = "user.name"; 243 244 /** 245 * The System property name {@value}. 246 */ 247 public static final String USER_REGION = "user.region"; 248 249 /** 250 * The System property name {@value}. 251 */ 252 public static final String USER_TIMEZONE = "user.timezone"; 253 254 /** 255 * Gets the current value from the system properties map. 256 * <p> 257 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 258 * </p> 259 * 260 * @return the current value from the system properties map. 261 */ 262 public static String getAwtToolkit() { 263 return getProperty(AWT_TOOLKIT); 264 } 265 266 /** 267 * Gets the current value for the property named {@code key} as an {@code boolean}. 268 * 269 * @param key 270 * The key 271 * @param defaultIfAbsent 272 * The default value 273 * @return an {@code boolean} or defaultIfAbsent 274 */ 275 public static boolean getBoolean(final String key, final BooleanSupplier defaultIfAbsent) { 276 final String str = getProperty(key); 277 return str == null ? defaultIfAbsent != null && defaultIfAbsent.getAsBoolean() : Boolean.parseBoolean(str); 278 } 279 280 /** 281 * Gets the current value from the system properties map. 282 * <p> 283 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 284 * </p> 285 * <p> 286 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 287 * </p> 288 * 289 * @return the current value from the system properties map. 290 */ 291 public static String getFileEncoding() { 292 return getProperty(FILE_ENCODING); 293 } 294 295 /** 296 * Gets the current value from the system properties map. 297 * <p> 298 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 299 * </p> 300 * 301 * @return the current value from the system properties map. 302 */ 303 public static String getFileSeparator() { 304 return getProperty(FILE_SEPARATOR); 305 } 306 307 /** 308 * Gets the current value for the property named {@code key} as an {@code int}. 309 * 310 * @param key 311 * The key 312 * @param defaultIfAbsent 313 * The default value 314 * @return an {@code int} or defaultIfAbsent 315 */ 316 public static int getInt(final String key, final IntSupplier defaultIfAbsent) { 317 final String str = getProperty(key); 318 return str == null ? defaultIfAbsent != null ? defaultIfAbsent.getAsInt() : 0 : Integer.parseInt(str); 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 */ 329 public static String getJavaAwtFonts() { 330 return getProperty(JAVA_AWT_FONTS); 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 */ 341 public static String getJavaAwtGraphicsenv() { 342 return getProperty(JAVA_AWT_GRAPHICSENV); 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 */ 353 public static String getJavaAwtHeadless() { 354 return getProperty(JAVA_AWT_HEADLESS); 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 */ 365 public static String getJavaAwtPrinterjob() { 366 return getProperty(JAVA_AWT_PRINTERJOB); 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 */ 377 public static String getJavaClassPath() { 378 return getProperty(JAVA_CLASS_PATH); 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 */ 389 public static String getJavaClassVersion() { 390 return getProperty(JAVA_CLASS_VERSION); 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 */ 401 public static String getJavaCompiler() { 402 return getProperty(JAVA_COMPILER); 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 */ 413 public static String getJavaEndorsedDirs() { 414 return getProperty(JAVA_ENDORSED_DIRS); 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 */ 425 public static String getJavaExtDirs() { 426 return getProperty(JAVA_EXT_DIRS); 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 */ 437 public static String getJavaHome() { 438 return getProperty(JAVA_HOME); 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 */ 449 public static String getJavaIoTmpdir() { 450 return getProperty(JAVA_IO_TMPDIR); 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 */ 461 public static String getJavaLibraryPath() { 462 return getProperty(JAVA_LIBRARY_PATH); 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 * <p> 471 * Java 9 and above. 472 * </p> 473 * @return the current value from the system properties map. 474 */ 475 public static String getJavaLocaleProviders() { 476 return getProperty(JAVA_LOCALE_PROVIDERS); 477 } 478 479 /** 480 * Gets the current value from the system properties map. 481 * <p> 482 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 483 * </p> 484 * 485 * @return the current value from the system properties map. 486 */ 487 public static String getJavaRuntimeName() { 488 return getProperty(JAVA_RUNTIME_NAME); 489 } 490 491 /** 492 * Gets the current value from the system properties map. 493 * <p> 494 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 495 * </p> 496 * 497 * @return the current value from the system properties map. 498 */ 499 public static String getJavaRuntimeVersion() { 500 return getProperty(JAVA_RUNTIME_VERSION); 501 } 502 503 /** 504 * Gets the current value from the system properties map. 505 * <p> 506 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 507 * </p> 508 * 509 * @return the current value from the system properties map. 510 */ 511 public static String getJavaSpecificationName() { 512 return getProperty(JAVA_SPECIFICATION_NAME); 513 } 514 515 /** 516 * Gets the current value from the system properties map. 517 * <p> 518 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 519 * </p> 520 * 521 * @return the current value from the system properties map. 522 */ 523 public static String getJavaSpecificationVendor() { 524 return getProperty(JAVA_SPECIFICATION_VENDOR); 525 } 526 527 /** 528 * Gets the current value from the system properties map. 529 * <p> 530 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 531 * </p> 532 * 533 * @return the current value from the system properties map. 534 */ 535 public static String getJavaSpecificationVersion() { 536 return getProperty(JAVA_SPECIFICATION_VERSION); 537 } 538 539 /** 540 * Gets the current value from the system properties map. 541 * <p> 542 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 543 * </p> 544 * 545 * @return the current value from the system properties map. 546 */ 547 public static String getJavaUtilPrefsPreferencesFactory() { 548 return getProperty(JAVA_UTIL_PREFS_PREFERENCES_FACTORY); 549 } 550 551 /** 552 * Gets the current value from the system properties map. 553 * <p> 554 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 555 * </p> 556 * 557 * @return the current value from the system properties map. 558 */ 559 public static String getJavaVendor() { 560 return getProperty(JAVA_VENDOR); 561 } 562 563 /** 564 * Gets the current value from the system properties map. 565 * <p> 566 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 567 * </p> 568 * 569 * @return the current value from the system properties map. 570 */ 571 public static String getJavaVendorUrl() { 572 return getProperty(JAVA_VENDOR_URL); 573 } 574 575 /** 576 * Gets the current value from the system properties map. 577 * <p> 578 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 579 * </p> 580 * 581 * @return the current value from the system properties map. 582 */ 583 public static String getJavaVersion() { 584 return getProperty(JAVA_VERSION); 585 } 586 587 /** 588 * Gets the current value from the system properties map. 589 * <p> 590 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 591 * </p> 592 * 593 * @return the current value from the system properties map. 594 */ 595 public static String getJavaVmInfo() { 596 return getProperty(JAVA_VM_INFO); 597 } 598 599 /** 600 * Gets the current value from the system properties map. 601 * <p> 602 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 603 * </p> 604 * 605 * @return the current value from the system properties map. 606 */ 607 public static String getJavaVmName() { 608 return getProperty(JAVA_VM_NAME); 609 } 610 611 /** 612 * Gets the current value from the system properties map. 613 * <p> 614 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 615 * </p> 616 * 617 * @return the current value from the system properties map. 618 */ 619 public static String getJavaVmSpecificationName() { 620 return getProperty(JAVA_VM_SPECIFICATION_NAME); 621 } 622 623 /** 624 * Gets the current value from the system properties map. 625 * <p> 626 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 627 * </p> 628 * 629 * @return the current value from the system properties map. 630 */ 631 public static String getJavaVmSpecificationVendor() { 632 return getProperty(JAVA_VM_SPECIFICATION_VENDOR); 633 } 634 635 /** 636 * Gets the current value from the system properties map. 637 * <p> 638 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 639 * </p> 640 * 641 * @return the current value from the system properties map. 642 */ 643 public static String getJavaVmSpecificationVersion() { 644 return getProperty(JAVA_VM_SPECIFICATION_VERSION); 645 } 646 647 /** 648 * Gets the current value from the system properties map. 649 * <p> 650 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 651 * </p> 652 * 653 * @return the current value from the system properties map. 654 */ 655 public static String getJavaVmVendor() { 656 return getProperty(JAVA_VM_VENDOR); 657 } 658 659 /** 660 * Gets the current value from the system properties map. 661 * <p> 662 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 663 * </p> 664 * 665 * @return the current value from the system properties map. 666 */ 667 public static String getJavaVmVersion() { 668 return getProperty(JAVA_VM_VERSION); 669 } 670 671 /** 672 * Gets the current value from the system properties map. 673 * <p> 674 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 675 * </p> 676 * 677 * @return the current value from the system properties map. 678 */ 679 public static String getLineSeparator() { 680 return getProperty(LINE_SEPARATOR); 681 } 682 683 /** 684 * Gets the current value for the property named {@code key} as a {@code long}. 685 * 686 * @param key 687 * The key 688 * @param defaultIfAbsent 689 * The default value 690 * @return a {@code long} or defaultIfAbsent 691 */ 692 public static long getLong(final String key, final LongSupplier defaultIfAbsent) { 693 final String str = getProperty(key); 694 return str == null ? defaultIfAbsent != null ? defaultIfAbsent.getAsLong() : 0 : Long.parseLong(str); 695 } 696 697 /** 698 * Gets the current value from the system properties map. 699 * <p> 700 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 701 * </p> 702 * 703 * @return the current value from the system properties map. 704 */ 705 public static String getOsArch() { 706 return getProperty(OS_ARCH); 707 } 708 709 /** 710 * Gets the current value from the system properties map. 711 * <p> 712 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 713 * </p> 714 * 715 * @return the current value from the system properties map. 716 */ 717 public static String getOsName() { 718 return getProperty(OS_NAME); 719 } 720 721 /** 722 * Gets the current value from the system properties map. 723 * <p> 724 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 725 * </p> 726 * 727 * @return the current value from the system properties map. 728 */ 729 public static String getOsVersion() { 730 return getProperty(OS_VERSION); 731 } 732 733 /** 734 * Gets the current value from the system properties map. 735 * <p> 736 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 737 * </p> 738 * 739 * @return the current value from the system properties map. 740 */ 741 public static String getPathSeparator() { 742 return getProperty(PATH_SEPARATOR); 743 } 744 745 /** 746 * Gets a System property, defaulting to {@code null} if the property cannot be read. 747 * <p> 748 * If a {@link SecurityException} is caught, the return value is {@code null}. 749 * </p> 750 * 751 * @param property the system property name 752 * @return the system property value or {@code null} if a security problem occurs 753 */ 754 public static String getProperty(final String property) { 755 return getProperty(property, Suppliers.nul()); 756 } 757 758 /** 759 * Gets a System property, defaulting to {@code null} if the property cannot be read. 760 * <p> 761 * If a {@link SecurityException} is caught, the return value is {@code null}. 762 * </p> 763 * 764 * @param property the system property name. 765 * @param defaultValue get this Supplier when the property is empty or throws SecurityException. 766 * @return the system property value or {@code null} if a security problem occurs 767 */ 768 static String getProperty(final String property, final Supplier<String> defaultValue) { 769 try { 770 if (StringUtils.isEmpty(property)) { 771 return defaultValue.get(); 772 } 773 final String value = System.getProperty(property); 774 return StringUtils.getIfEmpty(value, defaultValue); 775 } catch (final SecurityException ignore) { 776 // We are not allowed to look at this property. 777 // 778 // System.err.println("Caught a SecurityException reading the system property '" + property 779 // + "'; the SystemUtils property value will default to null."); 780 return defaultValue.get(); 781 } 782 } 783 784 /** 785 * Gets the current value from the system properties map. 786 * <p> 787 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 788 * </p> 789 * 790 * @return the current value from the system properties map. 791 */ 792 public static String getUserCountry() { 793 return getProperty(USER_COUNTRY); 794 } 795 796 /** 797 * Gets the current value from the system properties map. 798 * <p> 799 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 800 * </p> 801 * 802 * @return the current value from the system properties map. 803 */ 804 public static String getUserDir() { 805 return getProperty(USER_DIR); 806 } 807 808 /** 809 * Gets the current value from the system properties map. 810 * <p> 811 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 812 * </p> 813 * 814 * @return the current value from the system properties map. 815 */ 816 public static String getUserHome() { 817 return getProperty(USER_HOME); 818 } 819 820 /** 821 * Gets the current value from the system properties map. 822 * <p> 823 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 824 * </p> 825 * 826 * @return the current value from the system properties map. 827 */ 828 public static String getUserLanguage() { 829 return getProperty(USER_LANGUAGE); 830 } 831 832 /** 833 * Gets the current value from the system properties map. 834 * <p> 835 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 836 * </p> 837 * 838 * @return the current value from the system properties map. 839 */ 840 public static String getUserName() { 841 return getProperty(USER_NAME); 842 } 843 844 /** 845 * Gets the current value from the system properties map. 846 * <p> 847 * Returns {@code null} if the property cannot be read due to a {@link SecurityException}. 848 * </p> 849 * 850 * @return the current value from the system properties map. 851 */ 852 public static String getUserTimezone() { 853 return getProperty(USER_TIMEZONE); 854 } 855 }