Author: adietish
Date: 2010-08-25 10:16:41 -0400 (Wed, 25 Aug 2010)
New Revision: 24426
Added:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingSettings.java
Removed:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingEnablement.java
Modified:
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalUsageReportingSettingsTest.java
Log:
[JBIDE-6376] GlobalUsageReportingSettings renamed, tests corrected (false positive)
Deleted:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingEnablement.java
===================================================================
---
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingEnablement.java 2010-08-25
14:10:40 UTC (rev 24425)
+++
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingEnablement.java 2010-08-25
14:16:41 UTC (rev 24426)
@@ -1,168 +0,0 @@
-/*******************************************************************************
- * 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.BufferedInputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
-import java.net.HttpURLConnection;
-import java.net.URL;
-
-import org.eclipse.core.runtime.Assert;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Plugin;
-import org.jboss.tools.usage.util.HttpEncodingUtils;
-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 GlobalUsageReportingEnablement {
-
- private static final String REPORTING_ENABLEMENT_ENABLEDVALUE = "ENABLED";
-
- private static final String GET_METHOD_NAME = "GET"; //$NON-NLS-1$
- private static final String REPORTING_ENABLEMENT_URL =
"http://community.jboss.org/wiki/JBossToolsJBossDeveloperStudioUsageReportingEnablement";
//$NON-NLS-1$
- private static final String REPORTING_ENABLEMENT_STARTSEQUENCE = "Usage Reporting
is "; //$NON-NLS-1$
-
- private Plugin plugin;
- private HttpURLConnection urlConnection;
-
- public GlobalUsageReportingEnablement(Plugin plugin) throws IOException {
- Assert.isNotNull(plugin);
-
- this.plugin = plugin;
- this.urlConnection = createURLConnection(REPORTING_ENABLEMENT_URL);
- }
-
- public boolean isEnabled() {
- try {
- return parse(request(REPORTING_ENABLEMENT_URL));
- } catch (IOException e) {
- IStatus status = StatusUtils.getErrorStatus(
- plugin.getBundle().getSymbolicName()
- , UsageMessages.KillSwitchPreference_Error_Exception, e);
- plugin.getLog().log(status);
- return false;
- }
- }
-
- /**
- * 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.
- * @throws UnsupportedEncodingException
- *
- * @see HttpURLConnection
- */
- protected InputStreamReader request(String url) throws UnsupportedEncodingException {
- InputStreamReader responseReader = 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);
- responseReader = getInputStreamReader(urlConnection.getContentType());
- } 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, e, url);
- plugin.getLog().log(status);
- }
- return responseReader;
- }
-
- private InputStreamReader getInputStreamReader(String contentType) throws
UnsupportedEncodingException, IOException {
- String contentTypeCharset = HttpEncodingUtils.getContentTypeCharset(contentType);
- if (contentTypeCharset != null && contentTypeCharset.length() > 0) {
- return new InputStreamReader(new BufferedInputStream(urlConnection.getInputStream()),
- contentTypeCharset);
- } else {
- return new InputStreamReader(new
BufferedInputStream(urlConnection.getInputStream()));
- }
- }
-
- /**
- * Parses the given string and extracts the enablement value.
- *
- * @param input
- * stream that holds
- * @return true, if successful
- */
- private boolean parse(InputStreamReader reader) throws IOException {
- int i = 0;
- char[] reportingValueCharacters = new
char[REPORTING_ENABLEMENT_ENABLEDVALUE.length()];
- for (int character = 0; character != -1; character = reader.read()) {
- if (REPORTING_ENABLEMENT_STARTSEQUENCE.charAt(i) == (char) character) {
- if (++i == REPORTING_ENABLEMENT_STARTSEQUENCE.length()) {
- reader.read(reportingValueCharacters);
- return isEnabled(new String(reportingValueCharacters));
- }
- } else {
- i = 0;
- }
- }
- return false;
- }
-
- private boolean isEnabled(String enablementValue) {
- return (enablementValue != null &&
REPORTING_ENABLEMENT_ENABLEDVALUE.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();
- }
-}
Copied:
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingSettings.java
(from rev 24391,
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingEnablement.java)
===================================================================
---
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingSettings.java
(rev 0)
+++
trunk/usage/plugins/org.jboss.tools.usage/src/org/jboss/tools/usage/GlobalUsageReportingSettings.java 2010-08-25
14:16:41 UTC (rev 24426)
@@ -0,0 +1,168 @@
+/*******************************************************************************
+ * 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.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Plugin;
+import org.jboss.tools.usage.util.HttpEncodingUtils;
+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 GlobalUsageReportingSettings {
+
+ private static final String REPORTING_ENABLEMENT_ENABLEDVALUE = "ENABLED";
+
+ private static final String GET_METHOD_NAME = "GET"; //$NON-NLS-1$
+ private static final String REPORTING_ENABLEMENT_URL =
"http://community.jboss.org/wiki/JBossToolsJBossDeveloperStudioUsageReportingEnablement";
//$NON-NLS-1$
+ private static final String REPORTING_ENABLEMENT_STARTSEQUENCE = "Usage Reporting
is "; //$NON-NLS-1$
+
+ private Plugin plugin;
+ private HttpURLConnection urlConnection;
+
+ public GlobalUsageReportingSettings(Plugin plugin) throws IOException {
+ Assert.isNotNull(plugin);
+
+ this.plugin = plugin;
+ this.urlConnection = createURLConnection(REPORTING_ENABLEMENT_URL);
+ }
+
+ public boolean isEnabled() {
+ try {
+ return parse(request(REPORTING_ENABLEMENT_URL));
+ } catch (IOException e) {
+ IStatus status = StatusUtils.getErrorStatus(
+ plugin.getBundle().getSymbolicName()
+ , UsageMessages.KillSwitchPreference_Error_Exception, e);
+ plugin.getLog().log(status);
+ return false;
+ }
+ }
+
+ /**
+ * 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.
+ * @throws UnsupportedEncodingException
+ *
+ * @see HttpURLConnection
+ */
+ protected InputStreamReader request(String url) throws UnsupportedEncodingException {
+ InputStreamReader responseReader = 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);
+ responseReader = getInputStreamReader(urlConnection.getContentType());
+ } 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, e, url);
+ plugin.getLog().log(status);
+ }
+ return responseReader;
+ }
+
+ private InputStreamReader getInputStreamReader(String contentType) throws
UnsupportedEncodingException, IOException {
+ String contentTypeCharset = HttpEncodingUtils.getContentTypeCharset(contentType);
+ if (contentTypeCharset != null && contentTypeCharset.length() > 0) {
+ return new InputStreamReader(new BufferedInputStream(urlConnection.getInputStream()),
+ contentTypeCharset);
+ } else {
+ return new InputStreamReader(new
BufferedInputStream(urlConnection.getInputStream()));
+ }
+ }
+
+ /**
+ * Parses the given string and extracts the enablement value.
+ *
+ * @param input
+ * stream that holds
+ * @return true, if successful
+ */
+ private boolean parse(InputStreamReader reader) throws IOException {
+ int i = 0;
+ char[] reportingValueCharacters = new
char[REPORTING_ENABLEMENT_ENABLEDVALUE.length()];
+ for (int character = 0; character != -1; character = reader.read()) {
+ if (REPORTING_ENABLEMENT_STARTSEQUENCE.charAt(i) == (char) character) {
+ if (++i == REPORTING_ENABLEMENT_STARTSEQUENCE.length()) {
+ reader.read(reportingValueCharacters);
+ return isEnabled(new String(reportingValueCharacters));
+ }
+ } else {
+ i = 0;
+ }
+ }
+ return false;
+ }
+
+ private boolean isEnabled(String enablementValue) {
+ return (enablementValue != null &&
REPORTING_ENABLEMENT_ENABLEDVALUE.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/GlobalUsageReportingSettings.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalUsageReportingSettingsTest.java
===================================================================
---
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalUsageReportingSettingsTest.java 2010-08-25
14:10:40 UTC (rev 24425)
+++
trunk/usage/tests/org.jboss.tools.usage.test/src/org/jboss/tools/usage/test/GlobalUsageReportingSettingsTest.java 2010-08-25
14:16:41 UTC (rev 24426)
@@ -10,6 +10,7 @@
******************************************************************************/
package org.jboss.tools.usage.test;
+import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
@@ -27,27 +28,27 @@
@Test
public void canExtractEnabledValue() throws IOException {
- GlobalReportingEnablementFake reportEnablement = new
GlobalReportingEnablementFake("ENABLED");
- assertTrue(reportEnablement.isEnabled());
+ GlobalReportingSettingsFake reportSettings= new
GlobalReportingSettingsFake("ENABLED");
+ assertTrue(reportSettings.isEnabled());
}
@Test
public void canExtractDisabledValue() throws IOException {
- GlobalReportingEnablementFake reportEnablement = new
GlobalReportingEnablementFake("DISABLED");
- assertTrue(reportEnablement.isEnabled());
+ GlobalReportingSettingsFake reportSettings = new
GlobalReportingSettingsFake("DISABLED");
+ assertFalse(reportSettings.isEnabled());
}
@Test
public void canExtractDisabledOutUndefinedValue() throws IOException {
- GlobalReportingEnablementFake reportEnablement = new
GlobalReportingEnablementFake("Rubbish");
- assertTrue(reportEnablement.isEnabled());
+ GlobalReportingSettingsFake reportEnablement = new
GlobalReportingSettingsFake("Rubbish");
+ assertFalse(reportEnablement.isEnabled());
}
- private class GlobalReportingEnablementFake extends GlobalUsageReportingSettings {
+ private class GlobalReportingSettingsFake extends GlobalUsageReportingSettings {
private String enablementValue;
- public GlobalReportingEnablementFake(String enablementValue) throws IOException {
+ public GlobalReportingSettingsFake(String enablementValue) throws IOException {
super(JBossToolsUsageTestActivator.getDefault());
this.enablementValue = enablementValue;
}
@@ -112,10 +113,9 @@
+ " <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] --> "
+ + "ENABLED></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 "
+ + enablementValue
+ + "</h1></div><!--
[DocumentBodyEnd:e26c60c0-cb73-47b7-bded-f4eb7320305b] --> "
+ " "
+ " </div> "
+ " <div class=\"jive-content-footer\"> "