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 82 public class BasicServletConfiguration extends DefaultConfiguration { 83 private static Logger logObj = Logger.getLogger(BasicServletConfiguration.class); 84 85 public static final String CONFIGURATION_PATH_KEY = 86 "cayenne.configuration.path"; 87 public static final String DATA_CONTEXT_KEY = "cayenne.datacontext"; 88 89 protected ServletContext servletContext; 90 91 public synchronized static BasicServletConfiguration initializeConfiguration(ServletContext ctxt) { 92 95 97 Configuration oldConfiguration = Configuration.sharedConfiguration; 99 if (oldConfiguration instanceof BasicServletConfiguration) { 100 BasicServletConfiguration basicConfiguration = 101 (BasicServletConfiguration) oldConfiguration; 102 if (basicConfiguration.getServletContext() == ctxt) { 103 logObj.info( 104 "BasicServletConfiguration is already initialized, reusing."); 105 return basicConfiguration; 106 } 107 } 108 109 BasicServletConfiguration conf = new BasicServletConfiguration(ctxt); 110 Configuration.initializeSharedConfiguration(conf); 111 112 return conf; 113 } 114 115 119 public static DataContext getDefaultContext(HttpSession session) { 120 synchronized (session) { 121 DataContext ctxt = 122 (DataContext) session.getAttribute(DATA_CONTEXT_KEY); 123 124 if (ctxt == null) { 125 ctxt = DataContext.createDataContext(); 126 session.setAttribute( 127 BasicServletConfiguration.DATA_CONTEXT_KEY, 128 ctxt); 129 } 130 131 return ctxt; 132 } 133 } 134 135 public BasicServletConfiguration() { 136 super(); 137 138 } 139 140 public BasicServletConfiguration(ServletContext ctxt) { 141 super(); 142 this.setServletContext(ctxt); 143 144 ResourceLocator l = new WebApplicationResourceLocator(servletContext); 145 l.setSkipAbsolutePath(true); 146 l.setSkipClasspath(false); 147 l.setSkipCurrentDirectory(true); 148 l.setSkipHomeDirectory(true); 149 150 String configurationPath = 152 ctxt.getInitParameter(CONFIGURATION_PATH_KEY); 153 if (configurationPath != null 154 && configurationPath.trim().length() > 0) { 155 l.addFilesystemPath(configurationPath); 156 } 157 158 this.setResourceLocator(l); 159 } 160 161 165 public void setServletContext(ServletContext servletContext) { 166 this.servletContext = servletContext; 167 } 168 169 170 public ServletContext getServletContext() { 171 return servletContext; 172 } 173 174 public boolean canInitialize() { 175 return (getServletContext() != null); 176 } 177 } 178 | Popular Tags |