Search in sources :

Example 1 with UserAdminEvent

use of org.osgi.service.useradmin.UserAdminEvent in project felix by apache.

the class EventDispatcher method convertEvent.

/**
 * Converts a given {@link UserAdminEvent} to a {@link Event} that can be
 * dispatched through the {@link EventAdmin} service.
 *
 * @param event
 *            the event to convert, cannot be <code>null</code>.
 * @return a new {@link Event} instance containing the same set of
 *         information as the given event, never <code>null</code>.
 */
private Event convertEvent(UserAdminEvent event) {
    String topic = getTopicName(event.getType());
    Role role = event.getRole();
    ServiceReference serviceRef = event.getServiceReference();
    Properties props = new Properties();
    props.put(EventConstants.EVENT_TOPIC, TOPIC_BASE.concat(topic));
    props.put(EventConstants.EVENT, event);
    props.put("role", role);
    props.put("role.name", role.getName());
    props.put("role.type", new Integer(role.getType()));
    if (serviceRef != null) {
        props.put(EventConstants.SERVICE, serviceRef);
        Object property;
        property = serviceRef.getProperty(Constants.SERVICE_ID);
        if (property != null) {
            props.put(EventConstants.SERVICE_ID, property);
        }
        property = serviceRef.getProperty(Constants.OBJECTCLASS);
        if (property != null) {
            props.put(EventConstants.SERVICE_OBJECTCLASS, property);
        }
        property = serviceRef.getProperty(Constants.SERVICE_PID);
        if (property != null) {
            props.put(EventConstants.SERVICE_PID, property);
        }
    }
    return new Event(topic, props);
}
Also used : Role(org.osgi.service.useradmin.Role) UserAdminEvent(org.osgi.service.useradmin.UserAdminEvent) Event(org.osgi.service.event.Event) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with UserAdminEvent

use of org.osgi.service.useradmin.UserAdminEvent in project felix by apache.

the class EventDispatcherTest method createMockEvent.

/**
 * @param type
 * @return
 */
private UserAdminEvent createMockEvent(int type) {
    Role user = RoleFactory.createUser("user-" + System.currentTimeMillis());
    ServiceReference ref = new FakeServiceReference();
    return new UserAdminEvent(ref, type, user);
}
Also used : Role(org.osgi.service.useradmin.Role) UserAdminEvent(org.osgi.service.useradmin.UserAdminEvent) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with UserAdminEvent

use of org.osgi.service.useradmin.UserAdminEvent in project felix by apache.

the class UserAdminEventToEventAdminBridge method roleChanged.

public void roleChanged(final UserAdminEvent event) {
    final ServiceReference ref = m_context.getServiceReference(EventAdmin.class.getName());
    if (null != ref) {
        final EventAdmin eventAdmin = (EventAdmin) m_context.getService(ref);
        if (null != eventAdmin) {
            final String topic;
            switch(event.getType()) {
                case UserAdminEvent.ROLE_CHANGED:
                    topic = "org/osgi/service/useradmin/UserAdmin/ROLE_CHANGED";
                    break;
                case UserAdminEvent.ROLE_CREATED:
                    topic = "org/osgi/service/useradmin/UserAdmin/ROLE_CREATED";
                    break;
                case UserAdminEvent.ROLE_REMOVED:
                    topic = "org/osgi/service/useradmin/UserAdmin/ROLE_REMOVED";
                    break;
                default:
                    m_context.ungetService(ref);
                    return;
            }
            final Hashtable properties = new Hashtable();
            properties.put(EventConstants.EVENT, event);
            properties.put("role", event.getRole());
            properties.put("role.name", event.getRole().getName());
            properties.put("role.type", new Integer(event.getRole().getType()));
            final ServiceReference eventRef = event.getServiceReference();
            if (null == eventRef) {
                throw new IllegalArgumentException("UserAdminEvent.getServiceReference() may not be null");
            }
            properties.put(EventConstants.SERVICE, eventRef);
            properties.put(EventConstants.SERVICE_ID, eventRef.getProperty(EventConstants.SERVICE_ID));
            final Object objectClass = eventRef.getProperty(Constants.OBJECTCLASS);
            if (!(objectClass instanceof String[]) || !Arrays.asList((String[]) objectClass).contains(UserAdmin.class.getName())) {
                throw new IllegalArgumentException("Bad objectclass: " + objectClass);
            }
            properties.put(EventConstants.SERVICE_OBJECTCLASS, objectClass);
            properties.put(EventConstants.SERVICE_PID, eventRef.getProperty(EventConstants.SERVICE_PID));
            eventAdmin.postEvent(new Event(topic, properties));
            m_context.ungetService(ref);
        }
    }
}
Also used : UserAdmin(org.osgi.service.useradmin.UserAdmin) EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) UserAdminEvent(org.osgi.service.useradmin.UserAdminEvent) Event(org.osgi.service.event.Event) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)3 UserAdminEvent (org.osgi.service.useradmin.UserAdminEvent)3 Event (org.osgi.service.event.Event)2 Role (org.osgi.service.useradmin.Role)2 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1 EventAdmin (org.osgi.service.event.EventAdmin)1 UserAdmin (org.osgi.service.useradmin.UserAdmin)1