Skip to content

Commit fc58d61

Browse files
committed
improve documentation of stats.spi
1 parent 7780b74 commit fc58d61

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

hibernate-core/src/main/java/org/hibernate/stat/Statistics.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@
1010
import org.checkerframework.checker.nullness.qual.Nullable;
1111

1212
/**
13-
* Exposes statistics collected from all sessions belonging to a
14-
* particular {@link org.hibernate.SessionFactory}.
13+
* Exposes statistics collected from all sessions belonging to a given
14+
* {@link org.hibernate.SessionFactory}.
15+
* <ul>
16+
* <li>Collection of statistics is enabled if the configuration property
17+
* {@value org.hibernate.cfg.AvailableSettings#GENERATE_STATISTICS}
18+
* is set to {@code true}.
19+
* <li>Alternatively, statistics collection may be enabled or disabled
20+
* at runtime by calling {@link #setStatisticsEnabled(boolean)}.
21+
* </ul>
1522
* <p>
16-
* Collection of statistics is enabled if the configuration property
17-
* {@value org.hibernate.cfg.AvailableSettings#GENERATE_STATISTICS} is
18-
* set to {@code true}. It may be dynamically enabled or disabled at
19-
* runtime by calling {@link #setStatisticsEnabled(boolean)}.
23+
* A custom statistics collector may be supplied by implementing the
24+
* {@link org.hibernate.stat.spi.StatisticsImplementor} SPI, and
25+
* supplying a {@link org.hibernate.stat.spi.StatisticsFactory} via
26+
* the configuration setting
27+
* {@value org.hibernate.cfg.StatisticsSettings#STATS_BUILDER}.
2028
*
2129
* @author Emmanuel Bernard
2230
*/

hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ public Class<StatisticsImplementor> getServiceInitiated() {
3838

3939
@Override
4040
public StatisticsImplementor initiateService(SessionFactoryServiceInitiatorContext context) {
41-
final Object configValue = context.getServiceRegistry()
42-
.requireService( ConfigurationService.class )
43-
.getSettings()
44-
.get( STATS_BUILDER );
41+
final Object configValue =
42+
context.getServiceRegistry().requireService( ConfigurationService.class )
43+
.getSettings().get( STATS_BUILDER );
4544
return initiateServiceInternal( context.getSessionFactory(), configValue, context.getServiceRegistry() );
4645
}
4746

@@ -50,18 +49,30 @@ private StatisticsImplementor initiateServiceInternal(
5049
@Nullable Object configValue,
5150
ServiceRegistryImplementor registry) {
5251

53-
final StatisticsFactory statisticsFactory;
52+
final StatisticsFactory statisticsFactory = statisticsFactory( configValue, registry );
53+
final StatisticsImplementor statistics =
54+
statisticsFactory == null
55+
? new StatisticsImpl( sessionFactory ) // default impl
56+
: statisticsFactory.buildStatistics( sessionFactory );
57+
final boolean enabled = sessionFactory.getSessionFactoryOptions().isStatisticsEnabled();
58+
statistics.setStatisticsEnabled( enabled );
59+
LOG.debugf( "Statistics initialized [enabled=%s]", enabled );
60+
return statistics;
61+
}
62+
63+
private static @Nullable StatisticsFactory statisticsFactory(
64+
@Nullable Object configValue, ServiceRegistryImplementor registry) {
5465
if ( configValue == null ) {
55-
statisticsFactory = null; //We'll use the default
66+
return null; //We'll use the default
5667
}
57-
else if ( configValue instanceof StatisticsFactory ) {
58-
statisticsFactory = (StatisticsFactory) configValue;
68+
else if ( configValue instanceof StatisticsFactory factory ) {
69+
return factory;
5970
}
6071
else {
6172
// assume it names the factory class
6273
final ClassLoaderService classLoaderService = registry.requireService( ClassLoaderService.class );
6374
try {
64-
statisticsFactory = (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance();
75+
return (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance();
6576
}
6677
catch (HibernateException e) {
6778
throw e;
@@ -73,18 +84,5 @@ else if ( configValue instanceof StatisticsFactory ) {
7384
);
7485
}
7586
}
76-
final StatisticsImplementor statistics;
77-
if ( statisticsFactory == null ) {
78-
// Default:
79-
statistics = new StatisticsImpl( sessionFactory );
80-
}
81-
else {
82-
statistics = statisticsFactory.buildStatistics( sessionFactory );
83-
}
84-
final boolean enabled = sessionFactory.getSessionFactoryOptions().isStatisticsEnabled();
85-
statistics.setStatisticsEnabled( enabled );
86-
LOG.debugf( "Statistics initialized [enabled=%s]", enabled );
87-
return statistics;
8887
}
89-
9088
}

hibernate-core/src/main/java/org/hibernate/stat/spi/StatisticsFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
/**
1010
* Factory for custom implementations of {@link StatisticsImplementor}.
11+
* <p>
12+
* A custom implementation may be selected via the configuration property
13+
* {@value org.hibernate.cfg.StatisticsSettings#STATS_BUILDER}.
1114
*
1215
* @author Steve Ebersole
1316
*/

hibernate-core/src/main/java/org/hibernate/stat/spi/StatisticsImplementor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import static java.util.Collections.emptyMap;
1414

1515
/**
16-
* A service SPI for collecting statistics about various events that occur at runtime.
16+
* A service SPI for collecting statistics about various events occurring at runtime.
17+
* <p>
18+
* A custom implementation may be provided via a {@link StatisticsFactory}.
1719
*
1820
* @author Emmanuel Bernard
1921
*/

0 commit comments

Comments
 (0)