Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Fixes a jar-only URL resolution bug #7748

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -1425,6 +1426,14 @@ private void processPersistenceXmls(AfterBeanDiscovery event,
reader.close();
}
}
URL persistenceRootUrl;
URI persistenceXmlUri = url.toURI();
if ("jar".equalsIgnoreCase(persistenceXmlUri.getScheme())) {
String ssp = persistenceXmlUri.getSchemeSpecificPart();
persistenceRootUrl = new URI(ssp.substring(0, ssp.lastIndexOf('!'))).toURL();
} else {
persistenceRootUrl = persistenceXmlUri.resolve("..").toURL();
}
Collection<? extends Persistence.PersistenceUnit> persistenceUnits = persistence.getPersistenceUnit();
if (persistenceUnits != null && !persistenceUnits.isEmpty()) {
persistenceUnitInfos = new ArrayList<>();
Expand All @@ -1434,7 +1443,7 @@ private void processPersistenceXmls(AfterBeanDiscovery event,
.add(PersistenceUnitInfoBean.fromPersistenceUnit(persistenceUnit,
classLoader,
tempClassLoaderSupplier,
url.toURI().resolve("..").toURL(),
persistenceRootUrl,
unlistedManagedClassesByPersistenceUnitNames,
dataSourceProviderSupplier));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -563,8 +564,7 @@ private <T extends EntityManager> void saveContainerManagedEntityManagerQualifie
*
* @see #addContainerManagedJpaBeans(AfterBeanDiscovery)
*/
private void addSyntheticBeans(@Observes @Priority(LIBRARY_AFTER) AfterBeanDiscovery event, BeanManager bm)
throws URISyntaxException {
private void addSyntheticBeans(@Observes @Priority(LIBRARY_AFTER) AfterBeanDiscovery event, BeanManager bm) {
if (!this.enabled) {
return;
}
Expand Down Expand Up @@ -1011,8 +1011,7 @@ private void processPersistenceXmls(AfterBeanDiscovery event,
ClassLoader classLoader,
Enumeration<URL> persistenceXmlUrls,
Iterable<? extends PersistenceProvider> providers,
boolean userSuppliedPuiBeans)
throws URISyntaxException {
boolean userSuppliedPuiBeans) {
if (!persistenceXmlUrls.hasMoreElements()) {
return;
}
Expand Down Expand Up @@ -1050,16 +1049,28 @@ private void processPersistenceXmls(AfterBeanDiscovery event,
event.addDefinitionError(e);
continue;
}
URL persistenceRootUrl;
try {
URI persistenceXmlUri = persistenceXmlUrl.toURI();
if ("jar".equalsIgnoreCase(persistenceXmlUri.getScheme())) {
String ssp = persistenceXmlUri.getSchemeSpecificPart();
persistenceRootUrl = new URI(ssp.substring(0, ssp.lastIndexOf('!'))).toURL();
} else {
persistenceRootUrl = persistenceXmlUri.resolve("..").toURL();
}
} catch (MalformedURLException | URISyntaxException e) {
event.addDefinitionError(e);
continue;
}
for (Persistence.PersistenceUnit pu : persistence.getPersistenceUnit()) {
PersistenceUnitInfoBean pui;
try {
pui =
PersistenceUnitInfoBean.fromPersistenceUnit(pu,
classLoader,
tempClassLoaderSupplier,
persistenceXmlUrl.toURI().resolve("..").toURL(),
unlistedManagedClassesByUnitNames,
dataSourceProviderSupplier);
pui = PersistenceUnitInfoBean.fromPersistenceUnit(pu,
classLoader,
tempClassLoaderSupplier,
persistenceRootUrl,
unlistedManagedClassesByUnitNames,
dataSourceProviderSupplier);
} catch (MalformedURLException e) {
event.addDefinitionError(e);
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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.helidon.integrations.cdi.jpa;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

class TestPersistenceRootResolutionBehavior {

@Test
void testUriResolution() throws IOException, URISyntaxException {
Enumeration<URL> pxmls = Thread.currentThread().getContextClassLoader().getResources("META-INF/persistence.xml");
assertThat(pxmls.hasMoreElements(), is(true));
URL pxml = pxmls.nextElement();
assertThat(pxmls.hasMoreElements(), is(false));
URI pxmlUri = pxml.toURI();
assertThat(pxmlUri.isAbsolute(), is(true));
URI upOneLevel = pxmlUri.resolve("..");
assertThat(upOneLevel.isAbsolute(), is(true));
upOneLevel.toURL(); // make sure this doesn't blow up
}

@Test
void testJarStuff() throws IOException, URISyntaxException {
URI pxmlUri = URI.create("jar:file:///nonexistent/example.jar!/META-INF/persistence.xml");
assertThat(pxmlUri.isOpaque(), is(true));
assertThat(pxmlUri.isAbsolute(), is(true)); // implied by opacity
String ssp = pxmlUri.getSchemeSpecificPart();
assertThat(ssp, is("file:///nonexistent/example.jar!/META-INF/persistence.xml"));
URI sspUri = URI.create(ssp);
assertThat(sspUri.isAbsolute(), is(true));
assertThat(sspUri.isOpaque(), is(false));
URI up = URI.create("..");
assertThat(up.isAbsolute(), is(false));
assertThat(up.isOpaque(), is(false));
// Opacity means this should return, simply, and surprisingly to a quick glance, "..".
assertThat(pxmlUri.resolve(".."), is(up));
assertThat(URI.create("..").isOpaque(), is(false));
}

}