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