001: /*
002: * Copyright (c) 2007 the original author or authors.
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:
018: package org.jpublish.module.cayenne;
019:
020: import com.anthonyeden.lib.config.Configuration;
021: import org.apache.cayenne.access.DataContext;
022: import org.apache.cayenne.conf.ServletUtil;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.jpublish.JPublishContext;
026:
027: import javax.servlet.http.HttpSession;
028:
029: /**
030: * Action executed before rendering a page, so the Cayenne support can be added
031: *
032: * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
033: * @since $Revision$ (created: Sep 23, 2007 2:43:42 PM)
034: */
035: public class CayenneSupportBeforeAction extends CayenneSupportAction {
036: private static final Log log = LogFactory
037: .getLog(CayenneSupportBeforeAction.class);
038: private static JPCayenneService jpCayenneService = new JPCayenneService();
039:
040: public CayenneSupportBeforeAction(JPCayenneModule jpCayenneModule) {
041: super (jpCayenneModule);
042: }
043:
044: /**
045: * Execute the action using the given context.
046: *
047: * @param context The current context
048: * @param configuration The configuration
049: * @throws Exception Any error
050: */
051: public void execute(JPublishContext context,
052: Configuration configuration) throws Exception {
053: // Bind DataContext to the request thread
054: DataContext dc = getDataContext(context.getSession());
055: DataContext.bindThreadDataContext(dc);
056:
057: context.put(JPCayenneModule.JPCAYENNE_SERVICE_NAME,
058: jpCayenneService);
059: context.getRequest().setAttribute(
060: JPCayenneModule.JPCAYENNE_SERVICE_NAME,
061: jpCayenneService);
062: context.getRequest().setAttribute(
063: JPCayenneModule.JPCAYENNE_DATA_CONTEXT, dc);
064: context.put(JPCayenneModule.JPCAYENNE__UTILS, jpCayenneModule
065: .getJPCayenneUtils());
066: context.getRequest().setAttribute(
067: JPCayenneModule.JPCAYENNE__UTILS,
068: jpCayenneModule.getJPCayenneUtils());
069:
070: if (jpCayenneModule.isDebugEnabled()) {
071: context.put(JPCayenneModule.JPCAYENNE_DATA_CONTEXT, dc);
072: }
073: }
074:
075: /**
076: * Return a DataContext instance. If the DataContextFilter is configured
077: * to associate the DataContext with the session (which is the default
078: * behaviour), the DataContext will be bound to the users session. If
079: * the DataContext is already available, the existing DataContext will be
080: * used otherwise a new DataContex object will be created.
081: * <p/>
082: * If this filter is configured with <tt>create-each-request</tt> to be true
083: * then a new DataContext will be created for each request and the DataContext
084: * will not be bound to the session.
085: * <p/>
086: * If this filter is configured with <tt>use-shared-cache</tt> to be true
087: * (which is the default behaviour) this method will create DataContext objects
088: * using the Cayenne shared cache, otherwise they will not use the shared cache.
089: *
090: * @param session the users session
091: * @return the session DataContext object
092: */
093: protected synchronized DataContext getDataContext(
094: HttpSession session) {
095:
096: DataContext dataContext = null;
097:
098: if (jpCayenneModule.isSessionScope()) {
099: dataContext = (DataContext) session
100: .getAttribute(ServletUtil.DATA_CONTEXT_KEY);
101: }
102:
103: if (dataContext == null) {
104: dataContext = DataContext.createDataContext(jpCayenneModule
105: .isSharedCache());
106:
107: if (jpCayenneModule.isDebugEnabled()) {
108: String msg = "[DEBUG] created DataContex with shared-cache="
109: + jpCayenneModule.isSharedCache();
110: log.info(msg);
111: }
112:
113: if (jpCayenneModule.isSessionScope()) {
114: session.setAttribute(ServletUtil.DATA_CONTEXT_KEY,
115: dataContext);
116: }
117: }
118:
119: return dataContext;
120: }
121: }
|