001/*******************************************************************************
002 * Copyright (c) 2006-2014 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 ******************************************************************************/
009package org.nuxeo.runtime.trackers.concurrent;
010
011import org.nuxeo.runtime.services.event.Event;
012import org.nuxeo.runtime.services.event.EventListener;
013import org.nuxeo.runtime.services.event.EventService;
014
015/**
016 * Wrap a {@link ThreadEventHandler} for being enlisted in the {@link EventService}.
017 *
018 * @since 6.0
019 * @author Stephane Lacoin at Nuxeo (aka matic)
020 */
021public class ThreadEventListener implements EventListener {
022
023    protected final ThreadEventHandler handler;
024
025    protected boolean installed;
026
027    public ThreadEventListener(ThreadEventHandler anHandler) {
028        handler = anHandler;
029    }
030
031    @Override
032    public boolean aboutToHandleEvent(Event event) {
033        return true;
034    }
035
036    @Override
037    public void handleEvent(Event anEvent) {
038        ((ThreadEvent) anEvent).handle(handler);
039    }
040
041    public boolean isInstalled() {
042        return installed;
043    }
044
045    public boolean uninstall() {
046        if (!installed) {
047            return false;
048        }
049        ThreadEvent.ignore(this);
050        return true;
051    }
052
053    public boolean install() {
054        if (installed) {
055            return false;
056        }
057        ThreadEvent.listen(this);
058        return true;
059    }
060
061}