Package com.composum.sling.core
Class BeanContext.AbstractContext
- java.lang.Object
-
- org.apache.sling.api.adapter.SlingAdaptable
-
- com.composum.sling.core.BeanContext.AbstractContext
-
- All Implemented Interfaces:
BeanContext
,Cloneable
,org.apache.sling.api.adapter.Adaptable
- Direct Known Subclasses:
BeanContext.AbstractScriptContext
,BeanContext.Servlet
- Enclosing interface:
- BeanContext
public abstract static class BeanContext.AbstractContext extends org.apache.sling.api.adapter.SlingAdaptable implements BeanContext, Cloneable
the base class of the context interface with general methods
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.composum.sling.core.BeanContext
BeanContext.AbstractContext, BeanContext.AbstractScriptContext, BeanContext.Map, BeanContext.Page, BeanContext.Scope, BeanContext.Service, BeanContext.Servlet, BeanContext.Wrapper
-
-
Field Summary
Fields Modifier and Type Field Description protected Locale
locale
-
Fields inherited from interface com.composum.sling.core.BeanContext
ATTR_LOCALE, ATTR_REQUEST, ATTR_RESOLVER, ATTR_RESOURCE, ATTR_RESPONSE
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractContext()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete 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.protected Object
cloneContext()
Locale
getLocale()
Returns the locale declared determined using the context, determined and cached asBeanContext.getAttribute(String, Class)
(BeanContext.ATTR_LOCALE
).<T> T
getService(Class<T> type)
retrieves a service implementation using the 'sling' script helperClass<?>
getType(String className)
retrieves a class using the Slings DynamicClassLoaderManager implementationprotected abstract <T> T
retrieveService(Class<T> type)
protected <AdapterType>
AdapterTypetryToInstantiateSlingBean(Class<AdapterType> type)
If type is an instantiable SlingBean but not a Sling-Models bean, try to instantiate it.protected <AdapterType>
booleantypeFits(Class<?> type, Class<?> upperbound, AdapterType object, Class<AdapterType> defaultclass)
A type fits if it is below some upper bound (we don't want to return something for type, say, Object) and if it is assignable from the real class of the object.BeanContext
withLocale(Locale locale)
Returns a clone of this context with the locale overridden, orthis
if it already had this locale.-
Methods inherited from class org.apache.sling.api.adapter.SlingAdaptable
setAdapterManager, unsetAdapterManager
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.composum.sling.core.BeanContext
getAttribute, getRequest, getResolver, getResource, getResponse, getServices, setAttribute, withResource
-
-
-
-
Field Detail
-
locale
protected transient Locale locale
-
-
Method Detail
-
cloneContext
protected Object cloneContext()
-
retrieveService
protected abstract <T> T retrieveService(Class<T> type)
-
getLocale
public Locale getLocale()
Description copied from interface:BeanContext
Returns the locale declared determined using the context, determined and cached asBeanContext.getAttribute(String, Class)
(BeanContext.ATTR_LOCALE
).- Specified by:
getLocale
in interfaceBeanContext
-
withLocale
public BeanContext withLocale(Locale locale)
Description copied from interface:BeanContext
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.- Specified by:
withLocale
in interfaceBeanContext
- Parameters:
locale
- the locale; if this is nullBeanContext.getLocale()
will take this from the attributes.- Returns:
- a context with this locale, otherwise sharing everything else. Might be
this
.
-
getService
public <T> T getService(Class<T> type)
Description copied from interface:BeanContext
retrieves a service implementation using the 'sling' script helper- Specified by:
getService
in interfaceBeanContext
-
getType
public Class<?> getType(String className) throws ClassNotFoundException
Description copied from interface:BeanContext
retrieves a class using the Slings DynamicClassLoaderManager implementation- Specified by:
getType
in interfaceBeanContext
- Throws:
ClassNotFoundException
-
adaptTo
public <AdapterType> AdapterType adaptTo(@NotNull @NotNull Class<AdapterType> type)
Description copied from interface:BeanContext
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
- Specified by:
adaptTo
in interfaceBeanContext
- Overrides:
adaptTo
in classorg.apache.sling.api.adapter.SlingAdaptable
- 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)
-
tryToInstantiateSlingBean
protected <AdapterType> AdapterType tryToInstantiateSlingBean(Class<AdapterType> type)
If type is an instantiable SlingBean but not a Sling-Models bean, try to instantiate it. We have to check per reflection whether the Sling-Models @Models annotation is present since we cannot distinguish a failed Sling-Models instantiation (returning null) from not being a Sling-Models bean at all (also returning null), and the Sling-Models ModelFactory cannot be a dependency, here.
-
typeFits
protected <AdapterType> boolean typeFits(Class<?> type, Class<?> upperbound, AdapterType object, Class<AdapterType> defaultclass)
A type fits if it is below some upper bound (we don't want to return something for type, say, Object) and if it is assignable from the real class of the object. If the object is null, we will return null (and pass by the other Sling adaptable mechanisms) if the type is assignable from the default type the object would implement / extend.
-
-