forked from micronaut-projects/micronaut-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into issue-347
- Loading branch information
Showing
148 changed files
with
4,639 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ bin/ | |
.project | ||
*/test/ | ||
*/META-INF/ | ||
src/main/docs/guide/appendix/propertyReference.adoc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
apply plugin:"groovy" | ||
apply plugin:"java" | ||
|
||
dependencies { | ||
compile project(":inject") | ||
|
138 changes: 138 additions & 0 deletions
138
.../src/main/groovy/io/micronaut/documentation/asciidoc/AsciiDocPropertyReferenceWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package io.micronaut.documentation.asciidoc; | ||
|
||
import io.micronaut.core.util.CollectionUtils; | ||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.inject.configuration.ConfigurationMetadata; | ||
import io.micronaut.inject.configuration.ConfigurationMetadataBuilder; | ||
import io.micronaut.inject.configuration.ConfigurationMetadataWriter; | ||
import io.micronaut.inject.configuration.PropertyMetadata; | ||
import io.micronaut.inject.writer.ClassWriterOutputVisitor; | ||
import io.micronaut.inject.writer.GeneratedFile; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Writes out the asciidoc configuration property reference. | ||
* | ||
* @author graemerocher | ||
* @since 1.0 | ||
*/ | ||
public class AsciiDocPropertyReferenceWriter implements ConfigurationMetadataWriter { | ||
|
||
private static final Pattern PARAM_PATTERN = Pattern.compile("@param\\s*\\w+\\s*(.+)"); | ||
private static final ConfigurationMetadata EMPTY = new ConfigurationMetadata() { | ||
@Override | ||
public String getName() { | ||
return "EMPTY"; | ||
} | ||
}; | ||
|
||
@Override | ||
public void write(ConfigurationMetadataBuilder<?> metadataBuilder, ClassWriterOutputVisitor classWriterOutputVisitor) throws IOException { | ||
|
||
List<PropertyMetadata> props = new ArrayList<>(metadataBuilder.getProperties()) | ||
.stream() | ||
.filter(distinctByKey(PropertyMetadata::getPath)).collect(Collectors.toList()); | ||
|
||
List<ConfigurationMetadata> configs = new ArrayList<>(metadataBuilder.getConfigurations()) | ||
.stream() | ||
.sorted(Comparator.comparing(ConfigurationMetadata::getName)) | ||
.collect(Collectors.toList()); | ||
|
||
Collections.reverse(configs); | ||
|
||
Map<ConfigurationMetadata, List<PropertyMetadata>> map = props.stream().collect(Collectors.groupingBy(propertyMetadata -> | ||
configs.stream().filter(cm -> propertyMetadata.getPath().startsWith(cm.getName())).findFirst().orElseGet(() -> { | ||
// System.err.println("WARNING: No configuration found for property " + propertyMetadata.getPath()); | ||
return EMPTY; | ||
})) | ||
); | ||
|
||
if (CollectionUtils.isNotEmpty(map)) { | ||
|
||
Optional<GeneratedFile> file = classWriterOutputVisitor.visitMetaInfFile("config-properties.adoc"); | ||
|
||
if (file.isPresent()) { | ||
|
||
try (BufferedWriter w = new BufferedWriter(file.get().openWriter())) { | ||
|
||
for (Map.Entry<ConfigurationMetadata, List<PropertyMetadata>> entry : map.entrySet()) { | ||
ConfigurationMetadata cm = entry.getKey(); | ||
|
||
if (cm == null || cm == EMPTY) continue; | ||
|
||
if (entry.getValue() != null) { | ||
w.newLine(); | ||
w.append(".Configuration Properties for api:").append(cm.getType()).append("[]"); | ||
w.newLine(); | ||
w.append("|==="); | ||
w.newLine(); | ||
w.append("|Property |Type |Description"); | ||
w.newLine(); | ||
|
||
for (PropertyMetadata pm : entry.getValue()) { | ||
String path = pm.getPath(); | ||
String description = pm.getDescription(); | ||
|
||
if (path.contains("..")) continue; | ||
if (StringUtils.isEmpty(description)) description = ""; | ||
|
||
description = description.trim(); | ||
|
||
if (description.startsWith("@param")) { | ||
Matcher match = PARAM_PATTERN.matcher(description); | ||
if (match.find()) { | ||
description = match.group(1); | ||
} | ||
} else if (description.contains("@param")) { | ||
description = description.substring(0, description.indexOf("@param")).trim(); | ||
} | ||
|
||
String type = pm.getType(); | ||
|
||
if (type.startsWith("io.micronaut")) { | ||
type = "api:" + type + "[]"; | ||
} | ||
|
||
w.newLine(); | ||
w.append("| `").append(path).append('`'); | ||
w.newLine(); | ||
w.append("|").append(type); | ||
w.newLine(); | ||
w.append("|").append(description); | ||
w.newLine(); | ||
w.newLine(); | ||
} | ||
|
||
w.newLine(); | ||
w.append("|==="); | ||
w.newLine(); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
||
private <T> Predicate<T> distinctByKey( | ||
Function<? super T, ?> ke) { | ||
|
||
Map<Object, Boolean> seen = new ConcurrentHashMap<>(); | ||
return t -> seen.putIfAbsent(ke.apply(t), Boolean.TRUE) == null; | ||
} | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
...resources/META-INF/services/io.micronaut.inject.configuration.ConfigurationMetadataWriter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
io.micronaut.build.asciidoc.AsciiDocPropertyReferenceWriter | ||
io.micronaut.documentation.asciidoc.AsciiDocPropertyReferenceWriter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...rc/main/java/io/micronaut/configuration/hibernate/jpa/metrics/HibernateMetricsBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2017-2018 original authors | ||
* | ||
* 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.micronaut.configuration.hibernate.jpa.metrics; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.binder.jpa.HibernateMetrics; | ||
import io.micronaut.configuration.metrics.annotation.RequiresMetrics; | ||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.context.event.BeanCreatedEvent; | ||
import io.micronaut.context.event.BeanCreatedEventListener; | ||
import io.micronaut.core.convert.format.MapFormat; | ||
import io.micronaut.core.util.CollectionUtils; | ||
|
||
import javax.inject.Provider; | ||
import javax.inject.Singleton; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.micronaut.configuration.metrics.micrometer.MeterRegistryFactory.MICRONAUT_METRICS_BINDERS; | ||
|
||
/** | ||
* Binds metrics for Micrometer for each configured {@link EntityManagerFactory}. | ||
* | ||
* @author graemerocher | ||
* @since 1.0 | ||
*/ | ||
@Singleton | ||
@RequiresMetrics | ||
@Requires(property = MICRONAUT_METRICS_BINDERS + ".hibernate.enabled", value = "true", defaultValue = "true") | ||
public class HibernateMetricsBinder implements BeanCreatedEventListener<EntityManagerFactory> { | ||
|
||
private final Provider<MeterRegistry> meterRegistryProvider; | ||
private final List<Tag> tags; | ||
|
||
/** | ||
* Default constructor. | ||
* @param meterRegistryProvider The meter registry provider | ||
* @param tags The tags | ||
*/ | ||
public HibernateMetricsBinder( | ||
Provider<MeterRegistry> meterRegistryProvider, | ||
@Property(name = MICRONAUT_METRICS_BINDERS + ".hibernate.tags") | ||
@MapFormat(transformation = MapFormat.MapTransformation.FLAT) | ||
Map<String, String> tags) { | ||
this.meterRegistryProvider = meterRegistryProvider; | ||
if (CollectionUtils.isNotEmpty(tags)) { | ||
this.tags = tags.entrySet().stream().map(entry -> Tag.of(entry.getKey(), entry.getValue())).collect(Collectors.toList()); | ||
} else { | ||
this.tags = Collections.emptyList(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public EntityManagerFactory onCreated(BeanCreatedEvent<EntityManagerFactory> event) { | ||
EntityManagerFactory entityManagerFactory = event.getBean(); | ||
String sessionFactoryName = event.getBeanIdentifier().getName(); | ||
MeterRegistry meterRegistry = meterRegistryProvider.get(); | ||
HibernateMetrics.monitor( | ||
meterRegistry, | ||
entityManagerFactory, | ||
sessionFactoryName, | ||
tags | ||
); | ||
|
||
return entityManagerFactory; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...bernate-jpa/src/test/groovy/io/micronaut/configuration/hibernate/jpa/JavaBookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.micronaut.configuration.hibernate.jpa; | ||
|
||
import io.micronaut.configuration.hibernate.jpa.scope.CurrentSession; | ||
import io.micronaut.spring.tx.annotation.Transactional; | ||
|
||
import javax.inject.Singleton; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
@Singleton | ||
public class JavaBookService { | ||
|
||
@PersistenceContext | ||
EntityManager entityManagerField; | ||
|
||
|
||
private EntityManager entityManager; | ||
|
||
@PersistenceContext | ||
public void setEntityManager(@CurrentSession EntityManager entityManager) { | ||
this.entityManager = entityManager; | ||
} | ||
|
||
@Transactional | ||
public boolean testFieldInject() { | ||
entityManagerField.clear(); | ||
return true; | ||
} | ||
|
||
@Transactional | ||
public boolean testMethodInject() { | ||
entityManager.clear(); | ||
return true; | ||
} | ||
} |
Oops, something went wrong.