Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JBIDE-13990] correctly identify and report 64bit jvms #138

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,25 @@ public abstract class AbstractEclipseEnvironment extends AbstractGoogleAnalytics
private long visitCount;
protected IEclipseUserAgent eclipseUserAgent;

public AbstractEclipseEnvironment(String accountName, String hostName, IEclipsePreferences preferences) {
protected AbstractEclipseEnvironment(String accountName, String hostName, IEclipsePreferences preferences) {
this(accountName, hostName, IGoogleAnalyticsParameters.VALUE_NO_REFERRAL, preferences);
}

public AbstractEclipseEnvironment(String accountName, String hostName, String referral,
protected AbstractEclipseEnvironment(String accountName, String hostName, IEclipsePreferences preferences, IEclipseUserAgent userAgent) {
this(accountName, hostName, IGoogleAnalyticsParameters.VALUE_NO_REFERRAL, preferences, userAgent);
}

protected AbstractEclipseEnvironment(String accountName, String hostName, String referral,
IEclipsePreferences preferences) {
this(accountName, hostName, referral, preferences, new EclipseUserAgent());
}

protected AbstractEclipseEnvironment(String accountName, String hostName, String referral,
IEclipsePreferences preferences, IEclipseUserAgent eclipseUserAgent) {
super(accountName, hostName, referral);
this.random = new Random();
this.preferences = preferences;
eclipseUserAgent = createEclipseUserAgent();
this.eclipseUserAgent = eclipseUserAgent;
initScreenSettings();
initVisits();
}
Expand Down Expand Up @@ -79,10 +88,6 @@ private void initVisits() {
visitCount = preferences.getLong(IUsageReportPreferenceConstants.VISIT_COUNT, 1);
}

protected IEclipseUserAgent createEclipseUserAgent() {
return new EclipseUserAgent();
}

public String getBrowserLanguage() {
return eclipseUserAgent.getBrowserLanguage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,33 @@ public class EclipseUserAgent implements IEclipseUserAgent {
private static final String ECLIPSE_RUNTIME_BULDEID = "org.eclipse.core.runtime"; //$NON-NLS-1$

private static final String USERAGENT_WIN = "{0}/{1} (Windows; U; Windows NT {2}; {3})"; //$NON-NLS-1$
private static final String USERAGENT_WIN_64 = "{0}/{1} (Windows; U; Windows NT {2}; Win64; x64; {3})"; //$NON-NLS-1$
private static final String USERAGENT_MAC = "{0}/{1} (Macintosh; U; Intel Mac OS X {2}; {3})"; //$NON-NLS-1$
private static final String USERAGENT_LINUX = "{0}/{1} (X11; U; Linux i686; {3})"; //$NON-NLS-1$
private static final String USERAGENT_LINUX_64 = "{0}/{1} (X11; U; Linux x86_64; {3})"; //$NON-NLS-1$

public static final char VERSION_DELIMITER = '.'; //$NON-NLS-1$

private static final String PROP_OS_VERSION = "os.version"; //$NON-NLS-1$
private static final String PROP_SUN_ARCH = "sun.arch.data.model"; //$NON-NLS-1$

private static final CharSequence ARCHITECTURE_64 = "64";

private String browserLanguage;

public String toString() {
String productId = getApplicationName();
String productVersion = getApplicationVersion();

return MessageFormat.format(
getUserAgentPattern()
, productId
, productVersion
, getOSVersion()
, getBrowserLanguage()
);
}

private String createBrowserLanguage() {
String nl = getNL();
if (nl == null) {
Expand All @@ -51,11 +69,11 @@ private String createBrowserLanguage() {
return nl;
}

StringBuilder builder = new StringBuilder();
builder.append(nl.substring(0, indexOf));
builder.append(BROWSER_LOCALE_DELIMITER);
builder.append(nl.substring(indexOf + 1));
return builder.toString();
return new StringBuilder()
.append(nl.substring(0, indexOf))
.append(BROWSER_LOCALE_DELIMITER)
.append(nl.substring(indexOf + 1))
.toString();
}

protected String getNL() {
Expand All @@ -69,39 +87,53 @@ public String getBrowserLanguage() {
return browserLanguage;
}

public String toString() {
String productId = getApplicationName();
String productVersion = getApplicationVersion();

return MessageFormat.format(
getUserAgentPattern(getOS())
, productId
, productVersion
, getOSVersion()
, getBrowserLanguage()
);
}

public String getOS() {
return Platform.getOS();
}

public String getJavaArchitecture() {
return System.getProperty(PROP_SUN_ARCH);
}

public String getOSVersion() {
return System.getProperty(PROP_OS_VERSION);
}

private String getUserAgentPattern(String os) {
private String getUserAgentPattern() {
String os = getOS();
String userAgentPattern = ""; //$NON-NLS-1$
if (Platform.OS_LINUX.equals(os)) {
return USERAGENT_LINUX;
if (is64()) {
return USERAGENT_LINUX_64;
} else {
return USERAGENT_LINUX;
}
} else if (Platform.OS_MACOSX.equals(os)) {
return USERAGENT_MAC;
} else if (Platform.OS_WIN32.equals(os)) {
return USERAGENT_WIN;
if (is64()) {
return USERAGENT_WIN_64;
} else {
return USERAGENT_WIN;
}
}
return userAgentPattern;
}

/**
* Returns <code>true</code> if the jvm this is running in is a 64bit jvm.
*
* @param architecture
* @return
*
* @see <a href="// http://stackoverflow.com/questions/807263/how-do-i-detect-which-kind-of-jre-is-installed-32bit-vs-64bit">stackoverflow</a>
*/
private boolean is64() {
String architecture = getJavaArchitecture();
return architecture != null
&& architecture.equals(ARCHITECTURE_64);
}

public String getApplicationName() {
return getApplicationBundle().getSymbolicName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,21 @@ public void start(BundleContext context) throws Exception {
}

private void initBranding(BundleContext context) {
branding = new UsageBrandingMediator(
new JBossToolsUsageBranding()
, getBundle().getBundleContext());
branding = new UsageBrandingMediator(new JBossToolsUsageBranding(), getBundle().getBundleContext());
branding.open();
}

public IJBossToolsEclipseEnvironment getJBossToolsEclipseEnvironment() {
if (eclipseEnvironment == null) {
eclipseEnvironment = createEclipseEnvironment();
eclipseEnvironment = createEclipseEnvironment(getUsageBranding());
}
return eclipseEnvironment;
}

private IJBossToolsEclipseEnvironment createEclipseEnvironment() {
IUsageBranding branding = getUsageBranding();
protected IJBossToolsEclipseEnvironment createEclipseEnvironment(IUsageBranding branding) {
return new JBossToolsEclipseEnvironment(
branding.getGoogleAnalyticsAccount(), branding.getGoogleAnalyticsReportingHost(),
branding.getGoogleAnalyticsAccount(),
branding.getGoogleAnalyticsReportingHost(),
UsageReportPreferencesUtils.getPreferences());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.jboss.tools.usage.googleanalytics.IJBossToolsEclipseEnvironment;
import org.jboss.tools.usage.googleanalytics.eclipse.AbstractEclipseEnvironment;
import org.jboss.tools.usage.googleanalytics.eclipse.IEclipseUserAgent;
import org.jboss.tools.usage.internal.JBossToolsUsageActivator;
import org.osgi.framework.Bundle;

Expand All @@ -27,9 +28,10 @@
public class JBossToolsEclipseEnvironment extends AbstractEclipseEnvironment implements IJBossToolsEclipseEnvironment {

private static final String NOT_INSTALLED = "N/A"; //$NON-NLS-1$
private static final String TRUE = "true";
private static final String FALSE = "false";
private static final String TRUE = "true"; //$NON-NLS-1$
private static final String FALSE = "false"; //$NON-NLS-1$
private static final char JBOSS_COMPONENTS_DELIMITER = '-';

private static final String JBOSS_CENTRAL_PLUGIN_ID = "org.jboss.tools.central"; //$NON-NLS-1$
private static final String SHOW_JBOSS_CENTRAL_ON_STARTUP = "showJBossCentralOnStartup"; //$NON-NLS-1$
private static final boolean SHOW_JBOSS_CENTRAL_ON_STARTUP_DEFAULT_VALUE = true;
Expand All @@ -38,6 +40,11 @@ public JBossToolsEclipseEnvironment(String accountName, String hostName, IEclips
super(accountName, hostName, preferences);
}

protected JBossToolsEclipseEnvironment(String accountName, String hostName, IEclipsePreferences preferences,
IEclipseUserAgent userAgent) {
super(accountName, hostName, preferences, userAgent);
}

@Override
public String getKeyword() {
Collection<String> jbossComponentNames = JBossToolsComponents.getComponentIds(getBundleGroupProviders());
Expand Down Expand Up @@ -67,7 +74,8 @@ public boolean isLinuxDistro() {
}

/**
* TODO: support multiple events.
* TODO: support multiple events.
* most likely doesn't work to send multiple events in a single request: {@link}https://issues.jboss.org/browse/JBDS-2573
*/
public GoogleAnalyticsEvent getEvent() {
return new GoogleAnalyticsEvent("central", "showOnStartup", getCentralEnabledValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package org.jboss.tools.usage.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.regex.Matcher;
Expand All @@ -19,6 +20,7 @@
import org.jboss.tools.usage.googleanalytics.eclipse.AbstractEclipseEnvironment;
import org.jboss.tools.usage.internal.reporting.JBossToolsComponents;
import org.jboss.tools.usage.test.fakes.BundleGroupProviderFake;
import org.jboss.tools.usage.test.fakes.EclipsePreferencesFake;
import org.jboss.tools.usage.test.fakes.ReportingEclipseEnvironmentFake;
import org.junit.Test;

Expand Down Expand Up @@ -69,5 +71,45 @@ protected IBundleGroupProvider[] getBundleGroupProviders() {
assertTrue(keyword != null && keyword.length() == 0);
}

@Test
public void testVisitsOnFirstVisit() {
EclipsePreferencesFake preferences = new EclipsePreferencesFake();
AbstractEclipseEnvironment eclipseEnvironment = new ReportingEclipseEnvironmentFake(preferences);
String firstVisit = eclipseEnvironment.getFirstVisit();
assertEquals(1, eclipseEnvironment.getVisitCount());
assertEquals(firstVisit, eclipseEnvironment.getLastVisit());
assertEquals(firstVisit, eclipseEnvironment.getLastVisit());
assertEquals(firstVisit, eclipseEnvironment.getCurrentVisit());
}

@Test
public void testVisitsOnSecondVisit() throws InterruptedException {
EclipsePreferencesFake preferences = new EclipsePreferencesFake();
AbstractEclipseEnvironment eclipseEnvironment = new ReportingEclipseEnvironmentFake(preferences);
String firstVisit = eclipseEnvironment.getFirstVisit();
Thread.sleep(10);
eclipseEnvironment.visit();

assertEquals(2, eclipseEnvironment.getVisitCount());
assertEquals(firstVisit, eclipseEnvironment.getFirstVisit());
assertEquals(firstVisit, eclipseEnvironment.getLastVisit());
assertTrue(!firstVisit.equals(eclipseEnvironment.getCurrentVisit()));
}

@Test
public void testVisitsOnThirdVisit() throws InterruptedException {
EclipsePreferencesFake preferences = new EclipsePreferencesFake();
AbstractEclipseEnvironment eclipseEnvironment = new ReportingEclipseEnvironmentFake(preferences);
String firstVisit = eclipseEnvironment.getFirstVisit();
Thread.sleep(10);
eclipseEnvironment.visit();

String currentVisit = eclipseEnvironment.getCurrentVisit();
Thread.sleep(10);
eclipseEnvironment.visit();

assertEquals(3, eclipseEnvironment.getVisitCount());
assertEquals(currentVisit, eclipseEnvironment.getLastVisit());
assertTrue(!firstVisit.equals(eclipseEnvironment.getCurrentVisit()));
}
}