001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.core.services.impl;
016    
017    import org.apache.commons.configuration.AbstractConfiguration;
018    import org.apache.commons.configuration.CombinedConfiguration;
019    import org.apache.commons.configuration.Configuration;
020    import org.apache.commons.configuration.ConfigurationException;
021    import org.apache.commons.configuration.DatabaseConfiguration;
022    import org.apache.commons.configuration.HierarchicalINIConfiguration;
023    import org.apache.commons.configuration.JNDIConfiguration;
024    import org.apache.commons.configuration.PropertiesConfiguration;
025    import org.apache.commons.configuration.SystemConfiguration;
026    import org.apache.commons.configuration.XMLConfiguration;
027    import org.apache.commons.configuration.plist.PropertyListConfiguration;
028    import org.apache.tapestry5.ioc.Resource;
029    import org.chenillekit.core.services.ConfigurationService;
030    
031    import javax.naming.Context;
032    import javax.sql.DataSource;
033    
034    /**
035     * @version $Id: ConfigurationServiceImpl.java 670 2010-07-19 09:22:02Z mlusetti $
036     */
037    public class ConfigurationServiceImpl implements ConfigurationService
038    {
039        /**
040         * get the configuration from the named resource.
041         * <p/>
042         * the system properties merged auto. into the returned configuration
043         *
044         * @param configurationResource the configuration resource
045         *
046         * @return the configuration
047         */
048        public Configuration getConfiguration(Resource configurationResource)
049        {
050            return getConfiguration(configurationResource, true);
051        }
052    
053        /**
054         * get the configuration from the named resource.
055         *
056         * @param configurationResource the configuration resource
057         * @param mergeWithSysProps     merge the configuration resource with system properties
058         *
059         * @return the configuration
060         */
061        public Configuration getConfiguration(Resource configurationResource, boolean mergeWithSysProps)
062        {
063            Configuration configuration;
064    
065            assert configurationResource != null;
066    
067            if (!configurationResource.exists())
068                throw new RuntimeException(String.format("configuration resource '%s' not found", configurationResource.toString()));
069    
070            try
071            {
072                if (configurationResource.getFile().endsWith(".xml"))
073                    configuration = new XMLConfiguration(configurationResource.toURL());
074                else if (configurationResource.getFile().endsWith(".properties"))
075                    configuration = new PropertiesConfiguration(configurationResource.toURL());
076                else if (configurationResource.getFile().endsWith(".plist"))
077                    configuration = new PropertyListConfiguration(configurationResource.toURL());
078                else if (configurationResource.getFile().endsWith(".plist"))
079                    configuration = new PropertyListConfiguration(configurationResource.toURL());
080                else if (configurationResource.getFile().endsWith(".ini"))
081                    configuration = new HierarchicalINIConfiguration(configurationResource.toURL());
082                else
083                    throw new RuntimeException(String.format("cant resolve configuration type of resource '%s'", configurationResource.toString()));
084    
085                if (mergeWithSysProps)
086                {
087                    CombinedConfiguration mergedConfiguration = new CombinedConfiguration();
088                    mergedConfiguration.addConfiguration((AbstractConfiguration) configuration);
089                    mergedConfiguration.addConfiguration((AbstractConfiguration) getConfiguration());
090    
091                    configuration = mergedConfiguration;
092                }
093            }
094            catch (ConfigurationException e)
095            {
096                throw new RuntimeException(e);
097            }
098    
099            return configuration;
100        }
101    
102        /**
103         * get the configuration from JNDI context.
104         *
105         * @param context the JNDI context
106         *
107         * @return the configuration
108         */
109        public Configuration getConfiguration(Context context)
110        {
111            assert context != null;
112            return new JNDIConfiguration(context);
113        }
114    
115        /**
116         * get the configuration from system (JVM).
117         *
118         * @return the configuration
119         */
120        public Configuration getConfiguration()
121        {
122            return new SystemConfiguration();
123        }
124    
125        /**
126         * Build a configuration from a table containing multiple configurations.
127         *
128         * @param datasource  the datasource to connect to the database
129         * @param table       the name of the table containing the configurations
130         * @param nameColumn  the column containing the name of the configuration
131         * @param keyColumn   the column containing the keys of the configuration
132         * @param valueColumn the column containing the values of the configuration
133         * @param name        the name of the configuration
134         */
135        public Configuration getConfiguration(DataSource datasource, String table, String nameColumn,
136                                              String keyColumn, String valueColumn, String name)
137        {
138            return new DatabaseConfiguration(datasource, table, nameColumn, keyColumn, valueColumn, name);
139        }
140    }