01: /*
02: * Copyright (c) 2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
18: package org.jpublish.module.cayenne;
19:
20: import com.anthonyeden.lib.config.Configuration;
21: import org.apache.cayenne.access.DataContext;
22: import org.apache.cayenne.conf.ServletUtil;
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25: import org.jpublish.JPublishContext;
26:
27: /**
28: * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
29: * @since $Revision$ (created: Sep 23, 2007 2:53:29 PM)
30: */
31: public class CayenneReadOnlySupportAction extends CayenneSupportAction {
32: private static final Log log = LogFactory
33: .getLog(CayenneReadOnlySupportAction.class);
34: private static JPCayenneService jpCayenneService = new JPCayenneService();
35:
36: public CayenneReadOnlySupportAction(JPCayenneModule jpCayenneModule) {
37: super (jpCayenneModule);
38: }
39:
40: /**
41: * Execute the action using the given context.
42: *
43: * @param context The current context
44: * @param configuration The configuration
45: * @throws Exception Any error
46: */
47: public void execute(JPublishContext context,
48: Configuration configuration) throws Exception {
49: DataContext dataContext = (DataContext) context
50: .getSiteContext().getAttribute(
51: ServletUtil.DATA_CONTEXT_KEY);
52:
53: if (dataContext == null) {
54: dataContext = DataContext.createDataContext(jpCayenneModule
55: .isSharedCache());
56: context.getSiteContext().setAttribute(
57: ServletUtil.DATA_CONTEXT_KEY, dataContext);
58: if (jpCayenneModule.isDebugEnabled()) {
59: String msg = "[DEBUG] created GLOBAL DataContex with shared-cache="
60: + jpCayenneModule.isSharedCache();
61: log.info(msg);
62: }
63: }
64:
65: DataContext.bindThreadDataContext(dataContext);
66:
67: context.put(JPCayenneModule.JPCAYENNE_SERVICE_NAME,
68: jpCayenneService);
69: context.getRequest().setAttribute(
70: JPCayenneModule.JPCAYENNE_SERVICE_NAME,
71: jpCayenneService);
72: context.getRequest().setAttribute(
73: JPCayenneModule.JPCAYENNE_DATA_CONTEXT, dataContext);
74: if (jpCayenneModule.isDebugEnabled()) {
75: context.put(JPCayenneModule.JPCAYENNE_DATA_CONTEXT,
76: dataContext);
77: }
78: }
79: }
|