001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow;
020:
021: import org.apache.beehive.controls.runtime.servlet.ServletBeanContext;
022: import org.apache.beehive.controls.api.context.ControlContainerContext;
023: import org.apache.beehive.netui.pageflow.internal.AdapterManager;
024: import org.apache.beehive.netui.util.logging.Logger;
025:
026: import java.io.Serializable;
027: import java.util.concurrent.locks.ReentrantLock;
028: import javax.servlet.ServletContext;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: /**
033: * This class provide a set of method that deal with the ControlContainerContext that is scoped to
034: * the FlowController.
035: */
036: public class PageFlowControlContainerImpl implements
037: PageFlowControlContainer, Serializable {
038: private static final Logger _log = Logger
039: .getInstance(PageFlowControlContainerImpl.class);
040:
041: private ControlContainerContext _sharedContext;
042: private ReentrantLock _sharedLock;
043:
044: public ControlContainerContext getControlContainerContext(
045: PageFlowManagedObject pfmo) {
046: // Based upon the type of controller find and return the appropriate ControlContainerContext (CCC)
047: // For PageFlowController and FacesBackingBean the CCC is obtained from the object directly
048: // For SharedFlowController there is a shared object maintained here
049: if (pfmo instanceof SharedFlowController)
050: return _sharedContext;
051: if (pfmo instanceof PageFlowController)
052: return ((PageFlowController) pfmo)._beanContext;
053: if (pfmo instanceof FacesBackingBean)
054: return ((FacesBackingBean) pfmo)._beanContext;
055:
056: // Found an unknown type of PageFlowManagedObject. Assert this, log it, and then return null
057: _log.error("Unknown FlowController ControlBeanContenxt:"
058: + pfmo.getClass().getName());
059: assert (false) : "Unknown FlowController ControlBeanContenxt:"
060: + pfmo.getClass().getName();
061: return null;
062: }
063:
064: public void beginContextOnPageFlow(PageFlowManagedObject pfmo,
065: HttpServletRequest request, HttpServletResponse response,
066: ServletContext servletContext) {
067: // get the lock on the shared flow if the shared flow context exists. This is the
068: // reason that you must always call endContextOnPageFlow if you call this method.
069: if (_sharedContext != null) {
070: assert (_sharedLock != null) : "Forgot to create the shared lock";
071: _sharedLock.lock();
072:
073: if (_sharedContext instanceof ServletBeanContext) {
074: ((ServletBeanContext) _sharedContext).beginContext(
075: servletContext, request, response);
076: } else {
077: _sharedContext.beginContext();
078: }
079: }
080:
081: // if the page flow isn't a shared flow then we must begin context on that also
082: if (pfmo instanceof PageFlowController
083: || pfmo instanceof FacesBackingBean) {
084:
085: ControlContainerContext cbc = getControlContainerContext(pfmo);
086: if (cbc != null) {
087: if (cbc instanceof ServletBeanContext) {
088: ((ServletBeanContext) cbc).beginContext(
089: servletContext, request, response);
090: } else {
091: cbc.beginContext();
092: }
093: }
094: }
095: }
096:
097: public void createAndBeginControlBeanContext(
098: PageFlowManagedObject pfmo, HttpServletRequest request,
099: HttpServletResponse response, ServletContext servletContext) {
100: if (pfmo instanceof SharedFlowController) {
101: if (_sharedContext == null) {
102: _sharedContext = (ControlContainerContext) AdapterManager
103: .getServletContainerAdapter(servletContext)
104: .createControlBeanContext(request, response);
105: _sharedLock = new ReentrantLock();
106: }
107: } else if (pfmo instanceof PageFlowController) {
108: if (((PageFlowController) pfmo)._beanContext == null) {
109: ((PageFlowController) pfmo)._beanContext = (ControlContainerContext) AdapterManager
110: .getServletContainerAdapter(servletContext)
111: .createControlBeanContext(request, response);
112: }
113: } else if (pfmo instanceof FacesBackingBean) {
114: if (((FacesBackingBean) pfmo)._beanContext == null) {
115: ((FacesBackingBean) pfmo)._beanContext = (ControlContainerContext) AdapterManager
116: .getServletContainerAdapter(servletContext)
117: .createControlBeanContext(request, response);
118: }
119: } else {
120: _log.error("Unknown FlowController ControlBeanContenxt:"
121: + pfmo.getClass().getName());
122: assert (false) : "Unknown FlowController ControlBeanContenxt:"
123: + pfmo.getClass().getName();
124: return;
125: }
126: beginContextOnPageFlow(pfmo, request, response, servletContext);
127: }
128:
129: public void endContextOnPageFlow(
130: PageFlowManagedObject flowController) {
131: // You must reverse the order of the begin because the low level stuff uses a stack for
132: // this. We also do this with a try/finally so that we make sure to free up the lock.
133: try {
134: if (flowController instanceof PageFlowController
135: || flowController instanceof FacesBackingBean) {
136:
137: ControlContainerContext cbc = getControlContainerContext(flowController);
138: if (cbc != null) {
139: cbc.endContext();
140: }
141: }
142: } finally {
143: if (_sharedContext != null) {
144: assert (_sharedLock != null) : "The sharedLock was not allocated";
145: try {
146: _sharedContext.endContext();
147: } finally {
148: _sharedLock.unlock();
149: }
150: }
151: }
152: }
153: }
|