001 /*
002 * Copyright 2005 [ini4j] Development Team
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.ini4j;
018
019 import java.net.URL;
020 import java.net.MalformedURLException;
021
022 import javax.servlet.ServletContext;
023 import javax.servlet.ServletContextEvent;
024 import javax.servlet.ServletContextListener;
025
026 public class IniPreferencesFactoryListener extends IniPreferencesFactory implements ServletContextListener
027 {
028 public static final String DEFAULT_USER_LOCATION = "/WEB-INF/user.ini";
029 public static final String DEFAULT_SYSTEM_LOCATION = "/WEB-INF/system.ini";
030 private ServletContext _context;
031
032 public void contextInitialized(ServletContextEvent event)
033 {
034 _context = event.getServletContext();
035 System.setProperty("java.util.prefs.PreferencesFactory", getClass().getName());
036 }
037
038 public void contextDestroyed(ServletContextEvent event)
039 {
040 _context = null;
041 }
042
043 protected String getIniLocation(String key)
044 {
045 String location = _context.getInitParameter(key);
046
047 if ( location == null )
048 {
049 location = key.equals(KEY_USER) ? DEFAULT_USER_LOCATION : DEFAULT_SYSTEM_LOCATION;
050 }
051
052 return location;
053 }
054
055 protected URL getResource(String location) throws IllegalArgumentException
056 {
057 try
058 {
059 URL url = _context.getResource(location);
060
061 if ( url == null )
062 {
063 url = super.getResource(location);
064 }
065
066 return url;
067 }
068 catch (MalformedURLException x)
069 {
070 throw (IllegalArgumentException)new IllegalArgumentException().initCause(x);
071 }
072 }
073 }