001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.lang3; 018 019import org.apache.commons.lang3.arch.Processor; 020 021import java.util.HashMap; 022import java.util.Map; 023 024/** 025 * An utility class for the os.arch System Property. The class defines methods for 026 * identifying the architecture of the current JVM. 027 * <p> 028 * Important: The os.arch System Property returns the architecture used by the JVM 029 * not of the operating system. 030 * </p> 031 * @since 3.6 032 */ 033public class ArchUtils { 034 035 private static final Map<String, Processor> ARCH_TO_PROCESSOR; 036 037 static { 038 ARCH_TO_PROCESSOR = new HashMap<>(); 039 init(); 040 } 041 042 private static void init() { 043 init_X86_32Bit(); 044 init_X86_64Bit(); 045 init_IA64_32Bit(); 046 init_IA64_64Bit(); 047 init_PPC_32Bit(); 048 init_PPC_64Bit(); 049 } 050 051 private static void init_X86_32Bit() { 052 Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.X86); 053 addProcessors(processor, "x86", "i386", "i486", "i586", "i686", "pentium"); 054 } 055 056 private static void init_X86_64Bit() { 057 Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.X86); 058 addProcessors(processor, "x86_64", "amd64", "em64t", "universal"); 059 } 060 061 private static void init_IA64_32Bit() { 062 Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.IA_64); 063 addProcessors(processor, "ia64_32", "ia64n"); 064 } 065 066 private static void init_IA64_64Bit() { 067 Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.IA_64); 068 addProcessors(processor, "ia64", "ia64w"); 069 } 070 071 private static void init_PPC_32Bit() { 072 Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.PPC); 073 addProcessors(processor, "ppc", "power", "powerpc", "power_pc", "power_rs"); 074 } 075 076 private static void init_PPC_64Bit() { 077 Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.PPC); 078 addProcessors(processor, "ppc64", "power64", "powerpc64", "power_pc64", "power_rs64"); 079 } 080 081 /** 082 * Adds the given {@link Processor} with the given key {@link String} to the map. 083 * 084 * @param key The key as {@link String}. 085 * @param processor The {@link Processor} to add. 086 * @throws IllegalStateException If the key already exists. 087 */ 088 private static void addProcessor(String key, Processor processor) throws IllegalStateException { 089 if (!ARCH_TO_PROCESSOR.containsKey(key)) { 090 ARCH_TO_PROCESSOR.put(key, processor); 091 } else { 092 String msg = "Key " + key + " already exists in processor map"; 093 throw new IllegalStateException(msg); 094 } 095 } 096 097 /** 098 * Adds the given {@link Processor} with the given keys to the map. 099 * 100 * @param keys The keys. 101 * @param processor The {@link Processor} to add. 102 * @throws IllegalStateException If the key already exists. 103 */ 104 private static void addProcessors(Processor processor, String... keys) throws IllegalStateException { 105 for (String key : keys) { 106 addProcessor(key, processor); 107 } 108 } 109 110 /** 111 * Returns a {@link Processor} object of the current JVM. 112 * 113 * <p> 114 * Important: The os.arch System Property returns the architecture used by the JVM 115 * not of the operating system. 116 * </p> 117 * 118 * @return A {@link Processor} when supported, else <code>null</code>. 119 */ 120 public static Processor getProcessor() { 121 return getProcessor(SystemUtils.OS_ARCH); 122 } 123 124 /** 125 * Returns a {@link Processor} object the given value {@link String}. The {@link String} must be 126 * like a value returned by the os.arch System Property. 127 * 128 * @param value A {@link String} like a value returned by the os.arch System Property. 129 * @return A {@link Processor} when it exists, else <code>null</code>. 130 */ 131 public static Processor getProcessor(String value) { 132 return ARCH_TO_PROCESSOR.get(value); 133 } 134 135}