|
| 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 | +import com.google.gson.annotations.SerializedName; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class for Storing usage logging related info. |
| 24 | + */ |
| 25 | +public class UsageInfo { |
| 26 | + private static final String UNKNOWN = "unknown"; |
| 27 | + private static final String UNKNOWN_VER = "unknown version"; |
| 28 | + |
| 29 | + @SuppressWarnings("unused") |
| 30 | + private final String machine; |
| 31 | + @SuppressWarnings("unused") |
| 32 | + private final String hostname; |
| 33 | + //@SuppressWarnings("unused") |
| 34 | + //private String clangsa = UNKNOWN; |
| 35 | + @SuppressWarnings("unused") |
| 36 | + private final String version; |
| 37 | + // TODO make this parameter dynamic, from build parameters. |
| 38 | + @SuppressWarnings("unused") |
| 39 | + private final String pluginVersion; |
| 40 | + @SuppressWarnings("unused") |
| 41 | + private final String user; |
| 42 | + @SuppressWarnings("unused") |
| 43 | + @SerializedName("command_type") |
| 44 | + private final CommandType commandType; |
| 45 | + //@SuppressWarnings("unused") |
| 46 | + //private String clang_tidy = UNKNOWN; |
| 47 | + |
| 48 | + /** |
| 49 | + * Specify the event type and the CodeChecker version if in context. |
| 50 | + * |
| 51 | + * @param ct |
| 52 | + * The command (event) type to be logged. |
| 53 | + * @param ccVersion |
| 54 | + * The CodeChecker version to be logged. Not every context has |
| 55 | + * CodeChecker. |
| 56 | + */ |
| 57 | + public UsageInfo(CommandType ct, @Nullable String ccVersion) { |
| 58 | + StringBuilder tempos = new StringBuilder(System.getProperty("os.name")); |
| 59 | + tempos.append(" ").append(System.getProperty("os.version")); |
| 60 | + tempos.append(" / Eclipse ").append(getEclipseVersion()); |
| 61 | + machine = tempos.toString(); |
| 62 | + String tHostName = UNKNOWN; |
| 63 | + try { |
| 64 | + tHostName = InetAddress.getLocalHost().getHostName(); |
| 65 | + } catch (UnknownHostException ex) { |
| 66 | + ; |
| 67 | + } |
| 68 | + hostname = tHostName; |
| 69 | + |
| 70 | + if (ccVersion != null) |
| 71 | + version = ccVersion; |
| 72 | + else |
| 73 | + version = UNKNOWN_VER; |
| 74 | + |
| 75 | + pluginVersion = setPluginVersion(); |
| 76 | + user = System.getProperty("user.name"); |
| 77 | + commandType = ct; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Used for returning the eclipse version number. From: |
| 82 | + * https://stackoverflow.com/a/28855362/8149485 |
| 83 | + * |
| 84 | + * @return The eclipse version number in 1.2.3.v19700101-0000 format. |
| 85 | + */ |
| 86 | + private String getEclipseVersion() { |
| 87 | + String version = UNKNOWN_VER; |
| 88 | + String product = System.getProperty("eclipse.product"); |
| 89 | + IExtensionRegistry registry = Platform.getExtensionRegistry(); |
| 90 | + IExtensionPoint point = registry.getExtensionPoint("org.eclipse.core.runtime.products"); |
| 91 | + if (point != null) { |
| 92 | + IExtension[] extensions = point.getExtensions(); |
| 93 | + for (IExtension ext : extensions) |
| 94 | + if (product.equals(ext.getUniqueIdentifier())) { |
| 95 | + IContributor contributor = ext.getContributor(); |
| 96 | + if (contributor != null) { |
| 97 | + Bundle bundle = Platform.getBundle(contributor.getName()); |
| 98 | + if (bundle != null) |
| 99 | + version = bundle.getVersion().toString(); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return version; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets the plugin version from a properties file, which gets substituted during |
| 108 | + * build. |
| 109 | + */ |
| 110 | + private String setPluginVersion() { |
| 111 | + String ver = UNKNOWN_VER; |
| 112 | + try (InputStream is = FileLocator.openStream(FrameworkUtil.getBundle(getClass()), |
| 113 | + new Path("resources/config.properties"), false)) { |
| 114 | + Properties prop = new Properties(); |
| 115 | + prop.load(is); |
| 116 | + ver = prop.getProperty("version"); |
| 117 | + } catch (IOException e1) { |
| 118 | + e1.printStackTrace(); |
| 119 | + } |
| 120 | + return ver; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Command (Event) types. |
| 125 | + */ |
| 126 | + public enum CommandType { |
| 127 | + started, analyze_started |
| 128 | + } |
| 129 | +} |
0 commit comments