Package org.apache.sling.models.factory
Interface ModelFactory
- 
@ProviderType public interface ModelFactory
The ModelFactory instantiates Sling Model classes similar to #adaptTo but will throw an exception in case instantiation fails for some reason. 
- 
- 
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description booleancanCreateFromAdaptable(@NotNull java.lang.Object adaptable, @NotNull java.lang.Class<?> type)<ModelType>
ModelTypecreateModel(@NotNull java.lang.Object adaptable, @NotNull java.lang.Class<ModelType> type)Instantiates the given Sling Model class from the given adaptable.<T> TexportModel(@NotNull java.lang.Object model, @NotNull java.lang.String exporterName, @NotNull java.lang.Class<T> targetClass, @NotNull java.util.Map<java.lang.String,java.lang.String> options)Export the model object using the defined target class using the named exporter.<T> TexportModelForRequest(@NotNull SlingHttpServletRequest request, @NotNull java.lang.String exporterName, @NotNull java.lang.Class<T> targetClass, @NotNull java.util.Map<java.lang.String,java.lang.String> options)Export the model object registered to the request's resource's type using the defined target class using the named exporter.<T> TexportModelForResource(@NotNull Resource resource, @NotNull java.lang.String exporterName, @NotNull java.lang.Class<T> targetClass, @NotNull java.util.Map<java.lang.String,java.lang.String> options)Export the model object registered to the resource's type using the defined target class using the named exporter.@NotNull java.lang.ObjectgetModelFromRequest(@NotNull SlingHttpServletRequest request)Obtain an adapted model class based on the resource type of the request's resource.@NotNull java.lang.ObjectgetModelFromResource(@NotNull Resource resource)Obtain an adapted model class based on the resource type of the provided resource.<T> TgetModelFromWrappedRequest(@NotNull SlingHttpServletRequest request, @NotNull Resource resource, @NotNull java.lang.Class<T> targetClass)Create a wrapped request object with the specified resource and (try to) adapt the request object into the specified class.booleanisModelAvailableForRequest(@NotNull SlingHttpServletRequest request)Determine is a model class is available for the request's resource's resource type.booleanisModelAvailableForResource(@NotNull Resource resource)Determine is a model class is available for the resource's resource type.booleanisModelClass(@NotNull java.lang.Class<?> type)Checks if a given type can be instantiated though Sling Models.booleanisModelClass(@NotNull java.lang.Object adaptable, @NotNull java.lang.Class<?> type)Deprecated.UseisModelClass(Class)instead! 
 - 
 
- 
- 
Method Detail
- 
createModel
@NotNull <ModelType> ModelType createModel(@NotNull @NotNull java.lang.Object adaptable, @NotNull @NotNull java.lang.Class<ModelType> type) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelExceptionInstantiates the given Sling Model class from the given adaptable.- Parameters:
 adaptable- the adaptable to use to instantiate the Sling Model Classtype- the class to instantiate- Returns:
 - a new instance for the required model (never 
null) - Throws:
 MissingElementsException- in case no injector was able to inject some required values with the given typesInvalidAdaptableException- in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)ModelClassException- in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be calledPostConstructException- in case the post-construct method has thrown an exception itselfValidationException- in case validation could not be performed for some reason (e.g. no validation information available)InvalidModelException- in case the given model type could not be validated through the model validation
 
- 
canCreateFromAdaptable
boolean canCreateFromAdaptable(@NotNull @NotNull java.lang.Object adaptable, @NotNull @NotNull java.lang.Class<?> type)- Parameters:
 adaptable- the adaptable to checktype- the class to check- Returns:
 truein case the given class can be created from the given adaptable, otherwisefalse
 
- 
isModelClass
@Deprecated boolean isModelClass(@NotNull @NotNull java.lang.Object adaptable, @NotNull @NotNull java.lang.Class<?> type)Deprecated.UseisModelClass(Class)instead!- Parameters:
 adaptable- the adaptable to checktype- the class to check- Returns:
 - false in case no class with the Model annotation adapts to the requested type
 - See Also:
 Model
 
- 
isModelClass
boolean isModelClass(@NotNull @NotNull java.lang.Class<?> type)Checks if a given type can be instantiated though Sling Models. This checks that- there is a class annotated with 
Modelwhich adapts to the given type - this class is registered as Sling Model (i.e. the package is listed in the "Sling-Model-Packages" header from the bundles manifest and has been picked up already by the bundle listener)
 
true.- Parameters:
 type- the class to check- Returns:
 truein case the given type can be instantiated though Sling Models.
 - there is a class annotated with 
 
- 
isModelAvailableForResource
boolean isModelAvailableForResource(@NotNull @NotNull Resource resource)Determine is a model class is available for the resource's resource type.- Parameters:
 resource- a resource- Returns:
 trueif a model class is mapped to the resource type
 
