001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.nuxeo.runtime.trackers.concurrent;
017
018import org.nuxeo.runtime.api.Framework;
019import org.nuxeo.runtime.services.event.Event;
020import org.nuxeo.runtime.services.event.EventService;
021import org.nuxeo.runtime.trackers.files.FileEventTracker;
022
023/**
024 * Runtime events that be fired once a thread is about to enter in the nuxeo runtime or leave it. Could be used for
025 * cleaning resource such as the {@link FileEventTracker}. Producers should use the static
026 * {@link ThreadEvent#onEnter(Object, boolean)} and {@link ThreadEvent#onLeave(Object)} factory methods and fire events
027 * by invoking the event's {@link ThreadEvent#send()} method. Consumers should implements the {@link ThreadEventHandler}
028 * interface and register it in the {@link EventService} using the {@link ThreadEventListener} wrapper.
029 *
030 * @since 6.0
031 * @author Stephane Lacoin at Nuxeo (aka matic)
032 */
033public abstract class ThreadEvent extends Event {
034
035    public ThreadEvent(Class<? extends ThreadEvent> type, Object source, Object data) {
036        super(ThreadEvent.class.getName(), type.getSimpleName(), source, data);
037    }
038
039    protected static class EnterEvent extends ThreadEvent {
040
041        public EnterEvent(Object source, boolean isLongRunning) {
042            super(EnterEvent.class, source, isLongRunning);
043        }
044
045        public boolean isLongRunning() {
046            return (Boolean) getData();
047        }
048
049        @Override
050        public void handle(ThreadEventHandler handler) {
051            handler.onEnter((Boolean) getData());
052        }
053    }
054
055    protected static class LeaveEvent extends ThreadEvent {
056        public LeaveEvent(Object source) {
057            super(LeaveEvent.class, source, null);
058        }
059
060        @Override
061        public void handle(ThreadEventHandler handler) {
062            handler.onLeave();
063        }
064    }
065
066    public abstract void handle(ThreadEventHandler handler);
067
068    public void send() {
069        Framework.getService(EventService.class).sendEvent(this);
070    }
071
072    public static ThreadEvent onEnter(Object source, boolean isLongRunning) {
073        return new EnterEvent(source, isLongRunning);
074    }
075
076    public static ThreadEvent onLeave(Object source) {
077        return new LeaveEvent(source);
078    }
079
080    public static void listen(ThreadEventListener aListener) {
081        Framework.getService(EventService.class).addListener(ThreadEvent.class.getName(), aListener);
082    }
083
084    public static void ignore(ThreadEventListener aListener) {
085        Framework.getService(EventService.class).removeListener(ThreadEvent.class.getName(), aListener);
086    }
087}