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