Implementing the HTTP Server operations
In this section you'll use the httpd.conf file to fill in the values of the resource property fields. The implementation defines the three properties as read-only to remote clients so that you don't have to introduce WS-Security authentication; keeping the properties read-only also allows you to avoid the checks that would be needed to prevent changes while the httpd process is running.
Because your properties won't change after startup, you can read them in from httpd.conf once during initialization and keep them in the fields that WSDL2Java created in the MyCapability class. You should copy and paste the implementation code into your MyCapability.java file.
Listing 8. Implementation code
public void initialize()
throws SoapFault
{
super.initialize();
Map configParams = null;
try
{
configParams = readConfigFile("/apache-2.2/conf/httpd.conf");
}
catch (IOException error)
{
throw new SoapFault("Error while reading httpd.conf.", error);
}
_ServerName = (String)configParams.get("ServerName");
String portString = (String)configParams.get("Listen");
String threadsString = (String)configParams.get("ThreadsPerChild");
_PortNumber = Integer.valueOf(portString);
_ThreadsPerChild = Integer.valueOf(threadsString);
}
protected Map readConfigFile(String filePath)
throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String nextLine = null;
Map configParams = new HashMap();
while ((nextLine = reader.readLine()) != null)
{
nextLine = nextLine.trim();
// ignore comments and blank lines
if (nextLine.length() == 0 ||
nextLine.charAt(0) == '#' ||
nextLine.charAt(0) == '<')
continue;
int space = nextLine.indexOf(' ');
String name = nextLine.substring(0, space);
String value = nextLine.substring(space + 1);
configParams.put(name, value);
}
return configParams;
}
|
The first method overrides the base class's initialize method, which is where capabilities can perform all of their self-contained startup tasks; this method simply reads the name-value pairs in the httpd.conf file into a java.util.Map and then sets the class's fields using the values it finds. The initialize method is called before any SOAP requests are serviced by the Muse application.
The second method performs the actual task of parsing the file into java.util.Map, taking care to remove comments and blank lines from the input. Finally, the getter methods should remain exactly as they are -- they simply return the values that were read in from httpd.conf when called by Muse's GetResourceProperty implementation.





