Skip to content

Commit

Permalink
Tweak the InstantiatorService interface and use it to instantiate the…
Browse files Browse the repository at this point in the history
… XA TransactionManager
  • Loading branch information
chrisdennis committed Jul 21, 2023
1 parent 571e3ca commit 8bc033c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

public interface InstantiatorService extends Service {

<T> T instantiate(Class<T> clazz, Object[] arguments);
<T> T instantiate(Class<T> clazz, Object ... arguments) throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void stop() {
}

@Override
public <T> T instantiate(Class<T> clazz, Object[] arguments) {
public <T> T instantiate(Class<T> clazz, Object ... arguments) {
try {
return ConstructorUtils.invokeConstructor(clazz, arguments);
} catch (ReflectiveOperationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.ehcache.transactions.xa.txmgr.provider;

import jakarta.transaction.TransactionManager;
import org.ehcache.core.spi.service.InstantiatorService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceDependencies;
import org.ehcache.spi.service.ServiceProvider;
import org.ehcache.transactions.xa.txmgr.TransactionManagerWrapper;

Expand All @@ -37,10 +39,11 @@
* Note that in this scheme, the lookup instance is not expected to cache the {@code TransactionManagerWrapper}
* unless it can be considered a singleton.
*/
@ServiceDependencies(InstantiatorService.class)
public class LookupTransactionManagerProvider implements TransactionManagerProvider<TransactionManager> {

private final TransactionManagerLookup<TransactionManager> lookup;
private volatile TransactionManagerWrapper<TransactionManager> transactionManagerWrapper;
private final LookupTransactionManagerProviderConfiguration config;
private TransactionManagerWrapper<TransactionManager> transactionManagerWrapper;

/**
* Creates a new instance with the provided configuration.
Expand All @@ -53,11 +56,7 @@ public LookupTransactionManagerProvider(LookupTransactionManagerProviderConfigur
if (config == null) {
throw new NullPointerException("LookupTransactionManagerProviderConfiguration cannot be null");
}
try {
lookup = config.getTransactionManagerLookup().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("Could not instantiate lookup class", e);
}
this.config = config;
}

/**
Expand All @@ -73,7 +72,8 @@ public TransactionManagerWrapper<TransactionManager> getTransactionManagerWrappe
*/
@Override
public void start(ServiceProvider<Service> serviceProvider) {
this.transactionManagerWrapper = lookup.lookupTransactionManagerWrapper();
InstantiatorService instantiatorService = serviceProvider.getService(InstantiatorService.class);
this.transactionManagerWrapper = instantiatorService.instantiate(config.getTransactionManagerLookup()).lookupTransactionManagerWrapper();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.ehcache.transactions.xa.txmgr.provider;

import org.ehcache.core.spi.service.InstantiatorService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceDependencies;
import org.ehcache.spi.service.ServiceProvider;
import org.ehcache.transactions.xa.txmgr.TransactionManagerWrapper;

Expand All @@ -38,10 +40,11 @@
* Note that in this scheme, the lookup instance is not expected to cache the {@code TransactionManagerWrapper}
* unless it can be considered a singleton.
*/
@ServiceDependencies(InstantiatorService.class)
public class LookupTransactionManagerProvider implements TransactionManagerProvider<TransactionManager> {

private final TransactionManagerLookup<TransactionManager> lookup;
private volatile TransactionManagerWrapper<TransactionManager> transactionManagerWrapper;
private final LookupTransactionManagerProviderConfiguration config;
private TransactionManagerWrapper<TransactionManager> transactionManagerWrapper;

/**
* Creates a new instance with the provided configuration.
Expand All @@ -54,11 +57,7 @@ public LookupTransactionManagerProvider(LookupTransactionManagerProviderConfigur
if (config == null) {
throw new NullPointerException("LookupTransactionManagerProviderConfiguration cannot be null");
}
try {
lookup = config.getTransactionManagerLookup().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("Could not instantiate lookup class", e);
}
this.config = config;
}

/**
Expand All @@ -74,7 +73,8 @@ public TransactionManagerWrapper<TransactionManager> getTransactionManagerWrappe
*/
@Override
public void start(ServiceProvider<Service> serviceProvider) {
this.transactionManagerWrapper = lookup.lookupTransactionManagerWrapper();
InstantiatorService instantiatorService = serviceProvider.getService(InstantiatorService.class);
this.transactionManagerWrapper = instantiatorService.instantiate(config.getTransactionManagerLookup()).lookupTransactionManagerWrapper();
}

/**
Expand Down

0 comments on commit 8bc033c

Please sign in to comment.