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.local;
18  
19  import oshi.software.os.OperatingSystemVersion;
20  
21  import com.sun.jna.LastErrorException;
22  import com.sun.jna.Memory;
23  import com.sun.jna.Native;
24  import com.sun.jna.Pointer;
25  import com.sun.jna.ptr.IntByReference;
26  
27  /**
28   * @author alessandro[at]perucchi[dot]org
29   * @author widdis[at]gmail[dot]com
30   */
31  
32  public class OSVersionInfoEx implements OperatingSystemVersion {
33  
34  	private String _version = null;
35  	private String _codeName = null;
36  	private String _versionStr = null;
37  	private String _buildNumber = null;
38  
39  	public OSVersionInfoEx() {
40  	}
41  
42  	/**
43  	 * @return the _version
44  	 */
45  	public String getVersion() {
46  		if (this._version == null)
47  			this._version = System.getProperty("os.version");
48  		return this._version;
49  	}
50  
51  	/**
52  	 * @param version
53  	 *            the version to set
54  	 */
55  	public void setVersion(String version) {
56  		this._version = version;
57  	}
58  
59  	/**
60  	 * @return the _codeName
61  	 */
62  	public String getCodeName() {
63  		if (this._codeName == null) {
64  			if (getVersion() != null) {
65  				String[] versionSplit = getVersion().split("\\.");
66  				if (versionSplit.length > 1 && versionSplit[0].equals("10")) {
67  					switch (Integer.parseInt(versionSplit[1])) {
68  					case 0:
69  						this._codeName = "Cheetah";
70  						break;
71  					case 1:
72  						this._codeName = "Puma";
73  						break;
74  					case 2:
75  						this._codeName = "Jaguar";
76  						break;
77  					case 3:
78  						this._codeName = "Panther";
79  						break;
80  					case 4:
81  						this._codeName = "Tiger";
82  						break;
83  					case 5:
84  						this._codeName = "Leopard";
85  						break;
86  					case 6:
87  						this._codeName = "Snow Leopard";
88  						break;
89  					case 7:
90  						this._codeName = "Lion";
91  						break;
92  					case 8:
93  						this._codeName = "Mountain Lion";
94  						break;
95  					case 9:
96  						this._codeName = "Mavericks";
97  						break;
98  					case 10:
99  						this._codeName = "Yosemite";
100 						break;
101 					case 11:
102 						this._codeName = "El Capitan";
103 						break;
104 					default:
105 						this._codeName = "";
106 					}
107 
108 				} else
109 					this._codeName = "";
110 			}
111 		}
112 		return this._codeName;
113 	}
114 
115 	/**
116 	 * @param codeName
117 	 *            the codeName to set
118 	 */
119 	public void setCodeName(String codeName) {
120 		this._codeName = codeName;
121 	}
122 
123 	public String getBuildNumber() {
124 		if (this._buildNumber == null) {
125 			int[] mib = { SystemB.CTL_KERN, SystemB.KERN_OSVERSION };
126 			IntByReference size = new IntByReference();
127 			if (0 != SystemB.INSTANCE.sysctl(mib, mib.length, null, size, null,
128 					0))
129 				throw new LastErrorException("Error code: "
130 						+ Native.getLastError());
131 			Pointer p = new Memory(size.getValue() + 1);
132 			if (0 != SystemB.INSTANCE.sysctl(mib, mib.length, p, size, null, 0))
133 				throw new LastErrorException("Error code: "
134 						+ Native.getLastError());
135 			this._buildNumber = p.getString(0);
136 		}
137 		return this._buildNumber;
138 	}
139 
140 	public void setBuildNumber(String buildNumber) {
141 		this._buildNumber = buildNumber;
142 	}
143 
144 	@Override
145 	public String toString() {
146 		if (this._versionStr == null) {
147 			StringBuilder sb = new StringBuilder(getVersion());
148 			if (getCodeName().length() > 0)
149 				sb.append(" (").append(getCodeName()).append(")");
150 			sb.append(" build ").append(getBuildNumber());
151 			this._versionStr = sb.toString();
152 		}
153 		return this._versionStr;
154 	}
155 }