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 19 import java.util.HashMap; 20 import java.util.Map; 21 22 import org.apache.commons.lang3.arch.Processor; 23 import org.apache.commons.lang3.stream.Streams; 24 25 /** 26 * A utility class for the {@code os.arch} System Property. The class defines methods for 27 * identifying the architecture of the current JVM. 28 * <p> 29 * Important: The {@code os.arch} System Property returns the architecture used by the JVM 30 * not of the operating system. 31 * </p> 32 * @since 3.6 33 */ 34 public class ArchUtils { 35 36 private static final Map<String, Processor> ARCH_TO_PROCESSOR; 37 38 static { 39 ARCH_TO_PROCESSOR = new HashMap<>(); init()40 init(); 41 } 42 init()43 private static void init() { 44 init_X86_32Bit(); 45 init_X86_64Bit(); 46 init_IA64_32Bit(); 47 init_IA64_64Bit(); 48 init_PPC_32Bit(); 49 init_PPC_64Bit(); 50 init_Aarch_64Bit(); 51 } 52 init_Aarch_64Bit()53 private static void init_Aarch_64Bit() { 54 final Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.AARCH_64); 55 addProcessors(processor, "aarch64"); 56 } 57 init_X86_32Bit()58 private static void init_X86_32Bit() { 59 final Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.X86); 60 addProcessors(processor, "x86", "i386", "i486", "i586", "i686", "pentium"); 61 } 62 init_X86_64Bit()63 private static void init_X86_64Bit() { 64 final Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.X86); 65 addProcessors(processor, "x86_64", "amd64", "em64t", "universal"); 66 } 67 init_IA64_32Bit()68 private static void init_IA64_32Bit() { 69 final Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.IA_64); 70 addProcessors(processor, "ia64_32", "ia64n"); 71 } 72 init_IA64_64Bit()73 private static void init_IA64_64Bit() { 74 final Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.IA_64); 75 addProcessors(processor, "ia64", "ia64w"); 76 } 77 init_PPC_32Bit()78 private static void init_PPC_32Bit() { 79 final Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.PPC); 80 addProcessors(processor, "ppc", "power", "powerpc", "power_pc", "power_rs"); 81 } 82 init_PPC_64Bit()83 private static void init_PPC_64Bit() { 84 final Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.PPC); 85 addProcessors(processor, "ppc64", "power64", "powerpc64", "power_pc64", "power_rs64"); 86 } 87 88 /** 89 * Adds the given {@link Processor} with the given key {@link String} to the map. 90 * 91 * @param key The key as {@link String}. 92 * @param processor The {@link Processor} to add. 93 * @throws IllegalStateException If the key already exists. 94 */ addProcessor(final String key, final Processor processor)95 private static void addProcessor(final String key, final Processor processor) { 96 if (ARCH_TO_PROCESSOR.containsKey(key)) { 97 throw new IllegalStateException("Key " + key + " already exists in processor map"); 98 } 99 ARCH_TO_PROCESSOR.put(key, processor); 100 } 101 102 /** 103 * Adds the given {@link Processor} with the given keys to the map. 104 * 105 * @param keys The keys. 106 * @param processor The {@link Processor} to add. 107 * @throws IllegalStateException If the key already exists. 108 */ addProcessors(final Processor processor, final String... keys)109 private static void addProcessors(final Processor processor, final String... keys) { 110 Streams.of(keys).forEach(e -> addProcessor(e, processor)); 111 } 112 113 /** 114 * Gets a {@link Processor} object of the current JVM. 115 * 116 * <p> 117 * Important: The os.arch System Property returns the architecture used by the JVM 118 * not of the operating system. 119 * </p> 120 * 121 * @return A {@link Processor} when supported, else {@code null}. 122 */ getProcessor()123 public static Processor getProcessor() { 124 return getProcessor(SystemProperties.getOsArch()); 125 } 126 127 /** 128 * Gets a {@link Processor} object the given value {@link String}. The {@link String} must be 129 * like a value returned by the {@code os.arch} System Property. 130 * 131 * @param value A {@link String} like a value returned by the {@code os.arch} System Property. 132 * @return A {@link Processor} when it exists, else {@code null}. 133 */ getProcessor(final String value)134 public static Processor getProcessor(final String value) { 135 return ARCH_TO_PROCESSOR.get(value); 136 } 137 138 } 139