Skip to content

Instantly share code, notes, and snippets.

@navarroe
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navarroe/d2caed53ee6ebd819189 to your computer and use it in GitHub Desktop.
Save navarroe/d2caed53ee6ebd819189 to your computer and use it in GitHub Desktop.
From 72a422207a672cfe39df2b3c7da6d0deed3269f4 Mon Sep 17 00:00:00 2001
From: Eric Navarro <eric.navarro@yahoo.com>
Date: Mon, 14 Jul 2014 16:49:25 -0400
Subject: [PATCH 1/2] JERSEY-2336 - add wsjar to allowed jar/zip schemas so
that PackageNamesScanner works properly within Websphere
---
.../JarZipSchemeResourceFinderFactory.java | 6 +-
.../internal/scanning/PackageNamesScannerTest.java | 135 +++++++++++++++++++++
2 files changed, 138 insertions(+), 3 deletions(-)
create mode 100644 core-server/src/test/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScannerTest.java
diff --git a/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/JarZipSchemeResourceFinderFactory.java b/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/JarZipSchemeResourceFinderFactory.java
index c5c2f05..20cab9e 100644
--- a/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/JarZipSchemeResourceFinderFactory.java
+++ b/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/JarZipSchemeResourceFinderFactory.java
@@ -55,7 +55,7 @@ import org.glassfish.jersey.server.ResourceFinder;
import org.glassfish.jersey.uri.UriComponent;
/**
- * A "jar" and "zip" scheme URI scanner that recursively jar files.
+ * A "jar", "zip" and "wsjar" scheme URI scanner that recursively jar files.
* Jar entries are reported to a {@link ResourceProcessor}.
*
* @author Paul Sandoz
@@ -65,11 +65,11 @@ class JarZipSchemeResourceFinderFactory implements UriSchemeResourceFinderFactor
@Override
public Set<String> getSchemes() {
- return new HashSet<String>(Arrays.asList("jar", "zip"));
+ return new HashSet<String>(Arrays.asList("jar", "zip", "wsjar"));
}
/**
- * Create new "jar" and "zip" scheme URI scanner factory.
+ * Create new "jar", "zip" and "wsjar" scheme URI scanner factory.
*/
JarZipSchemeResourceFinderFactory() {
}
diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScannerTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScannerTest.java
new file mode 100644
index 0000000..4d8d759
--- /dev/null
+++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScannerTest.java
@@ -0,0 +1,135 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common Development
+ * and Distribution License("CDDL") (collectively, the "License"). You
+ * may not use this file except in compliance with the License. You can
+ * obtain a copy of the License at
+ * http://glassfish.java.net/public/CDDL+GPL_1_1.html
+ * or packager/legal/LICENSE.txt. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ * When distributing the software, include this License Header Notice in each
+ * file and include the License file at packager/legal/LICENSE.txt.
+ *
+ * GPL Classpath Exception:
+ * Oracle designates this particular file as subject to the "Classpath"
+ * exception as provided by Oracle in the GPL Version 2 section of the License
+ * file that accompanied this code.
+ *
+ * Modifications:
+ * If applicable, add the following below the License Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyright [year] [name of copyright owner]"
+ *
+ * Contributor(s):
+ * If you wish your version of this file to be governed by only the CDDL or
+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
+ * elects to include this software in this distribution under the [CDDL or GPL
+ * Version 2] license." If you don't indicate a single choice of license, a
+ * recipient has the option to distribute your version of this file under
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
+ * its licensees as provided above. However, if you add GPL Version 2 code
+ * and therefore, elected the GPL Version 2 license, then the option applies
+ * only if the new code is made subject to such option by the copyright
+ * holder.
+ */
+package org.glassfish.jersey.server.internal.scanning;
+
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Vector;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Eric Navarro
+ */
+public class PackageNamesScannerTest {
+
+ private String jaxRsApiFilePath;
+ private String[] packages = {"javax.ws.rs-api"};
+
+ @Before
+ public void setUp() throws Exception {
+ final String classPath = System.getProperty("java.class.path");
+ final String[] entries = classPath.split(System
+ .getProperty("path.separator"));
+
+ for (final String entry : entries) {
+ if (entry.contains("javax.ws.rs-api")) {
+ jaxRsApiFilePath = "file:/" + entry;
+ break;
+ }
+ }
+
+ if (jaxRsApiFilePath == null) {
+ fail("Could not find javax.ws.rs-api.");
+ }
+ }
+
+ @Test
+ public void testWsJarScheme() {
+ new PackageNamesScanner(createTestClassLoader("wsjar", createTestURLStreamHandler("wsjar"), jaxRsApiFilePath), packages, false);
+ }
+
+ @Test
+ public void testJarScheme() {
+ new PackageNamesScanner(createTestClassLoader("jar", null, jaxRsApiFilePath), packages, false);
+ }
+
+ @Test
+ public void testZipScheme() {
+ new PackageNamesScanner(createTestClassLoader("zip", createTestURLStreamHandler("zip"), jaxRsApiFilePath), packages, false);
+ }
+
+ @Test
+ public void testFileScheme() {
+ // Uses default class loader
+ new PackageNamesScanner(packages, false);
+ }
+
+ @Test(expected=ResourceFinderException.class)
+ public void testInvalidScheme() {
+ new PackageNamesScanner(createTestClassLoader("bad", createTestURLStreamHandler("bad"), jaxRsApiFilePath), packages, false);
+ }
+
+ private ClassLoader createTestClassLoader(final String scheme, final URLStreamHandler urlStreamHandler, final String resourceFilePath) {
+ return new ClassLoader() {
+ public Enumeration<URL> getResources(String name) throws IOException {
+ List<URL> list = new ArrayList<URL>();
+ list.add((urlStreamHandler == null
+ ? new URL(null, scheme + ":" + resourceFilePath + "!/" + name)
+ : new URL(null, scheme + ":" + resourceFilePath + "!/" + name, urlStreamHandler)));
+ return new Vector<URL>(list).elements();
+ }
+ };
+ }
+
+ // URLStreamHandler creation for the various schemes without having to add them as dependencies
+ private URLStreamHandler createTestURLStreamHandler(final String scheme) {
+ return new URLStreamHandler() {
+ @Override
+ protected URLConnection openConnection(URL u) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected void parseURL(URL u, String spec, int start, int limit) {
+ setURL(u, scheme, "", -1, null, null, spec.substring(scheme.length() + 1) , null, null);
+ }
+ };
+ }
+
+}
--
1.9.0.msysgit.0
From fa0350505b5b9afd80ae2a6a510bbab73bcd09e4 Mon Sep 17 00:00:00 2001
From: Eric Navarro <eric.navarro@yahoo.com>
Date: Tue, 15 Jul 2014 12:39:41 -0400
Subject: [PATCH 2/2] JERSEY-2336 - policy file change to allow tests to
specify stream handler
---
core-server/src/test/resources/server.policy | 2 ++
1 file changed, 2 insertions(+)
diff --git a/core-server/src/test/resources/server.policy b/core-server/src/test/resources/server.policy
index cace90c..8f2c6f8 100644
--- a/core-server/src/test/resources/server.policy
+++ b/core-server/src/test/resources/server.policy
@@ -58,6 +58,7 @@ grant codebase "file:${project.build.directory}/test-classes/-" {
permission java.lang.RuntimePermission "accessDeclaredMembers";
permission java.lang.RuntimePermission "createClassLoader";
permission java.lang.RuntimePermission "setContextClassLoader";
+ permission java.net.NetPermission "specifyStreamHandler";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
permission java.io.FilePermission "<<ALL FILES>>", "read,write,delete";
permission java.util.PropertyPermission "*", "read";
@@ -68,6 +69,7 @@ grant codebase "file:${project.build.directory}/classes/-" {
permission java.lang.RuntimePermission "accessDeclaredMembers";
permission java.lang.RuntimePermission "setContextClassLoader";
permission java.lang.RuntimePermission "getClassLoader";
+ permission java.net.NetPermission "specifyStreamHandler";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "<<ALL FILES>>", "read,write";
};
--
1.9.0.msysgit.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment