    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 java.util.HashMap;
    020import java.util.Map;
    021
    022import org.apache.commons.lang3.arch.Processor;
    023import org.apache.commons.lang3.stream.Streams;
    024
    025/**
    026 * Provides methods for identifying the architecture of the current JVM based on the {@code "os.arch"} system property.
    027 * <p>
    028 * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
    029 * </p>
    030 *
    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        init_Aarch_64Bit();
    050    }
    051
    052    private static void init_Aarch_64Bit() {
    053        addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.AARCH_64), "aarch64");
    054    }
    055
    056    private static void init_X86_32Bit() {
    057        addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.X86), "x86", "i386", "i486", "i586", "i686", "pentium");
    058    }
    059
    060    private static void init_X86_64Bit() {
    061        addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.X86), "x86_64", "amd64", "em64t", "universal");
    062    }
    063
    064    private static void init_IA64_32Bit() {
    065        addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.IA_64), "ia64_32", "ia64n");
    066    }
    067
    068    private static void init_IA64_64Bit() {
    069        addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.IA_64), "ia64", "ia64w");
    070    }
    071
    072    private static void init_PPC_32Bit() {
    073        addProcessors(new Processor(Processor.Arch.BIT_32, Processor.Type.PPC), "ppc", "power", "powerpc", "power_pc", "power_rs");
    074    }
    075
    076    private static void init_PPC_64Bit() {
    077        addProcessors(new Processor(Processor.Arch.BIT_64, Processor.Type.PPC), "ppc64", "power64", "powerpc64", "power_pc64", "power_rs64");
    078    }
    079
    080    /**
    081     * Adds the given {@link Processor} with the given key {@link String} to the map.
    082     *
    083     * @param key       The key as {@link String}.
    084     * @param processor The {@link Processor} to add.
    085     * @throws IllegalStateException If the key already exists.
    086     */
    087    private static void addProcessor(final String key, final Processor processor) {
    088        if (ARCH_TO_PROCESSOR.containsKey(key)) {
    089            throw new IllegalStateException("Key " + key + " already exists in processor map");
    090        }
    091        ARCH_TO_PROCESSOR.put(key, processor);
    092    }
    093
    094    /**
    095     * Adds the given {@link Processor} with the given keys to the map.
    096     *
    097     * @param keys      The keys.
    098     * @param processor The {@link Processor} to add.
    099     * @throws IllegalStateException If the key already exists.
    100     */
    101    private static void addProcessors(final Processor processor, final String... keys) {
    102        Streams.of(keys).forEach(e -> addProcessor(e, processor));
    103    }
    104
    105    /**
    106     * Gets a {@link Processor} object of the current JVM.
    107     *
    108     * <p>
    109     * Important: The {@code "os.arch"} system property returns the architecture used by the JVM not of the operating system.
    110     * </p>
    111     *
    112     * @return A {@link Processor} when supported, else {@code null}.
    113     */
    114    public static Processor getProcessor() {
    115        return getProcessor(SystemProperties.getOsArch());
    116    }
    117
    118    /**
    119     * 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
    120     * property.
    121     *
    122     * @param value A {@link String} like a value returned by the {@code os.arch} System Property.
    123     * @return A {@link Processor} when it exists, else {@code null}.
    124     */
    125    public static Processor getProcessor(final String value) {
    126        return ARCH_TO_PROCESSOR.get(value);
    127    }
    128
    129}