Author: pwang
Date: 2011-07-14 16:32:45 -0700 (Thu, 14 Jul 2011)
New Revision: 26187
Added:
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask2.java
Modified:
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask.java
Log:
Run proxy setting in a separated task, otherwise Cytoscape will be frozen
during tunable validating process
Modified:
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask.java
===================================================================
---
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask.java
2011-07-14 23:07:18 UTC (rev 26186)
+++
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask.java
2011-07-14 23:32:45 UTC (rev 26187)
@@ -29,124 +29,28 @@
* Dialog for assigning proxy settings.
* @author Pasteur
*/
-public class ProxySettingsTask extends AbstractTask implements
TunableValidator {
- static final List<String> KEYS = Arrays.asList("http.proxyHost",
"http.proxyPort", "socks.proxyHost", "socks.proxyPort");
+public class ProxySettingsTask extends AbstractTask {
- @Tunable(description="Type")
- public ListSingleSelection<String> type = new
ListSingleSelection<String>("direct", "http", "socks");
-
- @Tunable(description="Proxy
Server",groups={"param"},dependsOn="type!=direct",params="alignments=horizontal;displayState=hidden")
- public String hostname="";
-
-
@Tunable(description="Port",groups={"param"},dependsOn="type!=direct",params="alignments=horizontal;displayState=hidden")
- public int port=0;
-
final TaskManager taskManager;
final StreamUtil streamUtil;
-
- final Map<String,String> oldSettings = new HashMap<String,String>();
- final Properties properties = System.getProperties();
-
+
public ProxySettingsTask(final TaskManager taskManager, final
StreamUtil streamUtil) {
this.taskManager = taskManager;
this.streamUtil = streamUtil;
}
-
- public ValidationState getValidationState(final Appendable errMsg) {
- storeProxySettings();
-
- FutureTask<Exception> executor = new FutureTask<Exception>(new
TestProxySettings(streamUtil));
- Exception result = null;
- try {
- result = executor.get(10, TimeUnit.SECONDS);
- } catch (final InterruptedException e) {
- result = e;
- } catch (final ExecutionException e) {
- result = e;
- } catch (final TimeoutException e) {
- result = e;
- }
-
- revertProxySettings();
-
- if (result == null)
- return ValidationState.OK;
-
- try {
- errMsg.append("Cytoscape was unable to connect to the
internet because:\n\n" + result.getMessage());
- } catch (final Exception e) {
- /* Intentionally ignored! */
- }
-
- return ValidationState.INVALID;
- }
-
+
public void run(TaskMonitor taskMonitor) {
- storeProxySettings();
- oldSettings.clear();
- }
+ taskMonitor.setProgress(0.01);
+ taskMonitor.setTitle("Set proxy server");
+ taskMonitor.setStatusMessage("Setting proxy server...");
+
+ // We run ProxySeting in another task, because TunableValidator
is used. If we run
+ // it in the same task, Cytoscape will be frozen during
validating process
+ ProxySettingsTask2 task = new
ProxySettingsTask2(this.taskManager, this.streamUtil);
+
+ this.insertTasksAfterCurrentTask(task);
- void storeProxySettings() {
- oldSettings.clear();
- for (String key : KEYS) {
- if (properties.getProperty(key) != null)
- oldSettings.put(key,
properties.getProperty(key));
- }
-
- if (type.getSelectedValue().equals("direct")) {
- for (String key : KEYS) {
- if (properties.getProperty(key) != null)
- properties.remove(key);
- }
- } else if (type.getSelectedValue().equals("http")) {
- properties.remove("socks.proxyHost");
- properties.remove("socks.proxyPort");
- properties.setProperty("http.proxyHost", hostname);
- properties.setProperty("http.proxyPort",
Integer.toString(port));
- } else if (type.getSelectedValue().equals("socks")) {
- properties.remove("http.proxyHost");
- properties.remove("http.proxyPort");
- properties.setProperty("socks.proxyHost", hostname);
- properties.setProperty("socks.proxyPort",
Integer.toString(port));
- }
+ taskMonitor.setProgress(0.05);
}
-
- void revertProxySettings() {
- for (String key : KEYS) {
- if (properties.getProperty(key) != null)
- properties.remove(key);
-
- if (oldSettings.containsKey(key))
- properties.setProperty(key,
oldSettings.get(key));
- }
- oldSettings.clear();
- }
-
- void dumpSettings(String title) {
- System.out.println(title);
- for (String key : KEYS)
- System.out.println(String.format("%s: %s", key,
properties.getProperty(key)));
- }
}
-
-class TestProxySettings implements Callable<Exception> {
- static final String TEST_URL = "http://www.google.com";
- final StreamUtil streamUtil;
-
- public TestProxySettings(final StreamUtil streamUtil) {
- this.streamUtil = streamUtil;
- }
-
- public Exception call() {
- try {
- final URL url = new URL(TEST_URL);
- streamUtil.getInputStream(url).close();
- } catch (final Exception ex) {
- return ex;
- }
-
- return null;
- }
-}
-
Added:
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask2.java
===================================================================
---
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask2.java
(rev 0)
+++
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettingsTask2.java
2011-07-14 23:32:45 UTC (rev 26187)
@@ -0,0 +1,156 @@
+package org.cytoscape.task.internal.proxysettings;
+
+
+import org.cytoscape.io.util.StreamUtil;
+
+import org.cytoscape.work.AbstractTask;
+import org.cytoscape.work.TaskManager;
+import org.cytoscape.work.TaskMonitor;
+import org.cytoscape.work.Tunable;
+import org.cytoscape.work.TunableValidator;
+import org.cytoscape.work.TunableValidator.ValidationState;
+import org.cytoscape.work.util.ListSingleSelection;
+
+import java.net.URL;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Arrays;
+import java.util.Properties;
+import java.util.concurrent.Callable;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.TimeUnit;
+
+
+/**
+ * Dialog for assigning proxy settings.
+ * @author Pasteur
+ */
+public class ProxySettingsTask2 extends AbstractTask implements
TunableValidator {
+ static final List<String> KEYS = Arrays.asList("http.proxyHost",
"http.proxyPort", "socks.proxyHost", "socks.proxyPort");
+
+ @Tunable(description="Type")
+ public ListSingleSelection<String> type = new
ListSingleSelection<String>("direct", "http", "socks");
+
+ @Tunable(description="Proxy
Server",groups={"param"},dependsOn="type!=direct",params="alignments=horizontal;displayState=hidden")
+ public String hostname="";
+
+
@Tunable(description="Port",groups={"param"},dependsOn="type!=direct",params="alignments=horizontal;displayState=hidden")
+ public int port=0;
+
+ final TaskManager taskManager;
+ final StreamUtil streamUtil;
+
+ final Map<String,String> oldSettings = new HashMap<String,String>();
+ final Properties properties = System.getProperties();
+
+ public ProxySettingsTask2(final TaskManager taskManager, final
StreamUtil streamUtil) {
+ this.taskManager = taskManager;
+ this.streamUtil = streamUtil;
+ }
+
+ public ValidationState getValidationState(final Appendable errMsg) {
+
+ storeProxySettings();
+
+ FutureTask<Exception> executor = new FutureTask<Exception>(new
TestProxySettings(streamUtil));
+ Exception result = null;
+ try {
+ result = executor.get(10, TimeUnit.SECONDS);
+ } catch (final InterruptedException e) {
+ result = e;
+ } catch (final ExecutionException e) {
+ result = e;
+ } catch (final TimeoutException e) {
+ result = e;
+ }
+
+ revertProxySettings();
+
+ if (result == null)
+ return ValidationState.OK;
+
+ try {
+ errMsg.append("Cytoscape was unable to connect to the
internet because:\n\n" + result.getMessage());
+ } catch (final Exception e) {
+ /* Intentionally ignored! */
+ }
+
+ return ValidationState.INVALID;
+ }
+
+ public void run(TaskMonitor taskMonitor) {
+
+ storeProxySettings();
+ oldSettings.clear();
+
+ taskMonitor.setProgress(1.0);
+ }
+
+ void storeProxySettings() {
+ oldSettings.clear();
+ for (String key : KEYS) {
+ if (properties.getProperty(key) != null)
+ oldSettings.put(key,
properties.getProperty(key));
+ }
+
+ if (type.getSelectedValue().equals("direct")) {
+ for (String key : KEYS) {
+ if (properties.getProperty(key) != null)
+ properties.remove(key);
+ }
+ } else if (type.getSelectedValue().equals("http")) {
+ properties.remove("socks.proxyHost");
+ properties.remove("socks.proxyPort");
+ properties.setProperty("http.proxyHost", hostname);
+ properties.setProperty("http.proxyPort",
Integer.toString(port));
+ } else if (type.getSelectedValue().equals("socks")) {
+ properties.remove("http.proxyHost");
+ properties.remove("http.proxyPort");
+ properties.setProperty("socks.proxyHost", hostname);
+ properties.setProperty("socks.proxyPort",
Integer.toString(port));
+ }
+ }
+
+ void revertProxySettings() {
+ for (String key : KEYS) {
+ if (properties.getProperty(key) != null)
+ properties.remove(key);
+
+ if (oldSettings.containsKey(key))
+ properties.setProperty(key,
oldSettings.get(key));
+ }
+ oldSettings.clear();
+ }
+
+ void dumpSettings(String title) {
+ System.out.println(title);
+ for (String key : KEYS)
+ System.out.println(String.format("%s: %s", key,
properties.getProperty(key)));
+ }
+}
+
+
+class TestProxySettings implements Callable<Exception> {
+ static final String TEST_URL = "http://www.google.com";
+ final StreamUtil streamUtil;
+
+ public TestProxySettings(final StreamUtil streamUtil) {
+ this.streamUtil = streamUtil;
+ }
+
+ public Exception call() {
+ try {
+ final URL url = new URL(TEST_URL);
+ streamUtil.getInputStream(url).close();
+ } catch (final Exception ex) {
+ return ex;
+ }
+
+ return null;
+ }
+}
+
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.