+++ /dev/null
-/**
- * BioJava development code
- *
- * This code may be freely distributed and modified under the
- * terms of the GNU Lesser General Public Licence. This should
- * be distributed with the code. If you do not have a copy,
- * see:
- *
- * http://www.gnu.org/copyleft/lesser.html
- *
- * Copyright for this code is held jointly by the individual
- * authors. These should be listed in @author doc comments.
- *
- * For more information on the BioJava project and its aims,
- * or to join the biojava-l mailing list, visit the home page
- * at:
- *
- * http://www.biojava.org/
- *
- * Created on 19.03.2004
- * @author Andreas Prlic
- *
- */
-package org.biojava.dasobert.das;
-
-
-import java.net.URL ;
-import java.io.InputStream ;
-import org.xml.sax.InputSource ;
-import org.xml.sax.XMLReader ;
-import javax.xml.parsers.* ;
-import org.xml.sax.* ;
-import java.util.ArrayList ;
-import java.util.List;
-import java.util.logging.* ;
-import java.net.HttpURLConnection ;
-import java.io.IOException;
-import java.net.ConnectException;
-import java.lang.reflect.Method;
-
-
-
-
-/**
- * A class to perform a DAS features request
- *
- * @author Andreas Prlic
- *
- */
-public class DAS_FeatureRetrieve {
-
- List features ;
- Logger logger ;
- int comeBackLater;
- URL url;
- /**
- * @param url the URL the features should be downloaded from
- *
- */
- public DAS_FeatureRetrieve(URL url) {
- super();
-
- logger = Logger.getLogger("org.biojava.spice");
- features = new ArrayList() ;
- comeBackLater = -1;
- this.url=url;
- reload();
- }
-
-
- /** contact the DAS-feature server again. Usually
- * it is not necessary to call this again, because the constructor already does, but
- * if comeBackLater > -1 this should be called again.
- *
- */
- public void reload(){
-
- try {
-
- InputStream dasInStream = null;
- try {
- dasInStream = open(url);
- } catch (Exception e ){
- comeBackLater = -1;
- logger.log(Level.FINE,"could not open connection to " + url,e);
- return ;
- }
-
-
- SAXParserFactory spfactory =
- SAXParserFactory.newInstance();
-
- spfactory.setValidating(false);
-
- SAXParser saxParser = null ;
-
- try{
- saxParser =
- spfactory.newSAXParser();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- }
-
-
-
- String vali = System.getProperty("XMLVALIDATION");
-
- boolean validation = false ;
- if ( vali != null )
- if ( vali.equals("true") )
- validation = true ;
-
-
- XMLReader xmlreader = saxParser.getXMLReader();
-
- //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
- try {
- xmlreader.setFeature("http://xml.org/sax/features/validation", validation);
- } catch (SAXException e) {
- logger.log(Level.FINE,"Cannot set validation " + validation);
- }
-
- try {
- xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);
- } catch (SAXNotRecognizedException e){
- e.printStackTrace();
- logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
-
- }
-
- DAS_Feature_Handler cont_handle = new DAS_Feature_Handler() ;
- cont_handle.setDASCommand(url.toString());
- xmlreader.setContentHandler(cont_handle);
- xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
- InputSource insource = new InputSource() ;
- insource.setByteStream(dasInStream);
-
- try {
- xmlreader.parse(insource);
- features = cont_handle.get_features();
- comeBackLater = cont_handle.getComBackLater();
- }
- catch ( Exception e){
- e.printStackTrace();
- logger.log(Level.FINE,"error while parsing response from "+ url);
- comeBackLater = -1;
- features = new ArrayList();
- }
- }
- catch (Exception ex) {
- ex.printStackTrace();
- comeBackLater = -1;
- }
- }
-
-
- /** open HttpURLConnection. Recommended way to open
- * HttpURLConnections, since this take care of setting timeouts
- * properly for java 1.4 and 1.5*/
- public static HttpURLConnection openHttpURLConnection(URL url)
- throws IOException, ConnectException {
- HttpURLConnection huc = null;
- huc = (HttpURLConnection) url.openConnection();
-
- String os_name = java.lang.System.getProperty("os.name");
- String os_version = java.lang.System.getProperty("os.version");
- String os_arch = java.lang.System.getProperty("os.arch");
- String VERSION = "1.0";
-
- String userAgent = "Jalview " + VERSION + "("+os_name+"; "+os_arch + " ; "+ os_version+")";
- //e.g. "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803"
- huc.addRequestProperty("User-Agent", userAgent);
- //logger.finest("opening "+url);
-
-
- // use reflection to determine if get and set timeout methods for urlconnection are available
- // seems java 1.5 does not watch the System properties any longer...
- // and java 1.4 did not provide these...
- // for 1.4 see setSystemProperties
- int timeout = 15000;
- try {
- // try to use reflection to set timeout property
- Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");
-
- Method setconnecttimeout = urlconnectionClass.getMethod (
- "setConnectTimeout", new Class [] {int.class}
- );
- setconnecttimeout.invoke(huc,new Object[] {new Integer(timeout)});
-
- Method setreadtimeout = urlconnectionClass.getMethod (
- "setReadTimeout", new Class[] {int.class}
- );
- setreadtimeout.invoke(huc,new Object[] {new Integer(timeout)});
- //System.out.println("successfully set java 1.5 timeout");
- } catch (Exception e) {
- //e.printStackTrace();
- // most likely it was a NoSuchMEthodException and we are running java 1.4.
- }
- return huc;
- }
-
-
- private InputStream open(URL url)
- throws java.io.IOException, java.net.ConnectException
- {
- InputStream inStream = null;
-
-
- HttpURLConnection huc = openHttpURLConnection(url);
-
- inStream = huc.getInputStream();
-
- return inStream;
-
- }
-
- /** returns a List of Features
- * @return a List of Maps containing the features*/
- public List get_features() {
-
- return features;
- }
-
- /** returns the comeBackLater value - if a server returned suchh -
- *
- * @return comeBackLater in seconds, or -1 if not provided by server
- */
- public int getComeBackLater(){
-
- return comeBackLater;
-
- }
-
-
-}
+++ /dev/null
-/*
- * BioJava development code
- *
- * This code may be freely distributed and modified under the
- * terms of the GNU Lesser General Public Licence. This should
- * be distributed with the code. If you do not have a copy,
- * see:
- *
- * http://www.gnu.org/copyleft/lesser.html
- *
- * Copyright for this code is held jointly by the individual
- * authors. These should be listed in @author doc comments.
- *
- * For more information on the BioJava project and its aims,
- * or to join the biojava-l mailing list, visit the home page
- * at:
- *
- * http://www.biojava.org/
- *
- * Created on 19.03.2004
- * @author Andreas Prlic
- *
- */
-package org.biojava.dasobert.das;
-
-import org.xml.sax.helpers.DefaultHandler;
-import org.xml.sax.Attributes;
-
-import java.util.ArrayList ;
-import java.util.HashMap ;
-import java.util.List;
-
-/**
- * a class to parse the response of a DAS - Feature request
- * @author Andreas Prlic
- *
- */
-public class DAS_Feature_Handler extends DefaultHandler{
-
- /**
- *
- */
- List features ;
- boolean first_flag ;
- HashMap feature ;
- String featurefield ;
- String characterdata ;
- String dasCommand ;
-
- int comeBackLater ;
-
- int maxFeatures ;
-
- public DAS_Feature_Handler() {
- super();
-
- features= new ArrayList() ;
- first_flag = true ;
- featurefield = "" ;
- characterdata = "";
- dasCommand = "" ;
- comeBackLater = -1;
- maxFeatures = -1;
- }
-
- /** specifies a maximum number of features to be downloaded. if a
- server returns more, they will be ignored. default is to load
- all features
- @param max the maximium number of features to be downloaded
- */
-
- public void setMaxFeatures(int max) {
- maxFeatures = max;
- }
-
- public int getMaxFeatures() {
- return maxFeatures;
- }
-
- public void setDASCommand(String cmd) { dasCommand = cmd ;}
- public String getDASCommand() { return dasCommand; }
-
- public List get_features() {
- return features ;
- }
-
- public int getComBackLater(){
- return comeBackLater;
- }
-
- void start_feature(String uri, String name, String qName, Attributes atts) {
-
- if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) {
- characterdata = "";
- return;
- }
- feature = new HashMap() ;
- String id = atts.getValue("id");
- feature.put("id",id);
- feature.put("dassource",dasCommand);
- characterdata = "";
- }
-
- void add_featuredata(String uri, String name, String qName) {
- //System.out.println("featurefield "+featurefield+ " data "+characterdata);
- // NOTE can have multiple lines ..
-
- if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) {
- return;
- }
-
-
- String data = (String)feature.get(featurefield);
- if (data != null){
- characterdata = data + " " + characterdata;
- }
-
- feature.put(featurefield,characterdata);
- featurefield = "";
- characterdata = "";
- }
-
- private void addLink(String uri, String name, String qName, Attributes atts) {
- String href = atts.getValue("href");
- feature.put("LINK",href);
- characterdata="";
- featurefield = "LINK-TEXT";
-
- }
-
- public void startElement (String uri, String name, String qName, Attributes atts){
- //System.out.println("new element "+qName);
-
- if (qName.equals("FEATURE"))
- start_feature(uri, name, qName, atts);
- else if ( qName.equals("LINK"))
- addLink(uri,name,qName, atts);
- else if ( qName.equals("METHOD") ||
- qName.equals("TYPE") ||
- qName.equals("START") ||
- qName.equals("END") ||
- qName.equals("NOTE") ||
- qName.equals("SCORE")
- ){
- characterdata ="";
- featurefield = qName ;
- }
-
- }
-
- public void startDocument() {
- }
-
- public void endDocument () {
- }
- public void endElement(String uri, String name, String qName) {
-
- if ( qName.equals("METHOD") ||
- qName.equals("TYPE") ||
- qName.equals("START") ||
- qName.equals("END") ||
- qName.equals("NOTE") ||
- qName.equals("LINK") ||
- qName.equals("SCORE")
- ) {
- add_featuredata(uri,name,qName);
- }
- else if ( qName.equals("FEATURE")) {
-
- if ( maxFeatures > 0 ) {
- if ( features.size() < maxFeatures ) {
- features.add(feature);
- }
- } else {
- // no restriction
- features.add(feature);
- }
- }
- }
-
- public void characters (char ch[], int start, int length){
-
- for (int i = start; i < start + length; i++) {
-
- characterdata += ch[i];
- }
-
- }
-
-}
+++ /dev/null
-/*
- * BioJava development code
- *
- * This code may be freely distributed and modified under the
- * terms of the GNU Lesser General Public Licence. This should
- * be distributed with the code. If you do not have a copy,
- * see:
- *
- * http://www.gnu.org/copyleft/lesser.html
- *
- * Copyright for this code is held jointly by the individual
- * authors. These should be listed in @author doc comments.
- *
- * For more information on the BioJava project and its aims,
- * or to join the biojava-l mailing list, visit the home page
- * at:
- *
- * http://www.biojava.org/
- *
- * Created on 21.09.2004
- * @author Andreas Prlic
- *
- */
-
-package org.biojava.dasobert.das ;
-
-import java.util.*;
-import java.net.*;
-import java.util.logging.* ;
-import org.biojava.dasobert.eventmodel.FeatureListener;
-import org.biojava.dasobert.eventmodel.FeatureEvent;
-import org.biojava.dasobert.dasregistry.Das1Source;
-
-/** a thread that connects to a DAS - Feature service and gets the features
- *
- * @author Andreas Prlic
- */
-
-
-
-public class FeatureThread
- implements Runnable
-{
-
- /** number of times the client tries to reconnect to the server if a "come back later" is returned.
- * the server should provide a reasonable estimation how long it will take him to create results.
- * if this number of requests is still not successfull, give up.
- */
- public static int MAX_COME_BACK_ITERATIONS = 5;
-
- public static int MAX_NR_FEATURES = 300;
-
- static Logger logger = Logger.getLogger("org.biojava.spice");
-
- Das1Source dasSource;
- String ac ;
- List featureListeners;
- Thread thread;
-
- public FeatureThread (String accessionCode, Das1Source dasSource) {
- this.dasSource = dasSource;
- this.ac = accessionCode;
- featureListeners = new ArrayList();
- }
-
- public void addFeatureListener(FeatureListener li) {
- featureListeners.add(li);
- }
-
- public void clearFeatureListeners() {
- featureListeners.clear();
- }
-
- public synchronized void stop(){
- thread = null;
- notify();
- }
-
-
-
-
- public void run() {
- Thread me = Thread.currentThread();
- while ( thread == me) {
- String url = dasSource.getUrl();
- String queryString = url + "features?segment="+ ac ;
- URL cmd = null ;
- try {
- cmd = new URL(queryString);
- } catch (MalformedURLException e ) {
- logger.warning("got MalformedURL from das source " +dasSource);
- e.printStackTrace();
-
- }
-
- logger.info("requesting features from " + cmd);
- DAS_FeatureRetrieve ftmp = new DAS_FeatureRetrieve(cmd);
-
-
- int comeBackLater = ftmp.getComeBackLater();
- int securityCounter = 0;
- while ( (thread == me) && ( comeBackLater > 0 )) {
- securityCounter++;
- if ( securityCounter >= MAX_COME_BACK_ITERATIONS){
- comeBackLater = -1;
- break;
-
- }
- notifyComeBackLater(comeBackLater);
- // server is still calculating - asks us to come back later
- try {
- wait (comeBackLater);
- } catch (InterruptedException e){
- comeBackLater = -1;
- break;
- }
-
- ftmp.reload();
- comeBackLater = ftmp.getComeBackLater();
- }
-
- if ( ! (thread == me ) ) {
- break;
- }
-
- List features = ftmp.get_features();
-
- // a fallback mechanism to prevent DAS sources from bringing down spice
- if ( features.size() > MAX_NR_FEATURES){
- logger.warning("DAS source returned more than " + MAX_NR_FEATURES + "features. " +
- " throwing away excess features at " +cmd);
- features = features.subList(0,MAX_NR_FEATURES);
- }
-
-
- // notify FeatureListeners
- Map[] feats = (Map[])features.toArray(new Map[features.size()]);
- notifyFeatureListeners(feats);
-
- break;
-
-
- }
- thread = null;
-
- }
-
- public void start() {
- thread = new Thread(this);
- thread.start();
- }
-
- private void notifyFeatureListeners(Map[] feats){
- logger.finest("FeatureThread found " + feats.length + " features");
- FeatureEvent fevent = new FeatureEvent(feats,dasSource);
- Iterator fiter = featureListeners.iterator();
- while (fiter.hasNext()){
- FeatureListener fi = (FeatureListener)fiter.next();
- fi.newFeatures(fevent);
- }
- }
-
- /** the Annotation server requested to be queried again in a while
- *
- * @param comeBackLater
- */
- private void notifyComeBackLater(int comeBackLater){
- FeatureEvent event = new FeatureEvent(new HashMap[0],dasSource);
- event.setComeBackLater(comeBackLater);
- Iterator fiter = featureListeners.iterator();
- while (fiter.hasNext()){
- FeatureListener fi = (FeatureListener)fiter.next();
- fi.comeBackLater(event);
- }
-
- }
-
-
-}
-