In this exercise, you will create an administration service to allow the configuration of your application. You will also create your first small autonomic manager that will automatically configure this service.
Question 1 – Providing a configuration service: In the getting started section, we explain how to provide a service. You will now provide a service for configuring your application.
The FollowMeConfiguration service interface is really simple :
package org.example.follow.me.configuration; /** * The FollowMeConfiguration service allows one to configure the Follow Me * application. */ public interface FollowMeConfiguration { /** * Gets the maximum number of lights to turn on each time an user is * entering a room. * * @return the maximum number of lights to turn on */ public int getMaximumNumberOfLightsToTurnOn(); /** * Sets the maximum number of lights to turn on each time an user is * entering a room. * * @param maximumNumberOfLightsToTurnOn * the new maximum number of lights to turn on */ public void setMaximumNumberOfLightsToTurnOn(int maximumNumberOfLightsToTurnOn); }
Implement the FollowMeConfiguration interface into your main application class.
Provide this interface as a service (follow the instructions given in the getting started section here).
Export the package org.example.follow.me.configuration as explained in the using multiple bundles tutorial.
Question 2 – Implementing a small manager: You will create a very basic manager to adjust the number of lights to turn on in the rooms based on the administrator’s decisions.
Your manager will understand (more) “high level goals” such as “High Illuminance”, “Medium Illuminance”, “Low Illuminance” and configure the number of lights accordingly.
Create a new project “follow.me.manager” and add a main component FollowMeManager. The implementation class should be named FollowMeManagerImpl.java and put into the org.example.follow.me.manager.impl package.
Import the package org.example.follow.me.configuration as explained in the using multiple bundles tutorial.
Add the dependency to the FollowMeConfiguration configuration and write a manager so that the number of lights is adjusted depending on a targeted goal.
To begin to interpret these goals the configuration will be hand-coded and hardwired. You can use the following hand-coded values :
package org.example.follow.me.manager; /** * This enum describes the different illuminance goals associated with the * manager. */ public enum IlluminanceGoal { /** The goal associated with soft illuminance. */ SOFT(1), /** The goal associated with medium illuminance. */ MEDIUM(2), /** The goal associated with full illuminance. */ FULL(3); /** The number of lights to turn on. */ private int numberOfLightsToTurnOn; /** * Gets the number of lights to turn On. * * @return the number of lights to turn On. */ public int getNumberOfLightsToTurnOn() { return numberOfLightsToTurnOn; } /** * Instantiates a new illuminance goal. * * @param numberOfLightsToTurnOn * the number of lights to turn on. */ private IlluminanceGoal(int numberOfLightsToTurnOn) { this.numberOfLightsToTurnOn = numberOfLightsToTurnOn; } }
Question 3 – providing an administration interface for your manager: Your manager has to provide an administration interface to allow the administrator to express his or her goals. Once again, you will provide a service for that purpose.
The service interface will be :
package org.example.follow.me.manager; /** * The Interface FollowMeAdministration allows the administrator to configure * its preference regarding the management of the Follow Me application. */ public interface FollowMeAdministration { /** * Sets the illuminance preference. The manager will try to adjust the * illuminance in accordance with this goal. * * @param illuminanceGoal * the new illuminance preference */ public void setIlluminancePreference(IlluminanceGoal illuminanceGoal); /** * Get the current illuminance preference. * * @return the new illuminance preference */ public IlluminanceGoal getIlluminancePreference(); }
Your FollowMeManager class should implement this class and provide it as a service.
Question 4 – providing a command: Now that you can configure your manager, we propose that you build a command line so as to allow administrators to configure your manager.
Once again, you need to create a new component “Follow Me Command” and import and export the package “org.example.follow.me.manager”.
Commands are not currently supported by the IDE. You will need to use iPOJO annotations. Here is an example of command using iPOJO annotations and a provided specific handler for providing commands :
package org.example.follow.me.manager.command; import org.apache.felix.ipojo.annotations.Component; import org.apache.felix.ipojo.annotations.Instantiate; import org.apache.felix.ipojo.annotations.Requires; import org.example.follow.me.manager.FollowMeAdministration; import org.example.follow.me.manager.IlluminanceGoal; import fr.liglab.adele.icasa.command.handler.Command; import fr.liglab.adele.icasa.command.handler.CommandProvider; //Define this class as an implementation of a component : @Component //Create an instance of the component @Instantiate(name = "follow.me.mananger.command") //Use the handler command and declare the command as a command provider. The //namespace is used to prevent name collision. @CommandProvider(namespace = "followme") public class FollowMeManagerCommandImpl { // Declare a dependency to a FollowMeAdministration service @Requires private FollowMeAdministration m_administrationService; /** * Felix shell command implementation to sets the illuminance preference. * * @param goal the new illuminance preference ("SOFT", "MEDIUM", "FULL") */ // Each command should start with a @Command annotation @Command public void setIlluminancePreference(String goal) { // The targeted goal IlluminanceGoal illuminanceGoal; // TODO : Here you have to convert the goal string into an illuminance // goal and fail if the entry is not "SOFT", "MEDIUM" or "HIGH" //call the administration service to configure it : m_administrationService.setIlluminancePreference(illuminanceGoal); } @Command public void getIlluminancePreference(){ //TODO : implement the command that print the current value of the goal System.out.println("The illuminance goal is "); //... } }
As you may notice, the component and the dependency are directly declared in the code. There is no need to do it in the IDE.
Implement the two methods to achieve the conversion of the goal from String to IlluminanceGoal. The command can be then used directly in the Felix shell :
g! setIlluminancePreference MEDIUM
Create a new command getIlluminancePreference :
g! getIlluminancePreference The illuminance goal is MEDIUM.
Question 5 – test: Using the above command, check that your manager is working.