Skip to content
Draft
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
2 changes: 1 addition & 1 deletion appserver/featuresets/web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@

<dependency>
<groupId>org.glassfish.main.persistence</groupId>
<artifactId>jnosql-jakarta-persistence-integration</artifactId>
<artifactId>jnosql-integration</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
Expand Down
7 changes: 5 additions & 2 deletions appserver/persistence/jnosql-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
<version>8.0.0-SNAPSHOT</version>
</parent>

<artifactId>jnosql-jakarta-persistence-integration</artifactId>
<artifactId>jnosql-integration</artifactId>
<packaging>glassfish-jar</packaging>

<name>Eclipse JNoSQL Jakarta Persistence Integration</name>
<name>Eclipse JNoSQL Integration for GlassFish</name>
<description>
Integration of JNoSQL, and Jakarta Data repositories for both Jakarta Persistence and NoSQL entities
</description>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import jakarta.data.repository.DataRepository;
import jakarta.data.repository.Repository;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;

import java.util.Collection;
Expand All @@ -31,8 +30,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jnosql.jakartapersistence.JNoSQLJakartaPersistence;
import org.eclipse.jnosql.mapping.metadata.ClassScanner;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.classmodel.reflect.AnnotationModel;
Expand All @@ -45,19 +42,26 @@
import org.glassfish.internal.api.Globals;
import org.glassfish.internal.deployment.Deployment;

import static java.util.stream.Collectors.toUnmodifiableSet;

