001/*
002 * (C) Copyright 2006-2016 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.files;
017
018import java.io.File;
019
020import org.nuxeo.runtime.api.Framework;
021import org.nuxeo.runtime.services.event.Event;
022import org.nuxeo.runtime.services.event.EventService;
023
024/**
025 * Runtime events about transient files which should be deleted once the runtime leave the thread (
026 * {@link FileEventTracker}).
027 * <p>
028 * Producers should use the static {@link FileEvent#onFile(Object, File, Object)} factory method and fire events by
029 * invoking the event's {@link FileEvent#send()} method:
030 *
031 * <pre>
032 * FileEvent.onFile(source, aFile, aMarker).send();
033 * </pre>
034 * <p>
035 * Consumers should implements the {@link FileEventHandler} interface and register in the {@link EventService} using the
036 * {@link FileEventListener} wrapper:
037 *
038 * <pre>
039 * FileEventListener filesListener = new FileEventListener(new FileEventHandler() {
040 *     &#064;Override
041 *     public void onFile(File file, Object marker) {
042 *         ...
043 *     }
044 * });
045 * ...
046 * filesListener.install();
047 * ...
048 * filesListener.uninstall();
049 * </pre>
050 *
051 * @author Stephane Lacoin at Nuxeo (aka matic)
052 * @since 6.0
053 */
054public class FileEvent extends Event {
055
056    protected FileEvent(Object source, File aFile, Object aMarker) {
057        super(FileEvent.class.getName(), FileEvent.class.getName(), source, new Object[] { aFile, aMarker });
058    }
059
060    public static void listen(FileEventListener aListener) {
061        Framework.getService(EventService.class).addListener(FileEvent.class.getName(), aListener);
062    }
063
064    public static void ignore(FileEventListener aListener) {
065        Framework.getService(EventService.class).removeListener(FileEvent.class.getName(), aListener);
066    }
067
068    public void send() {
069        Framework.getService(EventService.class).sendEvent(this);
070    }
071
072    public void handle(FileEventHandler handler) {
073        handler.onFile(getFile(), getMarker());
074    }
075
076    protected File getFile() {
077        return (File) ((Object[]) getData())[0];
078    }
079
080    protected Object getMarker() {
081        return ((Object[]) getData())[1];
082    }
083
084    public static FileEvent onFile(Object source, File aFile, Object aMarker) {
085        return new FileEvent(source, aFile, aMarker);
086    }
087}