View Javadoc
1   /**
2    * Oshi (https://github.com/dblock/oshi)
3    * 
4    * Copyright (c) 2010 - 2015 The Oshi Project Team
5    * 
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   * 
11   * Contributors:
12   * dblock[at]dblock[dot]org
13   * alessandro[at]perucchi[dot]org
14   * widdis[at]gmail[dot]com
15   * https://github.com/dblock/oshi/graphs/contributors
16   */
17  package oshi.software.os.mac;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import oshi.hardware.HardwareAbstractionLayer;
23  import oshi.hardware.Memory;
24  import oshi.hardware.PowerSource;
25  import oshi.hardware.Processor;
26  import oshi.software.os.OSFileStore;
27  import oshi.software.os.mac.local.CentralProcessor;
28  import oshi.software.os.mac.local.GlobalMemory;
29  import oshi.software.os.mac.local.MacFileSystem;
30  import oshi.software.os.mac.local.MacPowerSource;
31  import oshi.software.os.mac.local.SystemB;
32  
33  import com.sun.jna.LastErrorException;
34  import com.sun.jna.Native;
35  import com.sun.jna.ptr.IntByReference;
36  
37  /**
38   * @author alessandro[at]perucchi[dot]org
39   * @author widdis[at]gmail[dot]com
40   */
41  public class MacHardwareAbstractionLayer implements HardwareAbstractionLayer {
42  
43  	private Processor[] _processors;
44  
45  	private Memory _memory;
46  
47  	private PowerSource[] _powerSources;
48  
49  	/*
50  	 * (non-Javadoc)
51  	 * 
52  	 * @see oshi.hardware.HardwareAbstractionLayer#getProcessors()
53  	 */
54  	@Override
55  	public Processor[] getProcessors() {
56  		if (this._processors == null) {
57  			int nbCPU = 1;
58  			List<Processor> processors = new ArrayList<>();
59  			int[] mib = { SystemB.CTL_HW, SystemB.HW_LOGICALCPU };
60  			com.sun.jna.Memory pNbCPU = new com.sun.jna.Memory(SystemB.INT_SIZE);
61  			if (0 != SystemB.INSTANCE.sysctl(mib, mib.length, pNbCPU,
62  					new IntByReference(SystemB.INT_SIZE), null, 0))
63  				throw new LastErrorException("Error code: "
64  						+ Native.getLastError());
65  			nbCPU = pNbCPU.getInt(0);
66  			for (int i = 0; i < nbCPU; i++)
67  				processors.add(new CentralProcessor(i));
68  
69  			this._processors = processors.toArray(new Processor[0]);
70  		}
71  		return this._processors;
72  	}
73  
74  	/*
75  	 * (non-Javadoc)
76  	 * 
77  	 * @see oshi.hardware.HardwareAbstractionLayer#getMemory()
78  	 */
79  	@Override
80  	public Memory getMemory() {
81  		if (this._memory == null) {
82  			this._memory = new GlobalMemory();
83  		}
84  		return this._memory;
85  	}
86  
87  	@Override
88  	public PowerSource[] getPowerSources() {
89  		if (this._powerSources == null) {
90  			this._powerSources = MacPowerSource.getPowerSources();
91  		}
92  		return this._powerSources;
93  	}
94  
95  	@Override
96  	public OSFileStore[] getFileStores() {
97  		return MacFileSystem.getFileStores();
98  	}
99  
100 }