001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.scop;
022
023import org.biojava.nbio.core.util.FileDownloadUtils;
024
025import java.net.URI;
026import java.net.URISyntaxException;
027
028/**
029 * Helper class to store paths to the four SCOP files
030 *
031 * The string "%s" is replaced with the version number.
032 * @author Spencer Bliven
033 *
034 */
035public class ScopMirror {
036        private String rootURL;
037        private final String claURL;
038        private final String desURL;
039        private final String hieURL;
040        private final String comURL;
041
042        /** Specify all keys individually */
043        public ScopMirror(String claURL, String desURL,
044                        String hieURL, String comURL) {
045                this.rootURL = null;
046                this.claURL = claURL;
047                this.desURL = desURL;
048                this.hieURL = hieURL;
049                this.comURL = comURL;
050        }
051        /** Specify a common root URL which is concatenated with individual filenames */
052        public ScopMirror(String url, String claURL, String desURL,
053                        String hieURL, String comURL) {
054                this(url+claURL, url+desURL, url+hieURL, url+comURL);
055                this.rootURL = url;
056        }
057        public ScopMirror(String url) {
058                this(url,
059                                "dir.cla.scop.txt_%s","dir.des.scop.txt_%s",
060                                "dir.hie.scop.txt_%s","dir.com.scop.txt_%s");
061        }
062        /** Use default MRC location */
063        public ScopMirror() {
064                this(ScopInstallation.SCOP_DOWNLOAD);
065        }
066
067        /**
068         * Get the URL for the root download directory, or null if none is set.
069         * @return
070         */
071        public String getRootURL() {
072                return rootURL;
073        }
074        public String getClaURL(String scopVersion) {
075                return String.format(claURL,scopVersion);
076        }
077        public String getDesURL(String scopVersion) {
078                return String.format(desURL,scopVersion);
079        }
080        public String getHieURL(String scopVersion) {
081                return String.format(hieURL,scopVersion);
082        }
083        public String getComURL(String scopVersion) {
084                return String.format(comURL,scopVersion);
085        }
086        public boolean isReachable() {
087                final int timeout = 800;
088                if(rootURL != null) {
089                        return FileDownloadUtils.ping(getRootURL(),timeout);
090                } else {
091                        try {
092                                URI cla = new URI(getClaURL("VERSION"));
093                                String host = cla.getHost();
094                                return FileDownloadUtils.ping(host,timeout);
095                        } catch (URISyntaxException e) {
096                                return false;
097                        }
098                }
099        }
100}