/*
 * $Id: JarConfigFile.java,v 1.7 2010-02-24 02:23:59 tlipkis Exp $
 */

/*

Copyright (c) 2000-2005 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.config;

import java.io.*;
import java.net.*;
import java.util.jar.*;

/**
 * A ConfigFile loaded from an entry in a JAR file.
 *
 * JAR Urls should look something like:
 *    jar:file:///path/to/jar!/some/resource
 */
public class JarConfigFile extends BaseConfigFile {
  private File m_jarFile;
//   private String m_entryTime;

  public JarConfigFile(String url) {
    super(url);
  }

  protected InputStream openInputStream() throws IOException {
    String lm = calcNewLastModified();

    // Only reload the file if the last modified timestamp is different.
    if (lm.equals(m_lastModified)) {
      log.debug2("Jar has not changed on disk, not reloading: " + m_fileUrl);
      return null;
    }
    log.debug2("Loading JAR config file: " + m_fileUrl);
    URL jarUrl = new URL(m_fileUrl);
    final JarURLConnection con = (JarURLConnection)jarUrl.openConnection();
    // Disable JarURLConnection's caching of JarFiles, as it runs afoul of
    // the auto-close wrapper below.
    con.setUseCaches(false);
    JarEntry entry = con.getJarEntry();
    if (m_jarFile == null) {
      JarFile jf = con.getJarFile();
      String name = jf.getName();
      log.debug3("jf.name: " + name);
      m_jarFile = new File(name);
    }
//     m_entryTime = Long.toString(entry.getTime());
    return new FilterInputStream(con.getInputStream()) {
    	public void close() throws IOException
    	{
    		super.close();
    		// close connection JAR file to unlock file on Windows
    		con.getJarFile().close();
    	}
    };
  }

  protected String calcNewLastModified() {
    if (m_jarFile != null) {
      return Long.toString(m_jarFile.lastModified());
    }
    return "Never";
  }

  File getFile() {
    return m_jarFile;
  }
}

@@ -31,9 +31,9 @@
 v 1.
-7
+8
  201
@@ -44,24 +44,22 @@
 -24 
-02:
 23:5
-9 tlipkis
+7:49 pgust
  Exp
@@ -1741,41 +1741,8 @@
 ile;
-%0A//   private String m_entryTime;
 %0A%0A  
@@ -2294,181 +2294,176 @@
  // 
-Disable JarURLConnection's caching of JarFiles, as it runs afoul of%0A    // the auto-close wrapper below.%0A    con.setUseCaches(false);%0A    JarEntry entry = con.getJarEntry();
+important: must disable caching to release file resource on close,%0A    // and force changed content to be reloaded from the file each time%0A    con.setUseCaches(false);%0A
 %0A   
@@ -2641,295 +2641,39 @@
   %7D%0A
-//     m_entryTime = Long.toString(entry.getTime());%0A    return new FilterInputStream(con.getInputStream()) %7B%0A    %09public void close() throws IOException%0A    %09%7B%0A    %09%09super.close();%0A    %09%09// close connection JAR file to unlock file on Windows%0A    %09%09con.getJarFile().close();%0A    %09%7D%0A    %7D
+    return con.getInputStream()
 ;%0A  

Return connection InputStream directly instead of wrapping in in a FilterInputStream. Turning off caching on the connection eliminated the need for special handling when the connection InputStream is closed.