- 
isModelAvailableForRequest
boolean isModelAvailableForRequest(@NotNull @NotNull SlingHttpServletRequest request)Determine is a model class is available for the request's resource's resource type.- Parameters:
 request- a request- Returns:
 trueif a model class is mapped to the resource type
 
- 
getModelFromResource
@NotNull @NotNull java.lang.Object getModelFromResource(@NotNull @NotNull Resource resource) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelExceptionObtain an adapted model class based on the resource type of the provided resource.- Parameters:
 resource- a resource- Returns:
 - an adapted model object
 - Throws:
 MissingElementsException- in case no injector was able to inject some required values with the given typesInvalidAdaptableException- in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)ModelClassException- in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be calledPostConstructException- in case the post-construct method has thrown an exception itselfValidationException- in case validation could not be performed for some reason (e.g. no validation information available)InvalidModelException- in case the given model type could not be validated through the model validation
 
- 
getModelFromRequest
@NotNull @NotNull java.lang.Object getModelFromRequest(@NotNull @NotNull SlingHttpServletRequest request) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelExceptionObtain an adapted model class based on the resource type of the request's resource.- Parameters:
 request- a request- Returns:
 - an adapted model object
 - Throws:
 MissingElementsException- in case no injector was able to inject some required values with the given typesInvalidAdaptableException- in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)ModelClassException- in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be calledPostConstructException- in case the post-construct method has thrown an exception itselfValidationException- in case validation could not be performed for some reason (e.g. no validation information available)InvalidModelException- in case the given model type could not be validated through the model validation
 
- 
exportModel
@NotNull <T> T exportModel(@NotNull @NotNull java.lang.Object model, @NotNull @NotNull java.lang.String exporterName, @NotNull @NotNull java.lang.Class<T> targetClass, @NotNull @NotNull java.util.Map<java.lang.String,java.lang.String> options) throws ExportException, MissingExporterExceptionExport the model object using the defined target class using the named exporter.- Type Parameters:
 T- the target class- Parameters:
 model- the model objectexporterName- the exporter nametargetClass- the target classoptions- any exporter options- Returns:
 - an instance of the target class
 - Throws:
 ExportException- if the export failsMissingExporterException- if the named exporter can't be found
 
- 
exportModelForResource
@NotNull <T> T exportModelForResource(@NotNull @NotNull Resource resource, @NotNull @NotNull java.lang.String exporterName, @NotNull @NotNull java.lang.Class<T> targetClass, @NotNull @NotNull java.util.Map<java.lang.String,java.lang.String> options) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException, ExportException, MissingExporterExceptionExport the model object registered to the resource's type using the defined target class using the named exporter.- Type Parameters:
 T- the target class- Parameters:
 resource- the resourceexporterName- the exporter nametargetClass- the target classoptions- any exporter options- Returns:
 - an instance of the target class
 - Throws:
 MissingElementsException- in case no injector was able to inject some required values with the given typesInvalidAdaptableException- in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)ModelClassException- in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be calledPostConstructException- in case the post-construct method has thrown an exception itselfValidationException- in case validation could not be performed for some reason (e.g. no validation information available)InvalidModelException- in case the given model type could not be validated through the model validationExportException- if the export failsMissingExporterException- if the named exporter can't be found
 
- 
exportModelForRequest
@NotNull <T> T exportModelForRequest(@NotNull @NotNull SlingHttpServletRequest request, @NotNull @NotNull java.lang.String exporterName, @NotNull @NotNull java.lang.Class<T> targetClass, @NotNull @NotNull java.util.Map<java.lang.String,java.lang.String> options) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException, ExportException, MissingExporterExceptionExport the model object registered to the request's resource's type using the defined target class using the named exporter.- Type Parameters:
 T- the target class- Parameters:
 request- the requestexporterName- the exporter nametargetClass- the target classoptions- any exporter options- Returns:
 - an instance of the target class
 - Throws:
 MissingElementsException- in case no injector was able to inject some required values with the given typesInvalidAdaptableException- in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)ModelClassException- in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be calledPostConstructException- in case the post-construct method has thrown an exception itselfValidationException- in case validation could not be performed for some reason (e.g. no validation information available)InvalidModelException- in case the given model type could not be validated through the model validationExportException- if the export failsMissingExporterException- if the named exporter can't be found
 
- 
getModelFromWrappedRequest
@Nullable <T> T getModelFromWrappedRequest(@NotNull @NotNull SlingHttpServletRequest request, @NotNull @NotNull Resource resource, @NotNull @NotNull java.lang.Class<T> targetClass)Create a wrapped request object with the specified resource and (try to) adapt the request object into the specified class. The wrapped request object will have a fresh set of script bindings so that any injected bindings references have the correct context.- Type Parameters:
 T- the target adapter class- Parameters:
 request- the current requestresource- the resource to set as the wrapped request's resourcetargetClass- the target adapter class- Returns:
 - an instance of the target class or null if the adaptation could not be done
 
 
 - 
 
 -