1 19 20 package org.apache.cayenne.conf; 21 22 import javax.servlet.ServletContext ; 23 import javax.servlet.http.HttpSession ; 24 25 import org.apache.cayenne.access.DataContext; 26 import org.apache.cayenne.util.ResourceLocator; 27 import org.apache.cayenne.util.WebApplicationResourceLocator; 28 29 47 public class ServletUtil { 48 49 53 public static final String CONFIGURATION_PATH_KEY = "cayenne.configuration.path"; 54 55 58 public static final String DATA_CONTEXT_KEY = "cayenne.datacontext"; 59 60 63 public synchronized static Configuration initializeSharedConfiguration( 64 ServletContext context) { 65 66 68 Configuration oldConfig = Configuration.sharedConfiguration; 70 if (oldConfig instanceof DefaultConfiguration) { 71 72 ResourceLocator locator = ((DefaultConfiguration) oldConfig) 73 .getResourceLocator(); 74 75 if (locator instanceof WebApplicationResourceLocator) { 76 if (((WebApplicationResourceLocator) locator).getServletContext() == context) { 77 return oldConfig; 78 } 79 } 80 } 81 82 DefaultConfiguration conf = new DefaultConfiguration( 84 Configuration.DEFAULT_DOMAIN_FILE, 85 createLocator(context)); 86 Configuration.initializeSharedConfiguration(conf); 87 88 return conf; 89 } 90 91 94 protected static ResourceLocator createLocator(ServletContext context) { 95 WebApplicationResourceLocator locator = new WebApplicationResourceLocator(); 96 locator.setSkipAbsolutePath(true); 97 locator.setSkipClasspath(false); 98 locator.setSkipCurrentDirectory(true); 99 locator.setSkipHomeDirectory(true); 100 101 locator.setServletContext(context); 102 String configurationPath = context.getInitParameter(CONFIGURATION_PATH_KEY); 103 if (configurationPath != null && configurationPath.trim().length() > 0) { 104 locator.addFilesystemPath(configurationPath.trim()); 105 } 106 107 return locator; 108 } 109 110 114 public static DataContext getSessionContext(HttpSession session) { 115 synchronized (session) { 116 DataContext ctxt = (DataContext) session.getAttribute(DATA_CONTEXT_KEY); 117 118 if (ctxt == null) { 119 ctxt = DataContext.createDataContext(); 120 session.setAttribute(ServletUtil.DATA_CONTEXT_KEY, ctxt); 121 } 122 123 return ctxt; 124 } 125 } 126 } 127 | Popular Tags |