ArchUtils.java

  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. package org.apache.commons.lang3;

  18. import java.util.HashMap;
  19. import java.util.Map;

  20. import org.apache.commons.lang3.arch.Processor;
  21. import org.apache.commons.lang3.stream.Streams;

  22. /**
  23.  * Provides methods for identifying the architecture of the current JVM based on the {@code "os.arch"} system property.
  24.  * <p>
  25.  * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
  26.  * </p>
  27.  *
  28.  * @since 3.6
  29.  */
  30. public class ArchUtils {

  31.     private static final Map<String, Processor> ARCH_TO_PROCESSOR;

  32.     static {
  33.         ARCH_TO_PROCESSOR = new HashMap<>();
  34.         init();
  35.     }

  36.     /**
  37.      * Adds the given {@link Processor} with the given key {@link String} to the map.
  38.      *
  39.      * @param key       The key as {@link String}.
  40.      * @param processor The {@link Processor} to add.
  41.      * @throws IllegalStateException If the key already exists.
  42.      */
  43.     private static void addProcessor(final String key, final Processor processor) {
  44.         if (ARCH_TO_PROCESSOR.containsKey(key)) {
  45.             throw new IllegalStateException("Key " + key + " already exists in processor map");
  46.         }
  47.         ARCH_TO_PROCESSOR.put(key, processor);
  48.     }

  49.     /**
  50.      * Adds the given {@link Processor} with the given keys to the map.
  51.      *
  52.      * @param keys      The keys.
  53.      * @param processor The {@link Processor} to add.
  54.      * @throws IllegalStateException If the key already exists.
  55.      */
  56.     private static void addProcessors(final Processor processor, final String... keys) {
  57.         Streams.of(keys).forEach(e -> addProcessor(e, processor));
  58.     }

  59.     /**
  60.      * Gets a {@link Processor} object of the current JVM.
  61.      *
  62.      * <p>
  63.      * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
  64.      * </p>
  65.      *
  66.      * @return A {@link Processor} when supported, else {@code null}.
  67.      */
  68.     public static Processor getProcessor() {
  69.         return getProcessor(SystemProperties.getOsArch());
  70.     }

  71.     /**
  72.      * Gets a {@link Processor} object the given value {@link String}. The {@link String} must be like a value returned by the {@code "os.arch"} system
  73.      * property.
  74.      *
  75.      * @param value A {@link String} like a value returned by the {@code os.arch} System Property.
  76.      * @return A {@link Processor} when it exists, else {@code null}.
  77.      */
  78.     public static Processor getProcessor(final String value) {
  79.         return ARCH_TO_PROCESSOR.get(value);
  80.     }

  81.     private static void init() {
  82.         init_X86_32Bit();
  83.         init_X86_64Bit();
  84.         init_IA64_32Bit();
  85.         init_IA64_64Bit();
  86.         init_PPC_32Bit();
  87.         init_PPC_64Bit();
  88.         init_Aarch_64Bit();
  89.         init_RISCV_32Bit();
  90.         init_RISCV_64Bit();
  91.     }

  92.     private static void init_Aarch_64Bit() {
  93.         addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.AARCH_64), "aarch64");
  94.     }

  95.     private static void init_IA64_32Bit() {
  96.         addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.IA_64), "ia64_32", "ia64n");
  97.     }

  98.     private static void init_IA64_64Bit() {
  99.         addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.IA_64), "ia64", "ia64w");
  100.     }

  101.     private static void init_PPC_32Bit() {
  102.         addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.PPC), "ppc", "power", "powerpc", "power_pc", "power_rs");
  103.     }

  104.     private static void init_PPC_64Bit() {
  105.         addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.PPC), "ppc64", "power64", "powerpc64", "power_pc64", "power_rs64");
  106.     }

  107.     private static void init_RISCV_32Bit() {
  108.         addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.RISC_V), "riscv32");
  109.     }

  110.     private static void init_RISCV_64Bit() {
  111.         addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.RISC_V), "riscv64");
  112.     }

  113.     private static void init_X86_32Bit() {
  114.         addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.X86), "x86", "i386", "i486", "i586", "i686", "pentium");
  115.     }

  116.     private static void init_X86_64Bit() {
  117.         addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.X86), "x86_64", "amd64", "em64t", "universal");
  118.     }

  119.     /**
  120.      * Make private in 4.0.
  121.      *
  122.      * @deprecated TODO Make private in 4.0.
  123.      */
  124.     @Deprecated
  125.     public ArchUtils() {
  126.         // empty
  127.     }

  128. }