Skip to content

Commit

Permalink
Merge pull request #8 from egonw/2-networkPrefs
Browse files Browse the repository at this point in the history
Allow setting of custom network properties. Addressing bug #3487.
  • Loading branch information
goglepox committed Feb 20, 2013
2 parents 4252d98 + 32aefbf commit 35af67e
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 18 deletions.
6 changes: 6 additions & 0 deletions plugins/net.bioclipse.opentox/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
name="OpenTox"
class="net.bioclipse.opentox.prefs.ServicesPreferencePage">
</page>
<page
category="net.bioclipse.opentox.prefs.ServicesPreferencePage"
class="net.bioclipse.opentox.prefs.NetworkPreferencePage"
id="net.bioclipse.opentox.prefs.NetworkPreferencePage"
name="Network">
</page>
</extension>

<extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.bioclipse.opentox.business.IJavaScriptOpentoxManager;
import net.bioclipse.opentox.business.IOpentoxManager;
import net.bioclipse.usermanager.business.IUserManager;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
Expand All @@ -25,9 +26,6 @@ public class Activator extends AbstractUIPlugin {

public static final String PLUGIN_ID="net.bioclipse.opentox";

/** HTTP time out in milliseconds. */
public static final Integer TIME_OUT = 5000;

// The shared instance
private static Activator plugin;

Expand Down Expand Up @@ -109,5 +107,4 @@ public IJavaScriptOpentoxManager getJavaScriptOpentoxManager() {
}
return manager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

public class OpenToxConstants {

/** Preference constants for OpenTox services */
public static final String PREFS_SEPERATOR = "__SEP__";
public static final String SERVICES = "opentox.services";
public static final String PREFERENCES_OBJECT_DELIMITER = null;
public static final String PLUGIN_ID="net.bioclipse.opentox";

/** Preference constants for Network preferences */
public static final String SHORTEST_WAIT_TIME_IN_SECS = "opentox.network.waittime.shortest";
public static final String LONGEST_WAIT_TIME_IN_SECS = "opentox.network.waittime.longest";
public static final String HTTP_TIMEOUT = "opentox.network.http.timeout";

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import net.bioclipse.core.business.BioclipseException;
import net.bioclipse.core.domain.IMolecule;
import net.bioclipse.core.domain.StringMatrix;
import net.bioclipse.opentox.Activator;
import net.bioclipse.opentox.OpenToxConstants;
import net.bioclipse.rdf.business.IRDFStore;
import net.bioclipse.rdf.business.RDFManager;

Expand Down Expand Up @@ -360,21 +362,23 @@ public static String createNewDataset(
logger.debug("Task: " + responseString);
// OK, we got a task... let's wait until it is done
String task = method.getResponseBodyAsString();
Thread.sleep(1000); // let's be friendly, and wait 1 sec
int startWait = 1000 * Activator.getDefault().getPreferenceStore().getInt(OpenToxConstants.SHORTEST_WAIT_TIME_IN_SECS);
int endWait = 1000 * Activator.getDefault().getPreferenceStore().getInt(OpenToxConstants.LONGEST_WAIT_TIME_IN_SECS);
Thread.sleep(startWait); // let's be friendly and wait a bit
TaskState state = Task.getState(task);
while (!state.isFinished() && !monitor.isCanceled()) {
// let's be friendly, and wait 2 secs and a bit and increase
// let's be friendly, and wait some time and increase
// that time after each wait
int waitingTime = andABit(2000*tailing);
logger.debug("Waiting " + waitingTime + "ms.");
int waitingTime = andABit(startWait*tailing);
logger.debug("Waiting " + waitingTime + " ms.");
waitUnlessInterrupted(waitingTime, monitor);
state = Task.getState(task);
if (state.isRedirected()) {
task = state.getResults();
logger.debug(" new task, new task!!: " + task);
}
// but wait at most 20 secs and a bit
if (tailing < 10) tailing++;
// but wait at most the set endWait
if (startWait*tailing < endWait) tailing++;
}
if (monitor.isCanceled()) Task.delete(task);
// OK, it should be finished now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;

import net.bioclipse.opentox.Activator;
import net.bioclipse.opentox.OpenToxConstants;
import net.bioclipse.opentox.OpenToxLogInOutListener;

import org.apache.commons.httpclient.HttpMethodBase;
Expand All @@ -31,8 +32,10 @@ public class HttpMethodHelper {
public static HttpMethodBase addMethodHeaders(HttpMethodBase method,
Map<String, String> extraHeaders) {
// set the time out
method.getParams().setParameter("http.socket.timeout",
new Integer(Activator.TIME_OUT) );
method.getParams().setParameter(
"http.socket.timeout",
Activator.getDefault().getPreferenceStore().getInt(OpenToxConstants.HTTP_TIMEOUT) * 1000
);
OpenToxLogInOutListener openToxLogInOutListener;
try {
openToxLogInOutListener = OpenToxLogInOutListener.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.bioclipse.core.business.BioclipseException;
import net.bioclipse.core.domain.StringMatrix;
import net.bioclipse.opentox.Activator;
import net.bioclipse.opentox.OpenToxConstants;
import net.bioclipse.opentox.api.TaskState.STATUS;
import net.bioclipse.rdf.business.IRDFStore;
import net.bioclipse.rdf.business.RDFManager;
Expand Down Expand Up @@ -62,7 +63,9 @@ public class Task {
public static void delete(String task) throws IOException, GeneralSecurityException {
HttpClient client = new HttpClient();
DeleteMethod method = new DeleteMethod(task);
method.getParams().setParameter("http.socket.timeout", new Integer(Activator.TIME_OUT));
method.getParams().setParameter("http.socket.timeout",
Activator.getDefault().getPreferenceStore().getInt(OpenToxConstants.HTTP_TIMEOUT) * 1000
);
client.executeMethod(method);
int status = method.getStatusCode();
switch (status) {
Expand All @@ -89,7 +92,9 @@ public static TaskState getState(String task)
HttpMethodHelper.addMethodHeaders(method,
new HashMap<String,String>() {{ put("Accept", "application/rdf+xml"); }}
);
method.getParams().setParameter("http.socket.timeout", new Integer(Activator.TIME_OUT));
method.getParams().setParameter("http.socket.timeout",
Activator.getDefault().getPreferenceStore().getInt(OpenToxConstants.HTTP_TIMEOUT) * 1000
);
method.setRequestHeader("Accept", "application/rdf+xml");
client.executeMethod(method);
int status = method.getStatusCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ public class OpentoxPreferenceInitializer extends AbstractPreferenceInitializer
@Override
public void initializeDefaultPreferences() {

Preferences preferences =
DefaultScope.INSTANCE
.getNode( OpenToxConstants.PLUGIN_ID );

logger.debug("Initializing OpenTox Network preferences");

preferences.put(OpenToxConstants.SHORTEST_WAIT_TIME_IN_SECS, "2");
preferences.put(OpenToxConstants.LONGEST_WAIT_TIME_IN_SECS, "20");
preferences.put(OpenToxConstants.HTTP_TIMEOUT, "50");

logger.debug("Initializing OpenTox services");

//A list of OpenTox services in order
List<OpenToxService> openToxServices = new ArrayList<OpenToxService>();
List<String[]> toPrefs = new ArrayList<String[]>();

Preferences preferences =
DefaultScope.INSTANCE
.getNode( OpenToxConstants.PLUGIN_ID );

// Get the (new) services from the extension point.
List<OpenToxService> epservices = ServiceReader
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* Copyright (c) 2013 The Bioclipse Team and others.
*
* All rights reserved. This program and the accompanying materials
* are 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
*/
package net.bioclipse.opentox.prefs;

import net.bioclipse.opentox.Activator;
import net.bioclipse.opentox.OpenToxConstants;

import org.apache.log4j.Logger;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
* @author egonw
*/
public class NetworkPreferencePage extends PreferencePage
implements IWorkbenchPreferencePage {

private static final Logger logger =
Logger.getLogger(NetworkPreferencePage.class.toString());

private IntegerFieldEditor shortestTime;
private IntegerFieldEditor longestTime;
private IntegerFieldEditor timeout;

public NetworkPreferencePage() {
super();
}

@Override
public void init(IWorkbench workbench) {
}

protected IPreferenceStore doGetPreferenceStore() {
System.out.println("Really getting prefs store...");
return Activator.getDefault().getPreferenceStore();
}

@Override
protected Control createContents(Composite parent) {
System.out.println("Creating controls for the OT Network prefs page...");

timeout = new IntegerFieldEditor("HTTPTimeOut", "HTTP Time Out", parent);
timeout.setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
timeout.setValidRange(2, 100);
timeout.setPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (timeout.isValid()) {
setErrorMessage(null);
setValid(true);
return;
}
setErrorMessage(timeout.getErrorMessage());
setValid(false);
}
});

shortestTime = new IntegerFieldEditor("ShortestWaitTime", "Shortest Waiting Time", parent);
shortestTime.setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
shortestTime.setValidRange(2, 100);
shortestTime.setPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (shortestTime.isValid()) {
setErrorMessage(null);
setValid(true);
return;
}
setErrorMessage(shortestTime.getErrorMessage());
setValid(false);
}
});

longestTime = new IntegerFieldEditor("LongestWaitTime", "Longest Waiting Time", parent);
longestTime.setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
longestTime.setValidRange(2, 100);
longestTime.setPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (longestTime.isValid()) {
setErrorMessage(null);
setValid(true);
return;
}
setErrorMessage(longestTime.getErrorMessage());
setValid(false);
}
});

initializeValues();

return parent;
}

@Override
public boolean performOk() {
System.out.println("Performing Ok...");
storeValues();
return super.performOk();
}

@Override
protected void performDefaults() {
System.out.println("Setting defaults...");
super.performDefaults();
initializeDefaults();
}

private void initializeValues() {
IPreferenceStore store = getPreferenceStore();
System.out.println("Initializing OT Network values from store...");
shortestTime.setStringValue(store.getString(OpenToxConstants.SHORTEST_WAIT_TIME_IN_SECS));
longestTime.setStringValue(store.getString(OpenToxConstants.LONGEST_WAIT_TIME_IN_SECS));
timeout.setStringValue(store.getString(OpenToxConstants.HTTP_TIMEOUT));
}

private void storeValues() {
System.out.println("Storing OT Network values...");
IPreferenceStore store = getPreferenceStore();
store.setValue(OpenToxConstants.SHORTEST_WAIT_TIME_IN_SECS, shortestTime.getStringValue());
store.setValue(OpenToxConstants.LONGEST_WAIT_TIME_IN_SECS, longestTime.getStringValue());
store.setValue(OpenToxConstants.HTTP_TIMEOUT, timeout.getStringValue());
}

private void initializeDefaults() {
System.out.println("Initializing OT Network value defaults...");
IPreferenceStore store = getPreferenceStore();
shortestTime.setStringValue(store.getDefaultString(OpenToxConstants.SHORTEST_WAIT_TIME_IN_SECS));
longestTime.setStringValue(store.getDefaultString(OpenToxConstants.LONGEST_WAIT_TIME_IN_SECS));
timeout.setStringValue(store.getDefaultString(OpenToxConstants.HTTP_TIMEOUT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ else if (index==2){

}

class ApplicationsContentProvider implements IStructuredContentProvider {
public class ApplicationsContentProvider implements IStructuredContentProvider {
@SuppressWarnings("unchecked")
public Object[] getElements(Object inputElement) {
if (inputElement instanceof ArrayList) {
Expand Down

0 comments on commit 35af67e

Please sign in to comment.