/*
 * Copyright 2016 Donald W - github@donaldw.com
 * Copyright 2011 Karl Pauls karlpauls@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.mypojo.framework.revision;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarRevision implements Revision {
    private static final Logger logger = LoggerFactory.getLogger(JarRevision.class);

    private final long m_lastModified;
    private final JarFile m_jar;
    private final URL m_url;
    private final String m_urlString;
    private final String m_prefix;

    public JarRevision(JarFile jar, URL url, String prefix, long lastModified) {
        m_jar = jar;
        m_url = url;
        m_urlString = m_url.toExternalForm();
        m_prefix = prefix;
        if (lastModified > 0) {
            m_lastModified = lastModified;
        } else {
            m_lastModified = System.currentTimeMillis();
        }
    }

    @Override
    public long getLastModified() {
        return m_lastModified;
    }

    public Enumeration<String> getEntries() {
        return new EntriesEnumeration(m_jar.entries(), m_prefix);
    }

    @Override
    public URL getEntry(String entryName) {
        try {
            if ("/".equals(entryName) || "".equals(entryName) || " ".equals(entryName)) {
                return new URL("jar:" + m_urlString + "!/" + ((m_prefix == null) ? "" : m_prefix));
            }
            if (entryName != null) {
                final String target = ((entryName.startsWith("/")) ? entryName.substring(1) : entryName);
                final JarEntry entry = m_jar.getJarEntry(((m_prefix == null) ? "" : m_prefix) + target);
                if (entry != null) {
                    URL result = new URL(null, "jar:" + m_urlString + "!/" + ((m_prefix == null) ? "" : m_prefix) + target, new URLStreamHandler() {


                        protected URLConnection openConnection(final URL u) throws IOException {
                            return new java.net.JarURLConnection(u) {

                                public JarFile getJarFile() {
                                    return m_jar;
                                }

                                public void connect() throws IOException {
                                    // TODO Auto-generated method stub

                                }

                                public InputStream getInputStream()
                                        throws IOException {

                                    String extF = u.toExternalForm();
                                    JarEntry targetEntry = entry;
                                    if (!extF.endsWith(target)) {
                                        extF = extF.substring(extF.indexOf('!') + 2);
                                        if (m_prefix != null) {
                                            if (!extF.startsWith(m_prefix)) {
                                                extF = m_prefix + extF;
                                            }
                                        }
                                        targetEntry = m_jar.getJarEntry(extF);
                                    }
                                    return m_jar.getInputStream(targetEntry);
                                }
                            };
                        }
                    });
                    return result;
                } else {
                    if (entryName.endsWith("/")) {
                        return new URL("jar:" + m_urlString + "!/" + ((m_prefix == null) ? "" : m_prefix) + target);
                    }
                }
            }
        } catch (IOException e) {
            logger.error("Whilst accessing: " + entryName, e);
        }
        return null;
    }
}

@@ -1192,24 +1192,26 @@
  m_jar;%0A    
+//
 private fina
@@ -1403,24 +1403,26 @@
 ar;%0A        
+//
 m_url = url;
@@ -1444,18 +1444,16 @@
 tring = 
-m_
 url.toEx
@@ -3039,17 +3039,16 @@
 od stub%0A
-%0A
         

Remove unused field