1 56 package org.objectstyle.cayenne.conf; 57 58 import javax.servlet.ServletContext ; 59 import javax.servlet.http.HttpSession ; 60 61 import org.apache.log4j.Logger; 62 import org.objectstyle.cayenne.access.DataContext; 63 import org.objectstyle.cayenne.util.ResourceLocator; 64 import org.objectstyle.cayenne.util.WebApplicationResourceLocator; 65 66 84 public class ServletUtil { 85 86 private static Logger logObj = Logger.getLogger(ServletUtil.class); 87 88 92 public static final String CONFIGURATION_PATH_KEY = "cayenne.configuration.path"; 93 94 97 public static final String DATA_CONTEXT_KEY = "cayenne.datacontext"; 98 99 102 public synchronized static Configuration initializeSharedConfiguration( 103 ServletContext context) { 104 105 107 Configuration oldConfig = Configuration.sharedConfiguration; 109 if (oldConfig instanceof DefaultConfiguration) { 110 111 ResourceLocator locator = ((DefaultConfiguration) oldConfig) 112 .getResourceLocator(); 113 114 if (locator instanceof WebApplicationResourceLocator) { 115 if (((WebApplicationResourceLocator) locator).getServletContext() == context) { 116 logObj 117 .info("Configuration is already initialized with this context, reusing."); 118 return oldConfig; 119 } 120 } 121 } 122 123 DefaultConfiguration conf = new DefaultConfiguration( 125 Configuration.DEFAULT_DOMAIN_FILE, 126 createLocator(context)); 127 Configuration.initializeSharedConfiguration(conf); 128 129 return conf; 130 } 131 132 135 protected static ResourceLocator createLocator(ServletContext context) { 136 WebApplicationResourceLocator locator = new WebApplicationResourceLocator(); 137 locator.setSkipAbsolutePath(true); 138 locator.setSkipClasspath(false); 139 locator.setSkipCurrentDirectory(true); 140 locator.setSkipHomeDirectory(true); 141 142 locator.setServletContext(context); 143 String configurationPath = context.getInitParameter(CONFIGURATION_PATH_KEY); 144 if (configurationPath != null && configurationPath.trim().length() > 0) { 145 locator.addFilesystemPath(configurationPath.trim()); 146 } 147 148 return locator; 149 } 150 151 155 public static DataContext getSessionContext(HttpSession session) { 156 synchronized (session) { 157 DataContext ctxt = (DataContext) session.getAttribute(DATA_CONTEXT_KEY); 158 159 if (ctxt == null) { 160 ctxt = DataContext.createDataContext(); 161 session.setAttribute(ServletUtil.DATA_CONTEXT_KEY, ctxt); 162 } 163 164 return ctxt; 165 } 166 } 167 } | Popular Tags |