Skip to content

Commit c55b680

Browse files
committed
fix BundleEntity impl scan, update spring version
1 parent 630367f commit c55b680

File tree

4 files changed

+22
-31
lines changed

4 files changed

+22
-31
lines changed

pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>com.kodgemisi.cigdem</groupId>
7+
<groupId>com.github.kodgemisi</groupId>
88
<artifactId>database-resource-bundle-message-source-starter</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.0-RC1</version>
1010

1111
<name>Database Resource Bundle Message Source Spring Boot Starter</name>
1212
<url>https://github.com/kodgemisi/database-resource-bundle-message-source-starter</url>
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>org.springframework.boot</groupId>
3131
<artifactId>spring-boot-dependencies</artifactId>
32-
<version>2.1.0.RELEASE</version>
32+
<version>2.2.1.RELEASE</version>
3333
<type>pom</type>
3434
<scope>import</scope>
3535
</dependency>

src/main/java/com/kodgemisi/cigdem/databasebasedmessagesource/DatabaseResourceBundleAutoconfiguration.java

+15-26
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020

2121
import javax.persistence.Entity;
2222
import javax.persistence.EntityManager;
23+
import javax.persistence.Query;
24+
import javax.persistence.metamodel.EntityType;
2325
import java.time.Duration;
26+
import java.util.ArrayList;
27+
import java.util.List;
2428
import java.util.Set;
2529

2630
/**
2731
* Created on May, 2018
2832
*
2933
* @author destan
3034
*/
35+
@Slf4j
3136
@Configuration
3237
@AutoConfigureBefore(MessageSourceAutoConfiguration.class)
33-
@Slf4j
3438
class DatabaseResourceBundleAutoconfiguration {
3539

3640
@Bean
@@ -42,7 +46,7 @@ MessageSourceProperties messageSourceProperties() {
4246
@Bean
4347
@ConditionalOnMissingBean(BundleContentLoaderStrategy.class)
4448
BundleContentLoaderStrategy bundleContentLoaderStrategy(EntityManager entityManager) throws ClassNotFoundException {
45-
return new JpaBundleContentLoaderStrategy<>(entityManager, findBundleEntityConcreteClass());
49+
return new JpaBundleContentLoaderStrategy<>(entityManager, findBundleEntityConcreteClass(entityManager));
4650
}
4751

4852
@Bean
@@ -80,39 +84,24 @@ private MessageSource createMessageSource(MessageSourceProperties properties, Bu
8084
}
8185

8286
@SuppressWarnings("unchecked")
83-
private Class<? extends BundleEntity> findBundleEntityConcreteClass() throws ClassNotFoundException {
84-
final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
85-
86-
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
87-
scanner.addIncludeFilter(new AssignableTypeFilter(BundleEntity.class));
87+
private Class<? extends BundleEntity> findBundleEntityConcreteClass(EntityManager entityManager) throws ClassNotFoundException {
8888

89-
final StopWatch stopWatch = new StopWatch();
90-
stopWatch.start("findCandidateComponents with 'com'");
89+
final List<Class<?>> entities = new ArrayList<>();
9190

92-
Set<BeanDefinition> entities = scanner.findCandidateComponents(
93-
"com");//first try this because giving a base significantly improves performance
94-
if (entities.isEmpty()) {
95-
stopWatch.stop();
96-
stopWatch.start("findCandidateComponents with '*'");
97-
entities = scanner.findCandidateComponents("");
91+
for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
92+
if (BundleEntity.class.isAssignableFrom(entity.getJavaType())) {
93+
entities.add(entity.getJavaType());
94+
}
9895
}
99-
stopWatch.stop();
10096

101-
log.debug("Scanned in {}", stopWatch.prettyPrint());
97+
if (entities.size() == 1) {
98+
return (Class<? extends BundleEntity>) entities.get(0);
99+
}
102100

103101
if (entities.size() > 1) {
104102
throw new IllegalStateException("There must be exactly one implementation of BundleEntity annotated with @Entity.");
105103
}
106104

107-
for (BeanDefinition bd : entities) {
108-
try {
109-
return (Class<? extends BundleEntity>) Class.forName(bd.getBeanClassName());
110-
}
111-
catch (ClassNotFoundException e) {
112-
log.error(e.getMessage(), e);
113-
}
114-
}
115-
116105
throw new ClassNotFoundException("There should be one class annotated with @Entity and implements BundleEntity.");
117106
}
118107

src/main/java/com/kodgemisi/cigdem/databasebasedmessagesource/DatabaseResourceBundleMessageSource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.kodgemisi.cigdem.databaseresourcbundle.BundleContentLoaderStrategy;
44
import com.kodgemisi.cigdem.databaseresourcbundle.DatabaseResourceBundleControl;
55
import lombok.AllArgsConstructor;
6+
import lombok.RequiredArgsConstructor;
67
import org.springframework.context.support.ResourceBundleMessageSource;
78

89
import java.io.Reader;
@@ -16,7 +17,7 @@
1617
*
1718
* @author destan
1819
*/
19-
@AllArgsConstructor
20+
@RequiredArgsConstructor
2021
public class DatabaseResourceBundleMessageSource extends ResourceBundleMessageSource {
2122

2223
private final BundleContentLoaderStrategy bundleContentLoaderStrategy;

src/main/java/com/kodgemisi/cigdem/databasebasedmessagesource/JpaBundleContentLoaderStrategy.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.kodgemisi.cigdem.databaseresourcbundle.BundleContentLoaderStrategy;
44
import lombok.AllArgsConstructor;
5+
import lombok.RequiredArgsConstructor;
56
import lombok.extern.slf4j.Slf4j;
67

78
import javax.persistence.EntityManager;
@@ -24,7 +25,7 @@
2425
* @author destan
2526
*/
2627
@Slf4j
27-
@AllArgsConstructor
28+
@RequiredArgsConstructor
2829
public class JpaBundleContentLoaderStrategy<E extends BundleEntity> implements BundleContentLoaderStrategy {
2930

3031
private final EntityManager entityManager;

0 commit comments

Comments
 (0)