4.5. Defining new observers

Suppose you are implementing a mail module with support for virtual domains, and you want to let other modules know when a virtual domain is deleted. You could define an abstract class called EBox::MailObserver, like this:

Example 4.4. Defining an observer that may be implemented by other modules

package EBox::MailObserver;

use strict;
use warnings;

sub new
{
	my $class = shift;
	my $self = {};
	bless($self, $class);
	return $self;
}

# When a virtual domain is deleted this method is called
sub virtualDomainDeleted # (domainName)
{}

When a relevant event takes place, your mail module would call the virtualDomainDeleted function on all the modules that extend the EBox::MailObserver class:

Example 4.5. Calling observer modules

sub removeVirtualDomain # (domainName)
{
	my ($self, $domain) = @_;

	#
	# do stuff
	#

	my $global = EBox::Global->getInstance();
	my @modules = @{$global->modInstancesOfType('EBox::MailObserver')};
	foreach my $mod (@modules) {
		$mod->virtualDomainDeleted($domain);
	}
}