/**
*
* @author Ondro Mihalyi
*/
public class GlassFishClassScanner implements ClassScanner {
abstract public class BaseGlassFishClassScanner {

private static final Logger LOG = Logger.getLogger(BaseGlassFishClassScanner.class.getName());

/**
* Whether the entity supported by this repository interface is supported by this provide (e.g. the entity class contains {@link Entity} annotation.
* @param entityType Type of the entity, analogous to the entity class
* @return True if the entity is supported, false otherwise
*/
abstract protected boolean isSupportedEntityType(ParameterizedInterfaceModel entityType);

private static final Logger LOG = Logger.getLogger(GlassFishClassScanner.class.getName());
abstract protected String getProviderName();

/* TODO: Optimization - initialize all sets returned from methods in the CDI extension and return them directly,
avoid searching for them each time */
public Types getTypes() {
protected Types getTypes() {
final ServiceLocator locator = Globals.getDefaultHabitat();
DeploymentContext deploymentContext
= locator != null
Expand All @@ -76,12 +80,7 @@ public Types getTypes() {
}
}

@Override
public Set<Class<?>> entities() {
return findClassesWithAnnotation(Entity.class);
}

private Set<Class<?>> findClassesWithAnnotation(Class<?> annotation) {
protected Set<Class<?>> findClassesWithAnnotation(Class<?> annotation) {
String annotationClassName = annotation.getName();
return getTypes().getAllTypes()
.stream()
Expand All @@ -92,56 +91,25 @@ private Set<Class<?>> findClassesWithAnnotation(Class<?> annotation) {
.collect(Collectors.toSet());
}

@Override
public Set<Class<?>> repositories() {
return repositoriesStream()
.collect(toUnmodifiableSet());
}

private Stream<Class<?>> repositoriesStream() {
return repositoriesStreamMatching(intfModel ->
intfModel.getParameterizedInterfaces().stream()
.anyMatch(this::isSupportedBuiltInInterface)
|| DataRepository.class.isAssignableFrom(typeModelToClass(intfModel)));
}

@Override
public Set<Class<?>> embeddables() {
return findClassesWithAnnotation(Embeddable.class);
}

@Override
public <T extends DataRepository<?, ?>> Set<Class<?>> repositories(Class<T> filter) {
Objects.requireNonNull(filter, "filter is required");
return repositoriesStream()
.filter(filter::isAssignableFrom)
.collect(toUnmodifiableSet());
}

@Override
public Set<Class<?>> repositoriesStandard() {
Predicate<ParameterizedInterfaceModel> isSupportedBuiltInInterface = this::isSupportedBuiltInInterface;
Predicate<ParameterizedInterfaceModel> directlyImplementsStandardInterface = this::directlyImplementsStandardInterface;
return repositoriesStreamMatching(intfModel -> intfModel.getParameterizedInterfaces().stream()
.anyMatch(isSupportedBuiltInInterface.and(directlyImplementsStandardInterface)))
.collect(toUnmodifiableSet());
private Class<?> typeModelToClass(ExtensibleType<?> type) throws RuntimeException {
return classForName(type.getName());
}

@Override
public Set<Class<?>> customRepositories() {
return repositoriesStreamMatching(this::noneOfExtendedInterfacesIsStandard)
.collect(toUnmodifiableSet());
private Class<?> classForName(String name) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

private boolean noneOfExtendedInterfacesIsStandard(InterfaceModel intfModel) {
Predicate<ParameterizedInterfaceModel> directlyImplementsStandardInterface = this::directlyImplementsStandardInterface;
return intfModel.getParameterizedInterfaces().isEmpty()
|| intfModel.getParameterizedInterfaces().stream()
.allMatch(directlyImplementsStandardInterface
.negate());
protected Stream<Class<?>> repositoriesStream() {
return repositoriesStreamMatching(intfModel ->
intfModel.getParameterizedInterfaces().stream()
.anyMatch(this::isSupportedBuiltInInterface));
}

private Stream<Class<?>> repositoriesStreamMatching(Predicate<InterfaceModel> predicate) {
protected Stream<Class<?>> repositoriesStreamMatching(Predicate<InterfaceModel> predicate) {
// TODO: Prepare a map of types per annotation on the class to avoid iteration over all types
return getTypes().getAllTypes()
.stream()
Expand All @@ -151,7 +119,7 @@ private Stream<Class<?>> repositoriesStreamMatching(Predicate<InterfaceModel> pr
if (repositoryAnnotation != null) {
String provider = repositoryAnnotation.getValue("provider", String.class);
if (Objects.equals(Repository.ANY_PROVIDER, provider)
|| JNoSQLJakartaPersistence.PROVIDER.equals(provider)) {
|| getProviderName().equals(provider)) {
return true;
}
}
Expand All @@ -162,26 +130,22 @@ private Stream<Class<?>> repositoriesStreamMatching(Predicate<InterfaceModel> pr
.map(this::typeModelToClass);
}

private Class<?> typeModelToClass(ExtensibleType<?> type) throws RuntimeException {
return classForName(type.getName());
}

private Class<?> classForName(String name) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
protected boolean noneOfExtendedInterfacesIsStandard(InterfaceModel intfModel) {
Predicate<ParameterizedInterfaceModel> directlyImplementsStandardInterface = this::directlyImplementsStandardInterface;
return intfModel.getParameterizedInterfaces().isEmpty()
|| intfModel.getParameterizedInterfaces().stream()
.allMatch(directlyImplementsStandardInterface
.negate());
}

private boolean isSupportedBuiltInInterface(ParameterizedInterfaceModel interf) {
protected boolean isSupportedBuiltInInterface(ParameterizedInterfaceModel interf) {
final Collection<ParameterizedInterfaceModel> parameterizedTypes = interf.getParametizedTypes();
return !parameterizedTypes.isEmpty()
&& isSupportedEntityType(parameterizedTypes.iterator().next())
&& isDataRepositoryInterface(interf);
}

private boolean directlyImplementsStandardInterface(ParameterizedInterfaceModel interf) {
protected boolean directlyImplementsStandardInterface(ParameterizedInterfaceModel interf) {
var types = getTypes();
Type basicRepositoryType = types.getBy(BasicRepository.class.getName());
Type crudRepositoryType = types.getBy(CrudRepository.class.getName());
Expand All @@ -199,10 +163,6 @@ private boolean isDataRepositoryInterface(ParameterizedInterfaceModel interf) {
|| DataRepository.class.isAssignableFrom(classForName(interf.getRawInterfaceName()));
}

private boolean isSupportedEntityType(ParameterizedInterfaceModel entityType) {
return null != entityType.getRawInterface().getAnnotation(Entity.class.getName());
}

private boolean canBeAssignedToOneOf(Class<?> clazz, Class<?>... assignables) {
for (Class<?> cls : assignables) {
if (cls.isAssignableFrom(clazz)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.jnosql.jakartapersistence.mapping.glassfishcontext;

import jakarta.data.repository.DataRepository;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;

import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

import org.eclipse.jnosql.jakartapersistence.JNoSQLJakartaPersistence;
import org.eclipse.jnosql.jakartapersistence.mapping.metadata.JakartaPersistenceClassScanner;
import org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel;

import static java.util.stream.Collectors.toUnmodifiableSet;

/**
*
* @author Ondro Mihalyi
*/
public class GlassFishJakartaPersistenceClassScanner extends BaseGlassFishClassScanner implements JakartaPersistenceClassScanner {

private boolean enabled = false;

@Override
public Set<Class<?>> entities() {
return findClassesWithAnnotation(Entity.class);
}

@Override
public Set<Class<?>> repositories() {
return repositoriesStream()
.collect(toUnmodifiableSet());
}

@Override
public Set<Class<?>> embeddables() {
return findClassesWithAnnotation(Embeddable.class);
}

@Override
public <T extends DataRepository<?, ?>> Set<Class<?>> repositories(Class<T> filter) {
Objects.requireNonNull(filter, "filter is required");
return repositoriesStream()
.filter(filter::isAssignableFrom)
.collect(toUnmodifiableSet());
}

@Override
public Set<Class<?>> repositoriesStandard() {
Predicate<ParameterizedInterfaceModel> isSupportedBuiltInInterface = this::isSupportedBuiltInInterface;
Predicate<ParameterizedInterfaceModel> directlyImplementsStandardInterface = this::directlyImplementsStandardInterface;
return repositoriesStreamMatching(intfModel -> intfModel.getParameterizedInterfaces().stream()
.anyMatch(isSupportedBuiltInInterface.and(directlyImplementsStandardInterface)))
.collect(toUnmodifiableSet());
}

@Override
public Set<Class<?>> customRepositories() {
return repositoriesStreamMatching(this::noneOfExtendedInterfacesIsStandard)
.collect(toUnmodifiableSet());
}

@Override
protected boolean isSupportedEntityType(ParameterizedInterfaceModel entityType) {
return null != entityType.getRawInterface().getAnnotation(Entity.class.getName());
}

@Override
protected String getProviderName() {
return JNoSQLJakartaPersistence.PROVIDER;
}

}
Loading
Loading