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 */
021
022package org.biojava.directory;
023
024import java.io.BufferedReader;
025import java.io.File;
026import java.io.IOException;
027import java.io.InputStreamReader;
028import java.net.URL;
029import java.util.ArrayList;
030import java.util.Iterator;
031import java.util.List;
032import java.util.StringTokenizer;
033
034/**
035 * <p>A registry that loads up the standard biodirectory files.</p>
036 *
037 * <p>
038 * This class will search for the following files in turn:
039 * <ol>
040 * <li>~/.bioinformatics/seqdatabase.ini where ~ is the JAVA user home system
041 * property</li>
042 * <li>/etc/bioinformatics/seqdatabase.ini</li>
043 * <li>"http://www.open-bio.org/registry/seqdatabase.ini</li>
044 * </ol>
045 * </p>
046 *
047 * <p>The default search path may be replaced by an alternative search
048 * path specified by the <code>OBDA_SEARCH_PATH</code> system
049 * environment variable. This environment variable is a "+" delimted
050 * string of files and URLs. The search order proceeds from read left
051 * to right.</p>
052 *
053 * @author Thomas Down
054 * @author Matthew Pocock
055 * @author Keith James
056 */
057public class SystemRegistry {
058
059    public static final String CONFIG_LOCATOR =
060        "http://www.open-bio.org/registry/seqdatabase.ini";
061
062    public static final String CONFIG_FILE = "seqdatabase.ini";
063
064    public static final String OBDA_SEARCH_ENV = "OBDA_SEARCH_PATH";
065
066    private static Registry systemRegistry;
067
068    /**
069     * Get the singleton Registry instance representing the system-wide
070     * default registry.
071     *
072     * @return the system-wide Registry object.
073     */
074    public static Registry instance() {
075        if (systemRegistry == null) {
076            RegistryConfiguration.Composite regConfig
077                = new RegistryConfiguration.Composite();
078            Iterator i = getRegistryPath().iterator();
079
080            while (i.hasNext()) {
081                try {
082                    String locator = (String) i.next();
083                    URL url = new URL(locator);
084
085                    if (url.getProtocol().equals("file")) {
086                        File file = new File(url.getPath());
087                        if (! file.exists() || ! file.canRead()) {
088                            // FIXME - log this
089                            continue;
090                        }
091                    }
092
093                    BufferedReader stream = null;
094
095                    try {
096                        stream = new BufferedReader(new InputStreamReader(url.openStream()));
097                    }
098                    catch (IOException ioe) {
099                        // FIXME - log this
100                    }
101
102                    if (stream != null) {
103                        try {
104                            RegistryConfiguration cfg =
105                                OBDARegistryParser.parseRegistry(stream, locator);
106                            regConfig.addBottomConfig(cfg);
107                        } catch (Exception ex) {
108                            // FIXME - log this
109                            ex.printStackTrace();
110                        }
111                    } 
112                } catch (Exception ex) {
113                    // FIXME - log this
114                    ex.printStackTrace();
115                }
116            }
117
118            systemRegistry = new Registry(regConfig);
119        }
120
121        return systemRegistry;
122    }
123
124    /**
125     * Get the list of places that will be searched for registry
126     * files.
127     *
128     * @return a List of strings that are URLs to bioregistry files.
129     */
130    public static List getRegistryPath() {
131        List registryPath = new ArrayList();
132
133        String customPath = System.getProperty(OBDA_SEARCH_ENV);
134        if (customPath != null) {
135            StringTokenizer st = new StringTokenizer(customPath, "+");
136            while (st.hasMoreTokens()) {
137                registryPath.add(st.nextToken());
138            }
139        } else {
140            String userHome = System.getProperty("user.home");
141            if (userHome != null) {
142                registryPath.add("file://"
143                                 + userHome
144                                 + "/.bioinformatics/"
145                                 + CONFIG_FILE);
146            }
147
148            registryPath.add("file:///etc/bioinformatics/" + CONFIG_FILE);
149            registryPath.add(CONFIG_LOCATOR);
150        }
151
152        return registryPath;
153    }
154}