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 *     bstefanescu
011 */
012package org.nuxeo.ecm.core.event.script;
013
014import java.io.File;
015import java.io.FileReader;
016import java.io.IOException;
017import java.io.Reader;
018
019/**
020 * Script that comes from a file.
021 *
022 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
023 */
024public class FileScript extends Script {
025
026    protected final File file;
027
028    public FileScript(File file) {
029        this.file = file;
030    }
031
032    public FileScript(String path) {
033        this(new File(path));
034    }
035
036    @Override
037    public String getExtension() {
038        return getExtension(file.getPath());
039    }
040
041    @Override
042    public String getLocation() {
043        return file.getAbsolutePath();
044    }
045
046    @Override
047    public Reader getReader() throws IOException {
048        return new FileReader(file);
049    }
050
051    @Override
052    public Reader getReaderIfModified() throws IOException {
053        long tm = file.lastModified();
054        if (tm > lastModified) {
055            synchronized (this) {
056                if (tm > lastModified) {
057                    lastModified = tm;
058                    return new FileReader(file);
059                }
060            }
061        }
062        return null;
063    }
064
065}