-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.java
More file actions
84 lines (77 loc) · 2.98 KB
/
Utils.java
File metadata and controls
84 lines (77 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package testpluginproject.utils;
/**
* This class is authored by Saminur Islam and is owned by Dr. Collin Lynch.
* It is licensed under the terms of the GNU Affero General Public License (AGPL) version 3.0 or later.
*/
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
public class Utils {
public static String getUsernameFromPref() {
IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode("csc.plugin.prefs.page");
//deal with ill formatted usernames
String username = preferences.get("USERNAME", "default").toLowerCase();
String email = preferences.get("EMAIL", "default").toLowerCase();
String semester = preferences.get("SEMESTER", "default");
String course = preferences.get("COURSE", "default");
String section = preferences.get("SECTION", "default");
System.out.print(semester+" "+course+" "+section);
if(email.contains("@ncsu.edu")) {
if(email.replace("@ncsu.edu", "").equals(username)) {
// They match, we can just move on
return username;
}
else {
// One of them doesn't match, it's impossible to know which is the problem
// For now, we default to the email
return email.replace("@ncsu.edu", "");
}
}
else {
// We know the email is illformated, so rely on the user name (if it is there)
if(username.equals("")) {
return "not entered";
}
else {
return username;
}
}
}
public static String getIpAddress() {
try {
InetAddress localHost = InetAddress.getLocalHost();
return localHost.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
public static String getMacAddress() {
// TODO Auto-generated method stub
try {
Enumeration<NetworkInterface> networkInterface = NetworkInterface.getNetworkInterfaces();
while(networkInterface.hasMoreElements()) {
NetworkInterface ni = networkInterface.nextElement();
byte[] hardwareAddress = ni.getHardwareAddress();
if(hardwareAddress!=null) {
StringBuilder macAddresBuilder = new StringBuilder();
for(int i = 0; i< hardwareAddress.length;i++) {
macAddresBuilder.append(String.format("%02X", hardwareAddress[i]));
if(i!=hardwareAddress.length-1) {
macAddresBuilder.append("-");
}
}
return macAddresBuilder.toString();
}
}
}catch (SocketException e) {
// TODO: handle exception
System.out.println("Exception happened due to. "+e.getMessage());
}
return null;
}
}