001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend.util;
017:
018: import org.apache.cocoon.components.flow.apples.AppleController;
019: import org.apache.cocoon.components.flow.apples.AppleRequest;
020: import org.apache.cocoon.components.flow.apples.AppleResponse;
021: import org.apache.cocoon.components.flow.apples.StatelessAppleController;
022: import org.apache.cocoon.environment.Request;
023: import org.apache.avalon.framework.context.Contextualizable;
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.avalon.framework.context.ContextException;
026: import org.outerj.daisy.frontend.FrontEndContext;
027:
028: import java.util.concurrent.locks.Lock;
029: import java.util.concurrent.locks.ReentrantLock;
030: import java.util.concurrent.TimeUnit;
031:
032: /**
033: * Base class for Apple controllers in the Daisy Wiki.
034: */
035: public abstract class AbstractDaisyApple implements AppleController,
036: Contextualizable {
037: private Lock lock = new ReentrantLock();
038: private String mountPoint;
039: private String layoutType;
040: private String continuationId;
041: private Context context;
042: /** Import: this is a request-scope variable! */
043: protected Request request;
044: /** Import: this is a request-scope variable! */
045: protected FrontEndContext frontEndContext;
046:
047: public final void contextualize(Context context)
048: throws ContextException {
049: this .context = context;
050: if (!(this instanceof StatelessAppleController))
051: continuationId = (String) context.get("continuation-id");
052: }
053:
054: public final void process(AppleRequest appleRequest,
055: AppleResponse appleResponse) throws Exception {
056: if (needsLock())
057: lock.tryLock(10000, TimeUnit.MILLISECONDS);
058: try {
059: request = appleRequest.getCocoonRequest();
060: frontEndContext = FrontEndContext.get(request);
061: mountPoint = frontEndContext.getMountPoint();
062: layoutType = frontEndContext.getLayoutType();
063:
064: processRequest(appleRequest, appleResponse);
065: } finally {
066: this .request = null;
067: this .frontEndContext = null;
068: if (needsLock())
069: lock.unlock();
070: }
071: }
072:
073: protected void processRequest(AppleRequest appleRequest,
074: AppleResponse appleResponse) throws Exception {
075: // override this to do something useful
076:
077: // default: call processInternal for backwards compatibility
078: processInternal(appleRequest, appleResponse);
079: }
080:
081: /**
082: * @deprecated use {@link #processRequest} instead.
083: */
084: protected void processInternal(AppleRequest appleRequest,
085: AppleResponse appleResponse) throws Exception {
086: }
087:
088: protected final String getMountPoint() {
089: return mountPoint;
090: }
091:
092: protected final String getLayoutType() {
093: return layoutType;
094: }
095:
096: protected final String getLayoutType(String defaultLayoutType) {
097: if (layoutType == null)
098: return defaultLayoutType;
099: else
100: return layoutType;
101: }
102:
103: protected final String getContinuationId() {
104: return continuationId;
105: }
106:
107: protected boolean needsLock() {
108: return !(this instanceof StatelessAppleController);
109: }
110:
111: protected final Context getContext() {
112: return context;
113: }
114: }
|