Skip to content

Commit

Permalink
Merge pull request #3481 from ebean-orm/feature/boostrap-service
Browse files Browse the repository at this point in the history
Use io.ebean.service.BootstrapService as common marker interface for service loading bootstrapped services
  • Loading branch information
rbygrave authored Sep 17, 2024
2 parents 0c01ecb + 92f04a2 commit d9e0a35
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 157 deletions.
10 changes: 4 additions & 6 deletions ebean-api/src/main/java/io/ebean/DatabaseFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import io.ebean.service.SpiContainerFactory;
import jakarta.persistence.PersistenceException;

import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.concurrent.locks.ReentrantLock;

/**
Expand Down Expand Up @@ -157,10 +155,10 @@ private static SpiContainer container(ContainerConfig containerConfig) {
* Create the container instance using the configuration.
*/
private static SpiContainer createContainer(ContainerConfig containerConfig) {
Iterator<SpiContainerFactory> factories = ServiceLoader.load(SpiContainerFactory.class).iterator();
if (factories.hasNext()) {
return factories.next().create(containerConfig);
SpiContainerFactory factory = XBootstrapService.containerFactory();
if (factory == null) {
throw new IllegalStateException("Service loader didn't find a SpiContainerFactory?");
}
throw new IllegalStateException("Service loader didn't find a SpiContainerFactory?");
return factory.create(containerConfig);
}
}
6 changes: 3 additions & 3 deletions ebean-api/src/main/java/io/ebean/FetchGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public interface FetchGroup<T> {
* @return The FetchGroup with the given select clause
*/
static <T> FetchGroup<T> of(Class<T> cls, String select) {
return XServiceProvider.fetchGroupOf(cls, select);
return XBootstrapService.fetchGroupOf(cls, select);
}

/**
Expand All @@ -108,14 +108,14 @@ static <T> FetchGroup<T> of(Class<T> cls, String select) {
* @return The FetchGroupBuilder with the given select clause which we will add fetch clauses to
*/
static <T> FetchGroupBuilder<T> of(Class<T> cls) {
return XServiceProvider.fetchGroupOf(cls);
return XBootstrapService.fetchGroupOf(cls);
}

/**
* Return a query to be used by query beans for constructing FetchGroup.
*/
static <T> SpiFetchGroupQuery<T> queryFor(Class<T> beanType) {
return XServiceProvider.fetchGroupQueryFor(beanType);
return XBootstrapService.fetchGroupQueryFor(beanType);
}

}
6 changes: 3 additions & 3 deletions ebean-api/src/main/java/io/ebean/ProfileLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public interface ProfileLocation {
* Create and return a new ProfileLocation.
*/
static ProfileLocation create() {
return XServiceProvider.profileLocationFactory().create();
return XBootstrapService.profileLocationFactory().create();
}

/**
* Create and return a new ProfileLocation with line number.
*/
static ProfileLocation createWithLine() {
return XServiceProvider.profileLocationFactory().createWithLine();
return XBootstrapService.profileLocationFactory().createWithLine();
}

/**
* Create and return a new ProfileLocation with a given lineNumber and label.
*/
static ProfileLocation create(String label) {
return XServiceProvider.profileLocationFactory().create(label);
return XBootstrapService.profileLocationFactory().create(label);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions ebean-api/src/main/java/io/ebean/RawSqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public interface RawSqlBuilder {
* resultSet.
*/
static RawSql resultSet(ResultSet resultSet, String... propertyNames) {
return XServiceProvider.rawSql().resultSet(resultSet, propertyNames);
return XBootstrapService.rawSql().resultSet(resultSet, propertyNames);
}

/**
* Create and return a SqlRow based on the resultSet with dbTrueValue and binaryOptimizedUUID options.
*/
static SqlRow sqlRow(ResultSet resultSet, final String dbTrueValue, boolean binaryOptimizedUUID) throws SQLException {
return XServiceProvider.rawSql().sqlRow(resultSet, dbTrueValue, binaryOptimizedUUID);
return XBootstrapService.rawSql().sqlRow(resultSet, dbTrueValue, binaryOptimizedUUID);
}

/**
Expand All @@ -38,7 +38,7 @@ static SqlRow sqlRow(ResultSet resultSet, final String dbTrueValue, boolean bina
* this query.
*/
static RawSqlBuilder unparsed(String sql) {
return XServiceProvider.rawSql().unparsed(sql);
return XBootstrapService.rawSql().unparsed(sql);
}

/**
Expand All @@ -55,7 +55,7 @@ static RawSqlBuilder unparsed(String sql) {
* </p>
*/
static RawSqlBuilder parse(String sql) {
return XServiceProvider.rawSql().parsed(sql);
return XBootstrapService.rawSql().parsed(sql);
}

/**
Expand Down
95 changes: 95 additions & 0 deletions ebean-api/src/main/java/io/ebean/XBootstrapService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.ebean;

import io.ebean.metric.MetricFactory;
import io.ebean.service.*;

import java.util.ServiceLoader;

/**
* Bootstrap internal services.
*/
public final class XBootstrapService {

private static final SpiContainerFactory containerFactory;
private static final SpiRawSqlService rawSqlService;
private static final SpiProfileLocationFactory profileLocationFactory;
private static final SpiFetchGroupService fetchGroupService;
private static final MetricFactory metricFactory;
private static final SpiJsonService jsonService;
static {
SpiContainerFactory _factory = null;
SpiRawSqlService _raw = null;
SpiProfileLocationFactory _profile = null;
SpiFetchGroupService _fetch = null;
MetricFactory _metric = null;
SpiJsonService _json = null;
for (BootstrapService extension : ServiceLoader.load(BootstrapService.class)) {
if (extension instanceof SpiContainerFactory) {
_factory = (SpiContainerFactory)extension;
} else if (extension instanceof SpiRawSqlService) {
_raw = (SpiRawSqlService)extension;
} else if (extension instanceof SpiProfileLocationFactory) {
_profile = (SpiProfileLocationFactory)extension;
} else if (extension instanceof SpiFetchGroupService) {
_fetch = (SpiFetchGroupService)extension;
} else if (extension instanceof MetricFactory) {
_metric = (MetricFactory)extension;
} else if (extension instanceof SpiJsonService) {
_json = (SpiJsonService)extension;
}
}
containerFactory = _factory;
rawSqlService = _raw;
profileLocationFactory = _profile;
fetchGroupService = _fetch;
metricFactory = _metric;
jsonService = _json;
}

/**
* Return the MetricFactory found in boostrap service loading.
*/
public static MetricFactory metricFactory() {
return metricFactory;
}

/**
* Return the SpiJsonService found in boostrap service loading.
*/
public static SpiJsonService jsonService() {
return jsonService;
}

static SpiContainerFactory containerFactory() {
return containerFactory;
}

static SpiRawSqlService rawSql() {
return rawSqlService;
}

static SpiProfileLocationFactory profileLocationFactory() {
return profileLocationFactory;
}

/**
* Return the FetchGroup with the given select clause.
*/
static <T> FetchGroup<T> fetchGroupOf(Class<T> cls, String select) {
return fetchGroupService.of(cls, select);
}

/**
* Return the FetchGroupBuilder with the given select clause.
*/
static <T> FetchGroupBuilder<T> fetchGroupOf(Class<T> cls) {
return fetchGroupService.of(cls);
}

/**
* Return the FetchGroup Query for building fetch groups via query beans.
*/
static <T> SpiFetchGroupQuery<T> fetchGroupQueryFor(Class<T> cls) {
return fetchGroupService.queryFor(cls);
}
}
76 changes: 0 additions & 76 deletions ebean-api/src/main/java/io/ebean/XServiceProvider.java

This file was deleted.

6 changes: 4 additions & 2 deletions ebean-api/src/main/java/io/ebean/metric/MetricFactory.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.ebean.metric;

import io.ebean.ProfileLocation;
import io.ebean.XBootstrapService;
import io.ebean.service.BootstrapService;

/**
* Factory to create timed metric counters.
*/
public interface MetricFactory {
public interface MetricFactory extends BootstrapService {

/**
* Return the factory instance.
*/
static MetricFactory get() {
return MetricServiceProvider.get();
return XBootstrapService.metricFactory();
}

/**
Expand Down
28 changes: 0 additions & 28 deletions ebean-api/src/main/java/io/ebean/metric/MetricServiceProvider.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.ebean.service;

/**
* Marker interface for internal services loaded at startup.
*/
public interface BootstrapService {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Provides shutdown of the entire container.
*/
public interface SpiContainerFactory {
public interface SpiContainerFactory extends BootstrapService {

/**
* Create the Container that builds EbeanServer instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Service that parses FetchGroup expressions.
*/
public interface SpiFetchGroupService {
public interface SpiFetchGroupService extends BootstrapService {

/**
* Return the FetchGroup with the given select clause.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* Supports converting between JSON content and simple java Maps/Lists.
*/
public interface SpiJsonService {
public interface SpiJsonService extends BootstrapService {

/**
* Write the nested Map/List as json.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Factory for creating profile locations.
*/
public interface SpiProfileLocationFactory {
public interface SpiProfileLocationFactory extends BootstrapService {

/**
* Create a profile location.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Service provided by Ebean for parsing and column mapping raw SQL queries.
*/
public interface SpiRawSqlService {
public interface SpiRawSqlService extends BootstrapService {

/**
* Create based on a JDBC ResultSet.
Expand Down
Loading

0 comments on commit d9e0a35

Please sign in to comment.