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 * Created on Oct 12, 2011
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package org.biojava.nbio.structure.scop;
026
027import org.biojava.nbio.structure.align.client.JFatCatClient;
028import org.biojava.nbio.structure.align.util.HTTPConnectionTools;
029import org.biojava.nbio.structure.domain.SerializableCache;
030import org.biojava.nbio.structure.scop.server.ScopDomains;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import java.io.IOException;
035import java.io.InputStream;
036import java.net.MalformedURLException;
037import java.net.URL;
038import java.util.ArrayList;
039import java.util.List;
040
041
042/** An extension of the RemoteScopInstallation that caches some of the data locally.
043 *
044 * @author Andreas Prlic
045 *
046 */
047public class CachedRemoteScopInstallation extends SerializableCache<String,ScopDomain> implements ScopDatabase {
048
049        private static final Logger logger = LoggerFactory.getLogger(CachedRemoteScopInstallation.class);
050
051        private static final String CACHE_FILE_NAME = "remotescopinstallation.ser";
052
053        RemoteScopInstallation proxy ;
054
055        SerializableCache<Integer,ScopDescription> scopDescriptionCache ;
056
057        public CachedRemoteScopInstallation() throws IOException {
058                this(true);
059        }
060
061        public CachedRemoteScopInstallation(boolean useCache) throws IOException {
062
063                super(CACHE_FILE_NAME);
064
065                proxy = new RemoteScopInstallation();
066
067                scopDescriptionCache = new SerializableCache<Integer,ScopDescription>("scopDescriptionCache.ser");
068
069                if ( ! useCache) {
070                        logger.warn(getClass().getSimpleName() + " disabling cache");
071                        disableCache();
072                        scopDescriptionCache.disableCache();
073                } else {
074
075                        if ( serializedCache.size() < 8000){
076                                loadRepresentativeDomains();
077                        }
078                }
079
080        }
081
082
083        /** get the ranges of representative domains from the centralized server
084         *
085         */
086        private void loadRepresentativeDomains() throws IOException {
087
088                URL u = null;
089                try {
090                        u = new URL(RemoteScopInstallation.DEFAULT_SERVER + "getRepresentativeScopDomains");
091                } catch (MalformedURLException e) {
092                        throw new IOException("URL " + RemoteScopInstallation.DEFAULT_SERVER + "getRepresentativeScopDomains" + " is wrong", e);
093                }
094                logger.info("Using " + u + " to download representative domains");
095                InputStream response = HTTPConnectionTools.getInputStream(u);
096                String xml = JFatCatClient.convertStreamToString(response);
097                ScopDomains results  = ScopDomains.fromXML(xml);
098
099                logger.info("got " + results.getScopDomain().size() + " domain ranges for Scop domains from server.");
100                for (ScopDomain dom : results.getScopDomain()){
101                        String scopId = dom.getScopId();
102                        serializedCache.put(scopId, dom);
103                }
104
105        }
106
107
108
109        @Override
110        public List<ScopDescription> getByCategory(ScopCategory category) {
111                return proxy.getByCategory(category);
112        }
113
114
115        @Override
116        public List<ScopDescription> filterByClassificationId(String query) {
117                return proxy.filterByClassificationId(query);
118        }
119
120
121        @Override
122        public List<ScopNode> getTree(ScopDomain domain) {
123                return proxy.getTree(domain);
124        }
125
126
127        @Override
128        public List<ScopDomain> filterByDomainName(String query) {
129                return proxy.filterByDomainName(query);
130        }
131
132
133        @Override
134        public List<ScopDescription> filterByDescription(String query) {
135                return proxy.filterByClassificationId(query);
136        }
137
138
139        @Override
140        public ScopDescription getScopDescriptionBySunid(int sunid) {
141
142                ScopDescription desc = scopDescriptionCache.get(sunid);
143                if ( desc != null)
144                        return desc;
145
146
147                desc =  proxy.getScopDescriptionBySunid(sunid);
148                if ( desc != null)
149                        scopDescriptionCache.cache(sunid,desc);
150                return desc;
151        }
152
153
154        @Override
155        public List<ScopDomain> getDomainsForPDB(String pdbId) {
156
157                return proxy.getDomainsForPDB(pdbId);
158        }
159
160
161        @Override
162        public ScopDomain getDomainByScopID(String scopId) {
163                ScopDomain dom;
164
165                if ( serializedCache != null){
166                        if ( serializedCache.containsKey(scopId)) {
167                                dom = serializedCache.get(scopId);
168                                if ( dom != null) {
169                                        return dom;
170                                }
171                        }
172                }
173
174                dom = proxy.getDomainByScopID(scopId);
175
176                if ( dom != null)
177                        cache(scopId, dom);
178
179
180                return dom;
181        }
182
183
184        @Override
185        public ScopNode getScopNode(int sunid) {
186                return proxy.getScopNode(sunid);
187        }
188
189
190        @Override
191        public String getScopVersion() {
192                return proxy.getScopVersion();
193        }
194
195        @Override
196        public void setScopVersion(String version) {
197                proxy.setScopVersion(version);
198        }
199
200
201        @Override
202        public List<ScopDomain> getScopDomainsBySunid(Integer sunid) {
203                return proxy.getScopDomainsBySunid(sunid);
204        }
205
206        @Override
207        public void flushCache() {
208                logger.info("flushing " + getClass().getSimpleName());
209                super.flushCache();
210                scopDescriptionCache.flushCache();
211        }
212
213        @Override
214        public List<String> getComments(int sunid) {
215                return new ArrayList<String>(1);
216        }
217
218
219}