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;
18  
19  import java.io.FileNotFoundException;
20  import java.io.FileReader;
21  import java.util.Scanner;
22  
23  import oshi.software.os.OperatingSystem;
24  import oshi.software.os.OperatingSystemVersion;
25  import oshi.software.os.linux.proc.OSVersionInfoEx;
26  
27  /**
28   * Linux is a family of free operating systems most commonly used on personal
29   * computers.
30   *
31   * @author alessandro[at]perucchi[dot]org
32   */
33  public class LinuxOperatingSystem implements OperatingSystem {
34  
35  	private OperatingSystemVersion _version = null;
36  	private String _family = null;
37  
38  	@Override
39  	public String getFamily() {
40  		if (this._family == null) {
41  			try (final Scanner in = new Scanner(new FileReader("/etc/os-release"))) {
42  				in.useDelimiter("\n");
43  				while (in.hasNext()) {
44  					String[] splittedLine = in.next().split("=");
45  					if (splittedLine[0].equals("NAME")) {
46  						// remove beginning and ending '"' characters, etc from
47  						// NAME="Ubuntu"
48  						this._family = splittedLine[1].replaceAll("^\"|\"$", "");
49  						break;
50  					}
51  				}
52  			} catch (FileNotFoundException e) {
53  				return "";
54  			}
55  		}
56  		return this._family;
57  	}
58  
59  	@Override
60  	public String getManufacturer() {
61  		return "GNU/Linux";
62  	}
63  
64  	@Override
65  	public OperatingSystemVersion getVersion() {
66  		if (this._version == null) {
67  			this._version = new OSVersionInfoEx();
68  		}
69  		return this._version;
70  	}
71  
72  	@Override
73  	public String toString() {
74  		StringBuilder sb = new StringBuilder();
75  		sb.append(getManufacturer());
76  		sb.append(" ");
77  		sb.append(getFamily());
78  		sb.append(" ");
79  		sb.append(getVersion().toString());
80  		return sb.toString();
81  	}
82  }