Package com.composum.sling.cpnl
Class ComponentTag
- java.lang.Object
-
- javax.servlet.jsp.tagext.TagSupport
-
- javax.servlet.jsp.tagext.BodyTagSupport
-
- com.composum.sling.cpnl.CpnlBodyTagSupport
-
- com.composum.sling.cpnl.ComponentTag
-
- All Implemented Interfaces:
Serializable,javax.servlet.jsp.tagext.BodyTag,javax.servlet.jsp.tagext.IterationTag,javax.servlet.jsp.tagext.JspTag,javax.servlet.jsp.tagext.Tag
public class ComponentTag extends CpnlBodyTagSupport
a tag to instantiate a bean or model object- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected StringattrPathprotected org.apache.sling.api.resource.ResourceattrResourceprotected Booleancloseprotected SlingBeancomponentprotected booleancomponentWasClosedprotected Booleanreplaceprotected ArrayList<Map<String,Object>>replacedAttributesstatic Map<String,Integer>SCOPESprotected Stringtypeprotected Stringvariableprotected IntegervarScope-
Fields inherited from class com.composum.sling.cpnl.CpnlBodyTagSupport
context, out, request, resource, resourceResolver
-
-
Constructor Summary
Constructors Constructor Description ComponentTag()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidadditionalInitialization(SlingBean component)Hook for perform additional initialization of the component.protected Objectavailable()Check for an existing instance of the same var and assignable typeprotected voidclear()Reset all member variables to the (default) start values.protected voidcloseComponent()If necessary, call close on the component, if required.protected SlingBeancreateComponent()Create the requested component instanceintdoEndTag()intdoStartTag()BooleangetClose()protected Class<? extends SlingBean>getComponentType()get the content type class objectorg.apache.sling.api.resource.ResourcegetModelResource(BeanContext context)Hook that can change the resource used forcreateComponent()if necessary.BooleangetReplace()protected Map<String,Object>getReplacedAttributes(int scope)retrieves the registry for one scopeStringgetType()StringgetVar()IntegergetVarScope()protected voidinjectServices(SlingBean component)Inject OSGI services for attributes marked for injection in a new component instance, if not already initialized e.g.protected voidrestoreAttributes()restores all replaced values and removes all attributes declared in this tagprotected <T> TretrieveFirstServiceOfType(Class<T> serviceType, String filter)protected voidsetAttribute(String key, Object value, int scope)each attribute set by a tag should use this method for attribute declaration; an existing value with the same key is registered and restored if the tag rendering endsvoidsetClose(Boolean close)Determines whetherAutoCloseablemodels are closed when the tag ends - default true.voidsetId(String id)Configure an var / variable name to store the component in the contextvoidsetPath(String path)voidsetReplace(Boolean flag)Determine the reuse policy if an appropriate instance is already existing.voidsetResource(org.apache.sling.api.resource.Resource resource)voidsetScope(String key)Determine the varScope (page,requestorsession) for the component instance attributevoidsetType(String type)Component class to instantiate (full notation as in Class.name)voidsetVar(String id)Configure an var / variable name to store the component in the contextvoidsetVarScope(Integer value)-
Methods inherited from class com.composum.sling.cpnl.CpnlBodyTagSupport
createContext, eval, getExpressionUtil, release, setPageContext
-
Methods inherited from class javax.servlet.jsp.tagext.BodyTagSupport
doAfterBody, doInitBody, getBodyContent, getPreviousOut, setBodyContent
-
Methods inherited from class javax.servlet.jsp.tagext.TagSupport
findAncestorWithClass, getId, getParent, getValue, getValues, removeValue, setParent, setValue
-
-
-
-
Field Detail
-
variable
protected String variable
-
type
protected String type
-
varScope
protected Integer varScope
-
replace
protected Boolean replace
-
close
protected Boolean close
-
attrPath
protected String attrPath
-
attrResource
protected org.apache.sling.api.resource.Resource attrResource
-
component
protected transient SlingBean component
-
componentWasClosed
protected transient boolean componentWasClosed
-
-
Method Detail
-
clear
protected void clear()
Description copied from class:CpnlBodyTagSupportReset all member variables to the (default) start values. Called prior processing the tag and at release time.- Overrides:
clearin classCpnlBodyTagSupport
-
doStartTag
public int doStartTag() throws javax.servlet.jsp.JspException- Specified by:
doStartTagin interfacejavax.servlet.jsp.tagext.Tag- Overrides:
doStartTagin classCpnlBodyTagSupport- Throws:
javax.servlet.jsp.JspException
-
doEndTag
public int doEndTag() throws javax.servlet.jsp.JspException- Specified by:
doEndTagin interfacejavax.servlet.jsp.tagext.Tag- Overrides:
doEndTagin classjavax.servlet.jsp.tagext.BodyTagSupport- Throws:
javax.servlet.jsp.JspException
-
closeComponent
protected void closeComponent()
If necessary, call close on the component, if required. We try to make sure it's called exactly once, sinceAutoCloseable.close()doesn't guarantee that's idempotent.
-
setId
public void setId(String id)
Configure an var / variable name to store the component in the context- Overrides:
setIdin classjavax.servlet.jsp.tagext.TagSupport
-
setVar
public void setVar(String id)
Configure an var / variable name to store the component in the context
-
getVar
public String getVar()
-
setType
public void setType(String type)
Component class to instantiate (full notation as in Class.name)
-
getType
public String getType()
-
setScope
public void setScope(String key)
Determine the varScope (page,requestorsession) for the component instance attribute
-
setVarScope
public void setVarScope(Integer value)
-
getVarScope
public Integer getVarScope()
-
setReplace
public void setReplace(Boolean flag)
Determine the reuse policy if an appropriate instance is already existing.- Parameters:
flag-false- (re)use an appropriate available instance;true- replace each potentially existing instance (default in 'page' context).
-
getReplace
public Boolean getReplace()
-
setPath
public void setPath(String path)
-
setResource
public void setResource(org.apache.sling.api.resource.Resource resource)
-
setClose
public void setClose(Boolean close)
Determines whetherAutoCloseablemodels are closed when the tag ends - default true.
-
getClose
public Boolean getClose()
-
getComponentType
protected Class<? extends SlingBean> getComponentType() throws ClassNotFoundException
get the content type class object- Throws:
ClassNotFoundException
-
available
protected Object available() throws ClassNotFoundException
Check for an existing instance of the same var and assignable type- Throws:
ClassNotFoundException
-
createComponent
protected SlingBean createComponent() throws ClassNotFoundException, IllegalAccessException, InstantiationException
Create the requested component instance
-
getModelResource
public org.apache.sling.api.resource.Resource getModelResource(BeanContext context)
Hook that can change the resource used forcreateComponent()if necessary. This implementation just uses the resource from theCpnlBodyTagSupport.context(BeanContext.getResource()) if not specified explicitly.
-
additionalInitialization
protected void additionalInitialization(SlingBean component)
Hook for perform additional initialization of the component. When called, the fields of the component are already initialized with Sling-Models orSlingBean.initialize(BeanContext)/SlingBean.initialize(BeanContext, Resource).
-
injectServices
protected void injectServices(SlingBean component) throws IllegalAccessException
Inject OSGI services for attributes marked for injection in a new component instance, if not already initialized e.g. by Sling-Models.- Throws:
IllegalAccessException
-
retrieveFirstServiceOfType
protected <T> T retrieveFirstServiceOfType(Class<T> serviceType, String filter)
-
getReplacedAttributes
protected Map<String,Object> getReplacedAttributes(int scope)
retrieves the registry for one scope
-
setAttribute
protected void setAttribute(String key, Object value, int scope)
each attribute set by a tag should use this method for attribute declaration; an existing value with the same key is registered and restored if the tag rendering ends
-
restoreAttributes
protected void restoreAttributes()
restores all replaced values and removes all attributes declared in this tag
-
-