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 String
attrPath
protected org.apache.sling.api.resource.Resource
attrResource
protected Boolean
close
protected SlingBean
component
protected boolean
componentWasClosed
protected Boolean
replace
protected ArrayList<Map<String,Object>>
replacedAttributes
static Map<String,Integer>
SCOPES
protected String
type
protected String
variable
protected Integer
varScope
-
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 void
additionalInitialization(SlingBean component)
Hook for perform additional initialization of the component.protected Object
available()
Check for an existing instance of the same var and assignable typeprotected void
clear()
Reset all member variables to the (default) start values.protected void
closeComponent()
If necessary, call close on the component, if required.protected SlingBean
createComponent()
Create the requested component instanceint
doEndTag()
int
doStartTag()
Boolean
getClose()
protected Class<? extends SlingBean>
getComponentType()
get the content type class objectorg.apache.sling.api.resource.Resource
getModelResource(BeanContext context)
Hook that can change the resource used forcreateComponent()
if necessary.Boolean
getReplace()
protected Map<String,Object>
getReplacedAttributes(int scope)
retrieves the registry for one scopeString
getType()
String
getVar()
Integer
getVarScope()
protected void
injectServices(SlingBean component)
Inject OSGI services for attributes marked for injection in a new component instance, if not already initialized e.g.protected void
restoreAttributes()
restores all replaced values and removes all attributes declared in this tagprotected <T> T
retrieveFirstServiceOfType(Class<T> serviceType, String filter)
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 endsvoid
setClose(Boolean close)
Determines whetherAutoCloseable
models are closed when the tag ends - default true.void
setId(String id)
Configure an var / variable name to store the component in the contextvoid
setPath(String path)
void
setReplace(Boolean flag)
Determine the reuse policy if an appropriate instance is already existing.void
setResource(org.apache.sling.api.resource.Resource resource)
void
setScope(String key)
Determine the varScope (page
,request
orsession
) for the component instance attributevoid
setType(String type)
Component class to instantiate (full notation as in Class.name)void
setVar(String id)
Configure an var / variable name to store the component in the contextvoid
setVarScope(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:CpnlBodyTagSupport
Reset all member variables to the (default) start values. Called prior processing the tag and at release time.- Overrides:
clear
in classCpnlBodyTagSupport
-
doStartTag
public int doStartTag() throws javax.servlet.jsp.JspException
- Specified by:
doStartTag
in interfacejavax.servlet.jsp.tagext.Tag
- Overrides:
doStartTag
in classCpnlBodyTagSupport
- Throws:
javax.servlet.jsp.JspException
-
doEndTag
public int doEndTag() throws javax.servlet.jsp.JspException
- Specified by:
doEndTag
in interfacejavax.servlet.jsp.tagext.Tag
- Overrides:
doEndTag
in 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:
setId
in 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
,request
orsession
) 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 whetherAutoCloseable
models 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
-
-