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.flex;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletException;
028    
029    import org.granite.config.GraniteConfig;
030    import org.granite.config.ServletGraniteConfig;
031    import org.granite.config.api.Configuration;
032    import org.granite.util.ServletParams;
033     
034    /**
035     * @author Franck WOLFF
036     */
037    public class ServletServicesConfig {
038    
039        ///////////////////////////////////////////////////////////////////////////
040        // Fields.
041    
042        private static final String SERVICES_CONFIG_KEY = ServletServicesConfig.class.getName() + "_CACHE";
043    
044    
045        ///////////////////////////////////////////////////////////////////////////
046        // Instance fields.
047    
048        private ServicesConfig config = null;
049    
050        
051        private ServletServicesConfig(ServletContext context, ServicesConfig config) {
052            this.config = config;
053        }
054    
055        public static synchronized ServletServicesConfig getServletConfig(ServletContext context) {
056            return (ServletServicesConfig)context.getAttribute(SERVICES_CONFIG_KEY);
057        }
058        
059        ///////////////////////////////////////////////////////////////////////////
060        // Static ServicesConfig loaders.
061        
062        public static synchronized ServicesConfig loadConfig(ServletContext context) throws ServletException {
063            ServletServicesConfig servletServicesConfig = (ServletServicesConfig)context.getAttribute(SERVICES_CONFIG_KEY);
064    
065            if (servletServicesConfig == null) {
066                    String path = null;
067                    
068                    Configuration configuration = (Configuration)context.getAttribute(ServletGraniteConfig.GRANITE_CONFIG_CONFIGURATION_KEY);
069                    if (configuration != null)
070                            path = configuration.getFlexServicesConfig();
071                    
072                    if (path == null)
073                            path = ServletParams.get(context, "servicesConfigPath", String.class, null);
074                    
075                if (path == null)
076                    path = "/WEB-INF/flex/services-config.xml";
077    
078                InputStream is = context.getResourceAsStream(path);
079    
080                try {
081                    GraniteConfig graniteConfig = ServletGraniteConfig.loadConfig(context);
082                    ServicesConfig servicesConfig = new ServicesConfig(is, configuration, graniteConfig.getScan());
083                    
084                    servletServicesConfig = loadConfig(context, servicesConfig);
085                }
086                catch (Exception e) {
087                    throw new ServletException("Could not load custom services-config.xml", e);
088                }
089                finally {
090                    try {
091                            if (is != null)
092                                    is.close();
093                    } catch (IOException e) {
094                            // Ignore...
095                    }
096                }
097                
098                context.setAttribute(SERVICES_CONFIG_KEY, servletServicesConfig);
099            }
100    
101            return servletServicesConfig.config;
102        }
103    
104        public static synchronized ServletServicesConfig loadConfig(ServletContext context, ServicesConfig servicesConfig) {
105            ServletServicesConfig servletServicesConfig = new ServletServicesConfig(context, servicesConfig);
106            
107            context.setAttribute(SERVICES_CONFIG_KEY, servletServicesConfig);
108            
109            return servletServicesConfig;
110        }
111    }