Author: adietish
Date: 2010-08-24 09:32:50 -0400 (Tue, 24 Aug 2010)
New Revision: 24378
Added:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalReportingEnablement.java
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalReportingEnablementTest.java
Modified:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/UsageMessages.java
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/messages.properties
trunk/usage/tests/org.jboss.tools.usage.test/META-INF/MANIFEST.MF
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/UsageTestSuite.java
Log:
[JBIDE-6880] initial implementation and tests
Added:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalReportingEnablement.java
===================================================================
---
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalReportingEnablement.java
(rev 0)
+++
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalReportingEnablement.java 2010-08-24
13:32:50 UTC (rev 24378)
@@ -0,0 +1,139 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.usage;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Plugin;
+import org.jboss.tools.usage.util.StatusUtils;
+
+/**
+ * A class that implements a global reporting enablement setting. The current
+ * implementation queries a given url and extracts the enablement value out of
+ * the response.
+ */
+public class GlobalReportingEnablement {
+
+ private static final String GET_METHOD_NAME = "GET"; //$NON-NLS-1$
+ private static final String REPORTING_ENABLEMENT_URL =
"https://community.jboss.org/wiki/JBossToolsJBossDeveloperStudioUsageReportingEnablement";
//$NON-NLS-1$
+ private static final String REPORTING_ENABLEMENT_REGEX = "Usage Reporting is
([A-Za-z]+)"; //$NON-NLS-1$
+
+ private Plugin plugin;
+ private HttpURLConnection urlConnection;
+ private Pattern pattern;
+
+ public GlobalReportingEnablement(Plugin plugin) throws IOException {
+ Assert.isNotNull(plugin);
+
+ this.plugin = plugin;
+ this.urlConnection = createURLConnection(REPORTING_ENABLEMENT_URL);
+ this.pattern = Pattern.compile(REPORTING_ENABLEMENT_REGEX);
+ }
+
+ public boolean isEnabled() {
+ return parse(request(REPORTING_ENABLEMENT_URL));
+ }
+
+ /**
+ * Sends a http GET request to the given URL. Returns the response string or
+ * <tt>null</tt> if an error occurred. The errors catched are Exceptions or
+ * HTTP error codes.
+ *
+ * @param url
+ * the url to send the GET request to
+ * @return the response or <tt>null</tt> if an error occured.
+ *
+ * @see HttpURLConnection
+ */
+ protected String request(String url) {
+ String response = null;
+ try {
+ urlConnection.connect();
+ int responseCode = getResponseCode(urlConnection);
+ if (responseCode == HttpURLConnection.HTTP_OK) {
+ IStatus status = StatusUtils.getDebugStatus(
+ plugin.getBundle().getSymbolicName()
+ , UsageMessages.KillSwitchPreference_Info_HttpQuery, url);
+ plugin.getLog().log(status);
+ response = urlConnection.getResponseMessage();
+ } else {
+ IStatus status = StatusUtils.getErrorStatus(
+ plugin.getBundle().getSymbolicName()
+ , UsageMessages.KillSwitchPreference_Error_Http, null, url);
+ plugin.getLog().log(status);
+ }
+ } catch (Exception e) {
+ IStatus status = StatusUtils.getErrorStatus(
+ plugin.getBundle().getSymbolicName()
+ , UsageMessages.KillSwitchPreference_Error_Http, null, url);
+ plugin.getLog().log(status);
+ }
+ return response;
+ }
+
+ /**
+ * Parses the given string and extracts the enablement value.
+ *
+ * @param response
+ * the response
+ * @return true, if successful
+ */
+ private boolean parse(String response) {
+ Matcher matcher = pattern.matcher(response);
+ if (matcher.find() && matcher.groupCount() >= 1) {
+ String enablementValue = matcher.group(1);
+ return isEnabled(enablementValue);
+ }
+ return false;
+ }
+
+ private boolean isEnabled(String enablementValue) {
+ return (enablementValue != null &&
"ENABLED".equals(enablementValue.toUpperCase()));
+ }
+
+ /**
+ * Creates a new url connection.
+ *
+ * @param urlString
+ * the url string
+ * @return the http url connection
+ * @throws IOException
+ * Signals that an I/O exception has occurred.
+ */
+ protected HttpURLConnection createURLConnection(String urlString) throws IOException {
+ URL url = new URL(urlString);
+ HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
+ urlConnection.setInstanceFollowRedirects(true);
+ urlConnection.setRequestMethod(GET_METHOD_NAME);
+ return urlConnection;
+ }
+
+ /**
+ * Returns the return code from the given {@link HttpURLConnection}.
+ * Provided to be called by test cases so that they can retrieve the return
+ * code.
+ *
+ * @param urlConnection
+ * to get the response code from
+ * @return the return code the HttpUrlConnection received
+ * @throws IOException
+ * Signals that an I/O exception has occurred.
+ */
+ protected int getResponseCode(HttpURLConnection urlConnection) throws IOException {
+ return urlConnection.getResponseCode();
+ }
+}
Property changes on:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalReportingEnablement.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/UsageMessages.java
===================================================================
---
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/UsageMessages.java 2010-08-24
13:10:20 UTC (rev 24377)
+++
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/UsageMessages.java 2010-08-24
13:32:50 UTC (rev 24378)
@@ -22,6 +22,10 @@
public static String HttpGetMethod_Error_Io;
public static String HttpGetMethod_Success;
+ public static String KillSwitchPreference_Error_Exception;
+ public static String KillSwitchPreference_Error_Http;
+ public static String KillSwitchPreference_Info_HttpQuery;
+
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, UsageMessages.class);
Modified:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/messages.properties
===================================================================
---
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/messages.properties 2010-08-24
13:10:20 UTC (rev 24377)
+++
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/messages.properties 2010-08-24
13:32:50 UTC (rev 24378)
@@ -5,3 +5,7 @@
Tracker_Asynchronous=Tracking asynchronously focusPoint \"{0}\"
Tracker_Error=Tracking failed, exception thrown: \"{0}\"
Tracker_Synchronous=Tracking synchronously focusPoint \"{0}\"
+
+KillSwitchPreference_Error_Exception=Could not query kill switch preference at
\"{0}\"
+KillSwitchPreference_Error_Http=Could not query kill switch preference at \"{0}\,
response code was \"{1}\""
+KillSwitchPreference_Info_HttpQuery=Queried kill switch preference at \"{0}\"
Modified: trunk/usage/tests/org.jboss.tools.usage.test/META-INF/MANIFEST.MF
===================================================================
--- trunk/usage/tests/org.jboss.tools.usage.test/META-INF/MANIFEST.MF 2010-08-24 13:10:20
UTC (rev 24377)
+++ trunk/usage/tests/org.jboss.tools.usage.test/META-INF/MANIFEST.MF 2010-08-24 13:32:50
UTC (rev 24378)
@@ -9,3 +9,4 @@
org.eclipse.core.runtime;bundle-version="3.5.0",
org.junit4;bundle-version="4.0.0"
Bundle-Activator: org.jboss.tools.usage.test.JBossToolsUsageTestActivator
+Bundle-ActivationPolicy: lazy
Added:
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalReportingEnablementTest.java
===================================================================
---
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalReportingEnablementTest.java
(rev 0)
+++
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalReportingEnablementTest.java 2010-08-24
13:32:50 UTC (rev 24378)
@@ -0,0 +1,130 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.usage.test;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+
+import org.jboss.tools.usage.GlobalReportingEnablement;
+import org.junit.Test;
+
+/**
+ * The Class GlobalReportingEnablementTest.
+ */
+public class GlobalReportingEnablementTest {
+
+ @Test
+ public void canExtractEnabledValue() throws IOException {
+ GlobalReportingEnablementFake reportEnablement = new
GlobalReportingEnablementFake("ENABLED");
+ assertTrue(reportEnablement.isEnabled());
+ }
+
+ @Test
+ public void canExtractDisabledValue() throws IOException {
+ GlobalReportingEnablementFake reportEnablement = new
GlobalReportingEnablementFake("DISABLED");
+ assertTrue(reportEnablement.isEnabled());
+ }
+
+ @Test
+ public void canExtractDisabledOutUndefinedValue() throws IOException {
+ GlobalReportingEnablementFake reportEnablement = new
GlobalReportingEnablementFake("Rubbish");
+ assertTrue(reportEnablement.isEnabled());
+ }
+
+ private class GlobalReportingEnablementFake extends GlobalReportingEnablement {
+
+ private String enablementValue;
+
+ public GlobalReportingEnablementFake(String enablementValue) throws IOException {
+ super(JBossToolsUsageTestActivator.getDefault());
+ this.enablementValue = enablementValue;
+ }
+
+ @Override
+ protected String request(String url) {
+ return getEnablementPageContent(enablementValue);
+
+ }
+ }
+
+ private String getEnablementPageContent(String enablementValue) {
+ return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Transitional//EN\""
+ + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
"
+ + " "
+ + " "
+ + "<html
xmlns=\"http://www.w3.org/1999/xhtml\"
xml:lang=\"en\" lang=\"en\"> "
+ + "<head> "
+ + " <title> JBoss Tools / JBoss Developer Studio Usage Reporting
Enablement - JBoss Community</title> "
+ + " "
+ + " <meta http-equiv=\"X-UA-Compatible\"
content=\"IE=EmulateIE7\" /> "
+ + " "
+ + " <script type=\"text/javascript\"> "
+ + " var javascriptIsCool = false;"
+ + " </script> "
+ + " "
+ + "</head> "
+ + "<body class=\"jive-body-content jive-body-content-document\"
> "
+ + " <div id=\"jive-body\"> "
+ + " "
+ + "<div class=\"jive-content\"> "
+ + " <div class=\"jive-content-header\"> "
+ + " <div class=\"jive-wiki-post-moderating
jive-content-header-moderating\"> "
+ + " <span class=\"jive-icon-med
jive-icon-moderation\"></span>Currently Being Moderated"
+ + " </div> "
+ + " <div class=\"jive-content-title\"> "
+ + " <h2><span class=\"jive-icon-big
jive-icon-document\"></span> JBoss Tools / JBoss Developer Studio Usage
Reporting Enablement</h2> "
+ + " </div> "
+ + " <div class=\"jive-content-header-version\"> "
+ + " VERSION 5 "
+ + " <a
href=\"/wiki/JBossToolsJBossDeveloperStudioUsageReportingEnablement/diff?secondVersionNumber=5\"
title=\"Click to view article history\"><img class=\"jive-icon-sml
jive-icon-search\" src=\"/4.0.5/images/transparent.png\" alt=\"Click
to view article history\" /></a> "
+ + " </div> "
+ + " <div class=\"jive-content-header-details\"> "
+ + " "
+ + "Created on: Aug 24, 2010 5:39 AM by"
+ + "<a href=\"/people/adietish\""
+ + "id=\"jive-72036899,987,346,482,238\""
+ +
"onmouseover=\"quickuserprofile.getUserProfileTooltip(72036);\""
+ + "onmouseout=\"quickuserprofile.cancelTooltip();\""
+ + "class=\"jiveTT-hover-user jive-username-link\""
+ + ">Andre Dietisheim</a> <span>-</span> "
+ + "Last Modified: "
+ + "Aug 24, 2010 5:53 AM"
+ + "by <a href=\"/people/adietish\""
+ + "id=\"jive-72036899,987,347,353,238\""
+ +
"onmouseover=\"quickuserprofile.getUserProfileTooltip(72036);\""
+ + "onmouseout=\"quickuserprofile.cancelTooltip();\""
+ + "class=\"jiveTT-hover-user jive-username-link\""
+ + ">Andre Dietisheim</a> </div> "
+ + " "
+ + " </div> "
+ + " <div class=\"jive-content-body\"> "
+ + " "
+ + "<!-- [DocumentBodyStart:e26c60c0-cb73-47b7-bded-f4eb7320305b]
--><div class=\"jive-rendered-content\"><p>This article is
queried by the JBoss Tools / JBoss Developer Studio usage reporting plugin. It implements
a global kill-switch that allows us to disable usage reporting stats. The plugin looks for
a string of the format:</p><p style=\"min-height: 8pt; height: 8pt; padding:
0px;\"> </p><p><strong>Usage 
Reporting  is <"
+ + enablementValue
+ + "></strong>. Any value that differs from ENABLED is interpreted
as DISABLED.</p><p style=\"min-height: 8pt; height: 8pt; padding:
0px;\"> </p><h1>Usage Reporting is
ENABLED</h1></div><!--
[DocumentBodyEnd:e26c60c0-cb73-47b7-bded-f4eb7320305b] --> "
+ + " "
+ + " </div> "
+ + " <div class=\"jive-content-footer\"> "
+ + " "
+ + " "
+ + " <!-- BEGIN content details --> "
+ + " <span class=\"jive-content-footer-item\"> "
+ + " 18 Views</a> "
+ + " </span> "
+ + " "
+ + " "
+ + " </div> "
+ + "</div> "
+ + "</body> "
+ + "</html> ";
+ }
+}
Property changes on:
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalReportingEnablementTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/UsageTestSuite.java
===================================================================
---
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/UsageTestSuite.java 2010-08-24
13:10:20 UTC (rev 24377)
+++
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/UsageTestSuite.java 2010-08-24
13:32:50 UTC (rev 24378)
@@ -18,7 +18,8 @@
FocusPointTest.class,
GoogleAnalyticsUrlStrategyTest.class,
JBossToolsUsageIntegrationTest.class,
- EclipseEnvironmenTest.class })
+ EclipseEnvironmenTest.class,
+ GlobalReportingEnablementTest.class})
/**
* @author Andre Dietisheim