Class Dispatcher
- All Implemented Interfaces:
InvocationHandler
Metadata
, Citation
,
etc.
Any call to a method in a metadata interface is redirected toward the invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
method.
This method uses reflection in order to find the caller's method and class name. The class
name is translated into a table name, and the method name is translated into a column name.
Then the information is fetched in the underlying metadata database.
There is usually a one-to-one correspondence between invoked methods and the columns to be read, but not always.
Some method invocations may actually trig a computation using the values of other columns. This happen for example
when invoking a deprecated method which computes its value from non-deprecated methods. Such situations happen in
the transition from ISO 19115:2003 to ISO 19115:2014 and may happen again in the future as standards are revised.
The algorithms are encoded in implementation classes like the ones in org.apache.sis.metadata.iso
packages,
and access to those implementation classes is enabled by the cache
field (which, consequently, is more than
only a cache).
Instance of this class shall be thread-safe.
- Since:
- 0.8
- Version:
- 1.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object
The metadata instance where to store the property (column) values, ornull
if not yet created.(package private) final String
The identifier used in order to locate the record for this metadata entity in the database.private long
A bitmask of properties having null values.(package private) int
Index in theCachedStatement
cache array where to search first.private final MetadataSource
The connection to the database. -
Constructor Summary
ConstructorsConstructorDescriptionDispatcher
(String identifier, MetadataSource source) Creates a new metadata handler. -
Method Summary
Modifier and TypeMethodDescription(package private) final String
Returns the error message for a failure to query the database for the property identified by the given method.private Object
fetchValue
(LookupInfo info, Method method) Gets, computes or read from the database a metadata property value.Invoked when any method from a metadata interface is invoked.private static Method
If the given method is superceded by a new method, the new method.toString()
Returns a string representation of this handler.private String
Returns a string representation of a metadata of the given type.
-
Field Details
-
identifier
The identifier used in order to locate the record for this metadata entity in the database. This is usually the primary key in the table which contains this entity. -
source
The connection to the database. All metadata handlers created from a single database should share the same source. -
preferredIndex
int preferredIndexIndex in theCachedStatement
cache array where to search first. This is only a hint for increasing the chances to find quickly aCachedStatement
instance for the right type and identifier.Design note: this field is declared in thisDispatcher
class instead ofCachedStatement
because we need it before aCachedStatement
instance can be found. Furthermore, twoDispatcher
instances may have differentpreferredIndex
values even if theirCachedStatement.type
value is the same, since theiridentifier
values are different. -
cache
The metadata instance where to store the property (column) values, ornull
if not yet created. For ISO 19115, this is an instance of one of the classes defined inorg.apache.sis.metadata.iso
package or sub-packages. The intent is not only to cache the property values, but also to leverage implementations that compute automatically some property values from other properties. The main usage is computing the value of a deprecated property from the values of non-deprecated ones, e.g. for transition from ISO 19115:2003 to ISO 19115:2014. -
nullValues
private transient long nullValuesA bitmask of properties having null values. Cached for avoiding to query the database many times. Bit indices are given byLookupInfo.asIndexMap(MetadataStandard)
. If a metadata contains more than 64 properties, no "null value" information will be stored for the extra properties. No damage will happen except more database accesses than needed.We do not need to synchronize this field because it is only an optimization. It is okay if a bit is wrongly zero; the only consequence is that it will cause one more database access than needed.
-
-
Constructor Details
-
Dispatcher
Creates a new metadata handler.- Parameters:
identifier
- the identifier used in order to locate the record for this metadata entity in the database. This is usually the primary key in the table which contains this entity.source
- the connection to the table which contains this entity.
-
-
Method Details
-
invoke
Invoked when any method from a metadata interface is invoked.- Specified by:
invoke
in interfaceInvocationHandler
- Parameters:
proxy
- the object on which the method is invoked.method
- the method invoked.args
- the argument given to the method.- Returns:
- the value to be returned from the public method invoked by the method.
-
fetchValue
private Object fetchValue(LookupInfo info, Method method) throws ReflectiveOperationException, SQLException, MetadataStoreException Gets, computes or read from the database a metadata property value. This method returns the first non-null value in the following choices:- If the property value is present in the cache, the cached value.
- If the "cache" can compute the value from other property values, the result of that computation. This case happen mostly for deprecated properties that are replaced by one or more newer properties.
- The value stored in the database. The database is queried only once for the requested property and the result is cached for future reuse.
- Parameters:
info
- information related to the interface of the metadata object for which a property value is requested. This is used for fetching information from theMetadataStandard
.method
- the method to be invoked. The class given byMethod.getDeclaringClass()
is usually the same than the one given byLookupInfo.getMetadataType()
, but not necessarily. The two classes may differ if the method is declared only in the implementation class.- Returns:
- the property value, or
null
if none. - Throws:
ReflectiveOperationException
- if an error occurred while querying thecache
.SQLException
- if an error occurred while querying the database.MetadataStoreException
- if a value was not found or cannot be converted to the expected type.
-
error
Returns the error message for a failure to query the database for the property identified by the given method. -
toString
Returns a string representation of a metadata of the given type. -
toString
Returns a string representation of this handler. This is mostly for debugging purpose. -
supercede
If the given method is superceded by a new method, the new method. This is a hack for transition from legacy ISO type to newer type:ResponsibleParty.getRole()
overridingResponsibility.getRole()
confuses thisDispatcher
class. We need the method in the base interface.- Throws:
NoSuchMethodException
-