public interface ConfigReader extends Immutable
metadata.xml
files and configures Metawidgets.
In spirit, metadata.xml
is a general-purpose mechanism for configuring JavaBeans
based on XML files. In practice, there are some Metawidget-specific features such as:
isImmutable
)XmlUtils.CachingContextHandler
)WEB-INF
using
ServletContext.getResource
(ConfigReader
implements
ResourceResolver
)
ConfigReader
's support for reusing immutable objects (eg. JpaInspector
)
that use config objects (eg. JpaInspectorConfig
) is dependant on the config object
overriding equals
and hashCode
. Failure to override these
methods may result in your object not being reused, or being reused inappropriately.
Object configure(String resource, Object toConfigure, String... names)
This version of configure
uses openResource
to open the specified
resource. It assumes the resource name is a unique key, so subsequent calls do not need to
re-open the resource, or re-parse it, making this version of configure
much
faster than configure( InputStream, Object )
.
This version further caches any immutable objects, in the same way as
configure( InputStream, Object )
(see the JavaDoc for that method).
resource
- resource name that will be looked up using openResourcetoConfigure
- object to configure. Can be a subclass of the one actually in the resourcenames
- path to a property within the object. If specified, siblings to this path will be
ignored. This allows ConfigReader to be used to initialise only a specific part of
an objectObject configure(InputStream stream, Object toConfigure, String... names)
This version of configure
caches any immutable objects (as determined by
isImmutable
) and reuses them for subsequent calls. This helps ensure there is
only ever one instance of a, say, Inspector
or WidgetBuilder
.
If the Object to configure is a Class
, this method will create and return an
instance of that class based on the configuration file. For example, if the configuration
file is...
<metawidget>
<myInspector config="myConfig">
<someConfigParameter/>
</myInspector>
</metawidget>
...then the code...
Inspector myInspector = myConfigReader.configure( stream, Inspector.class );
...will create a MyInspector
configured with someConfigParameter
.
Conversely, if the Object to configure is already an instance, this method will configure the instance. For example if the configuration file is...
<metawidget>
<swingMetawidget>
<opaque><boolean>true</boolean></opaque>
</swingMetawidget>
</metawidget>
...then the code...
JPanel panel = new JPanel();
myConfigReader.configure( stream, panel );
...will call setOpaque
on the given JPanel
.
stream
- XML input as a streamtoConfigure
- object to configure. Can be a subclass of the one actually in the resourcenames
- path to a property within the object. If specified, siblings to this path will be
ignored. This allows ConfigReader to be used to initialise only a specific part of
an objectCopyright © 2015. All Rights Reserved.