Package com.composum.sling.core
Interface BeanContext
-
- All Superinterfaces:
org.apache.sling.api.adapter.Adaptable
- All Known Implementing Classes:
BeanContext.AbstractContext
,BeanContext.AbstractScriptContext
,BeanContext.Map
,BeanContext.Page
,BeanContext.Service
,BeanContext.Servlet
,BeanContext.Wrapper
@Adaptable(adaptableClass=BeanContext.class, adapters=@Adapter(condition="If the context contains an entity of the requested type",value={org.apache.sling.api.resource.Resource.class,org.apache.sling.api.resource.ResourceResolver.class,org.apache.sling.api.SlingHttpServletRequest.class,org.apache.sling.api.SlingHttpServletResponse.class})) public interface BeanContext extends org.apache.sling.api.adapter.Adaptable
The interface for the different scripting contexts (JSP, Groovy, ...) and the basic implementations for this interface. This serves as a container for the basic objects often used to initialize models.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
BeanContext.AbstractContext
the base class of the context interface with general methodsstatic class
BeanContext.AbstractScriptContext
the enhancement of the base for scripting contextsstatic class
BeanContext.Map
a Map based implementation of the context interface (e.g.static class
BeanContext.Page
a JSP PageContext based implementation of the context interfacestatic class
BeanContext.Scope
the Scope enumeration according to the JSPs PageContextstatic class
BeanContext.Service
a Service based implementation for a background service or a job executionstatic class
BeanContext.Servlet
a servlet API based implementation of the context interface for Beans in a Servlet contextstatic class
BeanContext.Wrapper
a wrapper implementation base
-
Field Summary
Fields Modifier and Type Field Description static String
ATTR_LOCALE
static String
ATTR_REQUEST
static String
ATTR_RESOLVER
static String
ATTR_RESOURCE
static String
ATTR_RESPONSE
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <AdapterType>
AdapterTypeadaptTo(@NotNull Class<AdapterType> type)
Adapts to the componentsResource
,ResourceResolver
,SlingHttpServletRequest
,SlingHttpServletResponse
,Locale
,BeanContext
itself, aValueMap
for the request,SlingBean
s or possibly more if defined in Sling.<T> T
getAttribute(String name, Class<T> T)
Returns an attribute value from the context.Locale
getLocale()
Returns the locale declared determined using the context, determined and cached asgetAttribute(String, Class)
(ATTR_LOCALE
).org.apache.sling.api.SlingHttpServletRequest
getRequest()
Returns the request declared in the context.org.apache.sling.api.resource.ResourceResolver
getResolver()
Returns the resolver declared in the context.org.apache.sling.api.resource.Resource
getResource()
Returns the resource declared in the context.org.apache.sling.api.SlingHttpServletResponse
getResponse()
Returns the response declared in the context.<T> T
getService(Class<T> type)
retrieves a service implementation using the 'sling' script helper<T> T[]
getServices(Class<T> serviceType, String filter)
retrieves a set of services appropriate to the filterClass<?>
getType(String className)
retrieves a class using the Slings DynamicClassLoaderManager implementationvoid
setAttribute(String name, Object value, BeanContext.Scope scope)
Stores an attribute in the context in th given scope.BeanContext
withLocale(Locale locale)
Returns a clone of this context with the locale overridden, orthis
if it already had this locale.BeanContext
withResource(org.apache.sling.api.resource.Resource resource)
Returns a clone of this context with the resource overridden, orthis
if it already had this resource.
-
-
-
Field Detail
-
ATTR_RESOURCE
static final String ATTR_RESOURCE
- See Also:
- Constant Field Values
-
ATTR_RESOLVER
static final String ATTR_RESOLVER
- See Also:
- Constant Field Values
-
ATTR_REQUEST
static final String ATTR_REQUEST
- See Also:
- Constant Field Values
-
ATTR_RESPONSE
static final String ATTR_RESPONSE
- See Also:
- Constant Field Values
-
ATTR_LOCALE
static final String ATTR_LOCALE
- See Also:
- Constant Field Values
-
-
Method Detail
-
getResource
org.apache.sling.api.resource.Resource getResource()
Returns the resource declared in the context.
-
getResolver
org.apache.sling.api.resource.ResourceResolver getResolver()
Returns the resolver declared in the context.
-
getRequest
org.apache.sling.api.SlingHttpServletRequest getRequest()
Returns the request declared in the context.
-
getResponse
org.apache.sling.api.SlingHttpServletResponse getResponse()
Returns the response declared in the context.
-
getLocale
Locale getLocale()
Returns the locale declared determined using the context, determined and cached asgetAttribute(String, Class)
(ATTR_LOCALE
).
-
getAttribute
<T> T getAttribute(String name, Class<T> T)
Returns an attribute value from the context.
-
setAttribute
void setAttribute(String name, Object value, BeanContext.Scope scope)
Stores an attribute in the context in th given scope.
-
getService
<T> T getService(Class<T> type)
retrieves a service implementation using the 'sling' script helper
-
getServices
<T> T[] getServices(Class<T> serviceType, String filter) throws org.osgi.framework.InvalidSyntaxException
retrieves a set of services appropriate to the filter- Throws:
org.osgi.framework.InvalidSyntaxException
-
getType
Class<?> getType(String className) throws ClassNotFoundException
retrieves a class using the Slings DynamicClassLoaderManager implementation- Throws:
ClassNotFoundException
-
adaptTo
<AdapterType> AdapterType adaptTo(@NotNull @NotNull Class<AdapterType> type)
Adapts to the components
Resource
,ResourceResolver
,SlingHttpServletRequest
,SlingHttpServletResponse
,Locale
,BeanContext
itself, aValueMap
for the request,SlingBean
s or possibly more if defined in Sling.In case of
SlingBean
we try the basic sling mechanism (possibly calling Sling-Models) and, failing that, try to instantiate andSlingBean.initialize(BeanContext)
it ourselves, if it is instantiable and the Sling-Models @Model annotation is not present.Cached - multiple calls will always return the same object, except for
ValueMap
andSlingBean
s.- Specified by:
adaptTo
in interfaceorg.apache.sling.api.adapter.Adaptable
- Parameters:
type
- not null, the type to be adapted to- Returns:
- the component of type or whatever Sling has adapters for, or null if there is nothing.
- See Also:
SlingAdaptable.adaptTo(Class)
-
withResource
BeanContext withResource(org.apache.sling.api.resource.Resource resource)
Returns a clone of this context with the resource overridden, orthis
if it already had this resource. All other internal structures of this will be referenced by the copy, too.- Parameters:
resource
- the resource- Returns:
- a context with the
getResource()
resource
, everything else (except possibly resolver) unchanged. Might bethis
.
-
withLocale
BeanContext withLocale(Locale locale)
Returns a clone of this context with the locale overridden, orthis
if it already had this locale. All other internal structures of this will be referenced by the copy, too.- Parameters:
locale
- the locale; if this is nullgetLocale()
will take this from the attributes.- Returns:
- a context with this locale, otherwise sharing everything else. Might be
this
.
-
-