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.linux.proc;
18  
19  import java.io.FileNotFoundException;
20  import java.io.FileReader;
21  import java.util.Scanner;
22  
23  import oshi.software.os.OperatingSystemVersion;
24  
25  /**
26   * Contains operating system version information. The information includes major
27   * and minor version numbers, a build number, a platform identifier, and
28   * descriptive text about the operating system.
29   *
30   * @author alessandro[at]perucchi[dot]org
31   */
32  public class OSVersionInfoEx implements OperatingSystemVersion {
33  
34  	private String _version = null;
35  	private String _codeName = null;
36  	private String version = null;
37  
38  	public OSVersionInfoEx() {
39  		try (Scanner in = new Scanner(new FileReader("/etc/os-release"))) {
40  			in.useDelimiter("\n");
41  			while (in.hasNext()) {
42  				String[] splittedLine = in.next().split("=");
43  				if (splittedLine[0].equals("VERSION_ID")) {
44  					// remove beginning and ending '"' characters, etc from
45  					// VERSION_ID="14.04"
46  					setVersion(splittedLine[1].replaceAll("^\"|\"$", ""));
47  				}
48  				if (splittedLine[0].equals("VERSION")) {
49  					// remove beginning and ending '"' characters
50  					splittedLine[1] = splittedLine[1].replaceAll("^\"|\"$", "");
51  
52  					// Check basically if the code is between parenthesis or after
53  					// the comma-space
54  
55  					// Basically, until now, that seems to be the standard to use
56  					// parenthesis for the codename.
57  					String[] split = splittedLine[1].split("[()]");
58  					if (split.length <= 1)
59  						// We are probably with Ubuntu, so need to get that part
60  						// correctly.
61  						split = splittedLine[1].split(", ");
62  
63  					if (split.length > 1) {
64  						setCodeName(split[1]);
65  					} else {
66  						setCodeName(splittedLine[1]);
67  					}
68  				}
69  			}
70  		} catch (FileNotFoundException e) {
71  			return;
72  		}
73  	}
74  
75  	/**
76  	 * @return the _codeName
77  	 */
78  	public String getCodeName() {
79  		return this._codeName;
80  	}
81  
82  	/**
83  	 * @return the _version
84  	 */
85  	public String getVersion() {
86  		return this._version;
87  	}
88  
89  	/**
90  	 * @param _codeName
91  	 *			the _codeName to set
92  	 */
93  	public void setCodeName(String _codeName) {
94  		this._codeName = _codeName;
95  	}
96  
97  	/**
98  	 * @param _version
99  	 *			the _version to set
100 	 */
101 	public void setVersion(String _version) {
102 		this._version = _version;
103 	}
104 
105 	@Override
106 	public String toString() {
107 		if (this.version == null) {
108 			this.version = getVersion() + " (" + getCodeName() + ")";
109 		}
110 		return this.version;
111 	}
112 
113 }