The Configuration interface simply provides the
basic type for configurations in the system. Extensions of this interface
will then be able to directly read data from files through specified interface
methods.
Note: According the JVM specification, all methods
insides an interface are public. Declaring them to be public is redundant.
Therefore, the source for Carbon interfaces do not specifically declare
the methods as public since this is redundant.
An example configuration interface can be found below.
(This is only an example)
Example Configuration Interface
package examples;
public interface WebServiceConfiguration extends Configuration {
String getServiceName();
void setServiceName(String value);
String getServiceAddress();
void setServiceAddress(String serviceAddress);
boolean isSecureConnection();
void setSecureConnection(boolean secureConnection);
Class TransportClass = org.apache.soap.transport.SOAPHTTPConnection.class;
Class getTransportClass();
void setTransportClass(Class value);
}
An example configuration document for the above interface
would look like the following:
Example Configuration Data File
<Configuration
ConfigurationInterface="examples.WebServiceConfiguration">
<ServiceName>HelloWorld</ServiceName>
<ServiceAddress>http://ws.examples/HelloWorld</ServiceAddress>
<SecureConnection>true</SecureConnection>
</Configuration>
These configurations can then be accessed via an instance
of the above WebServicesConfiguration that is created by the Carbon
Configuration Service. The following example shows the basics of reading information
from the above configuration:
Example Use of Configuration
WebServiceConfiguration myConfig = (WebServiceConfiguration)
Config.getInstance().fetchConfiguration("/WS/example");
if (myConfig.isSecure()) {
// ... use ssl socket factory ...
} else {
// ... use standard socket factory ...
}
Transport transport = myConfig.getTransportClass().newInstance();
transport.send(new URL(myConfig.getServiceAddress()), "hello", // .....
You can see the way that the configuration service provides
objects implementing the specified configuration interface without any more
work than configuring the interface class. In a similar way the set methods
may be used to alter the data in that configuration and as needed saved to
the underlying store.
Default Values
If a value for an attribute defined by a configuration
interface is not present in the XML, null will
be returned if the return type is an Object or
an InvalidConfigurationException is thrown if the
return type is a primitive. If, instead, a default value should be returned,
a constant of the same name as the attribute should be defined in the configuration
interface as in the TransportClass attribute of
the WebServiceConfiguration above. The constant
should be the same type as the return type of get method of the attribute.