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

Introducing the Forbidden API maven checks for Hibernate ORM #15729

Merged
merged 3 commits into from
Mar 15, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
<!-- By default don't check dependencies -->
<revapi.checkdeps>false</revapi.checkdeps>

<!-- Forbidden API checks -->
<forbiddenapis-maven-plugin.version>3.1</forbiddenapis-maven-plugin.version>

<!-- platform properties -->
<platform.quarkus.native.builder-image>quay.io/quarkus/ubi-quarkus-native-image:21.0.0-java11</platform.quarkus.native.builder-image>

Expand Down
4 changes: 4 additions & 0 deletions extensions/hibernate-orm/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -748,25 +748,28 @@ private static void producePersistenceUnitDescriptorFromConfig(
.filter(i -> persistenceUnitConfig.datasource.get().equals(i.getName()))
.findFirst()
.orElseThrow(() -> new ConfigurationException(
String.format("The datasource '%1$s' is not configured but the persistence unit '%2$s' uses it."
+ " To solve this, configure datasource '%1$s'."
+ " Refer to https://quarkus.io/guides/datasource for guidance.",
String.format(Locale.ROOT,
"The datasource '%1$s' is not configured but the persistence unit '%2$s' uses it."
+ " To solve this, configure datasource '%1$s'."
+ " Refer to https://quarkus.io/guides/datasource for guidance.",
persistenceUnitConfig.datasource.get(), persistenceUnitName)));
dataSource = persistenceUnitConfig.datasource.get();
} else {
if (!PersistenceUnitUtil.isDefaultPersistenceUnit(persistenceUnitName)) {
// if it's not the default persistence unit, we mandate a datasource to prevent common errors
throw new ConfigurationException(
String.format("Datasource must be defined for persistence unit '%s'.", persistenceUnitName));
String.format(Locale.ROOT, "Datasource must be defined for persistence unit '%s'.",
persistenceUnitName));
}

jdbcDataSource = jdbcDataSources.stream()
.filter(i -> i.isDefault())
.findFirst()
.orElseThrow(() -> new ConfigurationException(
String.format("The default datasource is not configured but the persistence unit '%s' uses it."
+ " To solve this, configure the default datasource."
+ " Refer to https://quarkus.io/guides/datasource for guidance.",
String.format(Locale.ROOT,
"The default datasource is not configured but the persistence unit '%s' uses it."
+ " To solve this, configure the default datasource."
+ " Refer to https://quarkus.io/guides/datasource for guidance.",
persistenceUnitName)));
dataSource = DataSourceUtil.DEFAULT_DATASOURCE_NAME;
}
Expand Down Expand Up @@ -850,7 +853,7 @@ private static void producePersistenceUnitDescriptorFromConfig(
persistenceUnitConfig.query.queryPlanCacheMaxSize));

descriptor.getProperties().setProperty(AvailableSettings.DEFAULT_NULL_ORDERING,
persistenceUnitConfig.query.defaultNullOrdering.name().toLowerCase());
persistenceUnitConfig.query.defaultNullOrdering.name().toLowerCase(Locale.ROOT));

