2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.resource;
22 import java.io.FileOutputStream;
23 import java.io.FilterInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.net.JarURLConnection;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarInputStream;
31 import java.util.jar.Manifest;
33 import org.eclipse.jetty.util.IO;
34 import org.eclipse.jetty.util.URIUtil;
35 import org.eclipse.jetty.util.log.Log;
36 import org.eclipse.jetty.util.log.Logger;
39 /* ------------------------------------------------------------ */
40 public class JarResource extends URLResource
42 private static final Logger LOG = Log.getLogger(JarResource.class);
43 protected JarURLConnection _jarConnection;
45 /* -------------------------------------------------------- */
46 protected JarResource(URL url)
51 /* ------------------------------------------------------------ */
52 protected JarResource(URL url, boolean useCaches)
54 super(url, null, useCaches);
57 /* ------------------------------------------------------------ */
59 public synchronized void close()
65 /* ------------------------------------------------------------ */
67 protected synchronized boolean checkConnection()
69 super.checkConnection();
72 if (_jarConnection!=_connection)
81 return _jarConnection!=null;
84 /* ------------------------------------------------------------ */
86 * @throws IOException Sub-classes of <code>JarResource</code> may throw an IOException (or subclass)
88 protected void newConnection() throws IOException
90 _jarConnection=(JarURLConnection)_connection;
93 /* ------------------------------------------------------------ */
95 * Returns true if the respresenetd resource exists.
98 public boolean exists()
100 if (_urlString.endsWith("!/"))
101 return checkConnection();
103 return super.exists();
106 /* ------------------------------------------------------------ */
108 public File getFile()
114 /* ------------------------------------------------------------ */
116 public InputStream getInputStream()
117 throws java.io.IOException
120 if (!_urlString.endsWith("!/"))
121 return new FilterInputStream(super.getInputStream())
124 public void close() throws IOException {this.in=IO.getClosedStream();}
127 URL url = new URL(_urlString.substring(4,_urlString.length()-2));
128 InputStream is = url.openStream();
132 /* ------------------------------------------------------------ */
134 public void copyTo(File directory)
140 if(LOG.isDebugEnabled())
141 LOG.debug("Extract "+this+" to "+directory);
143 String urlString = this.getURL().toExternalForm().trim();
144 int endOfJarUrl = urlString.indexOf("!/");
145 int startOfJarUrl = (endOfJarUrl >= 0?4:0);
148 throw new IOException("Not a valid jar url: "+urlString);
150 URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
151 String subEntryName = (endOfJarUrl+2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
152 boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/")?true:false);
154 if (LOG.isDebugEnabled())
155 LOG.debug("Extracting entry = "+subEntryName+" from jar "+jarFileURL);
157 try (InputStream is = jarFileURL.openConnection().getInputStream();
158 JarInputStream jin = new JarInputStream(is))
161 boolean shouldExtract;
162 while((entry=jin.getNextJarEntry())!=null)
164 String entryName = entry.getName();
165 if ((subEntryName != null) && (entryName.startsWith(subEntryName)))
167 // is the subentry really a dir?
168 if (!subEntryIsDir && subEntryName.length()+1==entryName.length() && entryName.endsWith("/"))
171 //if there is a particular subEntry that we are looking for, only
175 //if it is a subdirectory we are looking for, then we
176 //are looking to extract its contents into the target
177 //directory. Remove the name of the subdirectory so
178 //that we don't wind up creating it too.
179 entryName = entryName.substring(subEntryName.length());
180 if (!entryName.equals(""))
183 shouldExtract = true;
186 shouldExtract = false;
189 shouldExtract = true;
191 else if ((subEntryName != null) && (!entryName.startsWith(subEntryName)))
193 //there is a particular entry we are looking for, and this one
195 shouldExtract = false;
199 //we are extracting everything
200 shouldExtract = true;
205 if (LOG.isDebugEnabled())
206 LOG.debug("Skipping entry: "+entryName);
210 String dotCheck = entryName.replace('\\', '/');
211 dotCheck = URIUtil.canonicalPath(dotCheck);
212 if (dotCheck == null)
214 if (LOG.isDebugEnabled())
215 LOG.debug("Invalid entry: "+entryName);
219 File file=new File(directory,entryName);
221 if (entry.isDirectory())
229 // make directory (some jars don't list dirs)
230 File dir = new File(file.getParent());
235 try (OutputStream fout = new FileOutputStream(file))
241 if (entry.getTime()>=0)
242 file.setLastModified(entry.getTime());
246 if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF")))
248 Manifest manifest = jin.getManifest();
249 if (manifest != null)
251 File metaInf = new File (directory, "META-INF");
253 File f = new File(metaInf, "MANIFEST.MF");
254 try (OutputStream fout = new FileOutputStream(f))
256 manifest.write(fout);
263 public static Resource newJarResource(Resource resource) throws IOException
265 if (resource instanceof JarResource)
267 return Resource.newResource("jar:" + resource + "!/");