11.2.4. User Membership event handlers

Interface ServiceEventListener declares 5 event handlers associated with User Membership. Three of them have a parameter of type MembershipUserEvent:

void onUserIncluded(MembershipUserEvent e)
void onUserUpdated(MembershipUserEvent e)
void onUserRemoved(MembershipUserEvent e)

They are invoked when a management action taken by the owner on users has affected only one membership user, excluding Guest and Stateless Guest. Parameter ‘e’ contains the affected membership user in the user field. The following example demonstrates this idea:

public void onUserUpdated(MembershipUserEvent e)
{
    System.out.println(String.format("User updated -----------------"));
    System.out.println(String.format("ID: %d", e.user.getId()));
    System.out.println(String.format("Name: %s", e.user.getName()));
    if(e.user.hasRoles()) {
        System.out.println("User roles:");
        for(String role: e.user.getRoles()) 
            System.out.println("   " + role);
    }
}

If the management action could have affected two or more membership users, except for Guest and Stateless Guest, the following handler is invoked:

void onUsersUpdated(ServiceEndpointEvent e)

This handler has a parameter of type ServiceEndpointEvent that does not convey any information about affected users. The application can only get an updated list of users from the endpoint, as shown in the following example:

public void onUsersUpdated(ServiceEndpointEvent e)
{
    ServiceEndpoint serviceEndpoint = e.getEndpoint();
    MembershipUser[] users = serviceEndpoint.getUsers();
    System.out.println(String.format("User list:"));    
    for(MembershipUser user: users) {
        System.out.println(user.getName());
        for(String role: user.getRoles()) 
            System.out.println("  " + role);
    }
}

We have one handler left to consider. It is invoked when the Guest status, allowed/denied, has changed:

void onGuestAccessChanged(ServiceEndpointEvent e)

As usual, to get the updated Guest status do the following. In the handler’s body, call the getEndpoint method on parameter ‘e’. It returns the endpoint that invoked the handler. Then call the endpoint’s isGuestAllowed method. It has the following signature:

public boolean isGuestAllowed()

The example below demonstrates these steps:

public void onGuestAccessChanged(ServiceEndpointEvent e)
{					
    ServiceEndpoint serviceEndpoint = e.getEndpoint();
    if(serviceEndpoint.isGuestAllowed())
        System.out.println(String.format("Guest Allowed"));
    else
        System.out.println(String.format("Guest Denied"));	
}

TABLE OF CONTENTS