/*
 * Created on Mar 10, 2005 <p>
 * By Eddie <p>
 * Usage of this class requires a biomoby.properties file to be specified and properly
 * configured.
 * An example file is detailed below:

#specify the domain of your local registry
#The RESOURCES and LSID resolver servlets, currently
#utilize this property. It allows users to replace the
#biomoby.org domain with there own domain.

domain = biomoby.org
#domain = localhost:8080

#specify the test registry url endpoint
test_url = http://mobycentral.cbr.nrc.ca:8080/cgi-bin/MOBY05/testmobycentral.pl

#specify the test registry uri
test_uri = http://mobycentral.cbr.nrc.ca:8080/MOBY/Central

#specify the path to mobycentral.config
#this file is usually kept in your apacheHome/conf directory.
#this variable is used for mySQL querying of the registry, among
#other things.
config = /usr/local/apache2/conf/mobycentral.config

#you can add any other properties that you would like applications
#to utilize and share.
 */
package org.biomoby.registry.properties;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.biomoby.shared.MobyException;

/**
 * @author Eddie Kawas
 * <p>This class was created to provide classes that depend on external variables a way to retrieve these variables.
 * <p>For questions, comments, or bugs
 * <p>email me at edward.kawas@gmail.com
 */
@SuppressWarnings("unchecked")
public class MobyProperties {
    private static Properties properties = new Properties();
    
    /**
     * 
     * Constructs a Properties object with properties obtained from environment 
     * <p><b>PRE:</b>None.
     * <p><b>POST:</b>None.
     * @return A Properties object with either the properties obtained from the environment or an empty map.
     */
    public final static Properties PROPERTIES() {
        Map map = new HashMap();
        // 
        if (System.getProperty("MOBY_CENTRAL_CONFIG") != null && !System.getProperty("MOBY_CENTRAL_CONFIG").equals(""))
        	map.put("MOBY_CENTRAL_CONFIG" , System.getProperty("MOBY_CENTRAL_CONFIG"));
        if (System.getProperty("MOBY_SERVER") != null && !System.getProperty("MOBY_SERVER").equals(""))
        	map.put("MOBY_SERVER" , System.getProperty("MOBY_SERVER"));
        if (System.getProperty("MOBY_URI") != null && !System.getProperty("MOBY_URI").equals(""))
        	map.put("MOBY_URI" , System.getProperty("MOBY_URI"));
        // did we fail to populate the map?
        if (map.isEmpty())
        	properties.putAll(RegistryOS.getEnv());
        return properties;
    }
    
    /**
     * 
     * Retrieve the properties for [mobyobject] in mobycentral.conf 
     * <p><b>PRE:</b>
     * <p><b>POST:The properties contained in mobycentral.conf for [mobyobject] are returned in a Properties object.</b>
     * @return Properties contained in the mobycentral.conf file.
     */
    public final static Properties OBJECT_PROPERTIES() {
        Map map = new HashMap();
        try {
            map = MobyCentralConfig.getMobyObject();
        } catch (MobyException e) {
            System.err.println("Error retrieving Moby Object Properties.\n" + e);
        }
        Properties p = new Properties();
        p.putAll(map);
        return p;
    }
    
    /**
     * 
     * Retrieve the properties for [mobycentral] in mobycentral.conf 
     * <p><b>PRE:</b>
     * <p><b>POST:The properties contained in mobycentral.conf for [mobycentral] are returned in a Properties object.</b>
     * @return Properties contained in the mobycentral.conf file.
     */
    public final static Properties SERVICE_INSTANCE_PROPERTIES() {
        Map map = new HashMap();
        try {
            map = MobyCentralConfig.getMobyCentral();
        } catch (MobyException e) {
            System.err.println("Error retrieving Moby Service Instance Properties.\n" + e);
        }
        Properties p = new Properties();
        p.putAll(map);
        return p;
    }
    
    /**
     * 
     * Retrieve the properties for [mobyservice] in mobycentral.conf 
     * <p><b>PRE:</b>
     * <p><b>POST:The properties contained in mobycentral.conf for [mobyservice] are returned in a Properties object.</b>
     * @return Properties contained in the mobycentral.conf file.
     */
    public final static Properties SERVICE_PROPERTIES() {
        Map map = new HashMap();
        try {
            map = MobyCentralConfig.getMobyService();
        } catch (MobyException e) {
            System.err.println("Error retrieving Moby Service Properties.\n" + e);
        }
        Properties p = new Properties();
        p.putAll(map);
        return p;
    }
    
    /**
     * 
     * Retrieve the properties for [mobynamespace] in mobycentral.conf 
     * <p><b>PRE:</b>
     * <p><b>POST:The properties contained in mobycentral.conf for [mobynamespace] are returned in a Properties object.</b>
     * @return Properties contained in the mobycentral.conf file.
     */
    public final static Properties NAMESPACE_PROPERTIES() {
        Map map = new HashMap();
        try {
            map = MobyCentralConfig.getMobyNamespace();
        } catch (MobyException e) {
            System.err.println("Error retrieving Moby Namespace Properties.\n" + e);
        }
        Properties p = new Properties();
        p.putAll(map);
        return p;
    }
    
    /**
     * 
     * Retrieve the properties for [mobyrelationship] in mobycentral.conf 
     * <p><b>PRE:</b>
     * <p><b>POST:The properties contained in mobycentral.conf for [mobyrelationship] are returned in a Properties object.</b>
     * @return Properties contained in the mobycentral.conf file.
     */
    public final static Properties RELATIONSHIP_PROPERTIES() {
        Map map = new HashMap();
        try {
            map = MobyCentralConfig.getMobyRelationship();
        } catch (MobyException e) {
            System.err.println("Error retrieving Moby Relationship Properties.\n" + e);
        }
        Properties p = new Properties();
        p.putAll(map);
        return p;
    }
    
    /* a little test method*/
    public static void main(String[] args) {
        System.out.println(NAMESPACE_PROPERTIES());
    }
}
