001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.config;
023
024import java.io.IOException;
025import java.io.InputStream;
026
027import javax.servlet.ServletContext;
028
029import org.granite.config.api.Configuration;
030import org.granite.config.api.internal.ConfigurationImpl;
031import org.granite.config.flex.ServicesConfig;
032import org.granite.config.flex.ServletServicesConfig;
033import org.granite.logging.Logger;
034import org.xml.sax.SAXException;
035
036
037public abstract class AbstractFrameworkGraniteConfig {
038
039    ///////////////////////////////////////////////////////////////////////////
040    // Static fields.
041
042    private static final Logger log = Logger.getLogger(AbstractFrameworkGraniteConfig.class);
043
044    private static final String GRANITE_CONFIG_DEFAULT = "/WEB-INF/granite/granite-config.xml";
045    private static final String SERVICES_CONFIG_DEFAULT = "/WEB-INF/flex/services-config.xml";
046
047    ///////////////////////////////////////////////////////////////////////////
048    // Instance fields.
049
050    private GraniteConfig graniteConfig = null;
051
052    private ServicesConfig servicesConfig = null;
053
054    ///////////////////////////////////////////////////////////////////////////
055    // Constructor.
056
057    ///////////////////////////////////////////////////////////////////////////
058    // Static GraniteConfig loaders.
059
060    protected void init(ServletContext servletContext, String configPath) throws IOException, SAXException {
061        String path = configuration.getGraniteConfig();
062        if (path == null)
063                path = GRANITE_CONFIG_DEFAULT;
064
065        InputStream is = servletContext.getResourceAsStream(path);
066        if (is == null) {
067            log.warn("Could not load custom granite-config.xml: %s (file does not exists)", path);
068            path = null;
069        }
070        
071        this.graniteConfig = new GraniteConfig(configPath, is, null, null);
072        
073        ServletGraniteConfig.loadConfig(servletContext, graniteConfig);
074        
075        path = configuration.getFlexServicesConfig();
076        if (path == null)
077                path = SERVICES_CONFIG_DEFAULT;
078
079        is = servletContext.getResourceAsStream(path);
080        if (is == null) {
081            log.warn("Could not load custom services-config.xml: %s (file does not exists)", path);
082            path = null;
083        }
084        
085        this.servicesConfig = new ServicesConfig(is, null, false);
086        
087        ServletServicesConfig.loadConfig(servletContext, servicesConfig);
088    }
089    
090    public GraniteConfig getGraniteConfig() {
091        return graniteConfig;
092    }
093    
094    public ServicesConfig getServicesConfig() {
095        return servicesConfig;
096    }
097    
098    protected Configuration configuration = new ConfigurationImpl();
099
100    public void setCustomGraniteConfigPath(String path) {
101        configuration.setGraniteConfig(path);
102    }
103
104    public void setCustomServicesConfigPath(String path) {
105        configuration.setFlexServicesConfig(path);
106    }
107}