PageRenderTime 94ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/cayenne/java/src/org/jpublish/module/cayenne/JPCayenneModule.java

http://jpublish.googlecode.com/
Java | 268 lines | 150 code | 46 blank | 72 comment | 17 complexity | 97b6a7e9a3f1d587975237e9837ba3ff MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * Copyright (c) 2007 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.jpublish.module.cayenne;
  18. import com.anthonyeden.lib.config.Configuration;
  19. import org.apache.cayenne.CayenneException;
  20. import org.apache.cayenne.access.DataContext;
  21. import org.apache.cayenne.conf.DefaultConfiguration;
  22. import org.apache.cayenne.conf.ServletUtil;
  23. import org.apache.cayenne.util.ResourceLocator;
  24. import org.apache.cayenne.util.WebApplicationResourceLocator;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.jpublish.JPublishModule;
  28. import org.jpublish.SiteContext;
  29. import org.jpublish.action.ActionWrapper;
  30. import org.jpublish.action.PathAction;
  31. import javax.servlet.ServletContext;
  32. import javax.sql.DataSource;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. /**
  37. * The Cayenne module
  38. *
  39. * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
  40. * @since $Revision$ (created: Sep 22, 2007 10:50:57 AM)
  41. */
  42. public class JPCayenneModule implements JPublishModule {
  43. public static final String JPCAYENNE_SERVICE_NAME = "jpCayenneService";
  44. public static final String JPCAYENNE_DATA_CONTEXT = "jpCayenne_DC";
  45. public static final String JPCAYENNE_OBJECT_CONTEXT = "jpCayenne_OC";
  46. public static final String JPCAYENNE_DATA_OBJECT_UTILS = "DataObjectUtils"; // I am not sure we need this one
  47. public static final String JPCAYENNE__UTILS = "jpCayenneUtils";
  48. private static final Log log = LogFactory.getLog(JPCayenneModule.class);
  49. private static final String NAME = "JPCayenne Module";
  50. private static final String VERSION = "v0.1b";
  51. private static final String DESCRIPTION = "Cayenne ORM support for JPublish";
  52. private static final String CAYENNE_VERSION = "Cayenne " + CayenneException.getExceptionLabel();
  53. private SiteContext site;
  54. private Map actions = new HashMap(5);
  55. private Map jpCayenneUtils = new HashMap(5);
  56. /**
  57. * Automatically rollback any changes to the DataContext at the end of
  58. * each request, the default value is true.
  59. * <p/>
  60. * This option is only useful for sessionScope DataObjects.
  61. */
  62. private boolean autoRollback = true;
  63. /**
  64. * Maintain user HttpSession scope DataContext object, the default value is
  65. * true. If sessionScope is false then a new DataContext object will be
  66. * created for each request.
  67. */
  68. private boolean sessionScope = true;
  69. /**
  70. * Create DataContext objects using the shared cache.
  71. */
  72. private boolean sharedCache = true;
  73. private boolean debugEnabled = false;
  74. //protected org.apache.cayenne.conf.Configuration configuration;
  75. DefaultConfiguration conf;
  76. protected DataSource dataSource;
  77. /**
  78. * Initialize the module. This corresponds with the
  79. * Servlet.init() method, except that it includes a reference
  80. * to the JPublish SiteContext.
  81. *
  82. * @param site The SiteContext
  83. * @param config The configuration object
  84. * @throws Exception
  85. */
  86. public void init(SiteContext site, Configuration config) throws Exception {
  87. log.info(NAME + " starting for: " + site.getServletContext().getServletContextName());
  88. this.site = site;
  89. String cayenneConfigPath = config.getChildValue("cayenne-config-path", "WEB-INF");
  90. log.info("Initializing the " + CAYENNE_VERSION + "framework from: " + cayenneConfigPath);
  91. // create new shared configuration
  92. conf = new DefaultConfiguration(
  93. org.apache.cayenne.conf.Configuration.DEFAULT_DOMAIN_FILE,
  94. createLocator(site.getServletContext(), cayenneConfigPath));
  95. org.apache.cayenne.conf.Configuration.initializeSharedConfiguration(conf);
  96. String value = config.getChildValue("auto-rollback", "true");
  97. autoRollback = "true".equalsIgnoreCase(value);
  98. value = config.getChildValue("session-scope", "true");
  99. sessionScope = "true".equalsIgnoreCase(value);
  100. value = config.getChildValue("shared-cache", "true");
  101. sharedCache = "true".equalsIgnoreCase(value);
  102. value = config.getChildValue("debug", "false");
  103. debugEnabled = "true".equalsIgnoreCase(value);
  104. String msg = "Cayenne DataContext initialized with: auto-rollback="
  105. + autoRollback + ", session-scope=" + sessionScope
  106. + ", shared-cache=" + sharedCache;
  107. log.info(msg);
  108. //jpCayenneUtils.put(JPCAYENNE_DATA_OBJECT_UTILS,
  109. // ClassUtilities.loadClass("org.apache.cayenne.DataObjectUtils"));
  110. log.info("Mapping Cayenne support...");
  111. List urls = config.getChild("cayenne-enabled-urls").getChildren();
  112. if (urls != null && !urls.isEmpty()) {
  113. for (int i = 0; i < urls.size(); i++) {
  114. Configuration url = (Configuration) urls.get(i);
  115. String path = url.getAttribute("path");
  116. if (path != null) {
  117. ActionWrapper aw;
  118. boolean ro = url.getAttribute("readonly", "false").equalsIgnoreCase("true");
  119. if (ro) {
  120. aw = new ActionWrapper(
  121. new PathAction(path, new CayenneReadOnlySupportAction(this)), config);
  122. } else {
  123. aw = new ActionWrapper(
  124. new PathAction(path, new CayenneSupportBeforeAction(this)), config);
  125. // add a post-evaluate action for all the R/W paths
  126. site.getActionManager().getPostEvaluationActions()
  127. .add(new ActionWrapper(
  128. new PathAction(path, new CayenneSupportAfterAction(this)), config)
  129. );
  130. }
  131. site.getActionManager().getPathActions().add(aw);
  132. log.info("... added " + (ro ? "read-only" : "read-write") + " support for: " + path);
  133. }
  134. }
  135. } else {
  136. log.info(" ... the 'cayenne-enabled-urls' node contains no mappings. " +
  137. "Cayenne support will be unavailable to the web requests.");
  138. }
  139. //todo implement a ShutDown action
  140. //populate the Application with a shared DataContext
  141. DataContext dataContext = (DataContext) site.getAttribute(ServletUtil.DATA_CONTEXT_KEY);
  142. if (dataContext == null) {
  143. dataContext = DataContext.createDataContext(isSharedCache());
  144. site.setAttribute(ServletUtil.DATA_CONTEXT_KEY, dataContext);
  145. if (isDebugEnabled()) {
  146. log.info("Created an Application wide DataContext with shared-cache="
  147. + isSharedCache());
  148. }
  149. }
  150. DataContext.bindThreadDataContext(dataContext);
  151. site.setAttribute(JPCayenneModule.JPCAYENNE_SERVICE_NAME, new JPCayenneService());
  152. log.info(this.toString() + " started.");
  153. }
  154. /**
  155. * Get a map of defined actions.
  156. *
  157. * @return The defined actions
  158. */
  159. public Map getDefinedActions() {
  160. return actions;
  161. }
  162. public static String getModuleDescription() {
  163. return DESCRIPTION;
  164. }
  165. public SiteContext getSite() {
  166. return site;
  167. }
  168. /**
  169. * Invoked when the module is destroyed. This corresponds to the
  170. * Servlet.destroy() method.
  171. */
  172. public void destroy() {
  173. log.info("Shutting down the Cayenne configuration ....");
  174. if (conf != null) {
  175. conf.shutdown();
  176. }
  177. }
  178. /**
  179. *
  180. * @return the Cayenne configuration used by this module
  181. */
  182. public DefaultConfiguration getCayenneConfiguration() {
  183. return conf;
  184. }
  185. /**
  186. * A helper method to create default ResourceLocator.
  187. *
  188. * @param context the Servlet context
  189. * @param cayenneConfigPath the path to the cayenne configuration file (e.g: WEB-INF)
  190. * @return a resourceLocator object
  191. */
  192. protected static ResourceLocator createLocator(ServletContext context, String cayenneConfigPath) {
  193. WebApplicationResourceLocator locator = new WebApplicationResourceLocator();
  194. locator.setSkipAbsolutePath(true);
  195. locator.setSkipClasspath(false);
  196. locator.setSkipCurrentDirectory(true);
  197. locator.setSkipHomeDirectory(true);
  198. locator.setServletContext(context);
  199. if (cayenneConfigPath != null && cayenneConfigPath.trim().length() > 0) {
  200. locator.addFilesystemPath(cayenneConfigPath.trim());
  201. }
  202. return locator;
  203. }
  204. public boolean isAutoRollback() {
  205. return autoRollback;
  206. }
  207. public boolean isSessionScope() {
  208. return sessionScope;
  209. }
  210. public boolean isSharedCache() {
  211. return sharedCache;
  212. }
  213. public boolean isDebugEnabled() {
  214. return debugEnabled;
  215. }
  216. public Map getJPCayenneUtils() {
  217. return jpCayenneUtils;
  218. }
  219. public String toString() {
  220. return NAME + " " + VERSION;
  221. }
  222. }