You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2016/05/04 10:33:04 UTC
svn commit: r1742246 - in /tomcat/trunk/java/org/apache/tomcat/util/scan:
AbstractInputStreamJar.java JarFileUrlNestedJar.java UrlJar.java
Author: markt
Date: Wed May 4 10:33:04 2016
New Revision: 1742246
URL: http://svn.apache.org/viewvc?rev=1742246&view=rev
Log:
Refactor to reduce code duplication
Added:
tomcat/trunk/java/org/apache/tomcat/util/scan/AbstractInputStreamJar.java (with props)
Modified:
tomcat/trunk/java/org/apache/tomcat/util/scan/JarFileUrlNestedJar.java
tomcat/trunk/java/org/apache/tomcat/util/scan/UrlJar.java
Added: tomcat/trunk/java/org/apache/tomcat/util/scan/AbstractInputStreamJar.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/scan/AbstractInputStreamJar.java?rev=1742246&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/scan/AbstractInputStreamJar.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/util/scan/AbstractInputStreamJar.java Wed May 4 10:33:04 2016
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.tomcat.util.scan;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.jar.JarEntry;
+
+/**
+ * Base implementation of Jar for implementations that use a JarInputStream to
+ * access the JAR file.
+ */
+public abstract class AbstractInputStreamJar implements Jar {
+
+ private final URL jarFileURL;
+
+ private NonClosingJarInputStream jarInputStream = null;
+ private JarEntry entry = null;
+
+ public AbstractInputStreamJar(URL jarFileUrl) {
+ this.jarFileURL = jarFileUrl;
+ }
+
+
+ @Override
+ public URL getJarFileURL() {
+ return jarFileURL;
+ }
+
+
+ @Override
+ public void nextEntry() {
+ if (jarInputStream == null) {
+ try {
+ jarInputStream = createJarInputStream();
+ } catch (IOException e) {
+ entry = null;
+ return;
+ }
+ }
+ try {
+ entry = jarInputStream.getNextJarEntry();
+ } catch (IOException ioe) {
+ entry = null;
+ }
+ }
+
+
+ @Override
+ public String getEntryName() {
+ if (entry == null) {
+ return null;
+ } else {
+ return entry.getName();
+ }
+ }
+
+
+ @Override
+ public InputStream getEntryInputStream() throws IOException {
+ return jarInputStream;
+ }
+
+
+ @Override
+ public boolean entryExists(String name) throws IOException {
+ gotoEntry(name);
+ return entry != null;
+ }
+
+
+ @Override
+ public InputStream getInputStream(String name) throws IOException {
+ gotoEntry(name);
+ if (entry == null) {
+ return null;
+ } else {
+ return jarInputStream;
+ }
+ }
+
+
+ @Override
+ public long getLastModified(String name) throws IOException {
+ gotoEntry(name);
+ if (entry == null) {
+ return -1;
+ } else {
+ return entry.getTime();
+ }
+ }
+
+
+ @Override
+ public String getURL(String entry) {
+ StringBuilder result = new StringBuilder("jar:");
+ result.append(getJarFileURL().toExternalForm());
+ result.append("!/");
+ result.append(entry);
+
+ return result.toString();
+ }
+
+
+ @Override
+ public void reset() throws IOException {
+ closeStream();
+ entry = null;
+ jarInputStream = createJarInputStream();
+ }
+
+
+ protected void closeStream() {
+ if (jarInputStream != null) {
+ try {
+ jarInputStream.reallyClose();
+ } catch (IOException ioe) {
+ // Ignore
+ }
+ }
+ }
+
+
+ protected abstract NonClosingJarInputStream createJarInputStream() throws IOException;
+
+
+ private void gotoEntry(String name) throws IOException {
+ if (entry != null && name.equals(entry.getName())) {
+ return;
+ }
+ reset();
+ JarEntry jarEntry = jarInputStream.getNextJarEntry();
+ while (jarEntry != null) {
+ if (name.equals(jarEntry.getName())) {
+ entry = jarEntry;
+ break;
+ }
+ jarEntry = jarInputStream.getNextJarEntry();
+ }
+ }
+}
Propchange: tomcat/trunk/java/org/apache/tomcat/util/scan/AbstractInputStreamJar.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tomcat/trunk/java/org/apache/tomcat/util/scan/JarFileUrlNestedJar.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/scan/JarFileUrlNestedJar.java?rev=1742246&r1=1742245&r2=1742246&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/scan/JarFileUrlNestedJar.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/scan/JarFileUrlNestedJar.java Wed May 4 10:33:04 2016
@@ -17,28 +17,23 @@
package org.apache.tomcat.util.scan;
import java.io.IOException;
-import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
-
/**
* Implementation of {@link Jar} that is optimised for file based JAR URLs that
* refer to a JAR file nested inside a WAR
* (e.g URLs of the form jar:file: ... .war!/ ... .jar).
*/
-public class JarFileUrlNestedJar implements Jar {
+public class JarFileUrlNestedJar extends AbstractInputStreamJar {
- private final URL jarFileURL;
private final JarFile warFile;
private final JarEntry jarEntry;
- private NonClosingJarInputStream jarInputStream = null;
- private JarEntry entry = null;
public JarFileUrlNestedJar(URL url) throws IOException {
- jarFileURL = url;
+ super(url);
JarURLConnection jarConn = (JarURLConnection) url.openConnection();
jarConn.setUseCaches(false);
warFile = jarConn.getJarFile();
@@ -51,77 +46,8 @@ public class JarFileUrlNestedJar impleme
@Override
- public URL getJarFileURL() {
- return jarFileURL;
- }
-
-
-
- @Override
- public boolean entryExists(String name) throws IOException {
- reset();
- JarEntry entry = jarInputStream.getNextJarEntry();
- while (entry != null) {
- if (name.equals(entry.getName())) {
- break;
- }
- entry = jarInputStream.getNextJarEntry();
- }
-
- return entry != null;
- }
-
-
- @Override
- public InputStream getInputStream(String name) throws IOException {
- reset();
- JarEntry entry = jarInputStream.getNextJarEntry();
- while (entry != null) {
- if (name.equals(entry.getName())) {
- break;
- }
- entry = jarInputStream.getNextJarEntry();
- }
-
- if (entry == null) {
- return null;
- } else {
- return jarInputStream;
- }
- }
-
-
- @Override
- public long getLastModified(String name) throws IOException {
- reset();
- JarEntry entry = jarInputStream.getNextJarEntry();
- while (entry != null) {
- if (name.equals(entry.getName())) {
- break;
- }
- entry = jarInputStream.getNextJarEntry();
- }
-
- if (entry == null) {
- return -1;
- } else {
- return entry.getTime();
- }
- }
-
- @Override
- public String getURL(String entry) {
- StringBuilder result = new StringBuilder("jar:");
- result.append(getJarFileURL().toExternalForm());
- result.append("!/");
- result.append(entry);
-
- return result.toString();
- }
-
- @Override
public void close() {
- closeInner();
+ closeStream();
if (warFile != null) {
try {
warFile.close();
@@ -132,61 +58,8 @@ public class JarFileUrlNestedJar impleme
}
- private void closeInner() {
- if (jarInputStream != null) {
- try {
- jarInputStream.reallyClose();
- } catch (IOException ioe) {
- // Ignore
- }
- }
- }
-
- private NonClosingJarInputStream createJarInputStream() throws IOException {
- return new NonClosingJarInputStream(warFile.getInputStream(jarEntry));
- }
-
-
@Override
- public void nextEntry() {
- if (jarInputStream == null) {
- try {
- jarInputStream = createJarInputStream();
- } catch (IOException e) {
- entry = null;
- return;
- }
- }
- try {
- entry = jarInputStream.getNextJarEntry();
- } catch (IOException ioe) {
- entry = null;
- }
- }
-
-
- @Override
- public String getEntryName() {
- if (entry == null) {
- return null;
- } else {
- return entry.getName();
- }
- }
-
-
- @Override
- public InputStream getEntryInputStream() throws IOException {
- if (jarInputStream == null) {
- jarInputStream = createJarInputStream();
- }
- return jarInputStream;
- }
-
-
- @Override
- public void reset() throws IOException {
- closeInner();
- jarInputStream = createJarInputStream();
+ protected NonClosingJarInputStream createJarInputStream() throws IOException {
+ return new NonClosingJarInputStream(warFile.getInputStream(jarEntry));
}
}
Modified: tomcat/trunk/java/org/apache/tomcat/util/scan/UrlJar.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/scan/UrlJar.java?rev=1742246&r1=1742245&r2=1742246&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/scan/UrlJar.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/scan/UrlJar.java Wed May 4 10:33:04 2016
@@ -17,151 +17,33 @@
package org.apache.tomcat.util.scan;
import java.io.IOException;
-import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
-import java.util.jar.JarEntry;
/**
* Implementation of {@link Jar} that is optimised for non-file based JAR URLs
* (e.g. JNDI based URLs of the form jar:jndi:...).
*/
-public class UrlJar implements Jar {
+public class UrlJar extends AbstractInputStreamJar {
- private NonClosingJarInputStream jarInputStream = null;
- private final URL url;
- private JarEntry entry = null;
-
- public UrlJar(URL url) throws IOException {
- this.url = url;
- this.jarInputStream = createJarInputStream();
+ public UrlJar(URL jarFileURL) {
+ super(jarFileURL);
}
- @Override
- public URL getJarFileURL() {
- return url;
- }
-
- @Override
- public boolean entryExists(String name) throws IOException {
- reset();
- JarEntry entry = jarInputStream.getNextJarEntry();
- while (entry != null) {
- if (name.equals(entry.getName())) {
- break;
- }
- entry = jarInputStream.getNextJarEntry();
- }
-
- return entry != null;
- }
-
- @Override
- public InputStream getInputStream(String name) throws IOException {
- reset();
- JarEntry entry = jarInputStream.getNextJarEntry();
- while (entry != null) {
- if (name.equals(entry.getName())) {
- break;
- }
- entry = jarInputStream.getNextJarEntry();
- }
-
- if (entry == null) {
- return null;
- } else {
- return jarInputStream;
- }
- }
@Override
- public long getLastModified(String name) throws IOException {
- reset();
- JarEntry entry = jarInputStream.getNextJarEntry();
- while (entry != null) {
- if (name.equals(entry.getName())) {
- break;
- }
- entry = jarInputStream.getNextJarEntry();
- }
-
- if (entry == null) {
- return -1;
- } else {
- return entry.getTime();
- }
+ public void close() {
+ closeStream();
}
- @Override
- public String getURL(String entry) {
- StringBuilder result = new StringBuilder("jar:");
- result.append(getJarFileURL().toExternalForm());
- result.append("!/");
- result.append(entry);
-
- return result.toString();
- }
@Override
- public void close() {
- if (jarInputStream != null) {
- try {
- jarInputStream.reallyClose();
- } catch (IOException ioe) {
- // Ignore
- }
- }
- }
-
- private NonClosingJarInputStream createJarInputStream() throws IOException {
- JarURLConnection jarConn = (JarURLConnection) url.openConnection();
+ protected NonClosingJarInputStream createJarInputStream() throws IOException {
+ JarURLConnection jarConn = (JarURLConnection) getJarFileURL().openConnection();
URL resourceURL = jarConn.getJarFileURL();
URLConnection resourceConn = resourceURL.openConnection();
resourceConn.setUseCaches(false);
return new NonClosingJarInputStream(resourceConn.getInputStream());
}
-
- @Override
- public void nextEntry() {
- if (jarInputStream == null) {
- try {
- jarInputStream = createJarInputStream();
- } catch (IOException e) {
- entry = null;
- return;
- }
- }
- try {
- entry = jarInputStream.getNextJarEntry();
- } catch (IOException ioe) {
- entry = null;
- }
- }
-
-
- @Override
- public String getEntryName() {
- if (entry == null) {
- return null;
- } else {
- return entry.getName();
- }
- }
-
-
- @Override
- public InputStream getEntryInputStream() throws IOException {
- if (jarInputStream == null) {
- jarInputStream = createJarInputStream();
- }
- return jarInputStream;
- }
-
-
- @Override
- public void reset() throws IOException {
- close();
- jarInputStream = createJarInputStream();
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org