View Source

h2. Global plugin setting
Plugin might have settings global to the system. For example, the [Ant|http://ant.apache.org] plugin defines path to Ant executable in the global plugin setting. In this tutorial, we will continue to evolve myplugin to define a message prefix in the plugin setting so that all user specified messages are automatically prefixed with certain string.
First let's define the plugin setting class _MyPluginSetting.java_:
{code}
package com.example.myplugin;

import org.hibernate.validator.NotEmpty;
import com.pmease.quickbuild.annotation.Editable;

// Plugin setting class must contains a default constructor
public class MyPluginSetting {

private String prefix;

@Editable(description="Specify a message prefix here.")
@NotEmpty
public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

}
{code}
And then tell QuickBuild about this class by modifying _MyPlugin.java_ as below:
{code}
... // omitted for brevity

public class MyPlugin extends AbstractPlugin {

@Override
public Object[] getExtensions() {
... // omitted for brevity
}

@Override
public Class<?> getSettingClass() {
return MyPluginSetting.class;
}

}
{code}

Finally we modify _MyAnotherStep.java_ to make use of the prefix:
{code}
... // omitted for brevity
public class MyAnotherStep extends Step {

... // omitted for brevity

@Override
public void run() {
// You may also get the plugin setting object through PluginSettingHelper
MyPluginSetting pluginSetting = (MyPluginSetting) getPlugin().getSetting(true);
Context.getLogger().info(pluginSetting.getPrefix() + ": " + getMessage());
}

}
{code}
Now start QuickBuild and it prints a warning message complaining that plugin _com.example.myplugin_ needs to be configured. The plugin will temporarily be disabled if stored plugin setting object vilolates constraints of the plugin class. For newly installed plugins, the plugin setting object is initially created by calling default constructor of the plugin setting class.
Run the root configuration, and you will see that all messages will start with the specified prefix.
{info}Refer to [bean editor] for more information of how to annotate plugin setting class to generate appropriate UI.{info}

h2. Configuration level plugin setting
Configuration level plugin setting is defined at configuration level. The setting will be inherited from parent configuration if it is not defined. Continue with myplugin, now assume that we need to specify message prefix per configuration, we can then modify _MyPlugin.java_ as below:
{code}
... // omitted for brevity
public class MyPlugin extends AbstractPlugin {

@Override
public Object[] getExtensions() {
... // omitted for brevity
}

/*
@Override
public Class<?> getSettingClass() {
return MyPluginSetting.class;
}
*/

@Override
public Class<?> getConfigurationSettingClass() {
return MyPluginSetting.class;
}

}
{code}
Also please modify _MyPluginSetting.java_ to provide a description:
{code}
... // omitted for brevity
@Editable(description="Specify plugin setting for myplugin.")
public class MyPluginSetting {
... // omitted for brevity
}
{code}
Finally modify _MyAnotherStep.java_ to make use of the configuration level prefix:
{code}
... // omitted for brevity
public class MyAnotherStep extends Step {

... // omitted for brevity

@Override
public void run() {
// You may also get the plugin setting object through PluginSettingHelper
MyPluginSetting pluginSetting = (MyPluginSetting) getPlugin().getConfigurationSetting(Context.getConfiguration(), true);
Context.getLogger().info(pluginSetting.getPrefix() + ": " + getMessage());
}

}
{code}
Save above classes, start QuickBuild, and edit advanced setting of the root configuration. You will see that the advanced setting of the configuration includes a section contributed by myplugin as below:
!plugin-configuration-setting.png!
All child configurations will by default inherit myplugin setting and can choose to override it by enabling myplugin setting.

Run the root configuration, and you will see that all messages will start with the prefix specified at root configuration level.
{info}Refer to [bean editor] for more information of how to annotate plugin configuration setting class to generate appropriate UI.{info}

h2. User level plugin setting
Now we change the plugin setting from configuration level to user level: users define their own prefixes in the profile and printed message will start with the user specific prefix. To do so, first modify _MyPlugin.java_ as below:
{code}
... // omitted for brevity
public class MyPlugin extends AbstractPlugin {

@Override
public Object[] getExtensions() {
... // omitted for brevity
}

/*
@Override
public Class<?> getSettingClass() {
return MyPluginSetting.class;
}

@Override
public Class<?> getConfigurationSettingClass() {
return MyPluginSetting.class;
}
*/

@Override
public Class<?> getUserSettingClass() {
return MyPluginSetting.class;
}

}
{code}
To make use of the user level prefix, please modify _MyAnotherStep.java_ as below:
{code}
... // omitted for brevity
public class MyAnotherStep extends Step {

... // omitted for brevity

@Override
public void run() {
if (Context.getUser() != null) {
// You may also get the plugin setting object through PluginSettingHelper
MyPluginSetting pluginSetting = (MyPluginSetting) getPlugin().getUserSetting(Context.getUser(), true);
Context.getLogger().info(pluginSetting.getPrefix() + ": " + getMessage());
}
}

}
{code}
Save above modifications, start QuickBuild, and switch to my setting page. It will include a section contributed by myplugin as below:
!plugin-user-setting.png!

Run the root configuration, and you will see that all messages will start with the prefix specified by current user.
{info}Refer to [bean editor] for more information of how to annotate plugin user setting class to generate appropriate UI.{info}

h2. Group level plugin setting
Group level plugin setting is very similar to user level plugin setting, except that:
# It is attached to each group and will be displayed and edited if you view or edit particular group.
# The setting class is specified by overriding method _getGroupSettingClass_
# The setting object can be retrieved by calling _PluginSettingHelper.getPluginSetting(<Plugin Main Class>, <group instance>)_

{info}Refer to [bean editor] for more information of how to annotate plugin group setting class to generate appropriate UI.{info}

h2. Combine different levels of plugin settings
Different levels of plugin settings can be combined together. The plugin _com.pmease.quickbuild.plugin.notifier.msn_ in [plugin examples] demonstrates this:
* It includes a global plugin setting class _MsnSetting.java_ to define MSN account used to login and send out build notifications through MSN messenger.
* It includes an user level plugin setting class _MsnUserSetting.java_ to define MSN account for each user, which will be used to receive build notifications sent out by QuickBuild server through MSN messenger.