Skip to content

Commit

Permalink
Merge branch 'master' into issue-347
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Sep 11, 2018
2 parents 639d3a9 + e0c522a commit 7bc612f
Show file tree
Hide file tree
Showing 148 changed files with 4,639 additions and 481 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bin/
.project
*/test/
*/META-INF/
src/main/docs/guide/appendix/propertyReference.adoc
2 changes: 1 addition & 1 deletion build-projects/asciidoc-config-props/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apply plugin:"groovy"
apply plugin:"java"

dependencies {
compile project(":inject")
Expand Down
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;
}
}

Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.micronaut.build.asciidoc.AsciiDocPropertyReferenceWriter
io.micronaut.documentation.asciidoc.AsciiDocPropertyReferenceWriter
29 changes: 8 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,17 @@ ext {
group:'com.google.code.findbugs',
name:'jsr305'
],
'micrometer.core' : [
version: micrometerVersion,
group : 'io.micrometer',
name : 'micrometer-core'
],
'micrometer.atlas' : [
version: micrometerVersion,
group : 'io.micrometer',
name : 'micrometer-registry-atlas'
],
'micrometer.graphite': [
version: micrometerVersion,
group : 'io.micrometer',
name : 'micrometer-registry-graphite'
],
'micrometer.prometheus' : [
version: micrometerVersion,
group : 'io.micrometer',
name : 'micrometer-registry-prometheus'
kafka: [
version:kafkaVersion,
group:'org.apache.kafka',
name:'kafka-clients',
modules: ['kafka_2.12', 'kafka-streams']
],
'micrometer.statsd' : [
'micrometer' : [
version: micrometerVersion,
group : 'io.micrometer',
name : 'micrometer-registry-statsd'
name : 'micrometer-core',
modules :['micrometer-registry-atlas', 'micrometer-registry-graphite', 'micrometer-registry-prometheus', 'micrometer-registry-statsd']
],
'mongo' : [
version: mongoVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import org.eclipse.aether.graph.Dependency
*/
class GradleBuildTokens extends BuildTokens {

static final SCOPE_MAP = [
compile: 'implementation',
runtime: 'runtimeOnly',
testCompile: 'testImplementation',
]

Map getTokens(Profile profile, List<Feature> features) {
Map tokens = [:]

Expand All @@ -52,8 +58,10 @@ class GradleBuildTokens extends BuildTokens {
dependencies = dependencies.unique()

dependencies = dependencies.sort({ Dependency dep -> dep.scope }).collect() { Dependency dep ->
String scope = SCOPE_MAP.get(dep.scope)
if (scope == null) scope = dep.scope
String artifactStr = resolveArtifactString(dep, 4)
" ${dep.scope}${artifactStr}".toString()
" ${scope}${artifactStr}".toString()
}.unique().join(ln)

def buildPlugins = profile.buildPlugins.collect() { String name ->
Expand Down
2 changes: 2 additions & 0 deletions configurations/hibernate-jpa/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ dependencies {
compile project(":configurations:hibernate-validator")
compileOnly project(":inject-java")
compileOnly project(":management")
compileOnly project(":configurations:micrometer-core")

testCompile project(":inject-groovy")
testCompile project(":inject-java")
testRuntime project(":configurations:jdbc-tomcat")
testRuntime 'com.h2database:h2:1.4.196'
testCompile project(":configurations:micrometer-core")
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
@Factory
@Requires(classes = HibernateTransactionManager.class)
@Replaces(DataSourceTransactionManagerFactory.class)
@Replaces(factory = DataSourceTransactionManagerFactory.class)
public class HibernateTransactionManagerFactory {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.core.convert.format.MapFormat;
import io.micronaut.core.naming.conventions.StringConvention;
import io.micronaut.core.util.ArrayUtils;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
Expand Down Expand Up @@ -100,7 +101,7 @@ public String[] getPackagesToScan() {
* @param jpaProperties The JPA properties
*/
public final void setProperties(
@MapFormat(transformation = MapFormat.MapTransformation.FLAT)
@MapFormat(transformation = MapFormat.MapTransformation.FLAT, keyFormat = StringConvention.UNDER_SCORE_SEPARATED_LOWER_CASE)
Map<String, Object> jpaProperties) {
this.jpaProperties = jpaProperties;
}
Expand Down
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;
}
}
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;
}
}
Loading

0 comments on commit 7bc612f

Please sign in to comment.