Author: Alex.Kolonitsky
Date: 2009-12-23 06:42:32 -0500 (Wed, 23 Dec 2009)
New Revision: 16200
Added:
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnHandlerComponentConfig.java
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandlerIterationContext.java
Modified:
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTag.java
branches/community/3.3.X/ui/dataTable/src/main/java/org/richfaces/taglib/ColumnTagHandler.java
Log:
ThreadLocal/Memory leak in ColumnsHandler
https://jira.jboss.org/jira/browse/RF-8216
Added:
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnHandlerComponentConfig.java
===================================================================
---
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnHandlerComponentConfig.java
(rev 0)
+++
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnHandlerComponentConfig.java 2009-12-23
11:42:32 UTC (rev 16200)
@@ -0,0 +1,83 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.taglib;
+
+import com.sun.facelets.tag.jsf.ComponentConfig;
+import com.sun.facelets.tag.Tag;
+import com.sun.facelets.tag.TagAttribute;
+import com.sun.facelets.tag.TagAttributes;
+import com.sun.facelets.FaceletHandler;
+
+/**
+ * @author akolonitsky
+ * @since Dec 20, 2009
+ */
+class ColumnHandlerComponentConfig implements ComponentConfig {
+
+ private static final String ITERATION_INDEX_EXPRESSION = "#{" +
ColumnsHandler.ITERATION_INDEX_VARIABLE + "}";
+
+ private Tag tag;
+ private final ComponentConfig config;
+
+ ColumnHandlerComponentConfig(ComponentConfig config) {
+ this.config = config;
+ Tag initialTag = config.getTag();
+ TagAttribute[] allInitialAttributes = initialTag.getAttributes().getAll();
+ TagAttribute[] attributes = new TagAttribute[allInitialAttributes.length];
+ for (int i = 0; i < allInitialAttributes.length; i++) {
+ TagAttribute initialAttribute = allInitialAttributes[i];
+ String localName = initialAttribute.getLocalName();
+ String attributeValue = initialAttribute.getValue();
+
+ if ("id".equals(localName)) {
+ attributeValue += ITERATION_INDEX_EXPRESSION;
+ }
+
+ attributes[i] = new TagAttribute(initialAttribute.getLocation(),
initialAttribute.getNamespace(),
+ localName, initialAttribute.getQName(), attributeValue);
+ }
+
+ TagAttributes tagAttributes = new TagAttributes(attributes);
+ this.tag = new Tag(initialTag, tagAttributes);
+ }
+
+ public String getComponentType() {
+ return config.getComponentType();
+ }
+
+ public String getRendererType() {
+ return config.getRendererType();
+ }
+
+ public FaceletHandler getNextHandler() {
+ return config.getNextHandler();
+ }
+
+ public Tag getTag() {
+ return tag;
+ }
+
+ public String getTagId() {
+ return config.getTagId();
+ }
+
+}
Modified:
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java
===================================================================
---
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java 2009-12-23
11:33:55 UTC (rev 16199)
+++
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java 2009-12-23
11:42:32 UTC (rev 16200)
@@ -27,42 +27,28 @@
package org.richfaces.taglib;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.el.ELException;
-import javax.el.ValueExpression;
-import javax.el.VariableMapper;
-import javax.faces.FacesException;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.FacesContext;
-import javax.servlet.jsp.JspTagException;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.richfaces.el.ELBuilder;
-import org.richfaces.iterator.ForEachIterator;
-import org.richfaces.iterator.SimpleForEachIterator;
-
import com.sun.facelets.FaceletContext;
-import com.sun.facelets.FaceletHandler;
import com.sun.facelets.tag.MetaRule;
import com.sun.facelets.tag.MetaRuleset;
import com.sun.facelets.tag.MetaTagHandler;
import com.sun.facelets.tag.Metadata;
import com.sun.facelets.tag.MetadataTarget;
-import com.sun.facelets.tag.Tag;
import com.sun.facelets.tag.TagAttribute;
-import com.sun.facelets.tag.TagAttributes;
import com.sun.facelets.tag.jsf.ComponentConfig;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.richfaces.el.ELBuilder;
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.el.VariableMapper;
+import javax.faces.FacesException;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+import java.io.IOException;
+import java.util.Iterator;
-
/**
* TODO Class description goes here.
*
@@ -73,80 +59,21 @@
private static final Log log = LogFactory.getLog(ColumnsHandler.class);
- com.sun.facelets.tag.jsf.ComponentHandler handler;
+ private com.sun.facelets.tag.jsf.ComponentHandler handler;
- private static final String ITERATION_INDEX_VARIABLE =
"__richfaces_iteration_index_variable";
+ static final String ITERATION_INDEX_VARIABLE =
"__richfaces_iteration_index_variable";
- private static final String ITERATION_INDEX_EXPRESSION = "#{" +
ITERATION_INDEX_VARIABLE + "}";
-
private static final String F_GENERATION_SERIES_ATTRIBUTE =
"org.richfaces.F_COLUMNS_GENERATION_SERIES";
/** value attribute */
private TagAttribute value;
- /** end attribute */
- private TagAttribute columns;
-
- /** begin attribute */
- private TagAttribute begin;
-
- /** var attribute */
- private TagAttribute var;
-
- /** index attribute */
+ /** index attribute */
private TagAttribute index;
- /** end attribute */
- private TagAttribute end;
+ private ThreadLocal<ColumnsHandlerIterationContext> iterationContextLocal = new
ThreadLocal<ColumnsHandlerIterationContext>();
- class IterationContext {
-
- /** Iterator for columns's tag value attribute */
- public ForEachIterator items; // our 'digested' items
-
- /** Value attribute value */
- public Object rawItems; // our 'raw' items
-
- /** Var attr - defines page variable for current item */
- public String _indexId;
-
- /** Integer value begin attr */
- public Integer _begin;
-
- /** Integer value end attr */
- public Integer _end;
-
- /** Integer value of end attr. */
- public Integer _columns;
-
- /** String value of var attr */
- public String _itemId = null;
-
- /** Current column counter */
- public Integer _index = 0;
-
- /** Expression for var item */
- public IteratedExpression iteratedExpression;
-
- public String valueExpr;
-
- public String getVarReplacement() {
- if (valueExpr == null) {
- return String.valueOf(index);
- }else if (items.getVarReplacement() != null) {
- return items.getVarReplacement();
- }
- return valueExpr + "[" + _index + "]";
- }
-
- public String getIndexReplacement() {
- return String.valueOf(_index);
- }
- };
-
- ThreadLocal<IterationContext> iterationContextLocal = new
ThreadLocal<IterationContext>();
-
- public IterationContext getIterationContext() {
+ private ColumnsHandlerIterationContext getIterationContext() {
return iterationContextLocal.get();
}
@@ -162,61 +89,13 @@
TagAttribute idAttribute = config.getTag().getAttributes().get("id");
if (idAttribute != null && idAttribute.isLiteral()) {
- columnConfig = new ComponentConfig() {
-
- private Tag tag;
-
- {
- Tag initialTag = config.getTag();
- TagAttribute[] allInitialAttributes = initialTag.getAttributes().getAll();
- TagAttribute[] attributes = new TagAttribute[allInitialAttributes.length];
- for (int i = 0; i < allInitialAttributes.length; i++) {
- TagAttribute initialAttribute = allInitialAttributes[i];
- String localName = initialAttribute.getLocalName();
- String attributeValue = initialAttribute.getValue();
-
- if ("id".equals(localName)) {
- attributeValue += ITERATION_INDEX_EXPRESSION;
- }
-
- attributes[i] = new TagAttribute(initialAttribute.getLocation(),
- initialAttribute.getNamespace(),
- localName,
- initialAttribute.getQName(),
- attributeValue);
- }
-
- TagAttributes tagAttributes = new TagAttributes(attributes);
- this.tag = new Tag(initialTag, tagAttributes);
- }
-
- public String getComponentType() {
- return config.getComponentType();
- }
-
- public String getRendererType() {
- return config.getRendererType();
- }
-
- public FaceletHandler getNextHandler() {
- return config.getNextHandler();
- }
-
- public Tag getTag() {
- return tag;
- }
-
- public String getTagId() {
- return config.getTagId();
- }
-
- };
+ columnConfig = new ColumnHandlerComponentConfig(config);
} else {
columnConfig = config;
}
handler = new ColumnTagHandler(columnConfig) {
-
+
@Override
protected MetaRuleset createMetaRuleset(Class type) {
MetaRuleset ruleset = super.createMetaRuleset(type);
@@ -225,21 +104,20 @@
@Override
public Metadata applyRule(final String name,
final TagAttribute attribute, MetadataTarget meta) {
- if (ColumnsAttributes.FILTER_ATTRIBUTES.indexOf(name) != -1 ||
+ if (ColumnsAttributes.FILTER_ATTRIBUTES.indexOf(name) != -1 ||
ColumnsAttributes.SORT_ATTRIBUTES.indexOf(name) != -1) {
-
+
return new Metadata() {
@Override
- public void applyMetadata(FaceletContext ctx,
- Object instance) {
+ public void applyMetadata(FaceletContext ctx, Object instance) {
if (!attribute.isLiteral()) {
String expr = attribute.getValue();
- IterationContext itContext = iterationContextLocal.get();
+ ColumnsHandlerIterationContext itContext = iterationContextLocal.get();
- ValueExpression ve = ELBuilder.createValueExpression(expr, Object.class,
ctx.getExpressionFactory(), ctx,
- itContext._itemId, itContext._indexId,
- itContext.getVarReplacement(), itContext.getIndexReplacement());
+ ValueExpression ve = ELBuilder.createValueExpression(expr, Object.class,
ctx.getExpressionFactory(), ctx,
+ itContext.getItemId(), itContext.getIndexId(),
+ itContext.getVarReplacement(index), itContext.getIndexReplacement());
((UIComponent)instance).setValueExpression(name, ve);
}else {
((UIComponent)instance).getAttributes().put(name, attribute.getValue());
@@ -264,248 +142,68 @@
};
}
+ /**
+ * Method prepares all we need for starting of tag rendering
+ *
+ * @param ctx
+ */
+ private void prepare(FaceletContext ctx) {
+ ColumnsHandlerIterationContext itContext = getIterationContext();
- /**
- * Extracts tags attributes values
- */
- private void initVariables(FaceletContext ctx) {
- initColumnsCount(ctx);
- initIndex(ctx);
- initVar(ctx);
- initBegin(ctx);
- initEnd(ctx);
- }
+ try {
+ this.index = getAttribute("index");
+ itContext.setIndex(ctx, this.index);
- /**
- * Method prepares all we need for starting of tag rendering
- *
- * @throws JspTagException
- */
- private void prepare(FaceletContext ctx) {
+ itContext.setColumnsCount(ctx, getAttribute("columns"));
+ itContext.setVar(ctx, getAttribute("var"));
+ itContext.setBegin(ctx, getAttribute("begin"));
+ itContext.setEnd(ctx, getAttribute("end"));
- initVariables(ctx);
+ this.value = getAttribute("value");
+ itContext.setValue(ctx, this.value);
- IterationContext itContext = getIterationContext();
+ } catch (Exception e) {
+ log.error(e.getLocalizedMessage(), e);
+ // TODO: handle exception
+ }
- try {
+ itContext.correctFirst();
+ }
- this.value = getAttribute("value");
-
- // produce the right sort of ForEachIterator
- if (this.value != null) {
- itContext.valueExpr = ELBuilder.getVarReplacement(this.value.getValue());
-
- // If this is a deferred expression, make a note and get
- // the 'items' instance.
-
- itContext.rawItems = this.value.getObject(ctx);
-
- // extract an iterator over the 'items' we've got
- itContext.items = SimpleForEachIterator
- .supportedTypeForEachIterator(itContext.rawItems);
- } else {
- // no 'items', so use 'begin' and 'end'
- itContext.items = SimpleForEachIterator.beginEndForEachIterator(itContext._columns -
1);
- }
- } catch (Exception e) {
- log.error(e.getLocalizedMessage(), e);
- // TODO: handle exception
- }
-
- correctFirst(ctx);
- }
-
- /**
- * Inits first iteration item
- */
- private void correctFirst(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- if (itContext.items != null) {
- if (itContext._begin > 0 && (itContext._index < itContext._begin)) {
- while ((itContext._index < itContext._begin && hasNext())) {
- next(ctx);
- }
- if (!hasNext()) {
- itContext._index = 0;
- }
- }
- }
- }
-
- /**
- * Return true if we didn't complete column's count
- *
- * @return
- * @throws JspTagException
- */
- private boolean hasNext() {
- IterationContext itContext = getIterationContext();
- try {
- if (itContext._end != 0) {
- return (itContext._index < itContext._end) ? itContext.items.hasNext() : false;
- } else {
- return itContext.items.hasNext();
- }
- } catch (Exception e) {
- return false;
- }
-
- }
-
- /**
- * Iterate to next column
- *
- * @return
- * @throws JspTagException
- */
- private Object next(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- try {
- Object o = itContext.items.next();
- itContext._index++;
- return o;
- } catch (Exception e) {
- return null;
- }
- }
-
- /**
- * Extracts integer value from end attr
- */
- private void initColumnsCount(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- this.columns = getAttribute("columns");
- if (columns != null) {
- try {
- itContext._columns = Integer.parseInt((String) columns.getObject(ctx));
- if (itContext._columns < 0) {
- itContext._columns = 0; // If end is negative set up zero
- }
- } catch (Exception e) {
- itContext._columns = 0;
- }
- } else {
- itContext._columns = 0;
- }
- }
-
- /**
- * Extracts integer value from begin attr
- */
- private void initBegin(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- this.begin = getAttribute("begin");
- if (begin != null) {
- try {
- Object o = begin.getObject(ctx);
- if (o instanceof Number) {
- itContext._begin = ((Number)o).intValue();
- }else if (o instanceof String) {
- itContext._begin = Integer.parseInt((String) o);
- }
- itContext._begin--;
- if (itContext._begin < 0) {
- itContext._begin = 0; // If end is negative set up zero
- }
- } catch (Exception e) {
- itContext._begin = 0;
- }
- } else {
- itContext._begin = 0;
- }
- }
-
- /**
- * Extracts integer value from end attr
- */
- private void initEnd(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- this.end = getAttribute("end");
- if (end != null) {
- try {
- Object o = end.getObject(ctx);
- if (o instanceof Number) {
- itContext._end = ((Number)o).intValue();
- }else if ( o instanceof String) {
- itContext._end = Integer.parseInt((String) o);
- }
- if (itContext._end < 0) {
- itContext._end = 0; // If end is negative set up zero
- }
- } catch (Exception e) {
- itContext._end = 0;
- }
- } else {
- itContext._end = 0;
- }
- }
-
- /**
- * Extracts string value from var attr
- */
- private void initVar(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- this.var = getAttribute("var");
- if (var != null) {
- try {
- itContext._itemId = (String) var.getObject(ctx);
- } catch (ClassCastException e) {
- itContext._itemId = null;
- }
-
- }
- }
-
- /**
- * Extracts string value from index attr
- */
- private void initIndex(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- this.index = getAttribute("index");
- if (index != null) {
- try {
- itContext._indexId = (String) index.getObject(ctx);
- } catch (ClassCastException e) {
- itContext._indexId = null;
- }
-
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.taglib.ComponentHandler#apply(com.sun.facelets.FaceletContext,
- * javax.faces.component.UIComponent)
- */
- //@Override
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.taglib.ComponentHandler#apply(com.sun.facelets.FaceletContext,
+ * javax.faces.component.UIComponent)
+ */
public void apply(FaceletContext ctx, UIComponent parent)
- throws IOException, FacesException, ELException {
+ throws IOException, FacesException, ELException {
- IterationContext iterationContext = new IterationContext();
- iterationContextLocal.set(iterationContext);
+ clearOldColumns(ctx.getFacesContext(), parent);
- clearOldColumns(ctx.getFacesContext(), parent);
- prepare(ctx); // prepare data
+ ColumnsHandlerIterationContext iterationContext = new
ColumnsHandlerIterationContext();
+ iterationContextLocal.set(iterationContext);
+ prepare(ctx);
try {
- while (hasNext()) { // for each
+ while (iterationContext.hasNext()) {
exposeVariables(ctx);
handler.apply(ctx, parent);
- next(ctx);
+ iterationContext.next();
}
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
// TODO: handle exception
} finally {
- release();
+ iterationContext.release();
unExposeVariables(ctx);
- }
+ iterationContextLocal.remove();
+ }
}
- private void clearOldColumns(FacesContext context, UIComponent parent) {
+ private static void clearOldColumns(FacesContext context, UIComponent parent) {
if (parent.getChildCount() > 0) {
Integer generatedId = RequestUniqueIdGenerator.generateId(context);
@@ -521,100 +219,62 @@
}
}
- /**
- * Sets page request variables
- *
- * @throws JspTagException
- */
- private void exposeVariables(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- VariableMapper vm = ctx.getVariableMapper();
- int k = itContext._index;
+ /**
+ * Sets page request variables
+ *
+ * @param ctx
+ */
+ private void exposeVariables(FaceletContext ctx) {
+ VariableMapper vm = ctx.getVariableMapper();
+ if (vm == null) {
+ return;
+ }
- if (itContext._itemId != null) {
- if (vm != null) {
- if (value != null) {
- ValueExpression srcVE = value.getValueExpression(ctx,
- Object.class);
- ValueExpression ve = getVarExpression(ctx, srcVE);
- vm.setVariable(itContext._itemId, ve);
- }
- }
+ ColumnsHandlerIterationContext itContext = getIterationContext();
+ if (itContext.getItemId() != null) {
+ if (value != null) {
+ ValueExpression srcVE = value.getValueExpression(ctx, Object.class);
+ ValueExpression ve = itContext.getVarExpression(ctx, srcVE);
+ vm.setVariable(itContext.getItemId(), ve);
+ }
+ }
- }
+ // Set up index variable
+ if (itContext.getIndexId() != null) {
+ ValueExpression ve = new IteratedIndexExpression(itContext.getIndex());
+ vm.setVariable(itContext.getIndexId(), ve);
+ }
- // Set up index variable
+ int componentsCount = itContext.getIndex() - itContext.getBegin();
+ if (componentsCount != 0) {
+ ValueExpression ve = ctx.getExpressionFactory().createValueExpression(
+ UIViewRoot.UNIQUE_ID_PREFIX + componentsCount, String.class);
+
+ vm.setVariable(ITERATION_INDEX_VARIABLE, ve);
+ }
+ }
- if (itContext._indexId != null) {
- if (vm != null) {
- ValueExpression ve = new IteratedIndexExpression(k);
- vm.setVariable(itContext._indexId, ve);
- }
+ /**
+ * Removes page attributes that we have exposed and, if applicable, restores
+ * them to their prior values (and scopes).
+ *
+ * @param ctx
+ */
+ private void unExposeVariables(FaceletContext ctx) {
+ VariableMapper vm = ctx.getVariableMapper();
+ if (vm == null) {
+ return;
+ }
- }
+ // "nested" variables are now simply removed
+ ColumnsHandlerIterationContext itContext = getIterationContext();
+ if (itContext.getItemId() != null) {
+ vm.setVariable(itContext.getItemId(), null);
+ }
+ if (itContext.getIndexId() != null) {
+ vm.setVariable(itContext.getIndexId(), null);
+ }
- int componentsCount = itContext._index - itContext._begin;
- if (componentsCount != 0) {
- ValueExpression ve =
ctx.getExpressionFactory().createValueExpression(UIViewRoot.UNIQUE_ID_PREFIX +
componentsCount, String.class);
- vm.setVariable(ITERATION_INDEX_VARIABLE, ve);
- }
- }
-
- /**
- * Removes page attributes that we have exposed and, if applicable, restores
- * them to their prior values (and scopes).
- */
- private void unExposeVariables(FaceletContext ctx) {
- IterationContext itContext = getIterationContext();
- VariableMapper vm = ctx.getVariableMapper();
- // "nested" variables are now simply removed
- if (itContext._itemId != null) {
- if (vm != null)
- vm.setVariable(itContext._itemId, null);
- }
- if (itContext._indexId != null) {
- if (vm != null)
- vm.setVariable(itContext._indexId, null);
- }
-
- vm.setVariable(ITERATION_INDEX_VARIABLE, null);
- }
-
- /**
- * Return expression for page variables
- *
- * @param expr
- * @return
- */
- private ValueExpression getVarExpression(FaceletContext ctx,
- ValueExpression expr/*, IterationContext itContext*/) {
- IterationContext itContext = getIterationContext();
- Object o = expr.getValue(ctx.getFacesContext().getELContext());
- int k = itContext._index;
- if (o.getClass().isArray() || o instanceof List) {
- return new IndexedValueExpression(expr, k);
- }
-
- if (o instanceof Collection || o instanceof Iterator
- || o instanceof Enumeration || o instanceof Map
- || o instanceof String) {
-
- if (itContext.iteratedExpression == null) {
- itContext.iteratedExpression = new IteratedExpression(expr, ",");
- }
- return new IteratedValueExpression(itContext.iteratedExpression, k);
- }
-
- throw new ELException("FOREACH_BAD_ITEMS");
- }
-
- /**
- * Release iteration variables
- */
- private void release() {
- IterationContext itContext = getIterationContext();
- itContext.items = null;
- itContext._index = 0;
- }
-
+ vm.setVariable(ITERATION_INDEX_VARIABLE, null);
+ }
}
Added:
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandlerIterationContext.java
===================================================================
---
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandlerIterationContext.java
(rev 0)
+++
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandlerIterationContext.java 2009-12-23
11:42:32 UTC (rev 16200)
@@ -0,0 +1,290 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.taglib;
+
+import org.richfaces.iterator.ForEachIterator;
+import org.richfaces.iterator.SimpleForEachIterator;
+import org.richfaces.el.ELBuilder;
+import com.sun.facelets.tag.TagAttribute;
+import com.sun.facelets.FaceletContext;
+
+import javax.servlet.jsp.JspTagException;
+import javax.el.ValueExpression;
+import javax.el.ELException;
+import java.util.List;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Enumeration;
+import java.util.Map;
+
+/**
+ * @author akolonitsky
+ * @since Dec 21, 2009
+ */
+class ColumnsHandlerIterationContext {
+ /**
+ * Iterator for columns's tag value attribute
+ */
+ private ForEachIterator items; // our 'digested' items
+
+ /**
+ * Var attr - defines page variable for current item
+ */
+ private String indexId;
+
+ /**
+ * Integer value begin attr
+ */
+ private Integer begin;
+
+ /**
+ * Integer value end attr
+ */
+ private Integer end;
+
+ /**
+ * Integer value of end attr.
+ */
+ private Integer columnsCount;
+
+ /**
+ * String value of var attr
+ */
+ private String itemId = null;
+
+ /**
+ * Current column counter
+ */
+ private Integer index = 0;
+
+ /**
+ * Expression for var item
+ */
+ private IteratedExpression iteratedExpression;
+
+ private String valueExpr;
+
+ public String getVarReplacement(TagAttribute index) {
+ if (valueExpr == null) {
+ return String.valueOf(index);
+ }
+
+ if (items != null && items.getVarReplacement() != null) {
+ return items.getVarReplacement();
+ }
+
+ return valueExpr + "[" + this.index + "]";
+ }
+
+ public String getIndexReplacement() {
+ return String.valueOf(index);
+ }
+
+ /**
+ * Return true if we didn't complete column's count
+ *
+ * @return hasNext
+ */
+ boolean hasNext() {
+ try {
+ return (end == 0 || index < end) && items.hasNext();
+ } catch (Exception e) {
+ return false;
+ }
+
+ }
+
+ /**
+ * Inits first iteration item
+ */
+ void correctFirst() {
+ if (items != null) {
+ if (begin > 0 && (index < begin)) {
+ while (index < begin && hasNext()) {
+ next();
+ }
+ if (!hasNext()) {
+ index = 0;
+ }
+ }
+ }
+ }
+
+ /**
+ * Iterate to next column
+ *
+ * @param ctx
+ * @return
+ */
+ Object next() {
+ try {
+ Object o = items.next();
+ index++;
+ return o;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ void setColumnsCount(FaceletContext ctx, TagAttribute columns) {
+ if (columns != null) {
+ try {
+ this.columnsCount = Integer.parseInt((String) columns.getObject(ctx));
+ if (this.columnsCount < 0) {
+ this.columnsCount = 0; // If end is negative set up zero
+ }
+ } catch (Exception e) {
+ this.columnsCount = 0;
+ }
+ } else {
+ this.columnsCount = 0;
+ }
+ }
+
+
+ void setBegin(FaceletContext ctx, TagAttribute begin) {
+ if (begin != null) {
+ try {
+ Object o = begin.getObject(ctx);
+ if (o instanceof Number) {
+ this.begin = ((Number)o).intValue();
+ }else if (o instanceof String) {
+ this.begin = Integer.parseInt((String) o);
+ }
+ this.begin--;
+ if (this.begin < 0) {
+ this.begin = 0; // If end is negative set up zero
+ }
+ } catch (Exception e) {
+ this.begin = 0;
+ }
+ } else {
+ this.begin = 0;
+ }
+ }
+
+
+ void setEnd(FaceletContext ctx, TagAttribute end) {
+ if (end != null) {
+ try {
+ Object o = end.getObject(ctx);
+ if (o instanceof Number) {
+ this.end = ((Number)o).intValue();
+ }else if ( o instanceof String) {
+ this.end = Integer.parseInt((String) o);
+ }
+ if (this.end < 0) {
+ this.end = 0; // If end is negative set up zero
+ }
+ } catch (Exception e) {
+ this.end = 0;
+ }
+ } else {
+ this.end = 0;
+ }
+ }
+
+ void setIndex(FaceletContext ctx, TagAttribute index) {
+ if (index != null) {
+ try {
+ indexId = (String) index.getObject(ctx);
+ } catch (ClassCastException e) {
+ indexId = null;
+ }
+ }
+ }
+
+ void setVar(FaceletContext ctx, TagAttribute var) {
+ if (var != null) {
+ try {
+ itemId = (String) var.getObject(ctx);
+ } catch (ClassCastException e) {
+ itemId = null;
+ }
+ }
+ }
+
+ void setValue(FaceletContext ctx, TagAttribute value) throws JspTagException {
+ // produce the right sort of ForEachIterator
+ if (value != null) {
+ valueExpr = ELBuilder.getVarReplacement(value.getValue());
+
+ // If this is a deferred expression, make a note and get the 'items'
instance.
+ // extract an iterator over the 'items' we've got
+ items =
SimpleForEachIterator.supportedTypeForEachIterator(value.getObject(ctx));
+ } else {
+ // no 'items', so use 'begin' and 'end'
+ items = SimpleForEachIterator.beginEndForEachIterator(columnsCount - 1);
+ }
+ }
+
+ /**
+ * Return expression for page variables
+ *
+ * @param ctx
+ * @param expr
+ * @return
+ */
+ ValueExpression getVarExpression(FaceletContext ctx, ValueExpression expr) {
+ Object o = expr.getValue(ctx.getFacesContext().getELContext());
+ int k = index;
+ if (o.getClass().isArray() || o instanceof List) {
+ return new IndexedValueExpression(expr, k);
+ }
+
+ if (o instanceof Collection || o instanceof Iterator
+ || o instanceof Enumeration || o instanceof Map
+ || o instanceof String) {
+
+ if (iteratedExpression == null) {
+ iteratedExpression = new IteratedExpression(expr, ",");
+ }
+ return new IteratedValueExpression(iteratedExpression, k);
+ }
+
+ throw new ELException("FOREACH_BAD_ITEMS");
+ }
+
+ /**
+ * Release iteration variables
+ */
+ void release() {
+ items = null;
+ index = 0;
+ }
+
+ public String getIndexId() {
+ return indexId;
+ }
+
+ public String getItemId() {
+ return itemId;
+ }
+
+ public Integer getBegin() {
+ return begin;
+ }
+
+ public Integer getIndex() {
+ return index;
+ }
+}
Modified:
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTag.java
===================================================================
---
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTag.java 2009-12-23
11:33:55 UTC (rev 16199)
+++
branches/community/3.3.X/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTag.java 2009-12-23
11:42:32 UTC (rev 16200)
@@ -261,8 +261,8 @@
* @param sortOrder -
* new value
*/
- public void setSortOrder(ValueExpression __sortOrder) {
- this._sortOrder = __sortOrder;
+ public void setSortOrder(ValueExpression sortOrder) {
+ this._sortOrder = sortOrder;
}
/**
@@ -272,8 +272,8 @@
* @param sortBy -
* new value
*/
- public void setSortBy(ValueExpression __sortBy) {
- this._sortBy = __sortBy;
+ public void setSortBy(ValueExpression sortBy) {
+ this._sortBy = sortBy;
}
/**
@@ -283,8 +283,8 @@
* @param style -
* new value
*/
- public void setStyle(ValueExpression __style) {
- this._style = __style;
+ public void setStyle(ValueExpression style) {
+ this._style = style;
}
/*
@@ -734,8 +734,8 @@
/**
* Create custom context with VariableMapper override.
*
- * @param context
- * @return
+ * @param cont
+ * @return
*/
private ELContext getContext(final ELContext cont) {
return new ELContext() {
@@ -921,8 +921,8 @@
* @param value -
* new value
*/
- public void setValue(ValueExpression __value) {
- this.__value = __value;
+ public void setValue(ValueExpression value) {
+ this.__value = value;
}
/**
@@ -932,8 +932,8 @@
* @param var -
* new value
*/
- public void setVar(ValueExpression __var) {
- this.__var = __var;
+ public void setVar(ValueExpression var) {
+ this.__var = var;
}
/**
@@ -942,8 +942,8 @@
* @param colspan -
* new value
*/
- public void setColspan(ValueExpression __colspan) {
- this._colspan = __colspan;
+ public void setColspan(ValueExpression colspan) {
+ this._colspan = colspan;
}
/**
@@ -952,8 +952,8 @@
* @param styleClass -
* new value
*/
- public void setStyleClass(ValueExpression __styleClass) {
- this._styleClass = __styleClass;
+ public void setStyleClass(ValueExpression styleClass) {
+ this._styleClass = styleClass;
}
/**
@@ -963,8 +963,8 @@
* @param width -
* new value
*/
- public void setWidth(ValueExpression __width) {
- this._width = __width;
+ public void setWidth(ValueExpression width) {
+ this._width = width;
}
/**
@@ -973,8 +973,8 @@
* @param breakBefore -
* new value
*/
- public void setBreakBefore(ValueExpression __breakBefore) {
- this._breakBefore = __breakBefore;
+ public void setBreakBefore(ValueExpression breakBefore) {
+ this._breakBefore = breakBefore;
}
/**
@@ -984,8 +984,8 @@
* @param comparator -
* new value
*/
- public void setComparator(ValueExpression __comparator) {
- this._comparator = __comparator;
+ public void setComparator(ValueExpression comparator) {
+ this._comparator = comparator;
}
/**
@@ -1004,8 +1004,8 @@
* @param filterBy -
* new value
*/
- public void setFilterBy(ValueExpression __filterBy) {
- this._filterBy = __filterBy;
+ public void setFilterBy(ValueExpression filterBy) {
+ this._filterBy = filterBy;
}
/**
@@ -1015,8 +1015,8 @@
* @param filterDefaultLabel -
* new value
*/
- public void setFilterDefaultLabel(ValueExpression __filterDefaultLabel) {
- this._filterDefaultLabel = __filterDefaultLabel;
+ public void setFilterDefaultLabel(ValueExpression filterDefaultLabel) {
+ this._filterDefaultLabel = filterDefaultLabel;
}
/**
@@ -1026,8 +1026,8 @@
* @param filterEvent -
* new value
*/
- public void setFilterEvent(ValueExpression __filterEvent) {
- this._filterEvent = __filterEvent;
+ public void setFilterEvent(ValueExpression filterEvent) {
+ this._filterEvent = filterEvent;
}
/**
@@ -1037,8 +1037,8 @@
* @param filterExpression -
* new value
*/
- public void setFilterExpression(ValueExpression __filterExpression) {
- this._filterExpression = __filterExpression;
+ public void setFilterExpression(ValueExpression filterExpression) {
+ this._filterExpression = filterExpression;
}
/**
@@ -1048,8 +1048,8 @@
* @param filterMethod -
* new value
*/
- public void setFilterMethod(MethodExpression __filterMethod) {
- this._filterMethod = __filterMethod;
+ public void setFilterMethod(MethodExpression filterMethod) {
+ this._filterMethod = filterMethod;
}
/**
@@ -1059,8 +1059,8 @@
* @param filterValue -
* new value
*/
- public void setFilterValue(ValueExpression __filterValue) {
- this._filterValue = __filterValue;
+ public void setFilterValue(ValueExpression filterValue) {
+ this._filterValue = filterValue;
}
/**
@@ -1070,8 +1070,8 @@
* @param footerClass -
* new value
*/
- public void setFooterClass(ValueExpression __footerClass) {
- this._footerClass = __footerClass;
+ public void setFooterClass(ValueExpression footerClass) {
+ this._footerClass = footerClass;
}
/**
@@ -1081,8 +1081,8 @@
* @param headerClass -
* new value
*/
- public void setHeaderClass(ValueExpression __headerClass) {
- this._headerClass = __headerClass;
+ public void setHeaderClass(ValueExpression headerClass) {
+ this._headerClass = headerClass;
}
/**
@@ -1091,8 +1091,8 @@
* @param rowspan -
* new value
*/
- public void setRowspan(ValueExpression __rowspan) {
- this._rowspan = __rowspan;
+ public void setRowspan(ValueExpression rowspan) {
+ this._rowspan = rowspan;
}
/**
@@ -1102,8 +1102,8 @@
* @param selfSorted -
* new value
*/
- public void setSelfSorted(ValueExpression __selfSorted) {
- this._selfSorted = __selfSorted;
+ public void setSelfSorted(ValueExpression selfSorted) {
+ this._selfSorted = selfSorted;
}
/**
@@ -1113,8 +1113,8 @@
* @param sortable -
* new value
*/
- public void setSortable(ValueExpression __sortable) {
- this._sortable = __sortable;
+ public void setSortable(ValueExpression sortable) {
+ this._sortable = sortable;
}
/**
@@ -1124,18 +1124,18 @@
* @param sortExpression -
* new value
*/
- public void setSortExpression(ValueExpression __sortExpression) {
- this._sortExpression = __sortExpression;
+ public void setSortExpression(ValueExpression sortExpression) {
+ this._sortExpression = sortExpression;
}
/**
* Attribute defines whether to render component or not
* Setter for rendered
*
- * @param __rendered - new value
+ * @param rendered - new value
*/
- public void setRendered(ValueExpression __rendered) {
- this._rendered = __rendered;
+ public void setRendered(ValueExpression rendered) {
+ this._rendered = rendered;
}
/**
Modified:
branches/community/3.3.X/ui/dataTable/src/main/java/org/richfaces/taglib/ColumnTagHandler.java
===================================================================
---
branches/community/3.3.X/ui/dataTable/src/main/java/org/richfaces/taglib/ColumnTagHandler.java 2009-12-23
11:33:55 UTC (rev 16199)
+++
branches/community/3.3.X/ui/dataTable/src/main/java/org/richfaces/taglib/ColumnTagHandler.java 2009-12-23
11:42:32 UTC (rev 16200)
@@ -56,7 +56,9 @@
public Metadata applyRule(String name, final TagAttribute attribute, MetadataTarget
metadataTarget) {
if (FILTER_METHOD.equals(name)) {
return new Metadata() {
- public void applyMetadata(FaceletContext context, Object object) {
+
+ @Override
+ public void applyMetadata(FaceletContext context, Object object)
{
FacesContext facesContext = context.getFacesContext();
MethodExpression expression =
facesContext.getApplication().getExpressionFactory().