001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.runtime.services.event;
016
017import java.util.Arrays;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.common.xmap.annotation.XNode;
022import org.nuxeo.common.xmap.annotation.XNodeList;
023import org.nuxeo.common.xmap.annotation.XObject;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028@XObject("listener")
029public class ListenerDescriptor {
030
031    private static final NullListener NULL_LISTENER = new NullListener();
032
033    private static final Log log = LogFactory.getLog(ListenerDescriptor.class);
034
035    @XNodeList(value = "topic", type = String[].class, componentType = String.class)
036    String[] topics;
037
038    EventListener listener;
039
040    @XNode("@class")
041    public void setListener(Class<EventListener> listenerClass) {
042        try {
043            listener = listenerClass.newInstance();
044        } catch (ReflectiveOperationException e) {
045            log.error(e);
046            listener = NULL_LISTENER;
047        }
048    }
049
050    @Override
051    public String toString() {
052        return listener + " { " + Arrays.toString(topics) + " }";
053    }
054
055}
056
057class NullListener implements EventListener {
058
059    @Override
060    public boolean aboutToHandleEvent(Event event) {
061        return false;
062    }
063
064    @Override
065    public void handleEvent(Event event) {
066    }
067
068}