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; 018import java.net.URLConnection; 019 020import org.osgi.framework.Bundle; 021 022/** 023 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 024 */ 025public class URLScript extends Script { 026 027 protected final URL url; 028 029 protected URLConnection conn; 030 031 public URLScript(URL url) throws IOException { 032 this.url = url; 033 conn = url.openConnection(); 034 } 035 036 public URLScript(String location) throws IOException { 037 this(new URL(location)); 038 } 039 040 public URLScript(Bundle bundle, String path) throws IOException { 041 this(bundle.getEntry(path)); 042 } 043 044 @Override 045 public String getExtension() { 046 return getExtension(url.getPath()); 047 } 048 049 @Override 050 public String getLocation() { 051 return url.toExternalForm(); 052 } 053 054 @Override 055 public Reader getReaderIfModified() throws IOException { 056 URLConnection conn = url.openConnection(); 057 long tm = conn.getLastModified(); 058 if (tm > lastModified) { 059 synchronized (this) { 060 if (tm > lastModified) { 061 lastModified = tm; 062 return new InputStreamReader(conn.getInputStream()); 063 } 064 } 065 } 066 return null; 067 } 068 069 @Override 070 public Reader getReader() throws IOException { 071 return new InputStreamReader(conn.getInputStream()); 072 } 073 074}