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 741 2010-11-16 15:20:59Z mlusetti $
036     * @deprecated to be removed in future release (soon)
037     */
038    @Deprecated
039    public class ConfigurationServiceImpl implements ConfigurationService
040    {
041        /**
042         * get the configuration from the named resource.
043         * <p/>
044         * the system properties merged auto. into the returned configuration
045         *
046         * @param configurationResource the configuration resource
047         *
048         * @return the configuration
049         */
050        public Configuration getConfiguration(Resource configurationResource)
051        {
052            return getConfiguration(configurationResource, true);
053        }
054    
055        /**
056         * get the configuration from the named resource.
057         *
058         * @param configurationResource the configuration resource
059         * @param mergeWithSysProps     merge the configuration resource with system properties
060         *
061         * @return the configuration
062         */
063        public Configuration getConfiguration(Resource configurationResource, boolean mergeWithSysProps)
064        {
065            Configuration configuration;
066    
067            assert configurationResource != null;
068    
069            if (!configurationResource.exists())
070                throw new RuntimeException(String.format("configuration resource '%s' not found", configurationResource.toString()));
071    
072            try
073            {
074                if (configurationResource.getFile().endsWith(".xml"))
075                    configuration = new XMLConfiguration(configurationResource.toURL());
076                else if (configurationResource.getFile().endsWith(".properties"))
077                    configuration = new PropertiesConfiguration(configurationResource.toURL());
078                else if (configurationResource.getFile().endsWith(".plist"))
079                    configuration = new PropertyListConfiguration(configurationResource.toURL());
080                else if (configurationResource.getFile().endsWith(".plist"))
081                    configuration = new PropertyListConfiguration(configurationResource.toURL());
082                else if (configurationResource.getFile().endsWith(".ini"))
083                    configuration = new HierarchicalINIConfiguration(configurationResource.toURL());
084                else
085                    throw new RuntimeException(String.format("cant resolve configuration type of resource '%s'", configurationResource.toString()));
086    
087                if (mergeWithSysProps)
088                {
089                    CombinedConfiguration mergedConfiguration = new CombinedConfiguration();
090                    mergedConfiguration.addConfiguration((AbstractConfiguration) configuration);
091                    mergedConfiguration.addConfiguration((AbstractConfiguration) getConfiguration());
092    
093                    configuration = mergedConfiguration;
094                }
095            }
096            catch (ConfigurationException e)
097            {
098                throw new RuntimeException(e);
099            }
100    
101            return configuration;
102        }
103    
104        /**
105         * get the configuration from JNDI context.
106         *
107         * @param context the JNDI context
108         *
109         * @return the configuration
110         */
111        public Configuration getConfiguration(Context context)
112        {
113            assert context != null;
114            return new JNDIConfiguration(context);
115        }
116    
117        /**
118         * get the configuration from system (JVM).
119         *
120         * @return the configuration
121         */
122        public Configuration getConfiguration()
123        {
124            return new SystemConfiguration();
125        }
126    
127        /**
128         * Build a configuration from a table containing multiple configurations.
129         *
130         * @param datasource  the datasource to connect to the database
131         * @param table       the name of the table containing the configurations
132         * @param nameColumn  the column containing the name of the configuration
133         * @param keyColumn   the column containing the keys of the configuration
134         * @param valueColumn the column containing the values of the configuration
135         * @param name        the name of the configuration
136         */
137        public Configuration getConfiguration(DataSource datasource, String table, String nameColumn,
138                                              String keyColumn, String valueColumn, String name)
139        {
140            return new DatabaseConfiguration(datasource, table, nameColumn, keyColumn, valueColumn, name);
141        }
142    }