|
| 1 | +package org.codechecker.eclipse.plugin.usage; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.net.InetAddress; |
| 6 | +import java.net.UnknownHostException; |
| 7 | +import java.util.Properties; |
| 8 | + |
| 9 | +import org.eclipse.core.runtime.FileLocator; |
| 10 | +import org.eclipse.core.runtime.IContributor; |
| 11 | +import org.eclipse.core.runtime.IExtension; |
| 12 | +import org.eclipse.core.runtime.IExtensionPoint; |
| 13 | +import org.eclipse.core.runtime.IExtensionRegistry; |
| 14 | +import org.eclipse.core.runtime.Path; |
| 15 | +import org.eclipse.core.runtime.Platform; |
| 16 | +import org.eclipse.jdt.annotation.Nullable; |
| 17 | +import org.osgi.framework.Bundle; |
| 18 | +import org.osgi.framework.FrameworkUtil; |
| 19 | + |
| 20 | +public class UsageInfo { |
| 21 | + private static final String UNKNOWN = "unknown"; |
| 22 | + private static final String UNKNOWN_VER = "unknown version"; |
| 23 | + |
| 24 | + @SuppressWarnings("unused") |
| 25 | + private String machine = UNKNOWN; |
| 26 | + @SuppressWarnings("unused") |
| 27 | + private String hostname = UNKNOWN; |
| 28 | + //@SuppressWarnings("unused") |
| 29 | + //private String clangsa = UNKNOWN; |
| 30 | + @SuppressWarnings("unused") |
| 31 | + private String version = UNKNOWN_VER; |
| 32 | + // TODO make this parameter dynamic, from build parameters. |
| 33 | + @SuppressWarnings("unused") |
| 34 | + private String pluginVersion = UNKNOWN_VER; |
| 35 | + @SuppressWarnings("unused") |
| 36 | + private String user = UNKNOWN; |
| 37 | + @SuppressWarnings("unused") |
| 38 | + private CommandType command_type; |
| 39 | + //@SuppressWarnings("unused") |
| 40 | + //private String clang_tidy = UNKNOWN; |
| 41 | + |
| 42 | + |
| 43 | + public UsageInfo(CommandType ct, @Nullable String ccVersion) { |
| 44 | + StringBuilder tempos = new StringBuilder(System.getProperty("os.name")); |
| 45 | + tempos.append(" ").append(System.getProperty("os.version")); |
| 46 | + tempos.append(" ").append("/ Eclipse ").append(getEclipseVersion()); |
| 47 | + machine = tempos.toString(); |
| 48 | + try { |
| 49 | + hostname = InetAddress.getLocalHost().getHostName(); |
| 50 | + } catch (UnknownHostException ex) { |
| 51 | + ; |
| 52 | + } |
| 53 | + if (ccVersion != null) |
| 54 | + version = ccVersion; |
| 55 | + setPluginVersion(); |
| 56 | + user = System.getProperty("user.name"); |
| 57 | + command_type = ct; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Used for returning the eclipse version number. From: |
| 62 | + * https://stackoverflow.com/a/28855362/8149485 |
| 63 | + * |
| 64 | + * @return The eclipse version number in 1.2.3.v19700101-0000 format. |
| 65 | + */ |
| 66 | + private String getEclipseVersion() { |
| 67 | + String version = UNKNOWN_VER; |
| 68 | + String product = System.getProperty("eclipse.product"); |
| 69 | + IExtensionRegistry registry = Platform.getExtensionRegistry(); |
| 70 | + IExtensionPoint point = registry.getExtensionPoint("org.eclipse.core.runtime.products"); |
| 71 | + if (point != null) { |
| 72 | + IExtension[] extensions = point.getExtensions(); |
| 73 | + for (IExtension ext : extensions) |
| 74 | + if (product.equals(ext.getUniqueIdentifier())) { |
| 75 | + IContributor contributor = ext.getContributor(); |
| 76 | + if (contributor != null) { |
| 77 | + Bundle bundle = Platform.getBundle(contributor.getName()); |
| 78 | + if (bundle != null) |
| 79 | + version = bundle.getVersion().toString(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return version; |
| 84 | + } |
| 85 | + |
| 86 | + private void setPluginVersion() { |
| 87 | + try (InputStream is = FileLocator.openStream(FrameworkUtil.getBundle(getClass()), |
| 88 | + new Path("resources/config.properties"), false)) { |
| 89 | + Properties prop = new Properties(); |
| 90 | + prop.load(is); |
| 91 | + pluginVersion = prop.getProperty("version"); |
| 92 | + } catch (IOException e1) { |
| 93 | + e1.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public enum CommandType { |
| 98 | + started, analyze_started |
| 99 | + } |
| 100 | +} |
0 commit comments