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