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