001: /*
002: * Copyright 2006-2007 Luca Garulli (luca.garulli@assetdata.it)
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: package org.romaframework.aspect.persistence;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.romaframework.aspect.persistence.feature.PersistenceFeatures;
021: import org.romaframework.aspect.session.SessionInfo;
022: import org.romaframework.core.flow.ContextLifecycleListener;
023: import org.romaframework.core.flow.Controller;
024: import org.romaframework.core.flow.ObjectContext;
025: import org.romaframework.core.flow.UserObjectEventListener;
026: import org.romaframework.core.schema.SchemaElement;
027: import org.romaframework.core.schema.SchemaField;
028:
029: /**
030: * Inject TxPersistenceAspect object inside Thread Local Context to be reachable by actions.
031: *
032: * @author Luca Garulli (luca.garulli@assetdata.it)
033: */
034: public class PersistenceContextInjector implements
035: UserObjectEventListener, ContextLifecycleListener {
036: private static Log log = LogFactory
037: .getLog(PersistenceContextInjector.class);
038:
039: public PersistenceContextInjector() {
040: Controller.getInstance().registerListener(
041: UserObjectEventListener.class, this );
042: Controller.getInstance().registerListener(
043: ContextLifecycleListener.class, this );
044: }
045:
046: public boolean onBeforeActionExecution(Object content,
047: SchemaElement action) {
048: PersistenceAspect pa = ObjectContext.getInstance()
049: .getContextComponent(PersistenceAspect.class);
050: if (pa == null) {
051: String mode = (String) action.getFeature(
052: PersistenceAspect.ASPECT_NAME,
053: PersistenceFeatures.MODE);
054: if (mode != null) {
055: setPersistenceAspectInContext(action, mode);
056: }
057: }
058:
059: return true;
060: }
061:
062: public void onAfterActionExecution(Object content,
063: SchemaElement action) {
064: removePersistenceAspectFromContext(action);
065: }
066:
067: public void onFieldRefresh(SessionInfo session, Object content,
068: SchemaField field) {
069: }
070:
071: public Object onBeforeFieldRead(Object content, SchemaField field,
072: Object currentValue) {
073: return currentValue;
074: }
075:
076: public Object onAfterFieldRead(Object content, SchemaField field,
077: Object currentValue) {
078: return currentValue;
079: }
080:
081: public Object onBeforeFieldWrite(Object content, SchemaField field,
082: Object currentValue) {
083: return currentValue;
084: }
085:
086: public Object onAfterFieldWrite(Object content, SchemaField field,
087: Object currentValue) {
088: return currentValue;
089: }
090:
091: public Object onContextRead(String iComponentName,
092: Object iComponentInstance) {
093: if (iComponentInstance == null
094: && iComponentName.toString()
095: .equals("PersistenceAspect")) {
096: return setPersistenceAspectInContext(null,
097: PersistenceConstants.MODE_TX);
098: }
099: return null;
100: }
101:
102: protected PersistenceAspect setPersistenceAspectInContext(
103: SchemaElement iSourceElement, String mode) {
104: if (mode == null)
105: return null;
106:
107: PersistenceAspect pa = null;
108: String componentName = null;
109:
110: if (mode.equals(PersistenceConstants.MODE_TX))
111: componentName = "TxPersistenceAspect";
112: else if (mode.equals(PersistenceConstants.MODE_ATOMIC))
113: componentName = "PersistenceAspect";
114: else if (mode.equals(PersistenceConstants.MODE_NOTX))
115: componentName = "NoTxPersistenceAspect";
116:
117: if (componentName != null) {
118: // BIND A NEW TX PERSISTENCE ASPECT
119: pa = ObjectContext.getInstance()
120: .getComponent(componentName);
121:
122: ObjectContext.getInstance().setContextComponent(
123: PersistenceAspect.class, pa);
124:
125: if (log.isDebugEnabled())
126: log
127: .debug("[PersistenceContextInjector.setPersistenceAspectInContext] mode: "
128: + mode
129: + " sourceElement: "
130: + iSourceElement);
131: }
132: return pa;
133: }
134:
135: private void removePersistenceAspectFromContext(
136: SchemaElement iSourceElement) {
137: PersistenceAspect pa = ObjectContext.getInstance()
138: .getContextComponent(PersistenceAspect.class);
139: if (pa != null) {
140: if (log.isDebugEnabled())
141: log
142: .debug("[PersistenceContextInjector.removePersistenceAspectFromContext] sourceElement: "
143: + iSourceElement);
144:
145: ObjectContext.getInstance().setContextComponent(
146: PersistenceAspect.class, null);
147: try {
148: // REMOVE PREVIOUS PERSISTENCE ASPECT BOUND
149: if (pa.isActive())
150: pa.commit();
151: } finally {
152: pa.close();
153: }
154: }
155: }
156: }
|