myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From we...@apache.org
Subject svn commit: r307160 - in /myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope: AfterScopePhaseListener.java ScopeHolder.java ScopeTag.java ScopeUtils.java UIScope.java
Date Fri, 07 Oct 2005 16:01:23 GMT
Author: werpu
Date: Fri Oct  7 09:00:48 2005
New Revision: 307160

URL: http://svn.apache.org/viewcvs?rev=307160&view=rev
Log:
added so that the source is safed for now we will add
the example and the tlds later

Added:
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/AfterScopePhaseListener.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeHolder.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeTag.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeUtils.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/UIScope.java

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/AfterScopePhaseListener.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/AfterScopePhaseListener.java?rev=307160&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/AfterScopePhaseListener.java
(added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/AfterScopePhaseListener.java
Fri Oct  7 09:00:48 2005
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.myfaces.custom.scope;
+
+import javax.faces.event.PhaseEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.event.PhaseListener;
+
+/**
+ * phase listener class which does
+ * the needed scope cleanup operations
+ *
+ * @author Werner Punz werpu@gmx.at
+ * @version $Revision$ $Date$
+ */
+public class AfterScopePhaseListener implements PhaseListener {
+
+	/**
+     *
+     */
+    private static final long serialVersionUID = 9137086632177423625L;
+
+    /**
+	 * we have to tackle the scope cleanup
+	 * at the latest stage possible
+	 * thus we intercept it at the last phase
+	 * at the after phase stage
+	 */
+	public void afterPhase(PhaseEvent arg0) {
+		ScopeHolder holder = (ScopeHolder) ScopeUtils.getManagedBean(UIScope.SCOPE_CONTAINER_KEY);
+		holder.pageRefresh();
+	}
+
+	public void beforePhase(PhaseEvent arg0) {
+	}
+
+	/**
+	 * last phase possible, the render responce phase
+	 */
+	public PhaseId getPhaseId() {
+		return PhaseId.RENDER_RESPONSE;
+	}
+
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeHolder.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeHolder.java?rev=307160&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeHolder.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeHolder.java Fri Oct
 7 09:00:48 2005
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.myfaces.custom.scope;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.TreeMap;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+/**
+ * A central sessions holder, which stores
+ * the triggered scopes and also does
+ * the scope synching at the end
+ *
+ * @author Werner Punz werpu@gmx.at
+ *
+ */
+public class ScopeHolder
+{
+
+    /*dunno of a log(n) map is suitable here, after all we have only a handful of scopes*/
+    Map scopeMap = Collections.synchronizedMap(new TreeMap());
+    Map oldScopes = Collections.synchronizedMap(new TreeMap());
+
+    /**
+     * fetches an old scope from the scope map
+     *
+     * @param key
+     * @return
+     */
+    public Object restoreScopeEntry(String key)
+    {
+        Object theentry = oldScopes.get(key);
+        return theentry;
+    }
+
+    /**
+     * note we use the global session internally not the portlet one, if you use
+     * portlets please-keep that in mind with your scope names
+     *
+     * @param parent
+     *            the parent component
+     * @param context
+     *            the current faces context
+     * @param scopeName
+     *            the scope name
+     * @param scopeValuen
+     *            the scope value
+     */
+    public void saveScopeEntry(UIComponent parent, FacesContext context,
+            ValueBinding scopeBindingValue)
+    {
+
+        scopeMap.put(scopeBindingValue.getExpressionString(), scopeBindingValue
+                .getValue(context));
+    }
+
+    /**
+     * the rendering is done we need to dump the unused scopes
+     *
+     */
+    public void pageRefresh()
+    {
+        oldScopes = scopeMap;
+        scopeMap = Collections.synchronizedMap(new TreeMap());
+    }
+
+    public void resetScopes()
+    {
+        oldScopes = Collections.synchronizedMap(new TreeMap());
+        scopeMap = Collections.synchronizedMap(new TreeMap());
+    }
+
+    public void resetScope(String key)
+    {
+        oldScopes.remove(key);
+        scopeMap.remove(key);
+    }
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeTag.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeTag.java?rev=307160&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeTag.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeTag.java Fri Oct 
7 09:00:48 2005
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.myfaces.custom.scope;
+
+import javax.faces.component.UIComponent;
+
+import org.apache.myfaces.taglib.UIComponentTagBase;
+
+/**
+ * A class very similar to savestate it allows to defined scopes for variables
+ * between session and request, so that you can traverse parameters over forms
+ * within sessions but you do not have the full session lifecycle the main
+ * advantage over SaveState for this one is, that you do not have to make the
+ * objects serializable, the main disadvantage is, scopes need session ram for the
+ * time of existence
+ *
+ * @author Werner Punz werpu@gmx.at
+ *
+ * @version $Revision$ $Date$
+ */
+
+public class ScopeTag extends UIComponentTagBase
+{
+
+    /**
+     * @return the component type
+     */
+    public String getComponentType()
+    {
+        return UIScope.COMPONENT_TYPE;
+    }
+
+    /**
+     * standard setProperties
+     */
+    protected void setProperties(UIComponent component)
+    {
+        super.setProperties(component);
+    }
+
+    /**
+     * standard release
+     */
+    public void release()
+    {
+        super.release();
+    }
+
+    /**
+     * we do not render anything, hence the renderer type results in a plain
+     * null
+     */
+    public String getRendererType()
+    {
+        return null;
+    }
+
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeUtils.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeUtils.java?rev=307160&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeUtils.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/ScopeUtils.java Fri Oct
 7 09:00:48 2005
@@ -0,0 +1,68 @@
+/**
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed 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.myfaces.custom.scope;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * Small helper to cope with the
+ * managed beans within the scope tag
+ * handler
+ *
+ * Thanks Derek Shen to allow me to relicense
+ * the Faces Utils code under Apache2
+ * this class is derived from it
+ *
+ * http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html
+ *
+ * @author Werner Punz werpu@gmx.at
+ *
+ */
+public class ScopeUtils
+{
+    private static final String EL_END = "}";
+    private static final String EL_BEGIN = "#{";
+
+    /**
+     * el checker
+     *
+     * @param beanName
+     * @return true if the bean is a managed bean
+     */
+    public static final boolean isEl(String beanName)
+    {
+        beanName = beanName.trim();
+        return beanName.startsWith("EL_BEGIN") && !beanName.endsWith(EL_END);
+    }
+
+    /**
+     * returns the managed bean from the given bean name
+     * @param beanName
+     * @return
+     */
+    public static Object getManagedBean(String beanName)
+    {
+        if (isEl(beanName))
+            return FacesContext.getCurrentInstance().getApplication()
+                    .createValueBinding(beanName).getValue(
+                            FacesContext.getCurrentInstance());
+        else
+            return FacesContext.getCurrentInstance().getApplication()
+                    .createValueBinding(EL_BEGIN + beanName + EL_END).getValue(
+                            FacesContext.getCurrentInstance());
+    }
+
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/UIScope.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/UIScope.java?rev=307160&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/UIScope.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/scope/UIScope.java Fri Oct  7
09:00:48 2005
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.myfaces.custom.scope;
+
+import javax.faces.component.UIParameter;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+/**
+ * Scope control
+ * which does basically the same as
+ * savestate but bypasses the serialization
+ * and utilizes the session directly
+ *
+ * @author Werner Punz werpu@gmx.at
+ * @version $Revision$ $Date$
+ */
+
+public class UIScope extends UIParameter
+{
+
+    public static final String SCOPE_CONTAINER_KEY = "ScopeContainer";
+    public static final String COMPONENT_TYPE = "net.sf.myfacessandbox.Scope";
+    public static final String COMPONENT_FAMILY = "javax.faces.Parameter";
+
+    ScopeHolder holder = (ScopeHolder) ScopeUtils
+            .getManagedBean(UIScope.SCOPE_CONTAINER_KEY);
+
+    public String getFamily()
+    {
+        return COMPONENT_FAMILY;
+    }
+
+    /**
+     * save state saves the scope value binding into the holder map
+     */
+    public Object saveState(FacesContext context)
+    {
+        Object values[] = new Object[1];
+        values[0] = super.saveState(context);
+
+        ValueBinding vb = getValueBinding("value");
+        holder.saveScopeEntry(this, context, vb);
+        return ((Object) (values));
+    }
+
+    /**
+     * in the restores state phase we get the binding and replace the one from
+     * the system with the one from the scope map
+     */
+    public void restoreState(FacesContext context, Object state)
+    {
+        Object values[] = (Object[]) state;
+        super.restoreState(context, values[0]);
+
+        /*
+         * fetch the old scoped object and bind it to the value binding if it
+         * exists, otherwise there will be no binding
+         */
+        ValueBinding vb = getValueBinding("value");
+        Object oldVal = holder.restoreScopeEntry(vb.getExpressionString());
+        if (oldVal == null)
+            return;
+        vb.setValue(context, oldVal);
+        setValueBinding("value", vb);
+
+    }
+
+    /**
+     * reset scope helper which allows to remove the scope from the system
+     * within the backend context
+     *
+     * @param context
+     */
+    public void resetScope(FacesContext context)
+    {
+        ValueBinding vb = getValueBinding("value");
+        vb.setValue(context, "");
+        setValueBinding("value", vb);
+        holder.resetScope(vb.getExpressionString());
+    }
+}



Mime
View raw message