// JDBC
persistenceUnitConfig.jdbc.timezone.ifPresent(
Expand Down Expand Up @@ -1040,7 +1043,7 @@ private static Map<String, Set<String>> getModelClassesAndPackagesPerPersistence
String candidatePersistenceUnitName = candidatePersistenceUnitEntry.getKey();

Set<String> candidatePersistenceUnitPackages = candidatePersistenceUnitEntry.getValue().packages
.orElseThrow(() -> new ConfigurationException(String.format(
.orElseThrow(() -> new ConfigurationException(String.format(Locale.ROOT,
"Packages must be configured for persistence unit '%s'.", candidatePersistenceUnitName)));

for (String packageName : candidatePersistenceUnitPackages) {
Expand Down Expand Up @@ -1099,7 +1102,7 @@ private static Map<String, Set<String>> getModelClassesAndPackagesPerPersistence
}

if (!modelClassesWithPersistenceUnitAnnotations.isEmpty()) {
throw new IllegalStateException(String.format(
throw new IllegalStateException(String.format(Locale.ROOT,
"@PersistenceUnit annotations are not supported at the class level on model classes:\n\t- %s\nUse the `.packages` configuration property or package-level annotations instead.",
String.join("\n\t- ", modelClassesWithPersistenceUnitAnnotations)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.quarkus.hibernate.orm.naming;

import java.util.Locale;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ImplicitEntityNameSource;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;

public class CustomImplicitNamingStrategy extends ImplicitNamingStrategyJpaCompliantImpl {
@Override
public Identifier determinePrimaryTableName(ImplicitEntityNameSource source) {
return toIdentifier("TBL_" + source.getEntityNaming().getEntityName().replace('.', '_').toUpperCase(),
return toIdentifier("TBL_" + source.getEntityNaming().getEntityName().replace('.', '_').toUpperCase(Locale.ROOT),
source.getBuildingContext());
}
}
87 changes: 87 additions & 0 deletions extensions/hibernate-orm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,91 @@
<module>runtime</module>
</modules>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
<version>${forbiddenapis-maven-plugin.version}</version>
<executions>
<execution>
<id>verify-forbidden-apis</id>
<configuration>
<!-- if the used Java version is too new, don't fail, just do nothing: -->
<failOnUnsupportedJava>false</failOnUnsupportedJava>
<excludes>
<!-- JBoss Logger formats messages according to default Locale (bug or feature?) -->
<exclude>**/*_$logger.class</exclude>
<!-- JBoss message bundles format messages -->
<exclude>**/*_$bundle.class</exclude>
</excludes>
<bundledSignatures>
<!-- These signatures on the top are not specific to any JDK version -->
<bundledSignature>jdk-system-out</bundledSignature>
<bundledSignature>jdk-non-portable</bundledSignature>
<!-- The jdk-reflection is not yet something we can avoid -->
<!--<bundledSignature>jdk-reflection</bundledSignature>-->

<!-- All following signatures should be replicated for each target JDK version we intend to support -->
<bundledSignature>jdk-unsafe-1.8</bundledSignature>
<bundledSignature>jdk-unsafe-9</bundledSignature>
<bundledSignature>jdk-unsafe-10</bundledSignature>
<bundledSignature>jdk-unsafe-11</bundledSignature>
<bundledSignature>jdk-unsafe-12</bundledSignature>
<bundledSignature>jdk-unsafe-13</bundledSignature>
<bundledSignature>jdk-unsafe-14</bundledSignature>
<bundledSignature>jdk-unsafe-15</bundledSignature>

<bundledSignature>jdk-deprecated-1.8</bundledSignature>
<bundledSignature>jdk-deprecated-9</bundledSignature>
<bundledSignature>jdk-deprecated-10</bundledSignature>
<bundledSignature>jdk-deprecated-11</bundledSignature>
<bundledSignature>jdk-deprecated-12</bundledSignature>
<bundledSignature>jdk-deprecated-13</bundledSignature>
<bundledSignature>jdk-deprecated-14</bundledSignature>
<bundledSignature>jdk-deprecated-15</bundledSignature>

<bundledSignature>jdk-internal-1.8</bundledSignature>
<bundledSignature>jdk-internal-9</bundledSignature>
<bundledSignature>jdk-internal-10</bundledSignature>
<bundledSignature>jdk-internal-11</bundledSignature>
<bundledSignature>jdk-internal-12</bundledSignature>
<bundledSignature>jdk-internal-13</bundledSignature>
<bundledSignature>jdk-internal-14</bundledSignature>
<bundledSignature>jdk-internal-15</bundledSignature>
</bundledSignatures>
<failOnMissingClasses>false</failOnMissingClasses>
<ignoreSignaturesOfMissingClasses>true</ignoreSignaturesOfMissingClasses>
</configuration>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>verify-forbidden-test-apis</id>
<configuration>
<!-- if the used Java version is too new, don't fail, just do nothing: -->
<failOnUnsupportedJava>false</failOnUnsupportedJava>
<ignoreSignaturesOfMissingClasses>true</ignoreSignaturesOfMissingClasses>
<bundledSignatures>
<!-- This will automatically choose the right signatures based on 'targetVersion': -->
<bundledSignature>jdk-unsafe</bundledSignature>
<bundledSignature>jdk-deprecated</bundledSignature>
<bundledSignature>jdk-non-portable</bundledSignature>
<bundledSignature>jdk-internal</bundledSignature>
</bundledSignatures>
</configuration>
<phase>test-compile</phase>
<goals>
<goal>testCheck</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
4 changes: 4 additions & 0 deletions extensions/hibernate-orm/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -54,7 +55,7 @@ public EntityManagerFactory getEntityManagerFactory(String unitName) {

if (lazyPersistenceUnit == null) {
throw new IllegalArgumentException(
String.format("Unable to find an EntityManagerFactory for persistence unit '%s'", unitName));
String.format(Locale.ROOT, "Unable to find an EntityManagerFactory for persistence unit '%s'", unitName));
}

return lazyPersistenceUnit.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hibernate.jpa.AvailableSettings.COLLECTION_CACHE_PREFIX;
import static org.hibernate.jpa.AvailableSettings.PERSISTENCE_UNIT_NAME;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -603,8 +604,8 @@ private void applyMetadataBuilderContributor() {

if (metadataBuilderContributorImplClass != null) {
try {
metadataBuilderContributor = metadataBuilderContributorImplClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
metadataBuilderContributor = metadataBuilderContributorImplClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new IllegalArgumentException("The MetadataBuilderContributor class ["
+ metadataBuilderContributorImplClass + "] could not be instantiated!", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.hibernate.orm.runtime.boot;

import java.lang.reflect.InvocationTargetException;

import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
Expand Down Expand Up @@ -51,9 +53,10 @@ private static InitialInitiatorListProvider initReactiveListProviderMaybe() {
//Use reflection as we don't want the Hibernate ORM extension to depend on the Hibernate Reactive extension:
final Class<?> forName = Class
.forName("io.quarkus.hibernate.reactive.runtime.boot.registry.ReactiveHibernateInitiatorListProvider");
final Object o = forName.newInstance();
final Object o = forName.getDeclaredConstructor().newInstance();
return (InitialInitiatorListProvider) o;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException
| InvocationTargetException e) {
//Be silent as this is a static initializer: most likely the Hibernate Reactive extension is
//not on the classpath, which implies we won't need this.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Locale;

import javax.enterprise.inject.Default;

Expand Down Expand Up @@ -55,7 +56,7 @@ public ConnectionProvider resolve(String tenantId) {
multiTenancySchemaDataSourceName);
if (dataSource == null) {
throw new IllegalStateException(
String.format("No instance of datasource found for persistence unit '%1$s' and tenant '%2$s'",
String.format(Locale.ROOT, "No instance of datasource found for persistence unit '%1$s' and tenant '%2$s'",
persistenceUnitName, tenantId));
}
if (multiTenancyStrategy == MultiTenancyStrategy.SCHEMA) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.hibernate.orm.runtime.tenant;

import java.util.Locale;

import javax.enterprise.inject.Default;

import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
Expand Down Expand Up @@ -60,8 +62,9 @@ private static TenantResolver tenantResolver(String persistenceUnitName) {
new PersistenceUnitLiteral(persistenceUnitName));
}
if (!resolverInstance.isAvailable()) {
throw new IllegalStateException(String.format("No instance of %1$s was found for persistence unit %2$s. "
+ "You need to create an implementation for this interface to allow resolving the current tenant identifier.",
throw new IllegalStateException(String.format(Locale.ROOT,
"No instance of %1$s was found for persistence unit %2$s. "
+ "You need to create an implementation for this interface to allow resolving the current tenant identifier.",
TenantResolver.class.getSimpleName(), persistenceUnitName));
}
return resolverInstance.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.hibernate.orm.runtime.tenant;

import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -68,8 +69,9 @@ private static ConnectionProvider resolveConnectionProvider(String persistenceUn
}
if (!instance.isAvailable()) {
throw new IllegalStateException(
String.format("No instance of %1$s was found for persistence unit %2$s. "
+ "You need to create an implementation for this interface to allow resolving the current tenant connection.",
String.format(
Locale.ROOT, "No instance of %1$s was found for persistence unit %2$s. "
+ "You need to create an implementation for this interface to allow resolving the current tenant connection.",
TenantConnectionResolver.class.getSimpleName(), persistenceUnitName));
}
TenantConnectionResolver resolver = instance.get();
Expand All @@ -95,8 +97,9 @@ private static TenantResolver tenantResolver(String persistenceUnitName) {
new PersistenceUnitLiteral(persistenceUnitName));
}
if (!resolverInstance.isAvailable()) {
throw new IllegalStateException(String.format("No instance of %1$s was found for persistence unit %2$s. "
+ "You need to create an implementation for this interface to allow resolving the current tenant identifier.",
throw new IllegalStateException(String.format(Locale.ROOT,
"No instance of %1$s was found for persistence unit %2$s. "
+ "You need to create an implementation for this interface to allow resolving the current tenant identifier.",
TenantResolver.class.getSimpleName(), persistenceUnitName));
}
return resolverInstance.get();
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<skip.gradle.tests>true</skip.gradle.tests>
<invoker.skip>true</invoker.skip> <!-- maven-invoker-plugin -->
<jbang.skip>true</jbang.skip> <!-- jbang-maven-plugin -->
<forbiddenapis.skip>true</forbiddenapis.skip> <!-- forbidden-apis maven plugin -->
</properties>
<build>
<defaultGoal>clean install</defaultGoal>
Expand All @@ -146,6 +147,7 @@
<skip.gradle.tests>true</skip.gradle.tests>
<invoker.skip>true</invoker.skip> <!-- maven-invoker-plugin -->
<jbang.skip>true</jbang.skip> <!-- jbang-maven-plugin -->
<forbiddenapis.skip>true</forbiddenapis.skip> <!-- forbidden-apis maven plugin -->
</properties>
</profile>
<profile>
Expand Down