You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by da...@apache.org on 2007/02/05 00:17:54 UTC

svn commit: r503511 - in /cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack: ./ CallScope.java CallStack.java

Author: danielf
Date: Sun Feb  4 15:17:53 2007
New Revision: 503511

URL: http://svn.apache.org/viewvc?view=rev&rev=503511
Log:
Ongoing work: I will introduce a custom Spring scope for servlet calls (and other calls as well, it will probably work for sitemap calls as well).
This far the code is incomplete and is not used anywhere.

Added:
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallScope.java   (with props)
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallStack.java   (with props)

Added: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallScope.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallScope.java?view=auto&rev=503511
==============================================================================
--- cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallScope.java (added)
+++ cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallScope.java Sun Feb  4 15:17:53 2007
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.callstack;
+
+import java.util.Map;
+
+import org.springframework.beans.factory.ObjectFactory;
+import org.springframework.beans.factory.config.Scope;
+
+
+/**
+ * Stack based scope implementation. It is based on the CallStack and
+ * an object is in scope when it is in the top frame of the stack.
+ *
+ * @version $Id$
+ * @since 2.2 
+ */
+public class CallScope implements Scope {
+
+    /* (non-Javadoc)
+     * @see org.springframework.beans.factory.config.Scope#get(java.lang.String, org.springframework.beans.factory.ObjectFactory)
+     */
+    public Object get(String name, ObjectFactory objectFactory) {
+        Map attributes = CallStack.getCurrentFrame();
+        Object scopedObject = attributes.get(name);
+        if (scopedObject == null) {
+            scopedObject = objectFactory.getObject();
+            attributes.put(name, scopedObject);
+        }
+        return scopedObject;
+    }
+
+    /* (non-Javadoc)
+     * @see org.springframework.beans.factory.config.Scope#remove(java.lang.String)
+     */
+    public Object remove(String name) {
+        Map attributes = CallStack.getCurrentFrame();
+        Object scopedObject = attributes.get(name);
+        if (scopedObject != null)
+            attributes.remove(name);
+        return scopedObject;
+    }
+
+    /* (non-Javadoc)
+     * @see org.springframework.beans.factory.config.Scope#getConversationId()
+     */
+    public String getConversationId() {
+        // There is no conversation id concept for the call stack
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.springframework.beans.factory.config.Scope#registerDestructionCallback(java.lang.String, java.lang.Runnable)
+     */
+    public void registerDestructionCallback(String name, Runnable callback) {
+        // TODO Auto-generated method stub
+        
+    }
+
+}

Propchange: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallScope.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallScope.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallStack.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallStack.java?view=auto&rev=503511
==============================================================================
--- cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallStack.java (added)
+++ cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallStack.java Sun Feb  4 15:17:53 2007
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.callstack;
+
+import java.util.Map;
+import java.util.Stack;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+/**
+ * Stack used for storing objects in the current call frame.
+ *
+ * @version $Id$
+ * @since 2.2 
+ */
+public class CallStack {
+
+    /** The call stack */
+    private static final ThreadLocal callStack = new ThreadLocal();
+    
+    /** Content map and objects that should be desctructed when the call frame is left */
+    private static class CallStackInfo {
+        public CallStackInfo(Map attributes, Map destructionCallbacks) {
+            this.attributes = attributes;
+            this.destructionCallbacks = destructionCallbacks;
+        }
+        public Map attributes;
+        public Map destructionCallbacks;
+    };
+
+    /**
+     * This hook must be called each time a call frame is entered.
+     *
+     * <p>This method should never raise an exception, except when the
+     * parameters are not set!</p>
+     *
+     * @throws ServletException if block is null
+     */
+    public static void enter(Map attributes)
+    throws ServletException {
+        if (null == attributes) {
+            throw new ServletException("Block is not set.");
+        }
+
+        Stack stack = (Stack)callStack.get();
+        if (stack == null) {
+            stack = new Stack();
+            callStack.set(stack);
+        }
+        CallStackInfo info = new CallStackInfo(attributes, null);
+        stack.push(info);
+    }
+
+    /**
+     * This hook must be called each time a block is left.
+     *
+     * <p>It's the counterpart to the {@link #enterBlock(Block)}
+     * method.</p>
+     */
+    public static void leave() {
+        final Stack stack = (Stack)callStack.get();
+        stack.pop();
+    }
+
+    /**
+     * Use this method for getting the context that should be used for
+     * resolving a block protocol call to a super block 
+     * @return a servlet context
+     */
+    public static Map getCurrentFrame() {
+        final Stack stack = (Stack)callStack.get();
+        if (stack != null && !stack.isEmpty()) {
+            return ((CallStackInfo)stack.peek()).attributes;
+        }
+        return null;
+    }
+}

Propchange: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallStack.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/callstack/CallStack.java
------------------------------------------------------------------------------
    svn:keywords = Id